java客户端连接elasticsearch做一些QueryBuilders简单条件查询测试[模糊查询、多条件查询、范围查询、精确查询]

作者: admin 分类: ELK 发布时间: 2019-05-07 10:24  阅读: 611 views

程序猿有时候面对海量的api接口,需要一一尝试、确定自己想要的是哪种实现方式与结果,这是一个探索的过程。文档有时候写的很明确,但结果不对啊。这需要自己尝试下,才能安心。最近需求有碰到用elasticsearch做一些条件查询的问题,这里整理记录下。主要是在一批数据中,找到符合条件要求的数据。

测试数据如下:

 * 数据结构如下 68(取前20条) 
 * {aliasName=111, createTime=1555556190000, name=抢购商品 优惠券测试,
 * sellerName=上海鲜在时网络科技有限公司蓝村路店, salesNum=4, id=1022730, type=1, categoryName=女装, storeCity=杭州市}
 * {aliasName=龙岩商家-1号商品, createTime=1554279893000, name=龙岩商家-1号商品, sellerName=龙岩琅峰生态农业发展有限公司,
 * salesNum=2176, id=1022600, type=1, categoryName=女装} {aliasName=bug啊, brandName=品牌yan1581,
 * createTime=1556592633000, name=小数位溢出, sellerName=上海鲜在时网络科技有限公司蓝村路店, salesNum=7, id=1022740,
 * type=1, categoryName=女装, storeCity=杭州市} {aliasName=中香届商家-4号商品, brandName=品牌yan5659,
 * createTime=1555575085000, name=中香届商家-4号商品, sellerName=北京中香界文化产业发展有限公司, salesNum=0, id=1022660,
 * type=1, categoryName=女装, storeCity=杭州市} {aliasName=别名, brandName=品牌yan5659,
 * createTime=1551083669000, name=校验字段, sellerName=北京中香界文化产业发展有限公司, salesNum=0, labelTitle=爆款,
 * id=1022492, type=1, categoryName=女装} {aliasName=test, createTime=1553933715000, name=RMB-直充商品,
 * sellerName=商家yan5497, salesNum=8, id=1022461, type=1, categoryName=女装} {aliasName=22,
 * labelCity=杭州, createTime=1550216092000, name=左邻右舍实物商品, sellerName=左领右舍, salesNum=1,
 * labelCategory=美宿, labelTitle=双11, id=1022121, type=1, categoryName=女装, storeCity=澳门特别行政区(澳)}
 * {aliasName=rmbzf, brandName=品牌yan1581, createTime=1551680244000, name=人民币商品特卖,
 * sellerName=龙岩琅峰生态农业发展有限公司, salesNum=2, id=1022521, type=1, categoryName=女装} {aliasName=云味鲜花云腿饼
 * 8枚装 当鲜花遇见宣品名, labelCity=杭州,成都, createTime=1545272269000, name=云味鲜花云腿饼 8枚装 当鲜花遇见宣威火腿 咸香回甘 荡回肠
 * 乡思标题, sellerName=测试111111, labelDescribe=酒水, salesNum=0, labelCategory=美宿,休闲,餐饮, labelTitle=双11,
 * id=1021990, type=1, categoryName=女装} {aliasName=11, brandName=测试品牌001, createTime=1553494474000,
 * name=老开贝商品-实物, sellerName=商家yan6225, salesNum=1, labelCategory=美宿, id=1022419, type=1,
 * categoryName=女装, storeCity=杭州市} {aliasName=佣金, brandName=品牌yan1581, createTime=1556262732000,
 * name=创建分销商品, sellerName=龙岩琅峰生态农业发展有限公司, salesNum=104, id=1022570, type=1, categoryName=女装}
 * {aliasName=品名, createTime=1555299059000, name=抢购商品售卖 , sellerName=上海鲜在时网络科技有限公司蓝村路店, salesNum=8,
 * id=1022751, type=1, categoryName=女装, storeCity=杭州市} {aliasName=品名, createTime=1552463620000,
 * name=春款特惠, sellerName=商家yan3129, salesNum=0, id=1022580, type=1, categoryName=女装}
 * {aliasName=test, brandName=测试品牌001, createTime=1551165911000, name=水果小姐, sellerName=商家yan6225,
 * salesNum=0, labelTitle=首单专享, id=1022494, type=1, categoryName=女装} {aliasName=蒋氏,
 * createTime=1551680930000, name=长裙, sellerName=商家yan5497, salesNum=1, labelTitle=限时抢, id=1022490,
 * type=1, categoryName=女装, storeCity=杭州市} {aliasName=普通类目, createTime=1555299059000,
 * name=普通售卖商品购物车测试修改后的名字普通售卖商品购物车测试修改后的名字普通售卖商品购物车测试修改后的名字, sellerName=上海鲜在时网络科技有限公司蓝村路店,
 * salesNum=19, id=1022700, type=1, categoryName=女装, storeCity=杭州市} {aliasName=阿萨德,
 * createTime=1556075408000, name=分销商品3, sellerName=龙岩琅峰生态农业发展有限公司, salesNum=0, labelTitle=爆款,
 * id=1022590, type=1, categoryName=女装} {aliasName=测试, brandName=品牌yan1581,
 * createTime=1554871900000, name=抢购商品测试, sellerName=上海鲜在时网络科技有限公司蓝村路店, salesNum=0, id=1022720,
 * type=1, categoryName=女装, storeCity=杭州市} {aliasName=中香界商家-2号商品, brandName=品牌yan5659,
 * createTime=1556590248000, name=中香界商家-2号商品, sellerName=北京中香界文化产业发展有限公司, salesNum=633, id=1022601,
 * type=1, categoryName=女装} {aliasName=商户端商品, createTime=1548656293000, name=商户端商品,
 * sellerName=喜林阁度假旅馆(鼓浪屿世界文化遗产店), salesNum=1, labelCategory=美宿, id=1022260, type=1,
 * categoryName=女装}

以下是本地搭建的简单测试代码

package com.example.demo;

import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;


/**
 *
 * 
 * @author chenhailong
 * @date 2019年4月15日 下午3:54:21
 */
public class test {

  /**
   * @param args
   */
  public static void main(String[] args) {

    String indexName = "testitem";
    String typeName = "_doc";
    BoolQueryBuilder qb = QueryBuilders.boolQuery();
//  match_all
    qb.must(QueryBuilders.matchAllQuery()); //匹配所有记录68条

    SearchSourceBuilder ssb = new SearchSourceBuilder().query(qb).from(0).size(60);// 影响聚合查询
    try {
      SearchResponse response = new test().getSearchResult(indexName, typeName, ssb);
      System.out.println(response.getHits().totalHits);
      SearchHit hits[] = response.getHits().getHits();
      for (SearchHit hit : hits) {
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        System.out.println(sourceAsMap);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  public SearchResponse getSearchResult(String indexName, String typeName,
      SearchSourceBuilder searchSourceBuilder) throws Exception {
    return getClient().search(
        new SearchRequest().indices(indexName).types(typeName).source(searchSourceBuilder),
        RequestOptions.DEFAULT);
  }

  public SearchResponse getSearchResult(String indexName, String typeName, Integer page,
      Integer pageSize, String sortDesc, QueryBuilder queryBuilder,
      AggregationBuilder aggregationBuilder) throws Exception {
    return getClient().search(
        new SearchRequest().indices(indexName).types(typeName)
            .source(new SearchSourceBuilder().query(queryBuilder).from(page * pageSize)
                .size(pageSize).sort(sortDesc, SortOrder.DESC).aggregation(aggregationBuilder)),
        RequestOptions.DEFAULT);
  }


  public RestHighLevelClient getClient() {
    RestHighLevelClient client = null;
    String userName = "elastic";
    String userPwd = "123456";
    String ip = "127.0.0.1";
    int port = 9200;

    try {
      /** 用户认证对象 */
      final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
      /** 设置账号密码 */
      credentialsProvider.setCredentials(AuthScope.ANY,
          new UsernamePasswordCredentials(userName, userPwd));
      /** 创建rest client对象 */
      RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 9200))
          .setHttpClientConfigCallback(new HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(
                HttpAsyncClientBuilder httpClientBuilder) {
              return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }
          });
      client = new RestHighLevelClient(builder);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return client;
  }
}

match_all条件

qb.must(QueryBuilders.matchAllQuery()); //匹配所有记录68条

must条件

qb.must(QueryBuilders.termQuery("type", "1")) //68条
qb.must(QueryBuilders.termQuery("type","1")).must(QueryBuilders.termQuery("categoryName","女装")) //0条
qb.must(QueryBuilders.termQuery("categoryName", "女装")); //0条

以上说明QueryBuilders.termQuery无法作为列的精确查询

commonTermsQuery

qb.must(QueryBuilders.commonTermsQuery("categoryName", "女装")); //24条
qb
 .must(QueryBuilders.commonTermsQuery("categoryName", "女装"))
 .must(QueryBuilders.commonTermsQuery("storeCity", "杭州市"))
 .must(QueryBuilders.commonTermsQuery("labelCategory", "美宿")); //1条,此类条件精确查询是完全符合

matchPhraseQuery

qb.must(QueryBuilders.matchPhraseQuery("categoryName", "女装")) //24条,这个类似于 multiMatchQuery

multiMatchQuery

qb.must(QueryBuilders.multiMatchQuery("杭州市","storeCity"));  //16条,包含杭州市的都会出来

queryStringQuery

qb
.must(QueryBuilders.queryStringQuery("1").field("type"))
.must(QueryBuilders.queryStringQuery("女装").field("categoryName")); //49条 , 对女装进行分词, 实际“女装”24条

qb
.must(QueryBuilders.queryStringQuery("1").field("type"))
.must(QueryBuilders.queryStringQuery("杭州市").field("storeCity")); //16条, 分词

qb
.must(QueryBuilders.queryStringQuery("1").field("type"))
.must(QueryBuilders.queryStringQuery("\""+"女装"+"\"").field("categoryName")); //24条,对字符串添加双引号,完全匹配查询(不分词) //"\"女装\""

fuzzyQuery

qb.must(QueryBuilders.fuzzyQuery("categoryName", "女")); // 24条,模糊匹配查询,
// 如果prefix_length设置为0并且max_expansions设置为高数,则此查询可能非常繁重。 
这可能导致索引中的每个术语都被检查!

prefixQuery

qb.must(QueryBuilders.prefixQuery("categoryName", "女")); //24条,同上。这个是前缀匹配,上面的是任意位置

rangeQuery

qb.must(QueryBuilders.rangeQuery("createTime").gte("1551753685000").lt("1551853685000"));
//1条, 取时间范围内的
qb.must(QueryBuilders.rangeQuery("id").gt("1022541").lt("1022666")); 
//14条,取id,区间范围内的

existsQuery

qb.must(QueryBuilders.existsQuery("storeCity")); //17条,存在storeCity字段的

wildcardQuery

qb.must(QueryBuilders.wildcardQuery("brandName", "*yan*"));  //17条, 只适合英文字符串和单字,多个汉字查询不出来,如*发展*

typeQuery

qb.must(QueryBuilders.typeQuery("_doc"));  //68条, 查询索引文档类型的

idsQuery

qb.must(QueryBuilders.idsQuery("_doc").addIds("1021490","1022462","1022290")); //3条,根据id值查询数据

需要组合查询,适当调整  must,should,filter等关联条件

官方地址:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/query-dsl.html


   原创文章,转载请标明本文链接: java客户端连接elasticsearch做一些QueryBuilders简单条件查询测试[模糊查询、多条件查询、范围查询、精确查询]

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

更多阅读