Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested query does not support filters

I have following query:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "should": [
            {
              "wildcard": {
                "translations.title": {
                  "value": "*abc*",
                  "boost": 2
                }
              }
            },
            {
              "wildcard": {
                "translations.subtitle": {
                  "value": "*abc*",
                  "boost": 1.9
                }
              }
            },
            {
              "match": {
                "translations.title": {
                  "query": "abc",
                  "fuzziness": 5
                }
              }
            },
            {
              "match": {
                "translations.subtitle": {
                  "query": "abc",
                  "fuzziness": 5
                }
              }
            },
            {
              "wildcard": {
                "series.translations.title": {
                  "value": "*abc*",
                  "boost": 0.5
                }
              }
            },
            {
              "wildcard": {
                "translations.subtitle": {
                  "value": "*abc*",
                  "boost": 0.5
                }
              }
            },
            {
              "wildcard": {
                "tags.text": {
                  "value": "*abc*",
                  "boost": 1.5
                }
              }
            },
            {
              "match": {
                "tags.text": {
                  "query": "abc",
                  "fuzziness": 5
                }
              }
            },
            {
              "wildcard": {
                "translations.content": {
                  "value": "*abc*"
                }
              }
            }
          ]
        }
      },
      "filter": {
        "and": [
          {
            "term": {
              "type": "video"
            }
          },
          {
            "term": {
              "videoType": "brightcove"
            }
          },
          {
            "type": {
              "value": "post-en"
            }
          },
          {
            "term": {
              "isPublished": true
            }
          },
          {
            "term": {
              "status": "published"
            }
          },
          {
            "term": {
              "CategoryId": "4"
            }
          },
          {
            "range": {
              "contentDuration": {
                "from": "300001"
              }
            }
          },
          {
            "nested": {
              "path": "languages",
              "filters": {
                "term": {
                  "languages.id": "148"
                }
              }
            }
          }
        ]
      }
    }
  },
  "size": 20,
  "from": 0,
}

And it returns error:

  "error": {
    "root_cause": [
      {
        "type": "query_parsing_exception",
        "reason": "[nested] query does not support [filters]",
        "index": "nowness",
        "line": 1,
        "col": 1003
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query_fetch",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "nowness",
        "node": "Wuh8rSunQ5mdAa2j-RYOBA",
        "reason": {
          "type": "query_parsing_exception",
          "reason": "[nested] query does not support [filters]",
          "index": "nowness",
          "line": 1,
          "col": 1003
        }
      }
    ]
  }
}

It complains about this fragment:

{
            "nested": {
              "path": "languages",
              "filters": {
                "term": {
                  "languages.id": "148"
                }
              }
            }
          }

It used to work, but it does not in latest ES version. How I can change this query to make it work?

like image 764
user606521 Avatar asked Dec 05 '22 15:12

user606521


2 Answers

Replace the nested filter with a nested query:

"nested" : {
  "path" : "languages",
  "query" : {
    "term": {
      "languages.id": "148"
    }
  }
}

https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-nested-query.html

like image 76
paqash Avatar answered Dec 22 '22 21:12

paqash


From Es2.0, The nested filter has been replaced by the Nested Query. It behaves as a query in “query context” and as a filter in “filter context”.

You can either filter your documents via query as specified by @paqash or

 {
    "nested" : {
      "path" : "languages",
      "query": { 
        "bool": { 
          "filter": [ 
            { "term":  { "languages.id": "148" }}
          ]
        }
      }
    }
}

I haven't tested it myself but should work as per the documentation on query clauses in filter context

Use query clauses in query context for conditions which should affect the score of matching documents (i.e. how well does the document match), and use all other query clauses in filter context.

like image 24
Rahul Avatar answered Dec 22 '22 22:12

Rahul