Wrong result in Bool Query

Mapping

PUT index
PUT /index/_mapping
{
  "properties": {
    "categories": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "integer"
        }
      }
    },
    "deleted": {
      "type": "boolean"
    },
    "id": {
      "type": "long"
    },
    "manufacturers": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "integer"
        }
      }
    },
    "vendors": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "long"
        },
        "isDeletedVendor": {
          "type": "boolean"
        },
        "isPublishedVendor": {
          "type": "boolean"
        }
      }
    },
    "published": {
      "type": "boolean"
    }
  }
}

Fill with Data

PUT index/_doc/1
{
  "categories": [
    {
      "id": 9
    }
  ],
  "manufacturers": [
    {
      "id": 2452
    }
  ],
  "vendors": [
    {
      "id": 8,
      "isDeletedVendor": true,
      "isPublishedVendor": true
    },
    {
      "id": 9,
      "isDeletedVendor": true,
      "isPublishedVendor": true
    }
  ],
  "published": true,
  "deleted": false
}

Query which returns wrong data

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "categories",
            "query": {
              "terms": {
                "categories.id": [
                  9
                ]
              }
            }
          }
        },
        {
          "terms": {
            "deleted": [
              false
            ]
          }
        },
        {
          "terms": {
            "published": [
              true
            ]
          }
        },
        {
          "nested": {
            "path": "vendors",
            "query": {
              "terms": {
                "vendors.isDeletedVendor": [
                  false
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "vendors",
            "query": {
              "terms": {
                "vendors.isPublishedVendor": [
                  true
                ]
              }
            }
          }
        }
      ],
      "should": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must_not": [
                          {
                            "nested": {
                              "path": "manufacturers",
                              "query": {
                                "exists": {
                                  "field": "manufacturers"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "terms": {
                            "vendors.id": [
                              87
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must_not": [
                          {
                            "nested": {
                              "path": "manufacturers",
                              "query": {
                                "exists": {
                                  "field": "manufacturers"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "terms": {
                            "vendors.id": [
                              56
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 1000,
  "_source": {
    "includes": [
      "modelName",
      "vendors.id", 
      "manufacturers.id", 
      "id"
    ]
  }
}

The logic of the query in should is like this

Select * from index where(manufacturers is null and vendorsid in(87)) or (manufacturers is null and vendorsidin(56)) but it gives me hits where manufacturers id are not null and vendors id are not in 87 or 56

It gives me result like this why?

"hits" : [
      {
        "_index" : "onoff-live",
        "_type" : "_doc",
        "_id" : "130011",
        "_score" : 5.0,
        "_source" : {
          "modelName" : "Galaxy A20",
          "manufacturers" : [
            {
              "id" : 216
            }
          ],
          "id" : 130011,
          "vendors" : [
            {
              "id" : 23
            }
          ]
        }
      }]

Quick sanity check - do the manufacturers and categories each have just a single property (id)?

There's no need to use "nested" types if you have single properties - just send arrays of values.
Nested is designed to solve the cross-matching problem if you query objects with multiple properties like people with a firstName and lastName

I have many properties on category and manufacturer nested object, but I wrote only ids...

Problem in this query is in must part... When i dont have must part there is only 3 hits, but with must part it is 9980... they should be combined with and logic....

Your example query matches neither of the example docs so I'm having a hard time understanding the problem.
Can you share the exact JSON for the docs and query that fail to behave as expected?

i have shared exact shared exact json for query.

That query doesn't match the example doc because the doc only has isDeleletedVendor: true

okey, sorry I will give you in a minute

try this query it doesn't need ispublishedvendor

  {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "categories",
                "query": {
                  "terms": {
                    "categories.id": [
                      9
                    ]
                  }
                }
              }
            },
          ],
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "should": [
                        {
                          "bool": {
                            "must_not": [
                              {
                                "nested": {
                                  "path": "manufacturers",
                                  "query": {
                                    "exists": {
                                      "field": "manufacturers"
                                    }
                                  }
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "should": [
                        {
                          "nested": {
                            "path": "vendors",
                            "query": {
                              "terms": {
                                "vendors.id": [
                                  87
                                ]
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "should": [
                        {
                          "bool": {
                            "must_not": [
                              {
                                "nested": {
                                  "path": "manufacturers",
                                  "query": {
                                    "exists": {
                                      "field": "manufacturers"
                                    }
                                  }
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "should": [
                        {
                          "nested": {
                            "path": "vendors",
                            "query": {
                              "terms": {
                                "vendors.id": [
                                  56
                                ]
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "size": 1000,
      "_source": {
        "includes": [
          "modelName",
          "vendors.id", 
          "manufacturers.id", 
          "id"
        ]
      }
    }

That matches your example doc. Were you expecting it not to?

You may have misunderstood the nature of shoulds and musts.
When you have musts (or filters) the shoulds are optional - "nice to haves" for extra scoring points.
When you only have should clauses it is mandatory at least one should match.
So a should clause may well match only 3 hits but when combined with a must clause it could bring in 9980.
Working as designed I suspect

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