【问题标题】:elasticsearch edgengram copy_to field partial search not workingelasticsearch edgengram copy_to 字段部分搜索不起作用
【发布时间】:2018-05-29 19:51:38
【问题描述】:

下面是弹性搜索映射,其中一个字段称为主机名,另一个字段称为 catch_all,基本上是 copy_to 字段(将有更多字段将值复制到此字段)

{
  "settings": {
    "analysis": {
            "filter": {
                "myNGramFilter": {
                  "type": "edgeNGram",
                  "min_gram": 1,
                  "max_gram": 40
            }},
            "analyzer": {
                "myNGramAnalyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": ["lowercase", "myNGramFilter"]
                }
            }
        }
  },
    "mappings": {
      "test": {
        "properties": {
          "catch_all": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "store": true,
                            "ignore_above": 256
                        },
                        "grams": {
                            "type": "text",
                            "store": true,
                            "analyzer": "myNGramAnalyzer"
                        }
                    }
          },
          "hostname": {
            "type": "text",
            "copy_to": "catch_all"
          }
        }
      }
    }
}

当我做的时候

GET index/_analyze
{
  "analyzer": "myNGramAnalyzer",
  "text": "Dell PowerEdge R630"
}
{
  "tokens": [
    {
      "token": "d",
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "de",
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "del",
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "dell",
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "p",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "po",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "pow",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "powe",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "power",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "powere",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "powered",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "poweredg",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "poweredge",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "r",
      "start_offset": 15,
      "end_offset": 19,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "r6",
      "start_offset": 15,
      "end_offset": 19,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "r63",
      "start_offset": 15,
      "end_offset": 19,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "r630",
      "start_offset": 15,
      "end_offset": 19,
      "type": "<ALPHANUM>",
      "position": 2
    }
  ]
}

有一个令牌叫做“powerge”。 现在我们使用以下查询进行搜索

{ 
  "query": {
    "multi_match": {
      "fields": ["catch_all.grams"],
      "query": "poweredge",
      "operator": "and"
    }
  }
}

当我们使用“poweredge”查询时,我们得到 1 个结果。但是当我们只通过“边缘”搜索时,没有结果。

即使匹配查询也不会产生搜索词“edge”的结果。

有人可以帮忙吗?

【问题讨论】:

  • 你搜索“catch_all.grams”吗?您能否在文本字段中提供带有 poweredge 的数据样本?
  • @Lupanoide 用查询更新了问题。

标签: elasticsearch full-text-search n-gram


【解决方案1】:

我建议不要针对您的用例使用 multi_match api 进行查询,而是使用匹配查询。 edgengram 以这种方式工作:它尝试在文本上由空白标记器生成的标记上生成 ngram。如文档中所述 - read here:

edge_ngram 标记器首先将文本分解为单词 遇到指定字符列表之一,然后发出 每个单词的 N-gram,其中 N-gram 的开头锚定到 单词的开头。

正如您在查询中测试以分析 API 一样,它不会产生“edge” - 来自 poweredge - 作为 ngram,因为它从单词的开头产生 ngram - 查看分析 API 调用的输出。看这里:https://www.elastic.co/guide/en/elasticsearch/guide/master/ngrams-compound-words.html

【讨论】:

  • 我也在“catch_all.grams”字段上使用了匹配查询,该字段不返回“edge”的结果。这是主要问题。
  • 好的,我已经回答过了。同样在您的调用 api - GET index/_analyze - 您可以观察到它不会从边缘 ngram 标记化过程中返回“边缘”,对吧?请仔细阅读我的答案并访问 ngram-compound-words 的文档页面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多