【问题标题】:Elastic search substring match with positions unchanged位置不变的弹性搜索子字符串匹配
【发布时间】:2015-02-05 00:26:02
【问题描述】:

我已经与 ES 合作了一个多月。我正在寻找与位置保持子字符串匹配相关的知识。

假设我已经为弹性搜索索引了一个文档。 2 个带有“doc_field”的文档:document 以及 id1 和 id2。

id1: " Once when a big Lion was asleep, a little Mouse began running up and down upon him. "
id2: " The mouse is very little"

我不知道应该保留索引“not_analyzed”还是“analyzed”。

我很好奇的是,如果我执行以下一组查询,它能否给我正确的匹配。

query = { "query":
           "match":{"document":"little mouse","operator": and }}

我希望它只返回那些有“小鼠标”的文件。它不应该返回在其他部分几乎没有鼠标的文档。简而言之,应该保留查询中的单词排列。帮助。

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    看看shingles TokenFilter (documentation)。它与ngram 非常相似,但使用标记而不是字符。

    使用默认设置,它会生成两个字长的标记。您可以使用 _analyze API 检查其行为:

    POST _analyze?tokenizer=whitespace&filters=shingle&text=The mouse is very little
    

    这将输出:

    {
       "tokens": [
          {
             "token": "The",
             "start_offset": 0,
             "end_offset": 3,
             "type": "word",
             "position": 1
          },
          {
             "token": "The mouse",
             "start_offset": 0,
             "end_offset": 9,
             "type": "shingle",
             "position": 1
          },
          {
             "token": "mouse",
             "start_offset": 4,
             "end_offset": 9,
             "type": "word",
             "position": 2
          },
          {
             "token": "mouse is",
             "start_offset": 4,
             "end_offset": 12,
             "type": "shingle",
             "position": 2
          },
          {
             "token": "is",
             "start_offset": 10,
             "end_offset": 12,
             "type": "word",
             "position": 3
          },
          {
             "token": "is very",
             "start_offset": 10,
             "end_offset": 17,
             "type": "shingle",
             "position": 3
          },
          {
             "token": "very",
             "start_offset": 13,
             "end_offset": 17,
             "type": "word",
             "position": 4
          },
          {
             "token": "very little",
             "start_offset": 13,
             "end_offset": 24,
             "type": "shingle",
             "position": 4
          },
          {
             "token": "little",
             "start_offset": 18,
             "end_offset": 24,
             "type": "word",
             "position": 5
          }
       ]
    }
    

    然后,通过查询该字段,您将看到两个示例文档之间的差异。

    您可以在权威指南的this section 中找到有关邻近搜索的详细说明。

    【讨论】:

    • 。我会注意的。非常感谢您的回复
    猜你喜欢
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多