首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止Elasticsearch仅匹配一个非英文字符与multi_match

如何防止Elasticsearch仅匹配一个非英文字符与multi_match
EN

Stack Overflow用户
提问于 2020-09-22 12:18:15
回答 1查看 96关注 0票数 0

我使用的是elasiticsearch-dsl和SmartCN分析器:

代码语言:javascript
复制
from elasticsearch_dsl import analyzer
analyzer_cn = analyzer(
    'smartcn',
    tokenizer=tokenizer('smartcn_tokenizer'),
    filter=['lowercase']
)

我使用multi_match在几个术语上进行匹配:

代码语言:javascript
复制
from elasticsearch_dsl import Q
q_new = Q("multi_match", query="SOME_QUERY", fields=["FEIDL_NAME"])

期望的行为是ES只返回至少有两个字符匹配的文档。我浏览了所有文档,但找不到阻止Elasticsearch匹配单个字符的方法。

任何方向/建议都是非常感谢的。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-09-22 12:59:17

期望的行为是ES只返回至少有两个字符匹配的文档。

我不熟悉SmartCN analyzer,但是如果你想匹配至少2个字符,那么根据你的用例,你可以使用N-gram tokenizer,当它遇到指定字符列表中的一个时,它首先将文本分解为单词,然后它发出指定长度的每个单词的N-gram。

索引映射:

代码语言:javascript
复制
{
    "settings": {
        "analysis": {
            "analyzer": {
                "my_analyzer": {
                    "tokenizer": "my_tokenizer"
                }
            },
            "tokenizer": {
                "my_tokenizer": {
                    "type": "ngram",
                    "min_gram": 2,    <-- note this
                    "max_gram": 20,
                    "token_chars": [
                        "letter",
                        "digit"
                    ]
                }
            }
        },
        "max_ngram_diff": 50
    },
    "mappings": {
        "properties": {
            "title": {
                "type": "text",
                "analyzer": "my_analyzer",
                "search_analyzer": "standard"
            }
        }
    }
}

索引数据:

代码语言:javascript
复制
{
    "title": "world"
}

分析应用编程接口

搜索查询将不匹配"title": "w",因为生成的令牌的最小长度为2(因为上面的索引映射中将min_gram定义为2)

生成的令牌包括:

代码语言:javascript
复制
POST/_analyze

{
  "tokens": [
    {
      "token": "wo",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "wor",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 1
    },
    {
      "token": "worl",
      "start_offset": 0,
      "end_offset": 4,
      "type": "word",
      "position": 2
    },
    {
      "token": "world",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 3
    },
    {
      "token": "or",
      "start_offset": 1,
      "end_offset": 3,
      "type": "word",
      "position": 4
    },
    {
      "token": "orl",
      "start_offset": 1,
      "end_offset": 4,
      "type": "word",
      "position": 5
    },
    {
      "token": "orld",
      "start_offset": 1,
      "end_offset": 5,
      "type": "word",
      "position": 6
    },
    {
      "token": "rl",
      "start_offset": 2,
      "end_offset": 4,
      "type": "word",
      "position": 7
    },
    {
      "token": "rld",
      "start_offset": 2,
      "end_offset": 5,
      "type": "word",
      "position": 8
    },
    {
      "token": "ld",
      "start_offset": 3,
      "end_offset": 5,
      "type": "word",
      "position": 9
    }
  ]
}

**Search Query:**

    {
        "query": {
            "match": {
                "title": "wo"
            }
        }
    }

搜索结果:

代码语言:javascript
复制
"hits": [
      {
        "_index": "stof_64003025",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.56802315,
        "_source": {
          "title": "world"
        }
      }
    ]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64003025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档