Elasticsearch "starts with"短语中的第一个词

标签 elasticsearch startswith

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

我试过:

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

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

"title": "Apfelpfannkuchen",

"标题": "阿芙佳朵",

"title": "Kalbsschnitzel an Aceto 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'
                            )
                        )
                    )
                )
            )
        );

最佳答案

如果您使用默认映射,则它不适合您。

您需要使用 keyword tokenizerlowercase filter在映射中。

映射将是:

{
    "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 开头的帖子标题

关于Elasticsearch "starts with"短语中的第一个词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29741641/

相关文章:

java - 在ElasticSearch 7.4 java客户端中的geoShapeQuery中使用geojson多边形

java - 查找 Elasticsearch 集群使用的磁盘空间总量

java - startswith vs JSON 反序列化性能?

elasticsearch - 如何按降序对kibana索引进行排序?

java - 如何在 Elasticsearch java api 中按别名查找索引?

elasticsearch - 使用NEST Elastic Search中的类型设置位置

Javascript获取以特定字符串开头的URL参数

javascript - 使用startsWith函数识别邮政编码的前三位数字

lucene - 如何使用Lucene.NET 3.0进行startsWith然后Contains搜索?

jQuery 选择带有 ID 和类的 div