编写不易,转载请注明(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"}
相关推荐
10. **数据导入与查询**:使用Elasticsearch的`_bulk` API进行批量数据导入,可以大大提高效率。查询时,利用JSON格式的请求体,可以构建复杂的查询条件和聚合分析。 以上内容涵盖了Elasticsearch 7.14.0的基本使用...
5. **查询接口设计**:利用 Elasticsearch 的 Query DSL 设计查询接口,支持多种查询条件和组合。 6. **结果排序与聚合**:实现搜索结果的排序和聚合统计,以提供更丰富的查询结果展示。 7. **监控与调优**:通过 ...
在实际开发中,为了方便操作Elasticsearch,开发者通常会封装一些工具类,如"ElasticsearchUtil",以便更高效地执行常见的查询、更新、删除和创建等操作。 ### 查询数据 默认查询通常是基于索引来获取匹配的数据,...
Elasticsearch数据导出工具是一种高效实用的解决方案,它允许用户方便地从Elasticsearch(ES)集群中抽取数据,并将其导出到不同的目标,如MySQL数据库或本地文件系统。这款工具尤其适用于需要进行数据迁移、备份或...
安装npm install -g elasticsearch-tools 安装后,您将可以使用以下命令行工具:出口 输入用法:es-export-bulk选项es-export-bulk --helpUsage: es-export-bulk [options] Options: -h, --help output usage ...
Elasticsearch是一个强大的分布式搜索引擎,广泛应用于大数据分析和实时数据检索。在Java环境中,与Elasticsearch进行交互通常会使用官方提供的Java REST客户端。本文将详细介绍如何构建一个封装了基本增删改查功能...
4. **查询与过滤**:Elasticsearch提供了丰富的查询语法,如全文搜索、范围查询、术语查询等。使用过滤器可以筛选出满足特定条件的文档,而查询则用于返回相关度高的结果。 5. **聚合分析**:聚合功能允许对数据...
总的来说,Elasticsearch的Java实现涵盖了索引生命周期管理、查询构建和执行等多个方面,通过Java代码可以灵活地进行数据操作,实现高效的数据检索和分析。开发者需要对Elasticsearch的原理有深入理解,并掌握Java ...
Elasticsearch(简称ES)是一款强大的开源分布式全文搜索引擎,基于Lucene库,但提供了更高级别的API和管理功能。它被广泛应用于日志分析、实时监控、数据搜索等领域,是现代数据分析体系中不可或缺的一部分。本资料...
这些方法基于Elasticsearch的查询DSL,让你能够方便地构建复杂的查询条件。 7. **性能优化**:在实际应用中,还需要考虑Elasticsearch的集群配置、索引分片和副本设置,以及批量操作等性能优化策略。确保Elastic...
**Elasticsearch Java API详解** Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,广泛应用于大数据领域的实时分析和信息检索。Java API是Elasticsearch官方提供的与Elasticsearch服务器进行交互的...
从文件内容来看,文档涉及到ElasticSearch Java客户端的多个方面,包括如何连接ElasticSearch集群、使用文档API进行索引、查询等操作,以及如何使用搜索API和聚合功能等高级特性。文档还提到了与ElasticSearch交互的...
安装过程中需要注意 Java 运行环境(JRE)的先决条件,因为 Elasticsearch 是用 Java 编写的。 - 配置文件 `elasticsearch.yml` 位于安装目录下,用于设置集群名称、网络监听地址、内存分配等参数。合理配置这些...
你可以通过 `QueryBuilder` 构建复杂的查询条件,例如布尔查询、范围查询等。 ```java SearchRequest searchRequest = new SearchRequest("my_index"); SearchSourceBuilder searchSourceBuilder = new ...
这包括了链式调用、条件过滤、排序、分页等多种功能,使得对Elasticsearch的查询更加直观和简洁。 在实际开发中,我们还需要考虑错误处理和日志记录,确保在Elasticsearch出现问题时能够及时发现并解决。此外,为了...
3. **查询驱动的计算**:Elasticsearch的强大查询能力可以驱动Hadoop作业,根据查询条件决定处理哪些数据,提高了处理效率。 4. **数据可视化**:结合Kibana,可以将Hadoop处理后的结果进行可视化展示,便于数据分析...
你可以使用Elasticsearch支持的各种查询语法来定义删除条件,比如`match_all`(删除所有文档)、`term`、`range`、`bool`等。例如,如果你想删除`user`索引中`age`字段大于30的所有文档,可以这样写: ```json ...