【问题标题】:Preserving order of terms in ElasticSearch query在 ElasticSearch 查询中保留术语的顺序
【发布时间】:2023-03-13 01:43:02
【问题描述】:

是否可以在 ElasticSearch 中形成一个保留术语顺序的查询?

一个简单的例子是使用标准分析器对这些文档进行索引:

  1. 你知道搜索
  2. 你知道搜索
  3. 知道搜索你

我可以查询+you +search,这将返回所有文档,包括第三个文档。

如果我只想检索具有此特定顺序的条款的文档怎么办?我可以形成一个可以为我做这件事的查询吗?

考虑到短语可以通过简单地引用文本:"you know"(检索第一个和第二个文档)我觉得应该有一种方法可以保留多个不相邻的术语的顺序。

在上面的简单示例中,我可以使用邻近搜索,但这并不涵盖更复杂的情况。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这正是 match_phrase 查询(参见 here)所做的。

    它检查术语的位置,在它们的存在之上。

    例如,这些文件:

    POST test/values
    {
      "test": "Hello World"
    }
    
    POST test/values
    {
      "test": "Hello nice World"
    }
    
    POST test/values
    {
      "test": "World, I don't say hello"
    }
    

    都可以通过基本的match 查询找到:

    POST test/_search
    {
      "query": {
        "match": {
          "test": "Hello World"
        }
      }
    }
    

    但是使用match_phrase,只会返回第一个文档:

    POST test/_search
    {
      "query": {
        "match_phrase": {
          "test": "Hello World"
        }
      }
    }
    
    {
       ...
       "hits": {
          "total": 1,
          "max_score": 2.3953633,
          "hits": [
             {
                "_index": "test",
                "_type": "values",
                "_id": "qFZAKYOTQh2AuqplLQdHcA",
                "_score": 2.3953633,
                "_source": {
                   "test": "Hello World"
                }
             }
          ]
       }
    }
    

    在您的情况下,您希望接受您的条款之间存在一定距离。这可以通过slop 参数来实现,该参数指示您允许您的术语与另一个术语相距多远:

    POST test/_search
    {
      "query": {
        "match": {
          "test": {
            "query": "Hello world",
            "slop":1,
            "type": "phrase"
          }
        }
      }
    }
    

    通过最后一个请求,您也可以找到第二个文档:

    {
       ...
       "hits": {
          "total": 2,
          "max_score": 0.38356602,
          "hits": [
             {
                "_index": "test",
                "_type": "values",
                "_id": "7mhBJgm5QaO2_aXOrTB_BA",
                "_score": 0.38356602,
                "_source": {
                   "test": "Hello World"
                }
             },
             {
                "_index": "test",
                "_type": "values",
                "_id": "VKdUJSZFQNCFrxKk_hWz4A",
                "_score": 0.2169777,
                "_source": {
                   "test": "Hello nice World"
                }
             }
          ]
       }
    }
    

    您可以在definitive guide 中找到有关此用例的整章。

    【讨论】:

    • 在 OP 的示例中,他也需要使用 slop 值。
    • 如果单词之间有任意距离怎么办? hello one two... n terms world?如果斜率最终很高,它也会匹配“世界,我不打招呼”。 slop 1 也会匹配“world hello”,对吧?
    • 短语匹配不能确保顺序 ;-)。如果你指定了足够多的斜率——例如 2——“hello world”将匹配“world hello”。
    • 是的,@AndreiStefan,这很糟糕。在我的用例中,保留顺序非常有用。
    • 不错的答案。我遇到过同样的问题。这真的很有帮助。
    【解决方案2】:

    短语匹配并不能确保顺序 ;-)。如果你指定了足够多的斜率——例如 2——“hello world”将匹配“world hello”。但这不一定是一件坏事,因为如果两个术语彼此“接近”并且它们的顺序无关紧要,通常搜索会更相关。而且我不认为此功能的作者会考虑匹配相距 1000 条斜线的单词。

    我可以找到一个解决方案来保持订单,但并不简单:使用脚本。这是一个例子:

    POST /my_index/my_type/_bulk
    { "index": { "_id": 1 }}
    { "title": "hello world" }
    { "index": { "_id": 2 }}
    { "title": "world hello" }
    { "index": { "_id": 3 }}
    { "title": "hello term1 term2 term3 term4 world" }
    
    POST my_index/_search
    {
      "query": {
        "filtered": {
          "query": {
            "match": {
              "title": {
                "query": "hello world",
                "slop": 5,
                "type": "phrase"
              }
            }
          },
          "filter": {
            "script": {
              "script": "term1Pos=0;term2Pos=0;term1Info = _index['title'].get('hello',_POSITIONS);term2Info = _index['title'].get('world',_POSITIONS); for(pos in term1Info){term1Pos=pos.position;}; for(pos in term2Info){term2Pos=pos.position;}; return term1Pos<term2Pos;",
              "params": {}
            }
          }
        }
      }
    }
    

    为了使脚本本身更具可读性,我在这里用缩进重写:

    term1Pos = 0;
    term2Pos = 0;
    term1Info = _index['title'].get('hello',_POSITIONS);
    term2Info = _index['title'].get('world',_POSITIONS);
    for(pos in term1Info) {
      term1Pos = pos.position;
    }; 
    for(pos in term2Info) {
      term2Pos = pos.position;
    }; 
    return term1Pos < term2Pos;
    

    上面是一个搜索“hello world”的查询,斜率为 5,在上面的文档中将匹配所有这些。但是脚本过滤器将确保单词“hello”在文档中的位置低于单词“world”在文档中的位置。这样,无论我们在查询中设置多少斜率,位置一个接一个的事实确保了顺序。

    这是section in the documentation,它阐明了上面脚本中使用的东西。

    【讨论】:

    • +1 表示努力和解决方案。我仍然希望有一个更优雅/更少hacky的解决方案来解决我的问题;)
    • 我也很好奇其他解决方案。这是我能想到的唯一方法。
    • 实际上已经尝试运行它我得到QueryPhaseExecutionException 这似乎是由GroovyScriptExecutionException[MissingPropertyException[No such property: title for class: Script4]]; 引起的我正在运行v1.4 beta。
    • title 只是您希望 hello 和 world 在其中查找匹配项的字段的名称。在我的示例中,该字段的名称是 title。跨度>
    【解决方案3】:

    您可以使用span_near 查询,它有一个in_order 参数。

    {
        "query": {
            "span_near": {
                "clauses": [
                    {
                        "span_term": {
                            "field": "you"
                        }
                    },
                    {
                        "span_term": {
                            "field": "search"
                        }
                    }
                ],
                "slop": 2,
                "in_order": true
            }
        }
    }
    

    【讨论】:

    • 想添加一些带通配符的query_string,这个一定要好几倍!
    • 非常感谢。它确实有效。虽然我希望有一种方法可以在不指定 slop 值的情况下做到这一点,尽管我知道我可以通过将 slop 设置为高值来作弊。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    相关资源
    最近更新 更多