Spring Data Elasticsearch存储库:嵌套对象的查询

时间:2016-12-22 09:20:45

标签: spring elasticsearch repository

我正在使用Spring Data Elasticsearch和一个存储库类。这是我要查询的My Object:

public class Person{
    String firstName;
    String lastName;
    Location location;
}

public class Location{
    String name;
}

@Query("{\"bool\" : {"
            + "\"should\" : [ "
                + "{"
                    + "\"term\" : {"
                        + "\"firstName\" : \"?0\""
                    + "}"
                + "},"
                + "{"
                    + "\"term\" : {"
                        + "\"lastName\" : \"?0\""
                    + "}"
                + "},"
                + "{"
                    + "\"term\" : {"
                        + "\"location.name\" : \"?0\""
                    + "}"
                + "}"
            + "]"
     + "}}")
List<Person> findByFirstNameOrLastName(String term);

我无法在location.name中搜索 - 如何更改查询,这将有效?

更新地图

{
    "properties" : {
        "firstName": {
            "type": "string",
            "analyzer": "firstNameNGram"
        },
        "lastName": {
            "type": "string",
            "analyzer": "firstNameNGram"
        },
        "location": {
            "type": "nested",
            "properties": {
                "name": {
                    "type": "string",
                    "analyzer": "firstNameNGram"
                },
                "company": {
                    "type": "nested",
                    "properties": {
                        "name": {
                            "type": "string",
                            "analyzer": "firstNameNGram"
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果嵌套了位置对象,您如何查看映射必须使用嵌套查询:

nested query

查询看起来像:

"nested" : {
  "query" : {
    "bool" : {
      "should" : [
        {
          "term" : {
            "location.name" : {
              "value" : "?0"
            }
          }
        }
      ]
    }
  },
  "path" : "location"
}