2014-07-14 189 views
0

我正在使用elasticsearch並創建以下映射,創建地理索引,長字段比較它(gte)和布爾字段以識別性別。elasticsearch過濾器不適用於聚合

curl -XDELETE 'http://localhost:9165/locations' && echo 
curl -XPUT 'http://localhost:9165/locations' && echo 
curl -XPUT 'http://localhost:9165/locations/location/_mapping' -d ' 
{ 
    "location": { 
     "properties": { 
      "location": {"type" : "geo_point"}, 
      "gender" : {"type" : "boolean"}, 
      "last_appearance": {"type": "long"} 
     } 
    } 
}' && echo "Mapping done" 

然後我做了以下插入索引:

curl -XPUT 'http://localhost:9165/locations/location/1' -d '{ 
    "location": 
    { 
    "lat": "47.785346", 
    "lon": "67.701857" 
    }, 
    "category_id": "5", 
    "gender": "true", 
    "city": "Almaty", 
    "last_appearance": "3333333", 
    "venue_id": "5229774711d2a3ec9e333d00" 
}' 

最後我嘗試過濾所有數據,其中的用戶最後一個出場的是更大然後一些測試值,在我的情況下,這將是222222222222222222城市是阿拉木圖,那麼我想用過濾的數據做一些聚合。

curl -X GET http://localhost:9165/locations/location/_search -d ' 
{ 
    "filter" : { 
     "and" : [ 
     { 
      "range" : 
      { 
      "last_appearance" : 
      { 
       "gte" : "222222222222222222" 
       } 
      } 
     }, 
     { 
      "term" : {"city" : "Almaty"} 
     } 
     ] 
    }, 

    "aggs" : { 
     "venues" : { 
      "terms" : { "field" : "venue_id"}, 
      "aggs": { 
       "genders" : { 
        "terms" : {"field" : "gender"} 
       } 
      } 
     } 
    } 
}' | python -mjson.tool 

我不想聚集在那種情況下任何東西,因爲是333333小於2222222222222222222,但彈性開始聚集:

{ 
    "_shards": { 
     "failed": 0, 
     "successful": 1, 
     "total": 1 
    }, 
    "aggregations": { 
     "venues": { 
      "buckets": [ 
       { 
        "doc_count": 1, 
        "genders": { 
         "buckets": [ 
          { 
           "doc_count": 1, 
           "key": "true" 
          } 
         ] 
        }, 
        "key": "5229774711d2a3ec9e333d00" 
       } 
      ] 
     } 
    }, 
    "hits": { 
     "hits": [], 
     "max_score": null, 
     "total": 0 
    }, 
    "timed_out": false, 
    "took": 1 
} 

請幫我處理的。感謝您閱讀這個長長的問題。

+2

這可以幫助你http://stackoverflow.com/questions/23807314/multiple-filters-and-an-aggregate-in-elasticsearch?rq=1 – nKandel

回答

1

感謝@nKandel的評論,這真的很有幫助。

最後的查詢是這樣的:

curl -X GET http://localhost:9165/locations/location/_search -d ' 
{ 
    "size" : 0, 
    "aggregations" : { 
     "cities" : { 
      "filter" : { 
       "term" : { 
        "city": "Almaty" 
       } 
      }, 
      "aggregations": { 
       "lasts" : { 
        "filter" : { 
         "range" : { 
          "last_appearance" : { 
           "gte" : 100 
          } 
         } 
        }, 
        "aggregations": { 
         "venues" : { 
          "terms" : { "field" : "venue_id"}, 
          "aggregations": { 
           "genders" : { 
            "terms" : {"field" : "gender"} 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 
'| python -mjson.tool