【问题标题】:Elasticsearch "match_phrase" query and "fuzzy" query - can both be used in conjunctionElasticsearch "match_phrase" 查询和 "fuzzy" 查询 - 都可以结合使用
【发布时间】:2018-11-29 14:18:32
【问题描述】:

我需要一个使用 match_phrase 和模糊匹配的查询。但是我找不到任何文档来构建这样的查询。此外,当我尝试组合查询(一个在另一个中)时,它会引发错误。是否可以构造这样的查询?

【问题讨论】:

标签: elasticsearch elastic-stack elasticsearch-6


【解决方案1】:

您需要使用Span Queries

以下查询将对champions league 执行短语匹配+模糊查询,例如在name 类型为text 的样本字段上

如果您想要多个字段,请添加另一个 must 子句。

请注意,我提到了 slop:0in_order:true,它们可以进行精确的短语匹配,而您在 match 查询中使用 fuzzy 查询实现模糊行为。

示例文档

POST span-index/mydocs/1
{
  "name": "chmpions leage"
}

POST span-index/mydocs/2
{
  "name": "champions league"
}

POST span-index/mydocs/3
{
  "name": "chompions leugue"
}

跨度查询:

POST span-index/_search
{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "span_near":{  
                  "clauses":[  
                     {  
                        "span_multi":{  
                           "match":{  
                              "fuzzy":{  
                                 "testField":"champions"
                              }
                           }
                        }
                     },
                     {  
                        "span_multi":{  
                           "match":{  
                              "fuzzy":{  
                                 "testField":"league"
                              }
                           }
                        }
                     }
                  ],
                  "slop":0,
                  "in_order":true
               }
            }
         ]
      }
   }
}

回应:

{
  "took": 19,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "span-index",
        "_type": "mydocs",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "name": "champions league"
        }
      },
      {
        "_index": "span-index",
        "_type": "mydocs",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "name": "chmpions leage"
        }
      },
      {
        "_index": "span-index",
        "_type": "mydocs",
        "_id": "3",
        "_score": 0.5753642,
        "_source": {
          "name": "chompions leugue"
        }
      }
    ]
  }
}

如果这有帮助,请告诉我!

【讨论】:

  • 所以我们需要将像“champions League”这样的查询划分为[“champions”,“league”],然后形成一个DSL查询?
  • @StoneLam 是的,这是正确的。您可以看到查询是如何为每个单词构建的。在某种程度上,Span 查询虽然冗长且冗长,但更灵活。
  • 感谢@Karmal,我尝试了这个解决方案,但模糊查询使“最佳汽车”命中“最佳猫”。还有很长的路要走。
猜你喜欢
  • 1970-01-01
  • 2018-06-03
  • 2014-10-22
  • 2018-12-11
  • 2016-11-24
  • 2023-03-10
  • 2022-01-14
  • 2020-01-15
  • 1970-01-01
相关资源
最近更新 更多