2016-06-07 96 views
0

我試圖找到所有內容字段包含單詞「敘利亞」,並具有紀元時間大於1465312440000的文檔。以下查詢運行,但只返回文檔包含單詞「敘利亞」。 (Elasticsearch版本2.2)Elasticsearch試圖查詢術語和時間範圍

{ 
    "query": { 
     "filtered": { 
     "query": { 
      "match": { 
       "content": "syria" 
      }, 
      "filter": { 
       "term": { 
        "sourceOriginator": "Twitter" 
       }, 
       "bool": { 

        "range": { 
         "epochCollectionDate": { 
          "gte": 1465312440 

         } 
        } 
       } 

      } 

     } 
    } 
} 
} 

回答

0

當然,如果沒有數據就很難測試,但過濾器是錯誤的。它應該與第二個查詢處於同一級別。我的看法的解決辦法是簡單:

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "match": { 
      "content": "syria" 
      } 
     } 
     ], 
     "filter": [ 
     { 
      "term": { 
      "sourceOriginator": "Twitter" 
      } 
     }, 
     { 
      "range": { 
      "epochCollectionDate": { 
       "gte": 1465312440 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
0

只是爲了補充@ Jettro的解決方案,只能在ES 2.0及更高版本,下面的人會在所有版本的工作提高到ES 5

{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match": { 
      "content": "syria" 
     } 
     }, 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "term": { 
       "sourceOriginator": "Twitter" 
       } 
      }, 
      { 
       "range": { 
       "epochCollectionDate": { 
        "gte": 1465312440 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

請注意,如果您使用的是ES 2.0或更高版本,則應該使用@ Jettro的解決方案,因爲filtered查詢在2.0中已被棄用。