MySQL"不在"Elasticsearch中的查询

Ami*_*thi 12 elasticsearch

我想SELECT * FROM myindex where _id not in (1, 2, 3)在Elasticsearch中实现类似的功能.一种方法是:

{
    "from": 0,
    "size": 200,
    "query": {
        "bool": {
            "must": {
                "bool": {
                    "must_not": {
                        "bool": {
                            "should": [
                                {
                                    "match": {
                                        "_id": {
                                            "query": 1,
                                            "type": "phrase"
                                        }
                                    }
                                },
                                {
                                    "match": {
                                        "_id": {
                                            "query": 2,
                                            "type": "phrase"
                                        }
                                    }
                                },
                                {
                                    "match": {
                                        "_id": {
                                            "query": 3,
                                            "type": "phrase"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这种方法不适用于大型数组,因为这将是一个巨大的查询.有一个更好的方法吗?

bar*_*rat 15

也许是这样的?

GET _search
{
  "query" : {
    "bool" : {
      "must_not" : {
        "terms" : {
          "_id" : [1,2,3]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


paq*_*ash 5

你可以很简单地做到这一点:

query: {
        bool: {
          must_not: {
            terms: {
              _id: array
            }
          }
        }
      }
Run Code Online (Sandbox Code Playgroud)