Alexis
Alexis

Reputation: 37

Search document from an index which _ids does not exists in another index

Is any way to search documents from one index that _ids do not exists in another index? Something like NOT EXISTS in MySQL.

Upvotes: 2

Views: 1009

Answers (1)

ibexit
ibexit

Reputation: 3667

There is no convinient way to do this within elasticsearch but only by using aggregations as a workaround:

GET index-a,index-b/_search
{
  "size": 0,
  "aggs": {
    "group_by_id": {
      "terms": {
        "field": "_id",
        "size": 1000
      },
      "aggs": {
        "containied_in_indices_count": {
          "cardinality": {
            "field": "_index"
          }
        },
        "filter_only_differences": {
          "bucket_selector": {
            "buckets_path": {
              "count": "containied_in_indices_count"
            },
            "script": "params.count < 2"
          }
        }
      }
    }
  }
}

Then you'll need to iterate over all buckets in group_by_id aggregation. Consider using a larger size, as it´s 10 by default and 1000 in my example. If there are more differences in your indicies you need to use bucket partitioning as described here: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_partitions

Upvotes: 2

Related Questions