简体   繁体   中英

Elastic termsQuery not giving expected result

I have an index where each of my objects has status field which can have some predefined values. I want to fetch all of them which has status INITIATED , UPDATED , DELETED , any match with these and hence created this query by java which I got printing on console, using Querybuilder and nativeSearchQuery, executing by ElasticsearchOperations :

 {
  "bool" : {
    "must" : [
      {
        "terms" : {
          "status" : [
            "INITIATED",
            "UPDATED",
            "DELETED"
          ],
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

I have data in my index with 'INITIATED' status but not getting anyone with status mentioned in the query. How to fix this query, please? If you need anything, please let me know.

Update: code added

NativeSearchQueryBuilder nativeSearchQueryBuilder=new NativeSearchQueryBuilder();
QueryBuildersingleQb=QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("status",statusList));
                                        
  Pageable pageable = PageRequest.of(0, 1, Sort.by(Defs.START_TIME).ascending());
                FieldSortBuilder sort = SortBuilders.fieldSort(Defs.START_TIME).order(SortOrder.ASC); 
  nativeSearchQueryBuilder.withQuery(singleQb);
  nativeSearchQueryBuilder.withSort(sort);
  nativeSearchQueryBuilder.withPageable(pageable);
                nativeSearchQueryBuilder.withIndices(Defs.SCHEDULED_MEETING_INDEX);
                nativeSearchQueryBuilder.withTypes(Defs.SCHEDULED_MEETING_INDEX);
    
 NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
 List<ScheduledMeetingEntity> scheduledList=elasticsearchTemplate.queryForList(searchQuery, ScheduledMeetingEntity.class);

Update 2: sample data:

I got this from kibana query on this index:
 "hits" : [
      {
        "_index" : "index_name",
        "_type" : "type_name",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "createTime" : "2021-03-03T13:09:59.198",
          "createTimeInMs" : 1614755399198,
          "createdBy" : "user1@domain.com",
          "editTime" : "2021-03-03T13:09:59.198",
          "editTimeInMs" : 1614755399198,
          "editedBy" : "user1@domain.com",
          "versionId" : 1,
          "id" : "1",
          "meetingId" : "47",
          "userId" : "129",
          "username" : "user1@domain.com",
          "recipient" : [
            "user1@domain.com"
          ],
          "subject" : "subject",
          "body" : "hi there",
          "startTime" : "2021-03-04T07:26:00.000",
          "endTime" : "2021-03-04T07:30:00.000",
          "meetingName" : "name123",
          "meetingPlace" : "placeName",
          "description" : "sfsafsdafsdf",
          "projectName" : "",
          "status" : "INITIATED",
          "failTry" : 0
        }
      }
    ]

Confirm your mapping:

GET /yourIndexName/_mapping

And see if it is valid

Your mapping needs to have keyword for TermsQuery to work.

{
  "status": {
    "type" "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  }
}

ES can automatically do the mapping for you (without you having to do it yourself) when you first push a document. However you probably have finer control if you do the mapping yourself.

Either way, you need to have keyword defined for your status field.

===================== Alternative Solution: (Case Insensitive)

If you have a Field named (status), and the values you want to search for are (INITIATED or UPDATED, or DELETED).

Then you can do it like this:

  BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(createStringSearchQuery());

public QueryBuilder createStringSearchQuery(){
            QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(" INITIATED OR UPDATED OR DELETED ");
            queryBuilder.defaultField("status");

     return queryBuilder;


}

Printing the QueryBuilder:

{
    "query_string" : {
      "query" : "INITIATED OR UPDATED OR DELETED",
      "default_field" : "status",
      "fields" : [ ],
      "type" : "best_fields",
      "default_operator" : "or",
      "max_determinized_states" : 10000,
      "enable_position_increments" : true,
      "fuzziness" : "AUTO",
      "fuzzy_prefix_length" : 0,
      "fuzzy_max_expansions" : 50,
      "phrase_slop" : 0,
      "escape" : false,
      "auto_generate_synonyms_phrase_query" : true,
      "fuzzy_transpositions" : true,
      "boost" : 1.0
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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