Elasticsearch must some values and must not all others

Coodie_d

so I have an index with the following mapping:

{
"tests": {
    "mappings": {
        "test": {
            "properties": {
                "arr": {
                    "properties": {
                        "name": {
                            "type": "long"
                        }
                    }
                }
            }
        }
    }
}

I have 3 documents with the following values for the field "arr":

  1. arr: [{name: 1}, {name: 2}, {name: 3}]
  2. arr: [{name: 1}, {name: 2}, {name: 4}]
  3. arr: [{name: 1}, {name: 2}]

I would like to search in a way that I could find the documents in which all name values are in an array of name. However, the document doesn't need to have all the values of the array, and if it has one value that is not in the array, then the document is not good.

For example:

  1. If my array of names is [1,2], then I only want document 3, but I have all of them
  2. If my array of names is [1,2,3], then I want document 1 and 3, but I only have document 1 with must and all of them with should
  3. If my array of names is [1,2,4], then I want document 2 and 3, but I only have document 2 with must and all of them with should
  4. If my array of names is [1], then I want no document, but I have all of them

Here, the arrays are small, in my project the arrays in the documents are much more bigger and the array of comparison as well.

So, to be specific, I need to search in a way that:

  1. All the names in the document are in the array of names
  2. The document does not need to have ALL the values in the array of name

Thank you in advance

Opster ES Ninja - Kamal

Using Scripting (No Mapping Change)

No mapping changes are required to what you already have.

I've come up with the below script. I believe the core logic is in the script which I think is self-explanatory.

POST test_index_arr/_search
{
  "query":{
    "bool": {
      "filter": { 
        "script": {
          "script": {
            "source" : """
              List myList = doc['arr.name'];
              int tempVal = myList.size();
              for(int i=0; i<params.ids.size(); i++){
                long temp = params.ids.get(i);
                if(myList.contains(temp))
                {
                  tempVal = tempVal - 1;
                }

                if(tempVal==0){
                  break;
                }
              }

              if(tempVal==0)
                return true;
              else
                return false;
            """,
            "lang"   : "painless",
            "params": {
              "ids": [1,2,4]
            }
          }
        }
      }
    }
  }
}

So what the scripting does is, it checks if a document has all its numbers in its arr present in the input, if so it would return the document.

In the above query, the part "ids": [1,2,4] acts as input. You need to add/remove/update values here this depending on your requirement.

Hope it helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to sort a list when certain values must appear later than others, potentially ignoring sort order for such items that need 'delaying'

elasticsearch bool query combine must with OR

Must match multiple values

Combining must_not in ElasticSearch Query

ValueError: If using all scalar values, you must pass an index

How to substitute all empty/blank values by "Others" in Elasticsearch?

elasticsearch MUST vs. MUST_NOT (inverse) differences

Elasticsearch search bool + must query

ElasticSearch query with MUST and SHOULD

How to perform SELECT where a column must match ALL values?

"query values must be an array"...?

ElasticSearch nesting Must and Should

AngularJs validation, all inputs must have different values

Elasticsearch must with multiple conditions

Combining filter and must in elasticsearch

Elasticsearch must with subquery intersection

Typescript: typesafety for object must have all keys in array values

elasticsearch must query combine OR?

Using bool must match and match _all in one elasticsearch query

elasticsearch must multivalue search

Sanitize some translate values but not others

ElasticSearch Filter not working with must AND must_not

Finding values in postgres array (some must be in, some must not be in at the same query)

queryDSL - elasticsearch - must along with must-not doesnot give expected result

Elasticsearch term query with must and should

Join tables using IN clause where must match ALL values

Button must have a tooltip in one of template, but not in the others

elasticsearch must OR must_not

Multiple must conditions in Elasticsearch

TOP Ranking

HotTag

Archive