自定义分析器在搜索elasticsearch时不工作

pb3skfrl  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(403)

我创造 documents 带类型的索引 _doc . 然后我设置了一个自定义分析器,如下所示

POST /documents/_close
PUT /documents/_settings
{
    "settings": {
        "analysis": {
            "analyzer": {
                "custom_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "word_delimiter_graph"
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "question": {
                "type": "text",
                "analyzer": "custom_analyzer"
            },
            "question_group": {
                "type": "text",
                "analyzer": "custom_analyzer"
            }
        }
    }
}
POST /documents/_open

我试着用这个 custom_analyzer 那就行了

POST http://localhost:9200/documents/_analyze
{
  "analyzer": "custom_analyzer",
  "text": "FIRE_DETECTED"
}

# And the result (lowercase and remove _ )

{
    "tokens": [
        {
            "token": "fire",
            "start_offset": 0,
            "end_offset": 4,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "detected",
            "start_offset": 5,
            "end_offset": 13,
            "type": "<ALPHANUM>",
            "position": 1
        }
    ]
}

但是当我尝试搜索“fire”或“fire detected”时,它不起作用。
当我尝试搜索“fire\u detected”时,它仍然有效(我索引了“fire\u detected”)


# This POST found nothing

POST /documents/_search
{
    "query": {
        "multi_match": {
            "query": "fire detected",
            "fields": [
                "question^2",
                "question_group"
            ]
        }
    }
}

解决方案
尝试使用新设置创建新索引(如上)

PUT /documents5
{
    "settings": {...}
}

索引数据

PUT http://localhost:9200/documents5/_doc/1
{
  "question": "fire_detected"
}

搜索

e7arh2l6

e7arh2l61#

这发生在您刚刚添加了 custom_analyzer 但是没有重新索引数据(索引中的文档),因此新的标记不会出现在倒排索引中。为了解决这个问题,只需重新索引的文件,你想来在你的搜索结果。
您正在使用 multi_match 内部使用 match 查询和这些查询进行分析,所以您不需要搜索时间分析器。 match 查询使用在字段上定义的同一分析器来创建搜索标记(即从搜索词创建)。
从匹配查询官方文档
返回与提供的文本、数字、日期或布尔值匹配的文档。匹配前对提供的文本进行分析。

相关问题