0

I have the next problem, my data structure has this shape:
{"editions":['optionA', 'optionB']}
I need to exclude ALL results with this shape:
{"editions":['optionB']}

But include ALL results with this shape:

{"editions":['optionA']}

or

{"editions":['optionA', 'optionB']}

Being more clear, I need to exclude all results that just have one entry in editions and his value is optionB specifically.

I've been trying something like:

bool: {
  must: [
    {
    query_string: {
        default_field: "name",
        query: `${params.text}*`
      },
    }
  ],
  must_not : {
    term : { "editions" : "optionB" },
    must : {
        script : {
            script : {
                inline: "doc['editions'].values.length < 2",
                lang: "painless"
             }
        }
    }
  }
}

But no results are given. Thanks in advance!

PD: I'm using ElasticSearch 7.*

Cesar Jr Rodriguez
  • 1,691
  • 4
  • 22
  • 35

1 Answers1

0

Your question is a bit confusing b/c first you referenced editions but your script references sections. Let's take editions then...

Since you're already using a script, you can go with the following:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "name",
            "query": `${params.text}*`
          }
        },
        {
          "term": {
            "editions": {
              "value": "optionA"
            }
          }
        }
      ],
      "must_not": [
        {
          "script": {
            "script": {
              "inline": "doc['editions'].size() == 1 && doc['editions'][0] == 'optionB'",
              "lang": "painless"
            }
          }
        }
      ]
    }
  }
}
Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68