编写不易,转载请注明(http://shihlei.iteye.com/blog/2408238)!
一 概述
ElasticSearch 批量条件查询方法Demo。
使用《ElasticSearch2.4.0基于Java API或Java Rest API进行CRUD 》的索引库结构开发Demo.
客户端版本:版本较低,见谅
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.4.0</version> </dependency>
二 索引库准备
1)创建索引库
curl -XPUT 'http://localhost:9200/indexdb' -d '{ "settings" : { "index" : { "number_of_shards" : 1, "number_of_replicas" : 1 } } }'
2)创建Mapping
curl -XPUT 'http://localhost:9200/indexdb/_mapping/docs' -d '{ "properties": { "id": { "type": "long", "index": "no" }, "title": { "type": "string", "index": "not_analyzed", "index_options": "docs" }, "author": { "type": "string", "index": "not_analyzed", "index_options": "docs" }, "tags": { "type": "string", "boost" : 3.0, "index_options": "docs" }, "publishTime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" } } }'
3)造测试数据
curl -XPOST 'http://localhost:9200/_bulk' -d ' { "index" : { "_index" : "indexdb", "_type" : "docs", "_id" : "1" } } {"author":"author_1","id":1,"publishTime":"2018-01-21 00:00:00","tags":["male","car"],"title":"title_1"} { "index" : { "_index" : "indexdb", "_type" : "docs", "_id" : "2" } } {"author":"author_2","id":2,"publishTime":"2018-01-21 00:00:00","tags":["male","game"],"title":"title_2"} { "index" : { "_index" : "indexdb", "_type" : "docs", "_id" : "3" } } {"author":"author_3","id":3,"publishTime":"2018-01-21 00:00:00","tags":["female","car"],"title":"title_3"} { "index" : { "_index" : "indexdb", "_type" : "docs", "_id" : "4" } } {"author":"author_4","id":4,"publishTime":"2018-01-21 00:00:00","tags":["female","game"],"title":"title_4"} '
4)查看造数据情况
curl -XGET 'http://localhost:9200/indexdb/docs/_search?pretty=true'
结果:
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }, "hits" : { "total" : 4, "max_score" : 1.0, "hits" : [ { "_index" : "indexdb", "_type" : "docs", "_id" : "1", "_score" : 1.0, "_source" : { "author" : "author_1", "id" : 1, "publishTime" : "2018-01-21 00:00:00", "tags" : [ "male", "car" ], "title" : "title_1" } }, { "_index" : "indexdb", "_type" : "docs", "_id" : "2", "_score" : 1.0, "_source" : { "author" : "author_2", "id" : 2, "publishTime" : "2018-01-21 00:00:00", "tags" : [ "male", "game" ], "title" : "title_2" } }, { "_index" : "indexdb", "_type" : "docs", "_id" : "3", "_score" : 1.0, "_source" : { "author" : "author_3", "id" : 3, "publishTime" : "2018-01-21 00:00:00", "tags" : [ "female", "car" ], "title" : "title_3" } }, { "_index" : "indexdb", "_type" : "docs", "_id" : "4", "_score" : 1.0, "_source" : { "author" : "author_4", "id" : 4, "publishTime" : "2018-01-21 00:00:00", "tags" : [ "female", "game" ], "title" : "title_4" } } ] } }
三 Http 查询
1)使用_msearch进行批量条件查询:
分别查询:tags为car 和tags为male的两个查询
curl -XGET 'http://localhost:9200/indexdb/docs/_msearch?pretty=true' -d ' {"index" : "indexdb"} { "query" : { "bool" : { "filter" : { "terms" : { "tags" : [ "car" ] } } } }, "fields" : [ "id", "title", "author", "publishTime"]} {"index" : "indexdb"} { "query" : { "bool" : { "filter" : { "terms" : { "tags" : [ "male" ] } } } }, "fields" : [ "id", "title", "author", "publishTime"]} '
2)结果
{ "responses" : [ { "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 0.0, "hits" : [ { "_index" : "indexdb", "_type" : "docs", "_id" : "1", "_score" : 0.0, "fields" : { "id" : [ 1 ], "author" : [ "author_1" ], "title" : [ "title_1" ], "publishTime" : [ "2018-01-21 00:00:00" ] } }, { "_index" : "indexdb", "_type" : "docs", "_id" : "3", "_score" : 0.0, "fields" : { "id" : [ 3 ], "author" : [ "author_3" ], "title" : [ "title_3" ], "publishTime" : [ "2018-01-21 00:00:00" ] } } ] } }, { "took" : 20, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 0.0, "hits" : [ { "_index" : "indexdb", "_type" : "docs", "_id" : "1", "_score" : 0.0, "fields" : { "id" : [ 1 ], "author" : [ "author_1" ], "title" : [ "title_1" ], "publishTime" : [ "2018-01-21 00:00:00" ] } }, { "_index" : "indexdb", "_type" : "docs", "_id" : "2", "_score" : 0.0, "fields" : { "id" : [ 2 ], "author" : [ "author_2" ], "title" : [ "title_2" ], "publishTime" : [ "2018-01-21 00:00:00" ] } } ] } } ] }
四 Java TCP Demo
1)程序
package x.search.es.simple.demo; import java.net.InetAddress; import java.util.Objects; import org.elasticsearch.action.search.MultiSearchRequestBuilder; import org.elasticsearch.action.search.MultiSearchResponse; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; public class DocumentMSearchDemo { private static final String ES_HOST = "localhost"; private static final int ES_TCP_PORT = 9300; private TransportClient client; public static void main(String[] args) throws Exception { DocumentMSearchDemo mSearchDemo = new DocumentMSearchDemo(); mSearchDemo.init(); mSearchDemo.msearch(); mSearchDemo.close(); } public void init() throws Exception { //特别注意:如果cluster 起了名字,需要在连接时指定名字,否则验证客户端连接的不是默认集群elasticsearch,会忽略,则无法找到节点 Settings settings = Settings.settingsBuilder() .put("cluster.name", "es_cluster").build(); client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ES_HOST), ES_TCP_PORT)); } public void close() { if (!Objects.isNull(client)) { client.close(); } } public void msearch() { //构建批量查询 MultiSearchRequestBuilder multiSearchRequestBuilder = client.prepareMultiSearch(); //创建查询条件 //查询tags:car SearchRequestBuilder carSearch = client.prepareSearch("indexdb"); carSearch.setQuery(QueryBuilders.termQuery("tags", "car")); multiSearchRequestBuilder.add(carSearch); //查询tags:male SearchRequestBuilder maleSearch = client.prepareSearch("indexdb"); maleSearch.setQuery(QueryBuilders.termQuery("tags", "male")); multiSearchRequestBuilder.add(maleSearch); //请求 MultiSearchResponse response = multiSearchRequestBuilder.get(); MultiSearchResponse.Item[] items = response.getResponses(); for (MultiSearchResponse.Item item : items) { SearchHits hits = item.getResponse().getHits(); //无查询结果 if (hits.totalHits() > 0) { SearchHit[] hitList = hits.getHits(); for (SearchHit searchHit : hitList) { System.out.println(searchHit.getSourceAsString()); } } System.out.println("-------------------------------------"); } } }
2)结果
{"author":"author_1","id":1,"publishTime":"2018-01-21 00:00:00","tags":["male","car"],"title":"title_1"} {"author":"author_3","id":3,"publishTime":"2018-01-21 00:00:00","tags":["female","car"],"title":"title_3"} ------------------------------------- {"author":"author_1","id":1,"publishTime":"2018-01-21 00:00:00","tags":["male","car"],"title":"title_1"} {"author":"author_2","id":2,"publishTime":"2018-01-21 00:00:00","tags":["male","game"],"title":"title_2"}