1

如何在弹性搜索中使用必须或条件中的多个匹配短语?

GET questiondetails/question/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "tags.keyword": "azure-data"
          }
        },
        {
          "match_phrase": {
            "body": "azure-data"
          }
        },
        {
          "match_phrase": {
            "title": "azure-data"
          }
        },
        {
          "match_phrase": {
            "answers.body": "azure-data"
          }
        }
      ],
      "minimum_should_match": 1,
      "filter": {
        "range": {
          "creation_date": {
            "gte": 1584748800,
            "lte": 1585612800
          }
        }
      }
    }
  },
  "size": "10000"
}

尝试了这个查询....我需要单词在标签中完全匹配或在正文或标题或 answer.body 上部分匹配的结果

但它不起作用。

添加评论

获取 questiondetails_new/question/_search? {“查询”:{“布尔”:{“应该”:[{“match_phrase”:{“tags.keyword”:“azure-data-factory”}},{“match_phrase”:{“title”:“azure -data-factory" } } ], "minimum_should_match": 1, "filter": { "range": { "creation_date": { "gte": 1585170170, "lte": 1585170180 } } }

在此查询中,我需要与 azure-data-factory 完全匹配或在其标题(字符串)中具有 azure-data-factory 的所有文档。它应该是一个或搜索。但它也与值为 azure-data-factory-2 的标签匹配

4

1 回答 1

0

添加一个字符过滤器,在索引时将“-”替换为“_”。您无需更改输入文本(它将与“-”一起使用)

PUT index38
{
  "settings": {
    "analysis": {
      "char_filter": {
        "my_char_filter": {
          "type": "mapping",
          "mappings": [
            "- => _"
          ]
        }
      },
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "char_filter": [
            "my_char_filter"
          ],
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
      "properties": {
        "tags": {
          "type": "text",
          "analyzer": "my_analyzer",
          "fields": {
            "keyword":{
              "type":"keyword"
            }
          }
        },
        "title": {
          "type": "text",
          "analyzer": "my_analyzer",
          "fields": {
            "keyword":{
              "type":"keyword"
            }
          }
        }
      }
    }
}

询问:

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "tags": "azure-data-factory"
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "match_phrase": {
                  "tags": "azure-data-factory"
                }
              }
            ],
            "should": [
              {
                "match_phrase": {
                  "body": "azure-data-factory"
                }
              },
              {
                "match_phrase": {
                  "title": "azure-data-factory"
                }
              },
              {
                "match_phrase": {
                  "answers.body": "azure-data-factory"
                }
              }
            ]
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "size": "100"
}

如果您将使用默认分析器分析文本“azure-data-factory”,它将生成 3 个标记 ["azure","data","factory"]

GET index38/_analyze
{
  "text": "azure-data-factory"
}

Result:

"tokens" : [
    {
      "token" : "azure",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "data",
      "start_offset" : 6,
      "end_offset" : 10,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "factory",
      "start_offset" : 11,
      "end_offset" : 18,
      "type" : "<ALPHANUM>",
      "position" : 2
    }
  ]

如果您将使用“my_analyzer”分析相同的文本,则会生成单个标记

GET index38/_analyze
{
  "text": "azure-data-factory",
  "analyzer": "my_analyzer"
}

Result:

"tokens" : [
    {
      "token" : "azure_data_factory",
      "start_offset" : 0,
      "end_offset" : 18,
      "type" : "<ALPHANUM>",
      "position" : 0
    }
]

于 2020-05-13T09:27:04.700 回答