如何使用ElasticSearch应用默认的帖子过滤器?

时间:2015-02-18 14:03:02

标签: elasticsearch

我想使用elasticsearch实现回测试引擎。为了能够做到这一点,我需要通过排除测试日期之后的那些来过滤命中,我想默认这样做,因为算法(我想要回测)不应该知道回溯测试

换句话说,是否可以将默认的后置过滤器应用于ElasticSearch查询?

例如,假设这些文档在ES中:

{ name: 'Jean', weight: 70, date: 2012-01-01 }
{ name: 'Jules', weight: 70, date: 2010-01-01 }
{ name: 'David', weight: 80, date: 2010-01-01 }

我想应用一个默认的过滤器来排除2011年之后的文档,如果我进行查询以获得每个重量为70的人,我唯一的结果就是Jules。

1 个答案:

答案 0 :(得分:2)

您可以使用Filtered Aliases执行此操作。当您通过别名查询时,过滤器会自动应用于您的查询...这会将其隐藏在您的应用程序中:

// Insert the data
curl -XPOST "http://localhost:9200/people/data/" -d'
{ "name": "Jean", "weight" : 70, "date": "2012-01-01" }'

curl -XPOST "http://localhost:9200/people/ata" -d'
{ "name": "Jules", "weight" : 70, "date": "2010-01-01" }'

curl -XPOST "http://localhost:9200/people/data/" -d'
{ "name": "David", "weight" : 80, "date": "2010-01-01" }'

// Add a filtered alias
curl -XPOST "http://localhost:9200/_aliases" -d'
{
    "actions" : [
        {
            "add" : {
                 "index" : "people",
                 "alias" : "filtered_people",
                 "filter" : { 
                    "range" : { 
                        "date" : { "gte" : "2011-01-01"} 
                    } 
                }
            }
        }
    ]
}'

现在,您针对filtered_people而不是基础people索引执行搜索:

curl -XGET "http://localhost:9200/filtered_people/_search" -d'
{
    "query": {
        "filtered": {
           "filter": {
               "term": {
                  "weight": 70
               }
           }
        }
    }
}'

只返回您感兴趣的文档:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "people",
            "_type": "ata",
            "_id": "AUudZPUfCSiheYJkTW-h",
            "_score": 1,
            "_source": {
               "name": "Jules",
               "weight": 70,
               "date": "2010-01-01"
            }
         }
      ]
   }
}