弹簧数据Elasticsearch存储库:查询嵌套对象



我正在使用带有存储库类的春季数据弹性搜索。这是我要查询的对象:

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);

我无法在位置搜索。名称 - 如何更改此查询的问题?

更新映射

{
    "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"
                        }
                    }
                }
            }
        }
    }
}

如果位置对象的嵌套,则必须使用嵌套查询:

嵌套查询

查询看起来像:

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

最新更新