Search code examples
javaelasticsearchspring-data-elasticsearch

Operator IN in ElasticSearch 7


I have a project on Spring data elasticSearch. Model:

@Document(indexName = "house", createIndex = false)
public class House {
    @Id
    private String id;
    private String aoGuid;
    private String buildNum;
    private String houseGuid;
    private String houseId;
    private String houseNum;
    private String postalCode;
    private String regionCode;
}

Repository:

@Query("{\n" +
        "  \"bool\": {\n" +
        "    \"must\": [\n" +
        "      {\n" +
        "        \"bool\": {\n" +
        "          \"must\": [\n" +
        "            {\n" +
        "              \"terms\": {\n" +
        "                \"aoGuid\": \"[?0]\"\n" +
        "              }\n" +
        "            }\n" +
        "          ]\n" +
        "        }\n" +
        "      }\n" +
        "    ]\n" +
        "  }\n" +
        "}")
List<House> findByAoGuidIn(Collection<String> aoGuid);

My index in Elastic:

{
  "house": {
    "aliases": {},
    "mappings": {
      "properties": {
        "_class": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "aoGuid": {
          "type": "keyword"
        },
        "buildNum": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "houseGuid": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "houseId": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "houseNum": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "postalCode": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "regionCode": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    },
    "settings": {
      "index": {
        "search": {
          "slowlog": {
            "threshold": {
              "query": {
                "info": "1ms"
              }
            }
          }
        },
        "number_of_shards": "1",
        "provided_name": "house",
        "creation_date": "1582210642568",
        "number_of_replicas": "1",
        "uuid": "c43T1LthTH6LhTphjZ-Ulw",
        "version": {
          "created": "7040099"
        }
      }
    }
  }
}

When I make a call to the findByAoGuidIn method, I get an error:

org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=parsing_exception, reason=[terms] query does not support [aoGuid]] at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177) ~[elasticsearch-7.4.0.jar:7.4.0] at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1727) ~[elasticsearch-rest-high-level-client-7.4.0.jar:7.4.0] at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1704) ~[elasticsearch-rest-high-level-client-7.4.0.jar:7.4.0] at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1467) ~[elasticsearch-rest-high-level-client-7.4.0.jar:7.4.0] at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1424) ~[elasticsearch-rest-high-level-client-7.4.0.jar:7.4.0] at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1394) ~[elasticsearch-rest-high-level-client-7.4.0.jar:7.4.0]

I took a query from the documentation at this link: https://docs.spring.io/spring-data/elasticsearch/docs/4.0.x/reference/html/#elasticsearch.query-methods

How can I fix a error?


Solution

  • Thanks @Val, you led me on the right path of thinking. The problem was how I form the collection. And I did not provide this in my question. I post the corrected code:

    List<String> aoGuidList = new ArrayList<>();
    it.getParts().forEach(itt -> {
        aoGuidList.add("\"" + itt.getAoGuid() + "\"");
    });
    
    queryHouseService.search(aoGuidList);
    

    And now my repository:

    @Query("{\n" +
            "  \"bool\": {\n" +
            "    \"must\": [\n" +
            "      {\n" +
            "        \"bool\": {\n" +
            "          \"must\": [\n" +
            "            {\n" +
            "              \"terms\": {\n" +
            "                \"aoGuid\": ?0\n" +
            "              }\n" +
            "            }\n" +
            "          ]\n" +
            "        }\n" +
            "      }\n" +
            "    ]\n" +
            "  }\n" +
            "}")
    List<House> findByAoGuidIn(Collection<String> aoGuid);
    

    Indeed, I had to make sure that my collection in the request corresponded to the pattern ["val1", "val2"]. And in my erroneous version, I got a collection of the form: [val1, val2] or ["val1, val2"]