本文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(); }
}

最新文章

  1. 虚拟机centos6.5 --开放端口
  2. Java中String和Int的相互转换
  3. 【项目经验】——JSON.parse() &amp;&amp; JSON.stringify()
  4. OpenResty 安装 drizzle-nginx-module
  5. 一种比较少见的C#代码段
  6. linux gcc头文件搜索路径
  7. C#中Config文件中,特殊符号的书写方法。
  8. Qt源代码分析
  9. Hadoop2.2 federnation联盟的搭建
  10. hihocoder 1038 01背包
  11. Android 四大组件之service与Broadcast
  12. Java 中泛型的全面解析(转)
  13. directdraw显示yuv420(YV12)
  14. [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二
  15. 在react中使用less(官方做法)
  16. Git-命令行-使用 Tag 标记你的代码
  17. c#dataGridView 知识
  18. (转)深入研究 蒋金楠(Artech)老师的 MiniMvc(迷你 MVC),看看 MVC 内部到底是如何运行的
  19. ROS学习(八)—— 理解ROS服务和参数
  20. length()

热门文章

  1. Windows server 2003 粘滞键后门+提权
  2. 网页网站基础入门篇: 使用Adobe Dreamweaver CS6 制作网页/网站
  3. gcc 在c代码中内嵌汇编调用c函数: 只是证明曾经我来过
  4. .net core 读取配置文件的值
  5. vue报错: Class constructor FileManager cannot be invoked without &#39;new&#39;.
  6. Linux 就该这么学 CH09 使用ssh服务管理远程主机
  7. 非mvn项目转为mvn项目并构建mvn私服
  8. cad.net 在位编辑的原理猜测及找到在位编辑状态的图元
  9. 2 Linux性能优化--工具图
  10. 【LeetCode】缺失的第一个正数【原地HashMap】