Search for queries into .percolator type

时间:2016-11-25 20:42:29

标签: elasticsearch indices elasticsearch-percolate

I have an Elasticsearch v2.4.2 index, I'm filling its .percolator type with a bunch of queries and some special values. query-docs looks like this:

"query" : {
      "query_string" : {
        "fields" : [ "title", "meta" ],
        "query" : "notebooks AND clamps"
      },
      "key_id" : 14,
      "owner_id" : 481,
      "priority" : 50,
      "special_id" : 477
    }

I'm trying to delete some of these queries from .percolator, specifically those with even "key_id" values.

The problem is that I'm trying to perform a search on the .percolator but I'm not getting the results. for instance, I tried these curl calls:

curl 'localhost:9200/percolate_index_d/.percolator/_search?q=key_id:14&pretty'


curl 'localhost:9200/percolate_index_d/.percolator/_search?&pretty' -d '{ query:{filtered:{filter:{term:{"key_id":"14"}}}} }'

But I always get this:

 {
      "took" : 6,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
      },
      "hits" : {
        "total" : 0,
        "max_score" : null,
        "hits" : [ ]
      }
    }

I even tried using query.key_id but no luck. Not sure if I'm doing something wrong, if it's possible to search on .percolator type or if there is some workaround for this.

1 个答案:

答案 0 :(得分:1)

您的上述查询根本不正确。尝试针对您的索引运行它,您会发现语法错误。

查询的正确语法如下:

{
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "fields": [
            "title",
            "meta"
          ],
          "query": "notebooks AND clamps"
        }
      },
      "filter": [
        {
          "term": {
            "key_id": 14
          }
        },
        {
          "term": {
            "owner_id": 481
          }
        },
        {
          "term": {
            "priority": 50
          }
        },
        {
          "term": {
            "special_id": 477
          }
        }
      ]
    }
  }
}

然后,您就可以使用key_id: 14搜索这样的查询:

curl 'localhost:9200/percolate_index_d/.percolator/_search?q=query.bool.filter.term.key_id:14&pretty'

更新

您的查询的元数据key_id | owner_id | priority | special_id字段未设置在正确的位置,您需要将它们设置在query字段之外,如此_

{
  "query" : {
    "query_string" : {
      "fields" : [ "title", "meta" ],
      "query" : "notebooks AND clamps"
    }
  },
  "key_id" : 14,
  "owner_id" : 481,
  "priority" : 50,
  "special_id" : 477
} 

执行此操作后,您将能够使用

检索查询
curl 'localhost:9200/percolate_index_d/.percolator/_search?q=key_id:14&pretty'