elasticsearch - 过滤查询错误

标签 elasticsearch

GET dspdocs/_search
{
 "query": {
  "filtered": {
    "filter": {
      "bool": {
        "must": ["must_term1", "must_term2"],
        "must_not": ["must_not_term", "must_not_term2"]
        }
      },
    "query": {
      "match": {
        "text": {
          "query": "query_term",
          "operator": "or"
        }
      }
    }
  }
}
}

我正在尝试执行上述查询,但出现以下错误:
"type": "query_parsing_exception",
"reason": "[_na] query malformed, must start with start_object",

我知道这意味着我的查询未正确编写,或者我在某个地方搞砸了一些命令,但是为了我的生命,我似乎无法弄清楚哪里出了问题。

我基本上是想过滤掉所有不包含must条款和must_not条款的文档。然后,我在过滤后的集中搜索所有包含query_term的文档。

(我首先进行过滤以稍微提高搜索速度)

最佳答案

您的问题在示例中:

...
  "bool": {
    "must": ["must_term1", "must_term2"],
    "must_not": ["must_not_term", "must_not_term2"]
    }
  },
...
mustmust_not的数组(以及该问题的shouldfilter)期望单个对象或对象数组。例如:
"bool": {
  "must": [
    {
      "term" : {
        "my_field" : "must_term1"
      }
    },
    {
      "term" : {
        "my_field" : "must_term2"
      }
    }
  ],
  "must_not": [
    {
      "term" : {
        "my_field" : "must_not_term"
      }
    },
    {
      "term" : {
        "my_field" : "must_not_term2"
      }
    }
  ]
}

请注意,我正在使用term查询,但是您可以自由使用任何类型的查询。在Elasticsearch 2.x +中,您应该将其编写为纯bool查询,而不是filtered查询:
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string" : {
            "query" : "query_term"
          }
        }
      ],
      "must_not": [
        {
          "term" : {
            "my_field" : "must_not_term"
          }
        },
        {
          "term" : {
            "my_field" : "must_not_term2"
          }
        }
      ],
      "filter": [
        {
          "term" : {
            "my_field" : "must_term1"
          }
        },
        {
          "term" : {
            "my_field" : "must_term2"
          }
        }
      ]
    }
  }
}

关于elasticsearch - 过滤查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37999703/

相关文章:

elasticsearch - es中一个query中的多个搜索条件,并根据条件区分items

Elasticsearch 5.6.16 补丁 log4j 与 2.16

elasticsearch - Elasticsearch术语聚合和查询

elasticsearch - 在Logstash中将字段从字符串转换为日期

elasticsearch - 使用 Xamarin.Forms 与 NEST 的 Guid ID 比较不起作用

Elasticsearch 按嵌套对象的数量排序

elasticsearch - 如何停止在 Elasticsearch 中重建索引?

elasticsearch - ElasticSearch 查询字符串的模糊查找

java - 如何使用 Java API 根据多个字段对 Elasticsearch 记录进行排序?

elasticsearch - Elasticsearch 中是否有与 Solr RequestHandler 等效的东西?