2014-09-21 109 views
3

在我eleasticsearch指数,我有两个文件索引,如下过滤器一起:期限和范围内,在elasticsearch查询

POST dyn-props/item 
{ 
    "name": "bar foo", 
    "properties": [ 
     { 
      "type": "foo", 
      "value": 1.45 
     }, 

     { 
      "type": "bar", 
      "value": 256.34 
     }, 

     { 
      "type": "foobar", 
      "value": 43.43 
     } 
    ] 
} 

POST dyn-props/item 
{ 
    "name": "foo bar", 
    "properties": [ 
     { 
      "type": "foo", 
      "value": 33.34 
     }, 

     { 
      "type": "bar", 
      "value": 22.23 
     } 
    ] 
} 

在此项目类型,我想查询具有物业项目其价值大于。我可以筛选结果下来有一个类型为FOO与下面的查询属性的项目:

POST dyn-props/item/_search 
{ 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": {} 
     }, 
     "filter": { 
      "term": { 
       "properties.type": "foo" 
      } 
     } 
     } 
    } 
} 

,但我不知道我怎么能应用范围过滤器。任何想法?

编辑:

发出下面的查询给了我预期的错误的结果:

POST dyn-props/item/_search 
{ 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": {} 
     }, 
     "filter": { 
      "bool": { 
      "must": [ 
       { 
        "term": { 
        "properties.type": "foo" 
        } 
       }, 

       { 
        "range": { 
         "properties.value": { 
         "gte" : 10 
         } 
        } 
       } 
      ] 
      } 
     } 
     } 
    } 
} 

结果:

{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 2, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "dyn-props", 
      "_type": "item", 
      "_id": "PetPVxwARLOcZqlv28xjpw", 
      "_score": 1, 
      "_source": { 
       "name": "bar foo", 
       "properties": [ 
        { 
        "type": "foo", 
        "value": 1.45 
        }, 
        { 
        "type": "bar", 
        "value": 256.34 
        }, 
        { 
        "type": "foobar", 
        "value": 43.43 
        } 
       ] 
      } 
     }, 
     { 
      "_index": "dyn-props", 
      "_type": "item", 
      "_id": "KqOTXcC9RG6FzPsDDDs8Hw", 
      "_score": 1, 
      "_source": { 
       "name": "foo bar", 
       "properties": [ 
        { 
        "type": "foo", 
        "value": 33.34 
        }, 
        { 
        "type": "bar", 
        "value": 22.23 
        } 
       ] 
      } 
     } 
     ] 
    } 
} 
+0

试试这个http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-numeric-range-filter。 html – 2014-09-21 13:14:06

+1

@Undefined_variable不确定你的意思。我知道我需要使用范围过滤器。问题在于如何。 – tugberk 2014-09-21 13:17:25

+1

使用和过滤器的细节可以在http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-and-filter.html – 2014-09-21 13:25:39

回答

5

找到了答案。这篇文章帮助了很多:ElasticSearch – nested mappings and filters

改变类型的映射:

PUT dyn-props 
{ 
    "mappings": { 
     "item": { 
       "properties": { 
        "name": { 
          "type": "string" 
        }, 
        "properties": { 
          "type": "nested" 
        } 
       } 
     } 
    } 
} 

通过使性能nested type,我能够保持typevalue领域之间的关联。

最后,我能发出这种嵌套查询:

POST dyn-props/item/_search 
{ 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": {} 
     }, 
     "filter": { 
      "nested": { 
       "path": "properties", 
       "filter": { 
        "bool": { 
        "must": [ 
         { 
          "term": { 
           "type": "foo" 
          } 
         }, 
         { 
          "range": { 
           "value": { 
           "gte": 10 
           } 
          } 
         } 
        ] 
        } 
       } 
      } 
     } 
     } 
    } 
} 

这让我正确的结果:

{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "dyn-props", 
      "_type": "item", 
      "_id": "CzTL4sseR2GVYtvf-0slVQ", 
      "_score": 1, 
      "_source": { 
       "name": "foo bar", 
       "properties": [ 
        { 
        "type": "foo", 
        "value": 33.34 
        }, 
        { 
        "type": "bar", 
        "value": 22.23 
        } 
       ] 
      } 
     } 
     ] 
    } 
} 
+1

找到我真的很感谢你分享你的发现,谢谢! – blong 2016-12-02 18:21:41