web-dev-qa-db-ja.com

Elasticsearchはフレーズの最初の単語で「始まる」

Elasticsearchを使用してコンテンツにA-Zナビゲーションを実装しようとしています。私が必要としているのは、例えばで始まるすべての結果を表示することです。 a、b、c、...など。

私はもう試した:

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

上記のクエリでも結果が表示され、文字列内でWordがaで始まります。例:

"タイトル": "Apfelpfannkuchen"、

「タイトル」:「アフォガート」、

"title": "Kalbsschnitzel a n [〜#〜] a [〜#〜] ceto Balsamico"、

最初の単語がで始まるフレーズのみを表示したい。

ここで私が使用するマッピング:

$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'
                            )
                        )
                    )
                )
            )
        );
15
alin

デフォルトのマッピングを使用している場合は、機能しません。

マッピングでは、 キーワードトークナイザー および 小文字フィルター を使用する必要があります。

マッピングは次のようになります:

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

test_indexでクエリを検索:

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

aで始まるすべての投稿タイトルを返します

13
Roopendra

this Gist に従って@Roopendraの回答を更新しています。したがって、更新があり、最近のバージョンではsearchおよびindexイニシャライザーが機能していないようで、initializersにのみ置き換えられ、stringも必要です。 textに置き換えられます。

したがって、次のマッピングファイルがあります。

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

次のクエリ

{
    "query": {
        "match_phrase_prefix": {
            "title": {
                "query": "a",
                "max_expansions": 100
            }
        }
    }
}

デフォルト値はmax_expansionsのようであるため、クエリに5を追加しました。そのため、誤った結果が得られました。この場合、値が高くなる可能性があります。

0
silgon