0

I'm working on a project creating integration tests and the data I have includes all the schema updates people have done in the past. For example My "documents" field has a property of user, which has been an array in the past, but is now an Object.

I'm trying to write an integration test that will ONLY use a document if the user type is Object, not array.

Is there a way to do this with an ExistsQuery? So far I have not been able to find anything. Here's my current query which brings back inconsistent data:

GET _search
{
 "query" : {
   "bool" : {
     "must" : {
       "exists" : {
         "document.user"
       }
     }
   }
 }
}

Ideally I'd like to check for document.user[] and filter those values out.

Dan
  • 979
  • 1
  • 8
  • 29

1 Answers1

0

You can use script query like below

{
  "query": {
    "script": {
      "script": "if (doc['document.keyword'].size()==1) return true;"
    }
  }
}
jaspreet chahal
  • 8,817
  • 2
  • 11
  • 29
  • I'm new to using painless scripts so please forgive me, but is that the entire JSON to use? Nothing else? I would assume you'd need to know about `document.user`, no? – Dan Oct 06 '22 at 09:08
  • @Dan please change "document" to your field name in query. Test it out and let me know if you face any issue – jaspreet chahal Oct 06 '22 at 09:17
  • which field should I change it to, "document" or "user"? Inside the ES doc we literally have a property called "document" (as crazy as that sounds) So should I change it to ```"script": "if (doc['document.user.keyword'].size()==1) return true;``` – Dan Oct 06 '22 at 09:21
  • @Dan can you add sample document and your mapping – jaspreet chahal Oct 06 '22 at 09:28
  • Unfort. no, I cannot. This issue is on a corp account which I cannot share. – Dan Oct 06 '22 at 13:04
  • @Dan try document.user.keyword, check if it works. If data type of field document is nested then you will need to use nested query – jaspreet chahal Oct 06 '22 at 15:26