# Strict array intersection

**URL:** <https://discuss.elastic.co/t/strict-array-intersection/32842>\
**Category:** Elasticsearch\
**Created:** [October 23, 2015, 10:50am UTC](https://discuss.elastic.co/t/strict-array-intersection/32842 "2015-10-23T10:50:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![111103](https://avatars.discourse-cdn.com/v4/letter/1/ee59a6/32.png) [@111103](https://discuss.elastic.co/u/111103)\
**Post date:** [October 23, 2015, 10:50am UTC](https://discuss.elastic.co/t/strict-array-intersection/32842/1 "2015-10-23T10:50:28Z")

</div>

Hi, I have this document in elastic index:

```
{
    "contexts": [
        {"ref": "A"},
        {"ref": "B"} 
    ]
}

```

And I know some allowedRefs, different for each query.

Suppose it for example equals to

`["A", "B", "C", "D", "E"]`.

I want to retrieve document from elastic if contexts field is empty (missing) or if allowedRefs contains ALL refs from document.

I have writen something like that:

```
POST /search/conversation/_search 
{
    "query": {
        "bool": {
            "should": [
                {
                    "constant_score" : {
                        "filter" : {
                            "missing" : { "field" : "contexts" }
                        }
                    }
                },
                {
                    "bool": {
                        "must": [
                            {
                                "term": { "contexts.objectRef": "A" } // A,B,C taken from for-loop in java, for each allowedRefs
                            }, 
                            {
                                "term": { "contexts.objectRef": "B" }
                            },
                            {
                                "term": { "contexts.objectRef": "C" }
                            }
                            ...
                        ]
                    }
                }
            ]
        }
    }
}

```

But this is wrong because I should check presence of document's references in allowedRefs, not backward. What kind of query can help me? Should I use scripting?

In other words,

- if document's references = A and B.
- if allowedRefs variable (read from database in runtime) contains A, B (it can contains a lot of other values)
- or if no references  
-\> then I want to retrieve the document

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:43pm UTC](https://discuss.elastic.co/t/strict-array-intersection/32842/2 "2017-07-05T23:43:03Z")

</div>

Closed on July 5, 2017, 11:43pm UTC


