编写不易,转载请注明(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"}
相关推荐
安装npm install -g elasticsearch-tools 安装后,您将可以使用以下命令行工具:出口 输入用法:es-export-bulk选项es-export-bulk --helpUsage: es-export-bulk [options] Options: -h, --help output usage ...
嵌入式Elasticsearch节点-JMEmbeddedElasticsearch Elasticsearch Client(运输)-JMElasticsearchClient 搜索查询SearchQueryBuilder 计数查询删除查询批量处理器版本先决条件: Java 11或更高版本安装签出源代码...
##先决条件Java 8 经过Elasticsearch 1.6测试(也应与早期版本一起使用) ##用法 mvn packagejava -jar target/elasticsearch-fileimport-0.6-SNAPSHOT-jar-with-dependencies.jar file_import_settings.yml为了...
从v1.5开始,我们支持创建自定义标头的功能,因此您可以轻松地传递不同的身份验证标头,通过Elasticsearch的Query DSL提供增强的过滤和批量更新数据。 从v2.0开始,我们支持构建多面搜索UI来测试相关性的功能。 您...
Elasticsearch 基础操作 2.1.1HTTP 操作 1)创建索引 2)查看所有索引 3)查看单个索引 4)删除索引 4)删除索引 2.1.1.2文档操作 1)创建文档 2)查看文档 3)修改文档 4)修改字段 5)删除文档 6)条件删除文档 ...
使用ES传输协议(不是HTTP / REST)将JSON文件直接作为文档导入到Elasticsearch中。 导入是通过批量请求完成的。 ##先决条件 Java 8 经过Elasticsearch 1.6测试(也应与早期版本一起使用) ##用法 mvn ...
本教程的目标是使用批量索引将整个Gmail收件箱加载到Elasticsearch中,然后开始查询集群以更好地了解正在发生的事情。 先决条件 设置并确保它在 一种运行Elasticsearch的快速方法是使用Docker:(确实不需要cors...
结果存储在适合Elasticsearch批量上传的ND-JSON文件中。 (此脚本曾经是一个用例。要进一步使用,必须对代码进行彻底的检查) 入门 先决条件 Google Vision API密钥需要在index.js中提交 需要在系统上安装Nodejs。 ...
批量插入相同类型的多个实体并返回生成的密钥 更新单个现有实体-一次更新实体的所有字段 获取多对一关系(部门公司) 获取一对多关系(公司部门) 更新实体一对多关系(公司部门)-添加两项,更新两项并删除一项-...
Restream的我们对Elasticsearch并不满意,因此创建了Reindexer作为性能更高的替代产品。 核心是用C ++编写的,而应用程序级别的API是用Go编写的。 本文档介绍Go连接器及其API。 要获取有关reindexer服务器和...
Rubberband Flamethrower 是将伪造数据插入 Elastic Search 服务器并提供基本基准测试的宝石。 它创建并插入具有三个字段(消息、用户名和 post_date)的假数据对象,并对过程进行计时。 它默认批量插入 500 个对象...
也可以自己搭建 Elasticsearch 并通过配置进行集成 后续会对搜索功能进行加强,实现条件过滤,结果高亮等,敬请期待。 计划中的特性 信任系统 通过历史数据对用户进行分级提权,让用户从浏览者逐步变为参与者...
19.2.5 BC-SRV-QUE ABAP 查询 362 19.2.6 BC-SRV-COM 通讯界面 363 19.3 BC-CCM 计算中心管理系统 363 19.3.1 BC-CCM-ADK 存档开发工具包 365 19.3.2 BC-CCM-PRN 打印和输出管理 366 19.3.3 BC-CCM-MON 监控 366 ...
jQuery1.2 API 中文版折叠展开折叠全部展开全部 英文说明 核心jQuery 核心函数 jQuery(expression,[context]) jQuery(expression,[context]) 这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组...