在ElasticSearch中使用查询字符串进行过滤

wnrlj8wa  于 2023-01-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(134)

我发送了这个查询,它运行良好,返回了经过筛选的数据:

{
    "query": {
        "bool": {
            "filter": [
                {
                    "match": {
                        "lang": "en"
                    }
                }
            ]
        }
    },
    "size": 10,
    "from": 0,
    "sort": []
}

如果我想用searchstring进行搜索,那么它也能很好地工作:

{
    "query": {
        "query_string": {
            "query": "big size"
        }
    },
    "size": 10,
    "from": 0,
    "sort": []
}

但我无法通过filter和searchstring一起从elastic中获取数据:

{
    "query": {
        "query_string": {
            "query": "big size"
        },
        "bool": {
            "filter": [
                {
                    "match": {
                        "lang": "en"
                    }
                }
            ]
        }
    },
    "size": 10,
    "from": 0,
    "sort": []
}

我收到下一个错误:

Error 400.
{"error":{"root_cause":[{"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":76}],"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":76},"status":400}
dzjeubhm

dzjeubhm1#

您的查询需要按如下所示重新构造。
质询:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "big size"
          }
        }
      ],
      "filter": [
        {
          "match": {
            "lang": "en"
          }
        }
      ]
    }
  },
  "size": 10,
  "from": 0,
  "sort": []
}

相关问题