2013-12-16 67 views
9

多个范围elasticsearch以下range_query返回预期的结果:查询与多个日期

{"query": { 
    "bool": { 
     "must": [ 
     { 
      "range": { 
      "created_at": { 
       "gte": "2013-12-09" 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

但是和荷兰国际集团共同几个范围查询,没有返回值:

{"query": { 
    "bool":{ 
     "must": [ 
     { 
      "and": [ 
      { 
       "range": { 
       "created_at": { 
        "gte": "2013-12-09" 
       } 
       } 
      }, 
      { 
       "range": { 
       "happens_on": { 
        "lte": "2013-12-16" 
       } 
       } 
      }, 
      { 
       "range": { 
       "created_at": { 
        "lte": "2013-12-14" 
       } 
       } 
      } 
      ] 
     } 
     ] 
    } 
    } 
} 

什么在多个字段上使用多个range_queries的正确方法?

编辑:啊,好的,所以这是我使用range_filter而不是range_query?这听起来很有希望,所以我只用一个范围过滤器重新编写了我的查询。在这里发布所有内容,以防万一我把查询搞乱了。我执行GET和源键内的一切实际上是JSON的,但我删除了逃跑的连字符的可读性:

{ 
    "source": { 
    "filtered": { 
     "filter": { 
     "and": [ 
      { 
      "term": { 
       "restricted": false 
      } 
      }, 
      { 
      "not": { 
       "term": { 
       "deleted": true 
       } 
      } 
      }, 
      { 
      "range": { 
       "happens_on": { 
       "lte": "2013-12-16" 
       } 
      } 
      } 
     ] 
     }, 
     "query": { 
     "bool": { 
      "must": [ 
      ] 
     } 
     } 
    }, 
    "from": 0, 
    "size": 10 
    } 
} 

遗憾的是,我的问题还是一样:我没有收到任何命中。

EDIT2:因此,在像Njal这样的必要条款内部的范围内走的时候,我们建议。这给了我一个这样的多范围查询:

{ 
    "source": { 
    "filter": { 
     "and": [ 
     { 
      "term": { 
      "restricted": false 
      } 
     }, 
     { 
      "not": { 
      "term": { 
       "deleted": true 
      } 
      } 
     } 
     ] 
    }, 
    "from": 0, 
    "query": { 
     "bool": { 
     "must": [ 
      { 
      "range": { 
       "happens_on": { 
       "gte": "2013-12-06" 
       } 
      } 
      }, 
      { 
      "range": { 
       "created_at": { 
       "gte": "2013-12-15" 
       } 
      } 
      }, 
      { 
      "range": { 
       "happens_on": { 
       "lte": "2013-12-17" 
       } 
      } 
      }, 
      { 
      "range": { 
       "created_at": { 
       "lte": "2013-12-17" 
       } 
      } 
      } 
     ] 
     } 
    }, 
    "size": 10 
    } 
} 

仍然没有结果返回。我在这里做了什么明显的错误?

回答

12

根据您的bool查询的must子句,无需将其包装在and中。没有and查询,也许你正在考虑and filter

Example runnable play为方便起见,卷曲的命令:

#!/bin/bash 

export ELASTICSEARCH_ENDPOINT="http://localhost:9200" 

# Create indexes 

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{ 
    "settings": {}, 
    "mappings": { 
     "type": { 
      "properties": { 
       "created_at": { 
        "type": "date", 
        "format": "dateOptionalTime" 
       }, 
       "name": { 
        "type": "string" 
       }, 
       "happens_on": { 
        "type": "date", 
        "format": "dateOptionalTime" 
       } 
      } 
     } 
    } 
}' 


# Index documents 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d ' 
{"index":{"_index":"play","_type":"type"}} 
{"name":"foo","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"} 
{"index":{"_index":"play","_type":"type"}} 
{"name":"bar","created_at":"2013-12-08T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"} 
{"index":{"_index":"play","_type":"type"}} 
{"name":"bar","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-17T00:00:00.000Z"} 
' 

# Do searches 

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' 
{ 
    "query": { 
     "bool": { 
      "must": [ 
       { 
        "range": { 
         "created_at": { 
          "gte": "2013-12-09T00:00:00.000Z" 
         } 
        } 
       }, 
       { 
        "range": { 
         "happens_on": { 
          "lte": "2013-12-16T00:00:00.000Z" 
         } 
        } 
       } 
      ] 
     } 
    } 
} 
' 
+0

感谢您的回答和对不起,我对这个混乱。我会尝试将我的范围嵌套在must子句中。 – thomax

+0

好吧,我试过你的建议,看看我的EDIT2的问题。仍然没有结果。这可能与日期格式有关吗? – thomax

+1

Riiiight。所以原来我Njals的建议看起来不适合我的原因是我的测试搞砸了。现在测试是固定的和解决方案的岩石谢谢! – thomax