2014-07-02 10 views
11

Sto provando a interrogare proprietà nidificate con più valori.Cerca un campo nidificato per più valori sullo stesso campo con elasticsearch

Ecco un esempio che sarà più chiaro.

Creare un indice con un campo nidificato

curl -X DELETE "http://localhost:9200/testing_nested_query/" 
    curl -X POST "http://localhost:9200/testing_nested_query/" -d '{ 
     "mappings": { 
      "class": { 
       properties: { 
       title: {"type": "string"}, 
       "students": { 
        "type": "nested", 
        "properties": { 
        "name": {"type": "string"} 
        } 
       } 
       } 
      } 
     } 

    }' 

Aggiungere alcuni valori

curl -XPUT 'http://localhost:9200/testing_nested_query/class/1' -d '{ 
     "title": "class1", 
     "students": [{"name": "john"},{"name": "jack"},{"name": "jim"}] 
    }' 

    curl -XPUT 'http://localhost:9200/testing_nested_query/class/2' -d '{ 
     "title": "class2", 
     "students": [{"name": "john"},{"name": "chris"},{"name": "alex"}] 
    }' 

query per tutte le classi in cui John è (2 risultati come previsto)

curl -XGET 'http://localhost:9200/testing_nested_query/class/_search' -d '{ 
    "query": { 
    "nested": { 
     "path":"students", 
     "query": { 
     "bool": { 
      "must": [ 
      {"match": {"students.name": "john"}} 
      ] 
     } 
     } 
    } 
    } 
}' 

query per le classi in cui entrambi John e Jack sono Parteciperò (0 risultati invece di 1)

curl -XGET 'http://localhost:9200/testing_nested_query/class/_search' -d '{ 
    "query": { 
    "nested": { 
     "path":"students", 
     "query": { 
     "bool": { 
      "must": [ 
      {"match": {"students.name": "john"}}, 
      {"match": {"students.name": "jack"}} 
      ] 
     } 
     } 
    } 
    } 
}' 

ho provato con la corrispondenza e filtro ma non posso mai avere la query per restituire i valori previsti.

+0

La query avrebbe funzionato semplicemente usando "dovrebbe" invece di "deve". – plmaheu

+0

No, con "dovrebbe" Restituisce 2 colpi invece di uno. –

+0

Hai ragione, ho letto male la domanda. – plmaheu

risposta

18

ha solo bisogno di un cambiamento po ':

{ 
    "query": { 
    "bool": { 
     "must": [ 
      { 
       "nested": { 
        "path":"students", 
        "query": { 
        "bool": { 
         "must": [ 
         {"match": {"name": "john"}} 
         ] 
        } 
        } 
       } 
      }, 
      { 
       "nested": { 
        "path":"students", 
        "query": { 
        "bool": { 
         "must": [ 
         {"match": {"name": "jack"}} 
         ] 
        } 
        } 
       } 
      } 
     ] 
    } 
    } 
} 

Perché?

Fondamentalmente, in una query nidificata, la query e il filtro vengono eseguiti collettivamente su un singolo documento nidificato - nel tuo caso un nome. Quindi la tua query raccoglierà tutti i documenti nidificati e cercherà di trovare tutti i documenti che hanno uguale a john e jack allo stesso tempo - il che è impossibile.

La mia domanda cerca di trovare un documento indicizzato che ha un documento nidificato con name pari a john e un altro documento nidificato con name pari a jack. Quindi, fondamentalmente una query nidificata cerca di abbinare completamente un documento nidificato.

per dimostrare quello che sto suggerendo, provate questo:

creare lo stesso indice con stessa mappatura, come avete fatto

** Poi indice i seguenti documenti **

curl -XPUT 'http://localhost:9200/testing_nested_query/class/1' -d '{ 
     "title": "class1", 
     "students": [{"name": "john", "age": 4},{"name": "jack", "age": 1},{"name": "jim", "age": 9}] 
    }' 

curl -XPUT 'http://localhost:9200/testing_nested_query/class/2' -d '{ 
     "title": "class1", 
     "students": [{"name": "john", "age": 5},{"name": "jack", "age": 4},{"name": "jim", "age": 9}] 
    }' 

Esegui ora le seguenti domande:

{ 
    "query": { 
     "nested": { 
      "path":"students", 
      "query": { 
      "bool": { 
       "must": [ 
       {"match": {"name": "john"}}, 
       {"match": {"age": 4}} 
       ] 
      } 
      } 
     } 
    } 
} 

Secondo le vostre aspettative, questo dovrebbe corrispondere a 2 documenti ma in realtà corrisponde solo a uno. Perché esiste un solo documento nidificato che ha sia john sia uguale a 4.

Spero che questo aiuti.

+0

Funziona perfettamente. Grazie per la spiegazione. –

2

Si potrebbe anche seguire la strada.dove non è necessario ripetere ancora una volta bool in un blocco nidificato, poiché non v'è un unico a corrispondere entro quel blocco, si può solo fare partita termine senza bool

{ 
 
    "query": { 
 
    "bool": { 
 
     "must": [{ 
 
     "nested": { 
 
      "path": "students", 
 
      "query": { 
 
      { 
 
       "term": { 
 
       "name": "john" 
 
       } 
 
      } 
 
      } 
 
     } 
 
     }, { 
 
     "nested": { 
 
      "path": "students", 
 
      "query": { 
 
      { 
 
       "term": { 
 
       "name": "jack" 
 
       } 
 
      } 
 
      } 
 
     } 
 
     }] 
 
    } 
 
    } 
 
}

Problemi correlati