数据库
首页 > 数据库> > elasticsearch Terms Query 实现类似于sql in查询

elasticsearch Terms Query 实现类似于sql in查询

作者:互联网

pom.xml:

<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.1.0</version>
        </dependency>

代码:

import com.alibaba.fastjson.JSON;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
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.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;


public class EsToHdfs {
    public static void main(String[] args) throws Exception {
        Settings settings = Settings.builder().put("cluster.name", "NEIMENG_ES").build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.26.82"), 9301))
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.26.83"), 9301));
        Set set = new HashSet<String>();set.add("50338118");
        set.add("67108867");
        set.add("67109804");
        set.add("67112880");
        set.add("67112989");
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.termsQuery("x_imsi", set));

        SearchResponse response = client.prepareSearch("neimeng_vpn_202001191559").setTypes("doc").setQuery(boolQuery)
                .setSize(10000).execute().actionGet();
for (SearchHit hit : response.getHits().getHits()) {
            System.out.println("x_imsi:" + hit.getSourceAsMap().get("x_imsi") + ", x_sip:" + hit.getSourceAsMap().get("x_sip") + ", seraddr_s:" + hit.getSourceAsMap().get("seraddr_s"));
        }//on shutdown
        client.close();
    }


}

kibana:

{
  "query": {
    "terms": {
      "x_imsi": [
         "50332823"
        ,"67108867"
        ,"67109804"
        ,"67112880"
        ,"67112989"
      ]
    }
  },
  "_source": ["x_imsi","x_sip","seraddr_s"]
}

 

标签:set,Terms,java,sql,client,elasticsearch,org,import
来源: https://www.cnblogs.com/chong-zuo3322/p/13897946.html