简体   繁体   English

如何使用Elasticsearch Java API执行以下查询?

[英]How to perform the following query with Elasticsearch Java API?

I am using spring data elasticsearch, I want to be able to perform the following search query on my index "fruits" of type "fruit": 我正在使用spring数据elasticsearch,我希望能够对类型为“ fruit”的索引“ fruits”执行以下搜索查询:

{
   'sort': {'fruitid': 'desc'}, 
   'query': {
      'query_string': {
         'query': u'Banana AND (start:>=1492274000000) AND (end:<=1386842400000)'
      }
   }, 
   'facets': {
      'fruit_color': {
          'terms': {'field': 'fruit_color', 'size': 5}
      }, 
      'fruit_weight': {
          'terms': {'field': 'fruit_weight', 'size': 5}
      }
}

How do I do this with the Java API? 我该如何使用Java API? Note I do not want to use any model class. 注意我不想使用任何模型类。

The Java code below shows you how to create two TermFacetRequest for the fruit color and weight and how to add them to your query by calling withFacet() . 下面的Java代码向您展示如何为水果的颜色和重量创建两个TermFacetRequest ,以及如何通过调用withFacet()将它们添加到查询中。 It also shows you how to add the query_string query using the QueryBuilders.queryStringQuery() method call. 这也说明了如何添加query_string使用查询QueryBuilders.queryStringQuery()方法调用。

// create the fruit color facet
TermFacetRequest fruitColor = new TermFacetRequest("fruit_color");
fruitColor.setFields("fruit_color");
fruitColor.setSize(5);

// create the fruit weight facet
TermFacetRequest fruitWeight = new TermFacetRequest("fruit_weight");
fruitWeight.setFields("fruit_weight");
fruitWeight.setSize(5);

SearchQuery searchQuery = new NativeSearchQueryBuilder()
    .withPageable(new PageRequest(0, 10))
    .withQuery(QueryBuilders.queryStringQuery("Banana AND (start:>=1492274000000) AND (end:<=1386842400000)"))                
    .withSort(SortBuilders.fieldSort("fruitId").order(SortOrder.DESC))                
    .withIndices("fruit").withTypes("fruit")
    .withFacet(fruitColor)            <--- add the fruit color facet
    .withFacet(fruitWeight);          <--- add the fruit weight facet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM