2015-04-20 19 views
7

Utilizzando il suggeritore di completamento Elasticsearch ho problemi a restituire suggerimenti di immissione di più parole che corrispondono a una query di una sola parola.Il completamento di Elasticsearch suggerisce la ricerca con input a più parole

struttura Esempio:

PUT /test_index/ 
{ 
    "mappings": { 
     "item": { 
     "properties": { 
      "test_suggest": { 
       "type": "completion", 
       "index_analyzer": "whitespace", 
       "search_analyzer": "whitespace", 
       "payloads": false 
      } 
     } 
     } 
    } 
} 

PUT /test_index/item/1 
{ 
    "test_suggest": { 
     "input": [ 
     "cat dog", 
     "elephant" 
     ] 
    } 
} 

ricerca di lavoro:

POST /test_index/_suggest 
{ 
    "test_suggest":{ 
     "text":"cat", 
     "completion": { 
      "field" : "test_suggest" 
     } 
    } 
} 

con esito

{ 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "test_suggest": [ 
     { 
     "text": "cat", 
     "offset": 0, 
     "length": 3, 
     "options": [ 
      { 
       "text": "cat dog", 
       "score": 1 
      } 
     ] 
     } 
    ] 
} 

interrogazione mancanza:

POST /test_index/_suggest 
{ 
    "test_suggest":{ 
     "text":"dog", 
     "completion": { 
      "field" : "test_suggest" 
     } 
    } 
} 

con esito

{ 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "test_suggest": [ 
     { 
     "text": "dog", 
     "offset": 0, 
     "length": 3, 
     "options": [] 
     } 
    ] 
} 

mi si aspetterebbe lo stesso risultato della query di lavoro, Matching 'cane gatto'. Qualche suggerimento qual è il problema e come far funzionare la query in errore? Ottengo gli stessi risultati quando uso l'analizzatore standard invece dell'analizzatore di spazi bianchi. Vorrei usare più parole per stringa di input come mostrato nell'esempio sopra.

risposta

10

Il suggester di completamento è un prefix suggester, nel senso che cerca di far corrispondere la query ai primi caratteri degli input che è stato dato. Se vuoi che il documento che hai postato corrisponda al testo "cane", dovrai specificare "cane" come input.

PUT /test_index/item/1 
{ 
    "test_suggest": { 
     "input": [ 
     "cat dog", 
     "elephant", 
     "dog" 
     ] 
    } 
} 

Nella mia esperienza, la limitazione di dover specificare ingressi per abbinare rende suggesters completamento meno utile che altri modi per implementare la corrispondenza prefisso. Mi piace lo edge ngrams per questo scopo. Recentemente ho scritto un post su come utilizzare ngrams che potreste trovare utili: http://blog.qbox.io/an-introduction-to-ngrams-in-elasticsearch

Come esempio veloce, ecco una mappatura si potrebbe usare

PUT /test_index 
{ 
    "settings": { 
     "analysis": { 
     "filter": { 
      "edge_ngram_filter": { 
       "type": "edge_ngram", 
       "min_gram": 2, 
       "max_gram": 20 
      } 
     }, 
     "analyzer": { 
      "edge_ngram_analyzer": { 
       "type": "custom", 
       "tokenizer": "standard", 
       "filter": [ 
        "lowercase", 
        "edge_ngram_filter" 
       ] 
      } 
     } 
     } 
    }, 
    "mappings": { 
     "item": { 
     "properties": { 
      "text_field": { 
       "type": "string", 
       "index_analyzer": "edge_ngram_analyzer", 
       "search_analyzer": "standard" 
      } 
     } 
     } 
    } 
} 

quindi indicizzare il documento come questo:

PUT /test_index/item/1 
{ 
    "text_field": [ 
     "cat dog", 
     "elephant" 
    ] 
} 

e una qualsiasi di queste domande sarà restituirlo:

POST /test_index/_search 
{ 
    "query": { 
     "match": { 
      "text_field": "dog" 
     } 
    } 
} 

POST /test_index/_search 
{ 
    "query": { 
     "match": { 
      "text_field": "ele" 
     } 
    } 
} 

POST /test_index/_search 
{ 
    "query": { 
     "match": { 
      "text_field": "ca" 
     } 
    } 
} 

suo e il codice tutto insieme:

http://sense.qbox.io/gist/4a08fbb6e42c34ff8904badfaaeecc01139f96cf

+2

ricerca + query non tornando testo e nel mio caso il testo può essere qualsiasi cosa tra tre campi, così come ho potuto mostrare il testo esatto di completamento automatico. –

Problemi correlati