实例

http://www.bubuko.com/infodetail-648214.html 

GET _search 

{ 

 "query": { 

 "match_all": {} 

 } 

} 


PUT /useraudit 


POST /useraudit/t_unparticiple_test/_mapping 

{ 

 "t_unparticiple_test": { 

 "properties": { 

 "uid": { 

 "type": "long" 

 }, 

 "country": { 

 "type": "string" 

 }, 

 "city": { 

 "type": "string" 

 }, 

 "area": { 

 "type": "string" 

 }, 

 "address": { 

 "type": "string" 

 }, 

 "time": { 

 "type": "long" 

 } 

 } 

 } 

} 


POST /useraudit/t_participle_test/_mapping 

{ 

 "t_participle_test": { 

 "properties": { 

 "uid": { 

 "type": "long" 

 }, 

 "country": { 

 "type": "string" 

 }, 

 "city": { 

 "type": "string" 

 }, 

 "area": { 

 "type": "string" 

 }, 

 "address": { 

 "type": "string", 

 "include_in_all": true, 

 "analyzer": "ik", 

 "term_vector": "with_positions_offsets" 

 }, 

 "time": { 

 "type": "long" 

 } 

 } 

 } 

} 


POST /useraudit/t_participle_index_test/_mapping 

{ 

 "t_participle_index_test": { 

 "properties": { 

 "uid": { 

 "type": "long", 

 "index": "not_analyzed" 

 }, 

 "country": { 

 "type": "string", 

 "index": "not_analyzed" 

 }, 

 "city": { 

 "type": "string", 

 "index": "not_analyzed" 

 }, 

 "area": { 

 "type": "string", 

 "index": "not_analyzed" 

 }, 

 "address": { 

 "type": "string", 

 "include_in_all": true, 

 "analyzer": "ik", 

 "term_vector": "with_positions_offsets" 

 }, 

 "time": { 

 "type": "long", 

 "index": "not_analyzed" 

 } 

 } 

 } 

} 


PUT /useraudit/t_unparticiple_test/1 

{ 

 "uid": 200111, 

 "country": "四川省", 

 "city":"眉山市", 

 "area":"东坡区", 

 "time": 1441293300, 

 "address": "红星东路二段139号" 

} 


PUT /useraudit/t_unparticiple_test/6 

{ 

 "uid": 200111, 

 "country": "四川省", 

 "city":"眉山市", 

 "area":"东坡区", 

 "time": 1441293300, 

 "address": "东坡胡公园" 

} 



PUT /useraudit/t_unparticiple_test/2 

{ 

 "uid": 200115, 

 "country": "四川省", 

 "city":"眉山市", 

 "area":"东坡区", 

 "time": 1441293300, 

 "address": "红星东路西三段123号冒菜店" 

} 


PUT /useraudit/t_unparticiple_test/3 

{ 

 "uid": 362201, 

 "country": "浙江省", 

 "city":"杭州市", 

 "area":"萧山区", 

 "time": 1441293300, 

 "address": "宁围镇二桥村顺发康庄4栋1单元902" 

} 


PUT /useraudit/t_unparticiple_test/4 

{ 

 "uid": 362203, 

 "country": "浙江省", 

 "city":"衢州市", 

 "area":"常山县", 

 "time": 1441293300, 

 "address": "新都工业区文东路852号天幕家纺" 

} 


PUT /useraudit/t_unparticiple_test/5 

{ 

 "uid": 662203, 

 "country": "浙江省", 

 "city":"衢州市", 

 "area":"常山县", 

 "time": 1441293300, 

 "address": "低塘街道新堰西路45号" 

} 


PUT /useraudit/t_participle_test/1 

{ 

 "uid": 200111, 

 "country": "四川省", 

 "city":"眉山市", 

 "area":"东坡区", 

 "time": 1441293300, 

 "address": "红星东路二段139号" 

} 


PUT /useraudit/t_participle_test/2 

{ 

 "uid": 200115, 

 "country": "四川省", 

 "city":"眉山市", 

 "area":"东坡区", 

 "time": 1441293300, 

 "address": "红星东路西三段123号冒菜店" 

} 


PUT /useraudit/t_participle_test/3 

{ 

 "uid": 362201, 

 "country": "浙江省", 

 "city":"杭州市", 

 "area":"萧山区", 

 "time": 1441293300, 

 "address": "宁围镇二桥村顺发康庄4栋1单元902" 

} 


PUT /useraudit/t_participle_test/4 

{ 

 "uid": 362203, 

 "country": "浙江省", 

 "city":"衢州市", 

 "area":"常山县", 

 "time": 1441293300, 

 "address": "新都工业区文东路852号天幕家纺" 

} 


PUT /useraudit/t_participle_test/5 

{ 

 "uid": 662203, 

 "country": "浙江省", 

 "city":"衢州市", 

 "area":"常山县", 

 "time": 1441293300, 

 "address": "低塘街道新堰西路45号" 

} 



PUT /useraudit/t_participle_index_test/1 

{ 

 "uid": 200111, 

 "country": "四川省", 

 "city":"眉山市", 

 "area":"东坡区", 

 "time": 1441293300, 

 "address": "红星东路二段139号" 

} 


PUT /useraudit/t_participle_index_test/2 

{ 

 "uid": 200115, 

 "country": "四川省", 

 "city":"眉山市", 

 "area":"东坡区", 

 "time": 1441293300, 

 "address": "红星东路西三段123号冒菜店" 

} 


PUT /useraudit/t_participle_index_test/3 

{ 

 "uid": 362201, 

 "country": "浙江省", 

 "city":"杭州市", 

 "area":"萧山区", 

 "time": 1441293300, 

 "address": "宁围镇二桥村顺发康庄4栋1单元902" 

} 


PUT /useraudit/t_participle_index_test/4 

{ 

 "uid": 362203, 

 "country": "浙江省", 

 "city":"衢州市", 

 "area":"常山县", 

 "time": 1441293300, 

 "address": "新都工业区文东路852号天幕家纺" 

} 


PUT /useraudit/t_participle_index_test/5 

{ 

 "uid": 662203, 

 "country": "浙江省", 

 "city":"衢州市", 

 "area":"常山县", 

 "time": 1441293300, 

 "address": "低塘街道新堰西路45号" 

} 



#精确查询 

GET /useraudit/t_unparticiple_test/_search?q=uid:200111 

GET /useraudit/t_unparticiple_test/_search 

{ 

 "query": { 

 "match": { 

 "uid": "200111" 

 } 

 } 

} 


# 字段为strin类型,mappin中未定义取消分词,精确查询不到数据 

GET /useraudit/t_unparticiple_test/_search 

{ 

 "query": { 

 "match": { 

 "city": "衢州市" 

 } 

 } 

} 


GET /useraudit/t_participle_test/_search 

{ 

 "query": { 

 "match": { 

 "country": "四川省" 

 } 

 } 

} 


# 字段为strin类型,mappin中定义取消分词,精确查询数据 

GET /useraudit/t_participle_index_test/_search 

{ 

 "query": { 

 "match": { 

 "country": "四川省" 

 } 

 } 

} 



#模糊查询 

GET /useraudit/t_participle_test/_search 

{ 

 "query" : { "term" : { "address" : "红星" }}, 

 "highlight" : { 

 "pre_tags" : ["<tag1>", "<tag2>"], 

 "post_tags" : ["</tag1>", "</tag2>"], 

 "fields" : { 

 "address" : {} 

 } 

 } 

} 

GET /useraudit/t_participle_index_test/_search 

{ 

 "query" : { "term" : { "address" : "红星" }}, 

 "highlight" : { 

 "pre_tags" : ["<tag1>", "<tag2>"], 

 "post_tags" : ["</tag1>", "</tag2>"], 

 "fields" : { 

 "address" : {} 

 } 

 } 

} 


POST /index/fulltext/_search 

{ 

 "query" : { "term" : { "content" : "中国" }}, 

 "highlight" : { 

 "pre_tags" : ["<tag1>", "<tag2>"], 

 "post_tags" : ["</tag1>", "</tag2>"], 

 "fields" : { 

 "content" : {} 

 } 

 } 

}



二, java客户端调用Elasticsearch集群
1, 精准查询
2, 模糊查询(使用分词)

public class ESQueryClient {
    static Client CLIENT = null;
    static {
        // on startup
        Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch_useraudit").put("client.transport.sniff", true)
                .build();
        CLIENT = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9301))
                .addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9302))
                .addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9303));
    }

    public static void main(String[] args) throws ElasticsearchException, IOException {
        // 通过id查询
        qryById();
        // 通过某个字段查询,例如uid, country
        SearchRequestBuilder builder = CLIENT.prepareSearch("useraudit").setTypes("t_participle_test").setSearchType(SearchType.DEFAULT)
                .setFrom(0).setSize(100);
        BoolQueryBuilder qb = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder("眉山市").field("city"));
        builder.setQuery(qb);
        SearchResponse response = builder.execute().actionGet();
        System.out.println("字段为strin类型, mappin中未定义取消分词,精确查询不到数据, 测试结果:");
        System.out.println("通过city字段查询:" + response);
        System.out.println("通过city字段查询信息数量:" + response.getHits().getTotalHits());

        // 通过某个字段查询,例如uid, country
        builder = CLIENT.prepareSearch("useraudit").setTypes("t_participle_index_test").setSearchType(SearchType.DEFAULT).setFrom(0).setSize(100);
        qb = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder("眉山市").field("city"));
        builder.setQuery(qb);
        response = builder.execute().actionGet();
        System.out.println("字段为strin类型, mappin中定义取消分词,精确查询数据 , 测试结果:");
        System.out.println("通过city字段查询:" + response);
        System.out.println("通过city字段查询信息数量:" + response.getHits().getTotalHits());

        // .should(new QueryStringQueryBuilder("太多").field("city"))
        CLIENT.close();
    }

    private static void qryById() {
        GetResponse response = CLIENT.prepareGet("useraudit", "t_participle_test", "1").setOperationThreaded(false).execute().actionGet();
        System.out.println("通过id查询:" + response.getSourceAsString());
    }
}



三, 索引数据的维护


四, 批量插入数据


测试数据量5千万


五, 压力测试并发精准查询和模糊查询