Help with a query - problem matching documents

Good afternoon,

I am trying to craft a query that shoudl return documents based on some criterias. First, this is the mapping of my documents :

   {  
      "subscriptions":{  
         "mappings":{  
            "properties":{  
               "entity":{  
                  "type":"integer"
               },
               "filters":{  
                  "type":"nested",
                  "properties":{  
                     "filter":{  
                        "type":"keyword"
                     }
                  }
               },
               "hasPushActive":{  
                  "type":"boolean"
               },
               "notify":{  
                  "type":"boolean"
               },
               "user":{  
                  "type":"integer"
               }
            }
         }
      }
   }

There is the query :

{  
    "_source":[  
        "user",
        "filters"
    ],
    "size":100,
    "query":{  
        "constant_score":{  
            "filter":{  
                "bool":{  
                    "must":[  
                        {  
                            "term":{  
                                "entity":2
                            }
                        },
                        {  
                            "term":{  
                                "notify":true
                            }
                        },
                        {  
                            "term":{  
                                "hasPushActive":true
                            }
                        }
                    ],
                    "should":[  
                        {  
                            "bool":{  
                                "must_not":[  
                                    {  
                                        "nested":{  
                                            "path":"filters",
                                            "query":{  
                                                "exists":{  
                                                    "field":"filters"
                                                }
                                            }
                                        }
                                    }
                                ]
                            }
                        },
                        {  
                            "bool":{  
                                "must":[  
                                    {  
                                        "nested":{  
                                            "path":"filters",
                                            "query":{  
                                                "bool":{  
                                                    "should":[  
                                                        {  
                                                            "match":{  
                                                                "filters.filter":"I"
                                                            }
                                                        },
                                                        {  
                                                            "match":{  
                                                                "filters.filter":"like"
                                                            }
                                                        },
                                                        {  
                                                            "match":{  
                                                                "filters.filter":"burgers!"
                                                            }
                                                        }
                                                    ]
                                                }
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        }
    }
}

The idea of the query is to return documents matching:

  • "hasPushActive" = true
  • "notify" = true
  • "entity" = the entity id (2 in my case)

Now there's the tricky part that seems not to work.

  • The document must not contains a filter OR
  • It should contains at least one of the matching terms I provide. In my case "I", "like", "burgers!"

ATM my query returns 4 documents, it should returns only 2. The ones without filters because the other two have filters (1st document have the filter "sushis", the other one have "pizzas").

Any idea on what the error would be? What am I doing wrong ? Hope someone has the courage to read this...

Thanks in advance,
Best regards,
Guillaume

Help? :frowning:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.