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