如何在elasticsearch中使用query_string和过滤器?

时间:2017-09-25 18:00:51

标签: elasticsearch filter kibana

我有一个简单的查询

GET data/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "last_name": "test"
          }
        },
        {
          "bool": {
            "should": {
              "query_string": {
                "query": "henderson OR Boulder City OR Sloan",
                "fields": [
                  "city_*"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

我想将query_string更改为过滤器。我不确定如何转换

      {
          "bool": {
            "should": {
              "query_string": {
                "query": "henderson OR Boulder City OR Sloan",
                "fields": [
                  "city_*"
                ]
              }
            }
          }
        }

类似

      "filter": {
        "query_string": {
              "query": "henderson OR Boulder City OR Sloan",
              "fields": [
                  "city_*"
              ]
          }
      }

并确保按所有这些城市henderson OR Boulder City OR Sloan以及所有这些字段city_*.keyword过滤

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

更改

{
      "bool": {
        "should": {
          "query_string": {
            "query": "henderson OR Boulder City OR Sloan",
            "fields": [
              "city_*"
            ]
          }
        }
      }
    }

  {
      "bool": {
        "filter": {
          "query_string": {
            "query": "henderson OR Boulder City OR Sloan",
            "fields": [
              "city_*"
            ]
          }
        }
      }
    }

这是否符合您的期望?在过滤器下使用相同的查询应该给出与应该相同的结果,但查询不会对评分进行加权。

编辑 - 我要做的另一个建议是将您的查询调整为:

GET data/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "last_name": "test"
          }
        }
      ],
      "filter": [
        {
          "query_string": {
            "query": "henderson OR Boulder City OR Sloan",
              "fields": [
                "city_*"
                ]
          }
        }
      ]        
    }
  }
}

编辑2 - 您可以放弃使用query_string,这是否可以提高速度? (你可以将#34; shoulds"更改为嵌套在过滤器中,如果你想要它们没有标记的话)

GET data/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "last_name": "test"
          }
        }
      ],
      "should": [
        {
          "match": {
            "city_*": "henderson"
          }
        },
        {
          "match": {
            "city_*": "Boulder City"
          }
        },
        {
          "match": {
            "city_*": "Sloan"
          }
        }
      ]        
    }
  }
}