elasticsearch,Search,elasticsearch" /> elasticsearch,Search,elasticsearch" />

ElasticSearch:如何组合嵌套和#x27;和';不相上下

ElasticSearch:如何组合嵌套和#x27;和';不相上下,search,elasticsearch,Search,elasticsearch,我希望生成搜索匹配的嵌套和不相等查询 这是我的elasticSearch查询: { "from":0,"size":1000, "query":{ "nested" : { "path" : "data", "query" : { "match" : { "data.city" : "california" }

我希望生成搜索匹配的嵌套和不相等查询

这是我的elasticSearch查询:

{
    "from":0,"size":1000,
    "query":{
        "nested" : {
            "path" : "data",
            "query" : {
                "match" : {
                    "data.city" : "california"
                }
            }
        },
        "filter":{
            "not":{
                "filter":{
                    "term":{
                        "_id":"01921asda01201"
                    }
                }
            }
        }
    }
}
但我有错误,我写错了什么吗?谢谢

您需要使用


您也可以将
bool Filter
must
must\u not
子句一起使用

{
"from": 0,
"size": 1000,
"filter": {
  "bool": {
     "must": [
        {
           "nested": {
              "path": "data",
              "query": {
                 "match": {
                    "data.city": "california"
                 }
              }
           }
        }
     ],
     "must_not": [
        {
           "term": {
              "_id": "01921asda01201"
           }
        }
      ]
    }
  }
 }
为此,您应该使用a,并将您的两个子句分别放在
must
must\u not
部分

如果您不关心
data.city
字段上的评分(从您的示例中不清楚),您可能希望使用
过滤器
部分,而不是
必须
部分

{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "data",
            "query": {
              "match": {
                "data.city": "california"
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "_id": "01921asda01201"
          }
        }
      ]
    }
  }
}

请注意,在发送有效负载时使用POST是一种很好的做法。在发送有效负载时,“引擎盖下检测”使用POST而不是GET。此外,自Elasticsearch 2.0.0Yes起,
过滤的
查询已被弃用。。在同一链接上还指定了另一种方法,即在这种情况下使用布尔查询。您确定顶层的
过滤器
部分,@Richa?@Val::您的意思是
bool过滤器
,在顶层,您有
from
size
过滤器
,它应该是
from
size
query
如果我没有错的话,我们可以直接使用过滤器而不使用query。是的,但那是和。如果OP向他的查询中添加聚合,那么它可能不会像预期的那样工作。
{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "data",
            "query": {
              "match": {
                "data.city": "california"
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "_id": "01921asda01201"
          }
        }
      ]
    }
  }
}