elasticsearch Terms Query 实现类似于sql in查询
本文demo基于elasticsearch 5.1.1, 项目中使用的还是较早的版本
例如
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient; import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set; public class ElasticSearchMain { public static void main(String[] args) throws UnknownHostException { TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.7.61"), 9300));
//继续添加其他地址 Set set = new HashSet<String>();
set.add("3503027400038206");
set.add("3503227700038105");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termsQuery("pt_number", set)); SearchResponse response = client.prepareSearch("pt_0628").setTypes("lw_point_location").setQuery(boolQuery)
.setSize(10000).execute().actionGet(); for(SearchHit hit : response.getHits().getHits()){
System.out.println(JSON.toJSONString(hit.getSource()));
} //on shutdown
client.close();
} }
以上可以查询索引类型中对应的字段是
3503027400038206、
3503227700038105 的数据。 此外terms query 还可查询字段的值包含在另外一个索引类型的字段之中。 如, 在kibana中运行:
PUT /users/user/2
{
"followers" : ["1", "3"]
}
建一个users索引, 有一个类型是user, id为 2。
有个followers字段,是数组。
PUT /tweets/tweet/1
{
"user" : "1"
}
建一个tweets索引。其类型是tweet, id为1.
然后查询tweets索引tweet类型中,user字段的值包含在users索引user类型的数组中
GET /tweets/_search
{
"query" : {
"terms" : {
"user" : {
"index" : "users",
"type" : "user",
"id" : "2",
"path" : "followers"
}
}
}
}
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "tweets",
"_type": "tweet",
"_id": "1",
"_score": 0.2876821,
"_source": {
"user": "1"
}
}
]
}
}
可以通过Java 发送http请求去查询。
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient; import java.util.Collections; public class JavaTermsHttp {
public static void main(String[] args) throws Exception { RestClient restClient = RestClient.builder(
new HttpHost("192.168.7.61", 9200, "http")).build(); Response response = restClient.performRequest("POST", "/tweets/_search", Collections.<String, String>emptyMap(),
new NStringEntity("{\n" +
" \"query\" : {\n" +
" \"terms\" : {\n" +
" \"user\" : {\n" +
" \"index\" : \"users\",\n" +
" \"type\" : \"user\",\n" +
" \"id\" : \"2\",\n" +
" \"path\" : \"followers\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON)); System.out.println(EntityUtils.toString(response.getEntity()));
restClient.close(); }
}
elasticsearch Terms Query 实现类似于sql in查询的更多相关文章
- Elasticsearch(5) --- Query查询和Filter查询
Elasticsearch(5) --- Query查询和Filter查询 这篇博客主要分为 :Query查询和Filter查询.有关复合查询.聚合查询也会单独写篇博客. 一.概念 1.概念 一个查询 ...
- 使用SQL语句查询Elasticsearch索引数据
Elasticsearch 的官方查询语言是 Query DSL,存在毕竟有存在的道理,存在即合理.SQL 作为一个数据库查询语言,它语法简洁,书写方便而且大部分服务端程序员都清楚了解和熟知它的写法. ...
- C#EF中,使用类似于SQL中的% 模糊查询
最近在做项目的时候需要使用到模糊查询,但是后台使用EF写的 而不是ADO或者是Dapper,如果是这样的话,我们就可以使用Sql语句直接进行模糊查询 现在我们需要在LINQ中使用类似于模糊查询 在EF ...
- 使用Query进行HQL语句查询和SQL语句查询
HQL的语法比较简单,与普通SQL的区别之处是针对对象的不同,在查询语句中将sql中的表名替换成了sql中的持久化类名,因为hibernate机制是基于对象进行查询的. 不带参数的查询,语句是“fro ...
- NSPredicate用法总结(Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取)
简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取. 定义(最常用到的方法): NSPredicate *ca = [NSPred ...
- ElasticSearch权威指南学习(结构化查询)
请求体查询 简单查询语句(lite)是一种有效的命令行adhoc查询.但是,如果你想要善用搜索,你必须使用请求体查询(request body search)API. 空查询 我们以最简单的 sear ...
- Elasticsearch(6):文档查询
为方便后续查询演示,我们先创建一个索引.创建索引请求如下:
- elasticsearch中的mapping映射配置与查询典型案例
elasticsearch中的mapping映射配置与查询典型案例 elasticsearch中的mapping映射配置示例比如要搭建个中文新闻信息的搜索引擎,新闻有"标题".&q ...
- Elasticsearch+Mongo亿级别数据导入及查询实践
数据方案: 在Elasticsearch中通过code及time字段查询对应doc的mongo_id字段获得mongodb中的主键_id 通过获得id再进入mongodb进行查询 1,数据情况: ...
随机推荐
- 异常DBG_PRINTEXCEPTION_C(0x40010006)和DBG_PRINTEXCEPTION_WIDE_C(0x4001000A)
简介 DBG_PRINTEXCEPTION_C,代码0x40010006:DBG_PRINTEXCEPTION_WIDE_C,代码0x4001000A:在调试器的控制台窗口打印异常信息/调试信息.它定 ...
- 学习-jdk8 特性
jdk8新特性 Lambda 表达式 Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中.方法引用 − 方法引用提供了非常有用的语法,可以直接引用已有Java类或对象(实例)的方法或构 ...
- Layui 表单赋值 编辑页面赋初值
原文:https://blog.csdn.net/yulongxue/article/details/97924591 //编辑 if (id > 0) { $.post("/Hand ...
- PATA1028 List Sorting
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
- virtualbox安装问题总结
还是老问题 重点重点: https://blog.csdn.net/Loisleen/article/details/84975165#1install_the_gcc_make_perl_packa ...
- 搭建git服务器配置gitolite[迁移原来的gitolite工程]
参考 https://www.liaoxuefeng.com/wiki/896043488029600/899998870925664 http://www.worldhello.net/gotgit ...
- 《Linux就该这么学》培训笔记_ch20使用LNMP架构部署动态网站环境
<Linux就该这么学>培训笔记_ch20使用LNMP架构部署动态网站环境 文章最后会post上书本的笔记照片. 文章主要内容: 源码包程序 LNMP动态网站架构 配置Mysql服务 配置 ...
- TreeMap源码分析2
package map; import org.junit.Test; import com.mysql.cj.api.x.Collection; import map.TreeMap1.Ascend ...
- [转帖]开发人员行走Unix的随身四艺
开发人员行走Unix的随身四艺 https://www.cnblogs.com/jiangzhaowei/p/3568226.html Unix系统永远只会越来越多,开发人员就没必要特意学习它们的安装 ...
- php filter_var()
定义和用法 filter_var() 函数通过指定的过滤器过滤变量. 如果成功,则返回已过滤的数据,如果失败,则返回 false. 语法 filter_var(variable, filter, op ...