嵌套查询不支持过​​滤器

时间:2017-03-06 14:09:05

标签: elasticsearch

我有以下查询:

{
  "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,
}

它返回错误:

  "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
        }
      }
    ]
  }
}

它抱怨这个片段:

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

它曾经工作,但它不是最新的ES版本。如何更改此查询以使其正常工作?

2 个答案:

答案 0 :(得分:7)

使用嵌套查询替换嵌套过滤器:

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

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

答案 1 :(得分:4)

来自Es2.0,The nested filter has been replaced by the Nested Query。它在“查询上下文”中表现为查询,在“过滤器上下文”中表现为过滤器。

您可以通过@paqash指定的查询过滤文档 或

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

我自己没有测试过,但应该按照documentation on query clauses in filter context

进行操作
  

在查询上下文中使用查询子句以了解应该影响的条件   匹配文件的分数(即文件的好坏程度   匹配),并在过滤器上下文中使用所有其他查询子句。