如何在Elasticsearch中合并通配符搜索和范围查询?

erhoui1w  于 2023-04-20  发布在  ElasticSearch
关注(0)|答案(2)|浏览(182)

我尝试在Elasticsearch查询中将通配符与日期范围结合,但没有根据通配符搜索给出响应。它返回的响应包含日期范围不正确的项目。

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "wildcard": {
                  "hostName": "*abc*"
                }
              },
              {
                "range": {
                  "requestDate": {
                    "gte": "2019-10-01T08:00:00.000Z"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

索引Map如下所示:

{
  "index_history": {
    "mappings": {
      "applications_datalake": {
        "properties": {
          "query": {
            "properties": {
              "term": {
                "properties": {
                  "server": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "index-data-type": {
        "properties": {
          "attributes": {
            "properties": {
              "wwnListForServer": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "hostName": {
            "type": "keyword"
          },
          "requestDate": {
            "type": "date"
          },
          "requestedBy": {
            "properties": {
              "id": {
                "type": "keyword"
              },
              "name": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
}
pftdvrlh

pftdvrlh1#

你错过了minimum_should_match参数,看看这个:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
我认为你的查询应该看起来像这样:

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "wildcard": {
                  "hostName": "*abc*"
                }
              },
              {
                "range": {
                  "requestDate": {
                    "gte": "2019-10-01T08:00:00.000Z"
                  }
                }
              }
            ],
            "minimum_should_match" : 2
          }
        }
      ]
    }
  }
}

来自文档:
可以使用minimum_should_match参数指定返回文档必须匹配的should子句的数量或百分比。
如果bool查询至少包含一个should子句,而不包含must或filter子句,则默认值为1。否则,默认值为0。

w8f9ii69

w8f9ii692#

根据您的Map,您必须调出hostNamerequestDate字段的完全限定属性。示例:

"wildcard": {    
  "index-data-type.hostName": {
    "value": "..."
  }
}

此外,还可以考虑使用must子句将复合查询减少为仅主bool查询,并应用过滤器。示例:

{
  "from": 0, 
  "size": 20, 
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "index-data-type.hostName": {
              "value": "*abc*"
            }
          }
        }
      ],
      "filter": {
        "range": {
          "index-data-type.requestDate": {
            "gte": "2019-10-01T08:00:00.000Z"
          }
        }
      }
    }
  }
}

过滤器上下文对_score没有贡献,但它减少了hits的数量。

警告:在通配符查询中使用前导星号(*)可能会对查询的性能产生严重影响。

相关问题