编程语言
首页 > 编程语言> > ElasticSearch6.8常用查询以及JavaAPI

ElasticSearch6.8常用查询以及JavaAPI

作者:互联网

本文基于es6.8

索引创建

    public void createIndex(){
        //创建请求
        CreateIndexRequest request = new CreateIndexRequest("myindex");

        //settings
        Settings settings = Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "1").build();
        request.settings(settings);
        //mapping
        request.mapping(FileUtil.readResourceFile("mapping/mapping.json"), XContentType.JSON);
        //alias
        request.alias(new Alias("haha_index"));
        //超时时间
        request.setTimeout(TimeValue.timeValueSeconds(5));
        //主节点超时时间
        request.setMasterTimeout(TimeValue.timeValueSeconds(5));
        //设置创建索引API返回响应之前等待活动分片的数量
        request.waitForActiveShards(ActiveShardCount.from(1));
        //别名
        request.alias(new Alias("myhahaindex"));
        //执行
        try {
            CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
            //获取数据
            System.out.println(response.isAcknowledged());
            System.out.println(response.isShardsAcknowledged());
            System.out.println(response.index());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

对应请求

PUT myindex?master_timeout=5s&wait_for_active_shards=1&timeout=5s
{
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    },
    "mappings":{
      "mymapping": {
        "properties": {
          "name": {
            "type": "text"
          },
          "description": {
            "type": "text",
            "analyzer":"english",
            "search_analyzer":"english"
          },
          "pic":{
            "type":"text",
            "index":false
          },
          "studymodel":{
            "type":"text"
          }
        }
      }
    }, 
    "aliases": {
        "myhahaindex": {}
    }
}

搜索

搜索全部

GET /book/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name","description"]
}

java

        SearchRequest request = new SearchRequest("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //获取指定字段
        searchSourceBuilder.fetchSource(new String[]{"name","description"},null);

        request.source(searchSourceBuilder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        SearchHit[] searchHits = hits.getHits();

        for (SearchHit searchHit : searchHits) {

            System.out.println(searchHit.getSourceAsString());

            Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }

分页查询

GET /book/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name","description"],
  "from": 0,
  "size": 2
}

java

        SearchRequest request = new SearchRequest("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //获取指定字段
        searchSourceBuilder.fetchSource(new String[]{"name","description"},null);

        searchSourceBuilder.from(1);
        searchSourceBuilder.size(2);

        request.source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        SearchHit[] searchHits = hits.getHits();

        for (SearchHit searchHit : searchHits) {

            System.out.println(searchHit.getSourceAsString());

            Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }

ids查询

GET /book/_search
{
    "query": {
        "ids": {
            "values": [1,2,10]
        }
    }
}

java

searchSourceBuilder.query(QueryBuilders.idsQuery().addIds("1","2","10"));

match

GET /book/_search
{
    "query": {
        "match": {
            "description": "java程序员"
        }
    }
}

java

searchSourceBuilder.query(QueryBuilders.matchQuery("description","java程序员"));

multi_match

searchSourceBuilder.query(QueryBuilders.multiMatchQuery("java","name","description"));

java

searchSourceBuilder.query(QueryBuilders.multiMatchQuery("java","name","description"));

bool

#filter不参与相关度评分
GET /book/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "description": "java程序员"
          }
        }
      ],
      "filter": {
        "range": {
          "price": {
            "gte": 80,
            "lte": 90
          }
        }
      }
    }
  }
}

java

        SearchRequest request = new SearchRequest("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(new MatchQueryBuilder("description","java程序员"));
        boolQueryBuilder.filter(new RangeQueryBuilder("price").gte(80).lte(90));
        searchSourceBuilder.query(boolQueryBuilder);

        request.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        System.out.println(searchResponse);
GET /book/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "description": "java程序员"
          }
        },
        {
          "range": {
            "price": {
              "gte": 80,
              "lte": 90
            }
          }
        }
      ]
    }
  }
}

java

        SearchRequest request = new SearchRequest("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(new MatchQueryBuilder("description","java程序员"));
        boolQueryBuilder.must(new RangeQueryBuilder("price").gte(80).lte(90));

sort

GET /book/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

java

        SearchRequest request = new SearchRequest("book");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.sort("price", SortOrder.DESC);

分组聚合查询

1

按照颜色分组,计算每个颜色卖出的个数

GET /tvs/_search
{
  "size": 0,
  "aggs": {
    "color_group": {
      "terms": {
        "field": "color"
      }
    }
  }
}

java

SearchRequest request = new SearchRequest("tvs");

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(0);
searchSourceBuilder.query(QueryBuilders.matchAllQuery());

AggregationBuilder aggregationBuilder = AggregationBuilders.terms("color_group")
    .field("color");
searchSourceBuilder.aggregation(aggregationBuilder);
request.source(searchSourceBuilder);

SearchResponse response = client.search(request, RequestOptions.DEFAULT);

Aggregations aggregations = response.getAggregations();

Terms terms = aggregations.get("color_group");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
for (Terms.Bucket bucket : buckets) {
    System.out.println(bucket.getKey());
    System.out.println(bucket.getDocCount());
}

2

按照颜色分组,计算每个颜色卖出的个数,每个颜色卖出的平均价格

GET /tvs/_search
{
  "size": 0,
  "aggs": {
    "color_group": {
      "terms": {
        "field": "color"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

java:

SearchRequest request = new SearchRequest("tvs");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(0);
searchSourceBuilder.query(QueryBuilders.matchAllQuery());

AggregationBuilder aggregationBuilder = AggregationBuilders.terms("color_group")
    .field("color");
AvgAggregationBuilder avgAgg = AggregationBuilders.avg("avg_price").field("price");
aggregationBuilder.subAggregation(avgAgg);

searchSourceBuilder.aggregation(aggregationBuilder);
request.source(searchSourceBuilder);

SearchResponse response = client.search(request, RequestOptions.DEFAULT);

Aggregations aggregations = response.getAggregations();

Terms terms = aggregations.get("color_group");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
for (Terms.Bucket bucket : buckets) {
    System.out.println(bucket.getKey());
    System.out.println(bucket.getDocCount());

    Aggregations subAggs = bucket.getAggregations();
    Avg avg = subAggs.get("avg_price");
    System.out.println(avg.getValue());

}

3

按照颜色分组,计算每个颜色卖出的个数,以及每个颜色卖出的价格平均值、最大值、最小值、总和

GET /tvs/_search
{
  "size": 0,
  "aggs": {
    "color_group": {
      "terms": {
        "field": "color"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        },
        "max_price":{
          "max": {
            "field": "price"
          }
        },
        "min_price":{
          "min": {
            "field": "price"
          }
        },
        "sum_price":{
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

java

SearchRequest request = new SearchRequest("tvs");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(0);
searchSourceBuilder.query(QueryBuilders.matchAllQuery());

AggregationBuilder aggregationBuilder = AggregationBuilders.terms("color_group")
    .field("color");

AvgAggregationBuilder avgPrice = AggregationBuilders.avg("avg_price").field("price");
MaxAggregationBuilder maxPrice = AggregationBuilders.max("max_price").field("price");
MinAggregationBuilder minPrice = AggregationBuilders.min("min_price").field("price");
SumAggregationBuilder sumPrice = AggregationBuilders.sum("sum_price").field("price");

aggregationBuilder.subAggregation(avgPrice);
aggregationBuilder.subAggregation(maxPrice);
aggregationBuilder.subAggregation(minPrice);
aggregationBuilder.subAggregation(sumPrice);

searchSourceBuilder.aggregation(aggregationBuilder);
request.source(searchSourceBuilder);

SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Aggregations aggregations = response.getAggregations();
Terms terms = aggregations.get("color_group");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
for (Terms.Bucket bucket : buckets) {
    System.out.println(bucket.getKey());
    System.out.println(bucket.getDocCount());

    Aggregations subAggs = bucket.getAggregations();
    Avg avg = subAggs.get("avg_price");
    System.out.println(avg.getValue());
    Max max = subAggs.get("max_price");
    System.out.println(max.getValue());
    Min min = subAggs.get("min_price");
    System.out.println(min.getValue());
    Sum sum = subAggs.get("sum_price");
    System.out.println(sum.getValue());

}

4

按照售价价格划分范围(2000),算出每个区间的销售总额

GET /tvs/_search
{
  "size": 0,
  "aggs": {
    "price_scope": {
      "histogram": {
        "field": "price",
        "interval": 2000
      },
      "aggs": {
        "sum_price": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

java

SearchRequest request = new SearchRequest("tvs");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(0);
searchSourceBuilder.query(QueryBuilders.matchAllQuery());

HistogramAggregationBuilder histogramAggregationBuilder = AggregationBuilders.histogram("price_scope").field("price").interval(2000);
SumAggregationBuilder sumAggregationBuilder = AggregationBuilders.sum("sum_price").field("price");
histogramAggregationBuilder.subAggregation(sumAggregationBuilder);

searchSourceBuilder.aggregation(histogramAggregationBuilder);
request.source(searchSourceBuilder);

SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);

Aggregations aggregations = searchResponse.getAggregations();

Histogram histogram = aggregations.get("price_scope");
List<? extends Histogram.Bucket> buckets = histogram.getBuckets();
for (Histogram.Bucket bucket : buckets) {
    System.out.println(bucket.getKey());
    System.out.println(bucket.getDocCount());
    Sum sum = bucket.getAggregations().get("sum_price");
    System.out.println(sum.getValue());
}

5

计算每个季度的销售总额,并按照总额降序查询

GET /tvs/_search
{
  "size": 0,
  "aggs": {
    "quarters_price": {
      "date_histogram": {
        "field": "sold_date",
        "interval": "quarter",
        "format": "yyyy-MM-dd",
        "order": {
          "sum_price": "desc"
        }
      },
      "aggs": {
        "sum_price": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

java

        SearchRequest request = new SearchRequest("tvs");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.size(0);
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());

        DateHistogramAggregationBuilder histogramAggregationBuilder = AggregationBuilders.dateHistogram("quarters_price")
                .field("sold_date")
                .dateHistogramInterval(DateHistogramInterval.QUARTER)
                .format("yyyy-MM-dd")
                .order(BucketOrder.aggregation("sum_price",false));

        SumAggregationBuilder sumPrice = AggregationBuilders.sum("sum_price").field("price");
        histogramAggregationBuilder.subAggregation(sumPrice);

        searchSourceBuilder.aggregation(histogramAggregationBuilder);
        request.source(searchSourceBuilder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        Histogram histogram = response.getAggregations().get("quarters_price");
        List<? extends Histogram.Bucket> buckets = histogram.getBuckets();
        for (Histogram.Bucket bucket : buckets) {
            System.out.println(bucket.getKeyAsString());
            System.out.println(bucket.getDocCount());
            Sum sum = bucket.getAggregations().get("sum_price");
            System.out.println(sum.getValue());
        }

标签:ElasticSearch6.8,searchSourceBuilder,price,request,查询,JavaAPI,println,new,out
来源: https://www.cnblogs.com/wwjj4811/p/14722521.html