2015-04-20 11 views
11

Cerco di implementare una navigazione A - Z per i miei contenuti con Elasticsearch. Ciò di cui ho bisogno, è la visualizzazione di tutti i risultati che iniziano con es. a, b, c, ... eccElasticsearch "inizia con" la prima parola nelle frasi

ho provato:

"query": { 
     "match_phrase_prefix" : { 
     "title" : { 
      "query" : "a" 
     } 
     } 
    } 

L'interrogazione di cui sopra anche visualizzare i risultati, dove all'interno della stringa una parola inizia con un. Esempio:

"title": "Apfelpfannkuchen",

"title": "Affogato",

"title": "Kalbsschnitzel un n Un ceto Balsamico",

Voglio visualizzare solo la frase in cui la prima parola inizia con a.

Qui la mappatura che uso:

$params = array(
      'index' => 'my_index', 
      'body' => array(
       'settings' => array(
        'number_of_shards' => 1, 
        'index' => array(
         'analysis' => array(
          'filter' => array(
           'nGram_filter' => array(
            'type' => 'nGram', 
            'min_gram' => 2, 
            'max_gram' => 20, 
            'token_chars' => array('letter', 'digit', 'punctuation', 'symbol') 
           ) 
          ), 
          'analyzer' => array(
           'nGram_analyzer' => array(
            'type' => 'custom', 
            'tokenizer' => 'whitespace', 
            'filter' => array('lowercase', 'asciifolding', 'nGram_filter') 
           ), 
           'whitespace_analyzer' => array(
            'type' => 'custom', 
            'tokenizer' => 'whitespace', 
            'filter' => array('lowercase', 'asciifolding') 
           ), 
           'analyzer_startswith' => array(
            'tokenizer' => 'keyword', 
            'filter' => 'lowercase' 
           ) 
          ) 
         ) 
        ) 
       ), 
       'mappings' => array(
        'tags' => array(
         '_all' => array(
          'type' => 'string', 
          'index_analyzer' => 'nGram_analyzer', 
          'search_analyzer' => 'whitespace_analyzer' 
         ), 
         'properties' => array() 

        ), 
        'posts' => array(
         '_all' => array(
          'index_analyzer' => 'nGram_analyzer', 
          'search_analyzer' => 'whitespace_analyzer' 
         ), 
         'properties' => array(
          'title' => array(
           'type' => 'string', 
           'index_analyzer' => 'analyzer_startswith', 
           'search_analyzer' => 'analyzer_startswith' 
          ) 
         ) 
        ) 
       ) 
      ) 
     ); 
+0

Puoi condividere la mappatura. – Roopendra

+0

Questo sembra elasticsearch2, potresti taggarlo come tale? –

risposta

10

Se si utilizza la mappatura di default, allora non funzionerà per voi.

È necessario utilizzare keyword tokenizer e lowercase filter nella mappatura.

mappatura sarà:

{ 
    "settings": { 
     "index": { 
      "analysis": { 
       "analyzer": { 
        "analyzer_startswith": { 
         "tokenizer": "keyword", 
         "filter": "lowercase" 
        } 
       } 
      } 
     } 
    }, 
    "mappings": { 
     "test_index": { 
      "properties": { 
       "title": { 
        "search_analyzer": "analyzer_startswith", 
        "index_analyzer": "analyzer_startswith", 
        "type": "string" 
       } 
      } 
     } 
    } 
} 

Query di ricerca sulla test_index:

{ 
    "query": { 
     "match_phrase_prefix": { 
      "title": { 
       "query": "a" 
      } 
     } 
    } 
} 

tornerà tutto titolo del post partendo a

+0

Funziona perfettamente. Grazie mille! – alin

+0

Benvenuto :-) – Roopendra

+0

Ho riscontrato un problema: la query non mostra più di 59 risultati. – alin