Can I have only filter context query?

Can I create a query with only filter context in it, without any query context or do I always have to have some query context in addition to the filter?
In addition I'd like it to be nested, but let me know if I can have filter without nested as well if combination of filter and nested is not possible.
May be use filtered?
The idea is that I don't need scoring, just a quick filter over the documents.

An example would for the query itself would be something like this:

{
  "query": {
    "nested": {
      "path": "properties",
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "properties.key": "propertyName"
              }
            },
            {
              "term": {
                "properties.value": "propertyValue"
              }
            }
          ]
        }
      }
    }
  }
}

The above doesn't work, while the one below does work:

{
  "query": {
    "nested": {
      "path": "properties",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "properties.key": "propertyName"
              }
            },
            {
              "match": {
                "properties.value": "propertyValue"
              }
            }
          ]
        }
      }
    }
  }
}
1 Like
{
      "query": {
        "nested": {
          "path": "properties",
          "query": {
            "bool": {
              "filter": [
                {
                  "term": {
                    "properties.key": {
                      "value": "VALUE"
                    }
                  }
                },
                {
                  "term": {
                    "properties.value": {
                      "value": "VALUE"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }

Change MUST to FILTER and replace your match query with term filters. Keep in mind that it matters how your data is analyzed:

The term query finds documents that contain the exact term specified in the inverted index.

Link

Thanks @byronvoorbach, that actually didn't work, but this is what worked for me:
The example below includes a filter by nested fields and an additional field

{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "filter": [
                  {
                    "match": {
                      "properties.key": "someKey"
                    }
                  },
                  {
                    "match": {
                      "properties.value": "someValue"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "match": {
            "someField": "someFieldValue"
          }
        }
      ]
    }
  }
}

Cool! Nice that you managed to work it out :slight_smile:

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