elasticsearch,java api, transport Client, 查询时索引库可以用通配符*和删除接口不能用

Posted 2412cyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch,java api, transport Client, 查询时索引库可以用通配符*和删除接口不能用相关的知识,希望对你有一定的参考价值。

1,获得transport client连接实例

  /**
     * 获取ES的连接client实例;
     *
     * @return TransportClient对象
     */
    private static TransportClient getTransportClient(String esIp) throws IOException {
        Settings settings = Settings.builder()
                .put(CLUSTER_NAME, "xxx")
                .put(ELASTIC.CLIENT_TRANSPORT_SNIFF, true)
                .build();
        TransportClient transportClient = TransportClient.builder().settings(settings).build();
        String[] ips =esIp.split(",");

        for (String ip : ips) {
            try {
                InetSocketTransportAddress ist = new InetSocketTransportAddress(InetAddress.getByName(ip),ELASTIC.ES_TRANSPORTCLIENT_PORT);
                transportClient.addTransportAddress(ist);
            }catch (Exception e){
            }
        }
        return transportClient;
    }

  

2,  封装查询对象,可以用* 通配索引库,以及设置最大返回数量

  @Test
    public  void querySearch() throws IOException {
   
        long l = System.currentTimeMillis();
        TransportClient transportClient = Factory.getTransportClient();

        //封装聚合的查询条件
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(TIME_FIELD).from(startTime).to(endTime);
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("xx", "xxx");
       // QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder().must(rangeQueryBuilder).must(matchQueryBuilder);
// 使用通配符‘*’来前缀匹配INDEX开头的所有索引库
        SearchResponse response=transportClient.prepareSearch("INDEX*")
        .setTypes("TYPE")
        // 设置查询类型
        // 1.SearchType.DFS_QUERY_THEN_FETCH = 精确查询
        // 2.SearchType.SCAN = 扫描查询,无序
        // 3.SearchType.COUNT = 不设置的话,这个为默认值,还有的自己去试试吧
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
        // 设置查询关键词
        .setQuery(boolQueryBuilder)
        // 设置查询数据的位置,分页用
        .setFrom(0)
        // 设置查询结果集的最大条数
        .setSize(1000)
        // 设置是否按查询匹配度排序
        .setExplain(true)
        // 最后就是返回搜索响应信息
        .execute()
        .actionGet();
        SearchHits searchHits=response.getHits();
//        System.out.println("-----------------在["+term+"]中搜索关键字["+queryString+"]---------------------");
        System.out.println(System.currentTimeMillis() - l);
        System.out.println("共匹配到:"+searchHits.getTotalHits()+"条记录!");
        SearchHit[]hits=searchHits.getHits();
        for(SearchHit searchHit:hits){
            Map<String, Object> sourceAsMap=searchHit.sourceAsMap();
            Set<String> keySet=sourceAsMap.keySet();
            for(String string:keySet){
                   //key value 值对应关系
                  System.out.println(string+":"+sourceAsMap.get(string));
            }
            System.out.println();
        }

    } 

3, 根据id删除索引文档接口,单个与批量,删除接口索引库不能用通配‘*‘来匹配

1,单个id删除索引文档

//INDEX:索引库名称(不能用通配符),TYPE:索引库的type ,ID索引库的ID  
DeleteResponse deleteResponse = client.prepareDelete("INDEX", "TYPE", "ID").get();       
//成功删除
if(deleteResponse.isFound()){
    successEs.add(rowkey);
 }else {
    failedEs.add(rowkey);
  }

2, 根据ids批量删除索引文档

@Test
public void removeDataFromEs() throws IOException { ArrayList<String> xxx= new ArrayList<>(); xxx.add("201801001"); xxx.add("201801001"); TransportClient client = Factory.getTransportClient(); BulkRequestBuilder builder=client.prepareBulk(); for(String id:xxx){ System.out.println(id); builder.add(client.prepareDelete("INDEX", "TYPE", id).request());
} } BulkResponse bulkResponse = builder.get();

2,Elasticseach根据索引的时间,进行范围查找数据时性能不太好。



以上是关于elasticsearch,java api, transport Client, 查询时索引库可以用通配符*和删除接口不能用的主要内容,如果未能解决你的问题,请参考以下文章

如何导入elasticsearch的JAVA API?

Elasticsearch Java API 很全的整理

Elasticsearch Java API - 客户端连接

java api操作elasticsearch

Elasticsearch Java API简要总汇

第08章 ElasticSearch Java API