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,数据情况: ...
随机推荐
- Codeforces Round #603 (Div. 2) E - Editor(线段树,括号序列)
- Problem B. 即时战略 ———2019.10.12
题目: 代码~:感谢土蛋 #include <iostream> #include <cstring> #include <cmath> #include &l ...
- jQuery事件绑定与切换
一.事件绑定 1.标准方式 1. jquery标准的绑定方式 * jq对象.事件方法(回调函数): * 注:如果调用事件方法,不传递回调函数,则会触发浏览器默认行为. * 表单对象.submit(); ...
- [GXOI/GZOI2019]与或和(位运算,单调栈)
题目链接懒得放了. 题目大意懒得写了. 省选原题哪有找不到的…… 说实话,其实这题是个大水题,被我十秒钟内口胡出来了. 首先位运算除了拆位还能干啥?以下以与为例,或是差不多的. 我们考虑有多少个子矩阵 ...
- Django ORM性能优化 和 图片验证码
一,ORM性能相关 1. 关联外键, 只拿一次数据 all_users = models.User.objects.all().values('name', 'age', 'role__name') ...
- 1473. [Ioi2000]Post加强版 n log^2 n做法
1473. [Ioi2000]Post加强版 n log^2 n做法 题面 有n个城市从负方向向正方向按照1至n标号,\(d[i]\)表示城市i离原点的距离并且\(d[1] = 0\),对于\(i \ ...
- Redis内存回收策略
如果使用Redis的时候,不合理使用内存,把什么东西都放在内存里面,又不设置过期时间,就会导致内存的堆积越来越大.根据28法则,除了20%的热点数据之外,剩余的80%的非热点或不怎么重要的数据都在占用 ...
- builder模式实例
package heapStark.blogCode.designPattern.builder; public class BaseBean { private int age; private S ...
- [Gamma]Scrum Meeting#7
github 本次会议项目由PM召开,时间为6月2日晚上10点30分 时长10分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客,组织例会 撰写博客,组织例会 swoip 大作业截止,请假 前 ...
- C# IE浏览器操作类
using System; using System.Collections.Generic; using System.Drawing; using System.Runtime.InteropSe ...