2016-12-22 3 views
0

Ich verwende Spring Data Elasticsearch mit einer Repository-Klasse. Hier ist mein Objekt, das ich will Abfrage durch:Spring Data Elasticsearch Repository: Abfrage für verschachteltes Objekt

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

Ich kann nicht in location.name suchen - wie kann ich die Abfrage ändern, dass dies funktionieren wird?

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

Antwort

1

Wie Ihr Mapping sieht, wenn Standort-Objekt verschachtelt Sie verschachtelte Abfrage verwenden müssen:

nested query

Abfrage wie folgt aussieht:

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

Aktualisiert mein Fragenabschnitt w ith added Mapping –

+0

Großartig, das hat für mich funktioniert !! –

Verwandte Themen