Elasticsearch - 查询文档缺少数组值

时间:2015-06-16 07:44:19

标签: elasticsearch

我想查询我的elasticsearch索引,以便检索不包含数组中特定值的文档。例如,如果我的查询是:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match_all": {}
                }
            ],
            "must_not": [],
            "should": []
        }
    },
    "from": 0,
    "size": 10,
    "sort": [],
    "facets": {}
}

数据集:

{
    "took": 1,
    "hits": {
        "total": 1,
        "hits": [
            {
                "_index": "product__1434374235336",
                "_type": "product",
                "_id": "AU33Xeny0K4pKlL-a7sr",
                "_source": {
                    "interdictions": ["S0P","SK3"],
                    "code": "foo"
                }
            },
            {
                "_index": "product__1434374235336",
                "_type": "product",
                "_id": "AU33Xeny0K4pKlL-a7sr",
                "_source": {
                    "interdictions": ["S0P","S2V","SK3"],
                    "code": "bar"
                }
            }
        ]
    }
}

目标是排除包含"S2V"阻截的每个产品。我最初想过使用missing filter

{
    "query": {
        "bool": {
            "must": [
                {
                    "match_all": {}
                }
            ],
            "must_not": [],
            "should": []
        }
    },
    "filter": {
        "missing": {
            "terms": {
                "interdictions": [
                    "S2V"
                ]
            }
        }
    },
    "from": 0,
    "size": 10,
    "sort": [],
    "facets": {}
}

但是elasticsearch无法解析查询:QueryParsingException[[product__1434374235336] [missing] filter does not support [interdictions]]; }]",。然后我尝试使用must_not

{
    "query": {
        "bool": {
            "must": [
                {
                    "match_all": {}
                }
            ],
            "must_not" : {
                "terms" : {
                    "interdictions" : ["S2V"]
                }
            }
        }
    },
    "from": 0,
    "size": 10
}

但是输出不正确,因为它返回了S2V阻断的产品。

那么......这样做的正确方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个(terms布尔的小写值

{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "must_not": {
        "terms": {
          "interdictions": [
            "s2v"
          ]
        }
      }
    }
  },
  "from": 0,
  "size": 10
}

最有可能的是,你有一个分析器(可能是standard默认值),它使术语小写,所以在ES索引中,值被索引为s2vsk3等。terms并不分析输入值,它按原样使用(在大多数情况下使用大写字母),因此它永远不会匹配。