0

I am using Spring Data Elasticsearch with a repository class. Here is My Object that I want to query through:

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

I can not search in location.name - how can I change the Query that this will work?

UPDATE Mapping

{
    "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 Answer 1

2

How looks your mapping if location object is nested you must use nested query :

nested query

query looks like:

"nested" : {
  "query" : {
    "bool" : {
      "should" : [
        {
          "term" : {
            "location.name" : {
              "value" : "?0"
            }
          }
        }
      ]
    }
  },
  "path" : "location"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Updated my Questionsection with added Mapping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.