ElasticSearch Golang的术语和多匹配搜索查询

ktecyv1j  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(126)

我有一个情况,我需要做基于多匹配的ElasticSearch。
情况1:关键字字段的短语搜索。应该只返回完整短语匹配的结果(不应该拆分短语)。情况2:对于剩余字段,1个关键字匹配必须返回结果。
我使用should组合了多匹配和术语查询。如下。
例如:如果我用“短语搜索多个字段”
首先,它应该在关键字字段中搜索完整的短语,然后它可以在其他字段的多匹配中搜索。注意:如果我用单个词搜索,如“多个”或“字段”,它不应该从关键字返回任何结果。

"search": {
    "method": "POST",
    "param": {
       "query": {
        "bool": {
            "should": [
            {
            "terms": {
            "keywords": [
               "Phrase search in multiple fields",
               " Phrase search in multiple fields"
              ]
             }
            },
            {
            "multi_match": {
                "query": "Phrase search in multiple fields",
                "fields": {
                "title": "title",
                    "metaTitle": "title",
                "href": "Path",
                },
                "fuzziness": "AUTO"
                   }
                }       
            ]
            }
           }
         }
q0qdq0h2

q0qdq0h21#

案例一:短语搜索关键字字段。应仅返回完整短语匹配的结果(不应拆分短语)

使用keyword字段类型。
不应该拆分短语=>如果你使用keyword字段类型,这意味着没有文本分析器,它不会拆分短语。

案例二:对于其余字段,必须返回1个关键字匹配的结果。

标准分析仪将
使用text字段类型。
1关键字匹配必须返回结果=>标准(默认)分析器使用空格(和一些特殊字符)标记短语并将其拆分为单词。
我分享了一些例子来深入解释,希望有帮助。

#Put the documents.
PUT test_stack_over_flow/_doc/1
{
  "title": "Phrase search in multiple fields"
}

#check the mapping
GET test_stack_over_flow
"mappings": {
  "properties": {
    "title": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

#1hit
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple fields"
            ]
          }
        }
      ]
    }
  }
}

#0hit
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple"
            ]
          }
        }
      ]
    }
  }
}

#1hit
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "multiple",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}

#1 hit (because of fuzziness enabled)
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "multipl3",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}

#1 hit because of minimum_should_match:1 (default) and multi_match hit.
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple"
            ]
          }
        },
        {
          "multi_match": {
            "query": "multiple",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}

#0 hit because niehtier terms nor multi_match hit
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple"
            ]
          }
        },
        {
          "multi_match": {
            "query": "multi",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}

#1 hit because terms hit.
GET test_stack_over_flow/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title.keyword": [
              "Phrase search in multiple fields"
            ]
          }
        },
        {
          "multi_match": {
            "query": "multi",
            "fields": ["title"],
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  }
}

相关问题