Home > OS >  Multimatch query failure
Multimatch query failure

Time:07-19

Hi Team I am using elasticsearch after a long time and facing some difficulties with multi_match queries.

Essentially my query need to have an exact match on 2 fields and should do a text search on 4 fields.

Here is the exact query that I have prepared which is giving expected results.

GET /maintenance_logs/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "vinNumber.keyword": "DH34ASD7SDFF84742"
          }
        },
        {
          "term": {
            "organizationId": 1
          }
        }
      ],
      "minimum_should_match": 1,
      "should": [
        {
          "multi_match": {
            "query": "Cabin pressure",
            "fields": [
              "dtcCode",
              "subSystem",
              "maintenanceActivity",
              "description"
            ]
          }
        }
      ]
    }
  }
}

However when I am trying to convert this into Elasticsearch java, I am unable to get data back.

Here is the error:

org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]

    Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/maintenance_logs/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: For input string: \"DH34ASD7SDFF84742\"","index_uuid":"VnZg-NkFQmS-nSHbYemZkQ","index":"maintenance_logs"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"maintenance_logs","node":"_XMtzvY5TW-02IijVrR2Ww","reason":{"type":"query_shard_exception","reason":"failed to create query: For input string: \"DH34ASD7SDFF84742\"","index_uuid":"VnZg-NkFQmS-nSHbYemZkQ","index":"maintenance_logs","caused_by":{"type":"number_format_exception","reason":"For input string: \"DH34ASD7SDFF84742\""}}}]},"status":400}
        at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:326) ~[elasticsearch-rest-client-7.12.1.jar:7.12.1]
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:296) ~[elasticsearch-rest-client-7.12.1.jar:7.12.1]
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:270) ~[elasticsearch-rest-client-7.12.1.jar:7.12.1]
        at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1654) ~[elasticsearch-rest-high-level-client-7.12.1.jar:7.12.1]

Here is the Request:

public static SearchRequest buildSearchRequest(final String indexName,
                                                   final ElasticSearchDTO dto,
                                                   final MaintenanceLogsSearchDto maintenanceLogsSearchDto,
                                                   Pageable pageable) {
        try {
            final int page = pageable.getPageNumber();
            final int size =  pageable.getPageSize();
            final int from = page <= 0 ? 0 : pageable.getPageSize();

            SearchRequest searchRequest = new SearchRequest(indexName);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();


            final QueryBuilder vinQuery = QueryBuilders.termsQuery("vinNumber.keyword", maintenanceLogsSearchDto.getVinNumber());
            final QueryBuilder orgIdQuery = QueryBuilders.termsQuery("organizationId", maintenanceLogsSearchDto.getVinNumber());

            boolQueryBuilder.must(vinQuery);
            boolQueryBuilder.must(orgIdQuery);

            boolQueryBuilder.minimumShouldMatch(1);

            MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(dto.getSearchTerm(), "dtcCode", "subSystem", "maintenanceActivity", "description");
            multiMatchQueryBuilder.operator(Operator.AND);
            boolQueryBuilder.should(multiMatchQueryBuilder);

            searchSourceBuilder.query(boolQueryBuilder);

            searchSourceBuilder
                    .from(from)
                    .size(size)
                    .sort(SortBuilders.fieldSort("statsDate")
                                  .order(SortOrder.DESC));

            searchRequest.source(searchSourceBuilder);
            return searchRequest;
        } catch (final Exception e) {
            e.printStackTrace();
            return null;
        }
    }

CodePudding user response:

Looks like issue is with your vinQuery as it uses this DH34ASD7SDFF84742 and it seems you are trying to assign the number to your vinNumber.keyword using your Java DTO maintenanceLogsSearchDto.getVinNumber(), and once you query it in Elasticsearch, Elasticsearch is not able to convert it to number as it can't be converted to number.

Below log line in your logs give hint about the issue.

reason":"failed to create query: For input string: "DH34ASD7SDFF84742"","index_uuid":"VnZg-NkFQmS-nSHbYemZkQ","index":"maintenance_logs","caused_by":{"type":"number_format_exception","reason":"For input string: "DH34ASD7SDFF84742""}}}]},"status":400}

  • Related