本文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 的数据。
elasticsearch Terms Query 实现类似于sql in查询
原文:https://www.cnblogs.com/chenmz1995/p/11248769.html