mirror of
https://github.com/typeable/bloodhound.git
synced 2024-12-03 14:12:36 +03:00
Add support for cardinality aggregations
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-aggregations-metrics-cardinality-aggregation.html
This commit is contained in:
parent
1467ffee92
commit
06dc6f4398
@ -45,6 +45,7 @@ module Database.Bloodhound.Types
|
||||
, mkTermsAggregation
|
||||
, mkTermsScriptAggregation
|
||||
, mkDateHistogram
|
||||
, mkCardinalityAggregation
|
||||
, mkDocVersion
|
||||
, docVersionNumber
|
||||
, toMissing
|
||||
@ -310,6 +311,7 @@ module Database.Bloodhound.Types
|
||||
, MissingAggregation(..)
|
||||
, ValueCountAggregation(..)
|
||||
, FilterAggregation(..)
|
||||
, CardinalityAggregation(..)
|
||||
, DateHistogramAggregation(..)
|
||||
, DateRangeAggregation(..)
|
||||
, DateRangeAggRange(..)
|
||||
@ -1694,6 +1696,7 @@ data Interval = Year
|
||||
| FractionalInterval Float TimeInterval deriving (Eq, Read, Show, Generic, Typeable)
|
||||
|
||||
data Aggregation = TermsAgg TermsAggregation
|
||||
| CardinalityAgg CardinalityAggregation
|
||||
| DateHistogramAgg DateHistogramAggregation
|
||||
| ValueCountAgg ValueCountAggregation
|
||||
| FilterAgg FilterAggregation
|
||||
@ -1716,6 +1719,10 @@ data TermsAggregation = TermsAggregation { term :: Either Text Text
|
||||
, termAggs :: Maybe Aggregations
|
||||
} deriving (Eq, Read, Show, Generic, Typeable)
|
||||
|
||||
data CardinalityAggregation = CardinalityAggregation { cardinalityField :: FieldName,
|
||||
precisionThreshold :: Maybe Int
|
||||
} deriving (Eq, Read, Show, Generic, Typeable)
|
||||
|
||||
data DateHistogramAggregation = DateHistogramAggregation { dateField :: FieldName
|
||||
, dateInterval :: Interval
|
||||
, dateFormat :: Maybe Text
|
||||
@ -1774,6 +1781,9 @@ mkTermsScriptAggregation t = TermsAggregation (Right t) Nothing Nothing Nothing
|
||||
mkDateHistogram :: FieldName -> Interval -> DateHistogramAggregation
|
||||
mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothing Nothing Nothing
|
||||
|
||||
mkCardinalityAggregation :: FieldName -> CardinalityAggregation
|
||||
mkCardinalityAggregation t = CardinalityAggregation t Nothing
|
||||
|
||||
instance ToJSON Version where
|
||||
toJSON Version {..} = object ["number" .= number
|
||||
,"build_hash" .= build_hash
|
||||
@ -1864,6 +1874,12 @@ instance ToJSON Aggregation where
|
||||
where
|
||||
toJSON' x = case x of { Left y -> "field" .= y; Right y -> "script" .= y }
|
||||
|
||||
toJSON (CardinalityAgg (CardinalityAggregation field precisionThreshold)) =
|
||||
object ["cardinality" .= omitNulls [ "field" .= field,
|
||||
"precisionThreshold" .= precisionThreshold
|
||||
]
|
||||
]
|
||||
|
||||
toJSON (DateHistogramAgg (DateHistogramAggregation field interval format preZone postZone preOffset postOffset dateHistoAggs)) =
|
||||
omitNulls ["date_histogram" .= omitNulls [ "field" .= field,
|
||||
"interval" .= interval,
|
||||
|
@ -1244,6 +1244,17 @@ main = hspec $ do
|
||||
searchExpectAggs search
|
||||
searchValidBucketAgg search "users" toTerms
|
||||
|
||||
it "returns cardinality aggregation results" $ withTestEnv $ do
|
||||
_ <- insertData
|
||||
let cardinality = CardinalityAgg $ mkCardinalityAggregation $ FieldName "user"
|
||||
let search = mkAggregateSearch Nothing $ mkAggregations "users" cardinality
|
||||
let search' = search { Database.Bloodhound.from = From 0, size = Size 0 }
|
||||
searchExpectAggs search'
|
||||
let docCountPair k n = (k, object ["value" .= Number n])
|
||||
res <- searchTweets search'
|
||||
liftIO $
|
||||
fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
|
||||
|
||||
it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
|
||||
_ <- insertData
|
||||
let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }
|
||||
|
Loading…
Reference in New Issue
Block a user