Elasticsearch - 无法使用过滤器来处理query_string

时间:2015-06-12 11:43:57

标签: elasticsearch

我试图过滤结果,只返回带有标记' news'

-> POST https://host.com/harvester-dev/entries/_search?
   _source=description%2Cimage%2Ctitle%2CupdateTime%2Ctags%2Curl
  {
    "filtered": {
      "query": {
        "query_string": {
          "query": "suksess-duo",
          "default_operator": "AND"
        }
      },
      "filter": {
        "bool": {
          "must": {
            "term": {
              "tags": "news"
            }
          }
        }
      }
    },
    "aggs": {
      "harvester-test": {
        "global": {}
      }
    }
  }
  <- 400
  {
    "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[yXZEi0emQkuToiYBcj1wUA][harvester-dev][0]: SearchParseException[[harvester-dev][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"filtered\":{\"query\":{\"query_string\":{\"query\":\"suksess-duo\",\"default_operator\":\"AND\"}},\"filter\":{\"bool\":{\"must\":{\"term\":{\"tags\":\"news\"}}}}},\"aggs\":{\"harvester-test\":{\"global\":{}}}}]]]; nested: SearchParseException[[harvester-dev][0]: from[-1],size[-1]: Parse Failure [No parser for element [filtered]]]; }{[6gUDVyvDTSGUeYnoIN8N8Q][harvester-dev][1]: RemoteTransportException[[elastic_search148_aws_eu_west_1_data_0_dblayer_com][inet[/NN.NN.NN.NN:9300]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[harvester-dev][1]: from[-1],size[-1]: Parse Failure [Failed to parse source

这就是数据的样子:

{
  hits: {
    total: 15921,
    max_score: 1,
    hits: [
    {
      _index: "harvester-dev",
      _type: "entries",
      _id: "5574156f",
      _score: 1,
      _source: {
        image: "image-url",
        description: "fine description",
        updateTime: "2015-06-07T09:57:03.654Z",
        title: "our title",
        url: "http://example.com",
        tags: [
          "computers",
          "news"
        ]
      }
    },]}}

我希望只获取包含标签&#34; news&#34;的文档。 E.g文件匹配&#34;计算机&#34;标签&#34; news&#34;。我应该如何制作我的查询?

1 个答案:

答案 0 :(得分:0)

我的坏。 filtered新闻必须嵌套在query

  {
    "query": {
      "filtered": {
        "query": {
          "query_string": {
            "query": "computers",
            "default_operator": "AND"
          }
        },
        "filter": {
          "bool": {
            "must": {
              "term": {
                "tags": "news"
              }
            }
          }
        }
      }
    },