2014-12-04 12 views
7

Ho un campo in Elasticsearch con il valore "PEI.H.02354.01.". Quando cerco con querystring comeRicerca Elasticsearch non riesce in campo con carattere speciale e carattere jolly

{ 
    "query":{ 
     "query_string":{ 
     "query":"field:PEI.H.02354.01.", 
     "default_operator":"AND" 
     } 
    } 
} 

poi il risultato viene restituito, che è il comportamento corretto. Ma se eseguo una ricerca con un carattere jolly, non vengono restituiti risultati, ad es.

{ 
    "query":{ 
     "query_string":{ 
     "query":"field:PEI.H.02354.01.*", 
     "default_operator":"AND" 
     } 
    } 
} 

Il campo è di tipo stringa e analizzato. Di seguito è riportato il codice che crea l'indice, inclusi l'analizzatore e i mapping.

{ 
    "settings":{ 
     "analysis":{ 
     "analyzer":{ 
      "number":{ 
       "type":"custom", 
       "tokenizer":"keyword", 
       "filter":[ 
        "lowercase" 
       ], 
       "char_filter":[ 
        "number_filter" 
       ] 
      }, 
      "diacritical":{ 
       "type":"custom", 
       "tokenizer":"standard", 
       "filter":[ 
        "standard", 
        "lowercase", 
        "asciifolding", 
        "nfd_normalizer" 
       ] 
      } 
     }, 
     "filter":{ 
      "nfd_normalizer":{ 
       "type":"icu_normalizer", 
       "name":"nfc" 
      } 
     }, 
     "char_filter":{ 
      "number_filter":{ 
       "type":"pattern_replace", 
       "pattern":"[^\\d]+", 
       "replacement":"" 
      } 
     } 
     } 
    }, 
    "mappings":{ 
     "testType":{ 
     "_source":{ 
      "enabled":false 
     }, 
     "_all":{ 
      "enabled":false 
     }, 
     "_timestamp":{ 
      "enabled":"true", 
      "store":"yes" 
     }, 
     "properties":{ 
      "field":{ 
       "store":"yes", 
       "type":"string", 
       "index":"analyzed", 
       "analyzer":"diacritical" 
      } 
     } 
    }  
} 

Infine, un inserto campione è

{ 
    field: "PEI.H.02354.01." 
} 

Qualcuno ha qualche idea del perché questo sta accadendo e come risolvere questo?

risposta

8

Vedere la documentazione query_string:

termini caratteri jolly non vengono analizzati per default - sono in minuscolo (lowercase_expanded_terms default è vero), ma non occorre un'ulteriore analisi è fatto

i dati memorizzati è suddiviso in due termini:

curl -XGET 'localhost:9200/myindex/_analyze?analyzer=diacritical&pretty' -d 'PEI.H.02354.01' 
{ 
    "tokens" : [ { 
    "token" : "pei.h", 
    "start_offset" : 0, 
    "end_offset" : 5, 
    "type" : "<ALPHANUM>", 
    "position" : 1 
    }, { 
    "token" : "02354.01", 
    "start_offset" : 6, 
    "end_offset" : 14, 
    "type" : "<NUM>", 
    "position" : 2 
    } ] 
} 

ma come il termine di ricerca con un carattere jolly è attivata solo in pei.h.02354.01.* esso non corrisponderà.

comunque con analyze_wildcard set per vero, si vuole ricevere visite:

curl -XGET "http://localhost:9200/myindex/testType/_search?pretty" -d' 
> { 
> "query":{ 
>  "query_string":{ 
>   "query":"field:PEI.H.02354.01.*", 
>   "default_operator":"AND", 
>   "analyze_wildcard": true 
>  } 
> } 
> }' 
{ 
    "took" : 5, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 2, 
    "max_score" : 1.4142135, 
+0

Tramite questa ritorna in effetti i risultati, ma non tutti sono corrette, per esempio Ottengo anche PEI.H.4545.01. Ciò ha senso se, come hai detto, il termine di ricerca viene analizzato e in sostanza ciò che ES cerca è pei.h OR 02354.01. Ma c'è un modo per evitarlo e cercare PEI.H.02354.01. come un totale, invece di romperlo in due termini? – dchar

+0

Sembra che si desideri trattare PEI.H.02354.01 come una singola stringa, in tal caso è necessario modificare l'analizzatore, ad es. tokenise solo su spazi. –

+0

Forse per questo tipo di ricerca dovresti usare un multi-campo con un analizzatore diverso. –

Problemi correlati