代码之家  ›  专栏  ›  技术社区  ›  meallhour

需要在elasticsearch中结合通配符搜索和范围查询的帮助吗?

  •  0
  • meallhour  · 技术社区  · 5 年前

    我试图在弹性搜索查询中将通配符与日期范围结合起来,但没有给出基于通配符搜索的响应。它返回的响应包含日期范围不正确的项目。

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

    索引映射如下所示:

    {
      "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"
                  }
                }
              }
            }
          }
        }
      }
    }
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   baitmbarek Naman Agarwal    5 年前

    你错过了 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参数指定数字 或返回文件的百分比必须匹配。

    如果bool查询至少包含一个shoul子句,并且没有must或 筛选子句,默认值为1。否则,默认值为 是0。

        2
  •  0
  •   James Woodruff    5 年前

    根据映射,必须为 hostName requestDate 领域。例子:

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

    此外,还可以考虑将复合查询简化为主查询。 bool 使用must子句进行查询,并应用筛选器。例子:

    {
      "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

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