Elasticsearch以"短语中的第一个单词开头"开头

ali*_*lin 15 startswith elasticsearch

我尝试使用Elasticsearch为我的内容实现A-Z导航.我需要的是显示所有结果,例如a,b,c,......等.

我试过了:

"query": {
        "match_phrase_prefix" : {
        "title" : {
            "query" : "a"
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

上面提到的查询也显示结果,其中字符串中的单词以a开头.例:

"title":"Apfelpfannkuchen",

"标题":"Affogato",

"title":"Kalbsschnitzel a a A ceto Balsamico",

我想只显示第一个单词以a开头的短语.

这里我使用的映射:

$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'
                            )
                        )
                    )
                )
            )
        );
Run Code Online (Sandbox Code Playgroud)

Roo*_*dra 14

如果您使用默认映射,那么它将不适合您.

您需要在映射中使用关键字标记器和小写过滤器.

映射将是:

{
    "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"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

搜索查询test_index:

{
    "query": {
        "match_phrase_prefix": {
            "title": {
                "query": "a"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它会返回所有帖子标题 a

  • 有没有办法在不使用`match_phrase_prefix`或`prefix`的情况下实现此功能? (2认同)