@@ -51,6 +51,7 @@ module Database.V1.Bloodhound.Types
51 , toMissing
51 , toMissing
53 , toDateHistogram
53 , toDateHistogram
54 , omitNulls
55 , omitNulls
@@ -340,6 +341,7 @@ module Database.V1.Bloodhound.Types
340 , DateMathAnchor(..)
341 , DateMathAnchor(..)
341 , DateMathModifier(..)
342 , DateMathModifier(..)
342 , DateMathUnit(..)
343 , DateMathUnit(..)
344+ , TopHitsAggregation(..)
344 , Highlights(..)
346 , Highlights(..)
345 , FieldHighlight(..)
347 , FieldHighlight(..)
@@ -357,6 +359,7 @@ module Database.V1.Bloodhound.Types
357 , TermsResult(..)
359 , TermsResult(..)
358 , DateHistogramResult(..)
360 , DateHistogramResult(..)
359 , DateRangeResult(..)
361 , DateRangeResult(..)
361 , EsUsername(..)
364 , EsUsername(..)
362 , EsPassword(..)
365 , EsPassword(..)
@@ -1722,7 +1725,15 @@ data Aggregation = TermsAgg TermsAggregation
1722 | ValueCountAgg ValueCountAggregation
1725 | ValueCountAgg ValueCountAggregation
1723 | FilterAgg FilterAggregation
1726 | FilterAgg FilterAggregation
1724 | DateRangeAgg DateRangeAggregation
1727 | DateRangeAgg DateRangeAggregation
1728+ | MissingAgg MissingAggregation
1729+ | TopHitsAgg TopHitsAggregation
1725− | MissingAgg MissingAggregation deriving (Eq, Read, Show, Generic, Typeable)
1730+ deriving (Eq, Read, Show, Generic, Typeable)
1732+data TopHitsAggregation = TopHitsAggregation
1733+ { taFrom :: Maybe From
1734+ , taSize :: Maybe Size
1735+ , taSort :: Maybe Sort
1736+ } deriving (Eq, Read, Show)
1727data MissingAggregation = MissingAggregation
1738data MissingAggregation = MissingAggregation
1728 { maField :: Text
1739 { maField :: Text
@@ -1923,6 +1934,13 @@ instance ToJSON Aggregation where
1923 toJSON (MissingAgg (MissingAggregation{..})) =
1934 toJSON (MissingAgg (MissingAggregation{..})) =
1924 object ["missing" .= object ["field" .= maField]]
1935 object ["missing" .= object ["field" .= maField]]
1937+ toJSON (TopHitsAgg (TopHitsAggregation mfrom msize msort)) =
1938+ omitNulls ["top_hits" .= omitNulls [ "size" .= msize
1926instance ToJSON DateRangeAggregation where
1944instance ToJSON DateRangeAggregation where
1927 toJSON DateRangeAggregation {..} =
1945 toJSON DateRangeAggregation {..} =
1928 omitNulls [ "field" .= draField
1946 omitNulls [ "field" .= draField
@@ -1967,6 +1985,9 @@ data BucketValue = TextValue Text
1968data MissingResult = MissingResult { missingDocCount :: Int } deriving (Show)
1986data MissingResult = MissingResult { missingDocCount :: Int } deriving (Show)
1988+data TopHitResult a = TopHitResult { tarHits :: (SearchHits a)
1970data TermsResult = TermsResult { termKey :: BucketValue
1991data TermsResult = TermsResult { termKey :: BucketValue
1971 , termsDocCount :: Int
1992 , termsDocCount :: Int
1972 , termsAggs :: Maybe AggregationResults } deriving (Read, Show)
1993 , termsAggs :: Maybe AggregationResults } deriving (Read, Show)
@@ -1993,6 +2014,9 @@ toDateHistogram = toAggResult
1993toMissing :: Text -> AggregationResults -> Maybe MissingResult
2014toMissing :: Text -> AggregationResults -> Maybe MissingResult
1994toMissing = toAggResult
2015toMissing = toAggResult
2017+toTopHits :: (FromJSON a) => Text -> AggregationResults -> Maybe (TopHitResult a)
2018+toTopHits = toAggResult
1996toAggResult :: (FromJSON a) => Text -> AggregationResults -> Maybe a
2020toAggResult :: (FromJSON a) => Text -> AggregationResults -> Maybe a
1997toAggResult t a = M.lookup t a >>= deserialize
2021toAggResult t a = M.lookup t a >>= deserialize
1998 where deserialize = parseMaybe parseJSON
2022 where deserialize = parseMaybe parseJSON
@@ -2028,10 +2052,10 @@ instance FromJSON MissingResult where
2028 where parse v = MissingResult <$> v .: "doc_count"
2052 where parse v = MissingResult <$> v .: "doc_count"
2030instance FromJSON TermsResult where
2054instance FromJSON TermsResult where
2031− parseJSON (Object v) = TermsResult <$>
2055+ parseJSON (Object v) = TermsResult <$>
2032 v .: "key" <*>
2056 v .: "key" <*>
2033 v .: "doc_count" <*>
2057 v .: "doc_count" <*>
2034− v .:? "aggregations"
2058+ (pure $ getNamedSubAgg v ["key", "doc_count"])
2035 parseJSON _ = mempty
2059 parseJSON _ = mempty
2037instance FromJSON DateHistogramResult where
2061instance FromJSON DateHistogramResult where
@@ -2039,7 +2063,11 @@ instance FromJSON DateHistogramResult where
2039 v .: "key" <*>
2063 v .: "key" <*>
2040 v .:? "key_as_string" <*>
2064 v .:? "key_as_string" <*>
2041 v .: "doc_count" <*>
2065 v .: "doc_count" <*>
2042− v .:? "aggregations"
2066+ (pure $ getNamedSubAgg v [ "key"
2043 parseJSON _ = mempty
2071 parseJSON _ = mempty
2045instance FromJSON DateRangeResult where
2073instance FromJSON DateRangeResult where
@@ -2051,13 +2079,25 @@ instance FromJSON DateRangeResult where
2051 (fmap posixMS <$> v .:? "to") <*>
2079 (fmap posixMS <$> v .:? "to") <*>
2052 v .:? "to_as_string" <*>
2080 v .:? "to_as_string" <*>
2053 v .: "doc_count" <*>
2081 v .: "doc_count" <*>
2054− v .:? "aggregations"
2082+ (pure $ getNamedSubAgg v [ "key"
2056instance FromJSON POSIXMS where
2091instance FromJSON POSIXMS where
2057 parseJSON = withScientific "POSIXMS" (return . parse)
2092 parseJSON = withScientific "POSIXMS" (return . parse)
2058 where parse n = let n' = truncate n :: Integer
2093 where parse n = let n' = truncate n :: Integer
2059 in POSIXMS (posixSecondsToUTCTime (fromInteger (n' `div` 1000)))
2094 in POSIXMS (posixSecondsToUTCTime (fromInteger (n' `div` 1000)))
2096+instance (FromJSON a) => FromJSON (TopHitResult a) where
2097+ parseJSON (Object v) = TopHitResult <$>
2099+ parseJSON _ = fail "Failure in FromJSON (TopHitResult a)"
2061instance Monoid Filter where
2101instance Monoid Filter where
2062 mempty = IdentityFilter
2102 mempty = IdentityFilter
2063 mappend a b = AndFilter [a, b] defaultCache
2103 mappend a b = AndFilter [a, b] defaultCache
@@ -2250,6 +2290,15 @@ fieldTagged f o = case HM.toList o of
2250 [(k, Object o')] -> f (FieldName k) o'
2290 [(k, Object o')] -> f (FieldName k) o'
2251 _ -> fail "Expected object with 1 field-named key"
2291 _ -> fail "Expected object with 1 field-named key"
2293+-- Try to get an AggregationResults when we don't know the
2294+-- field name. We filter out the known keys to try to minimize the noise.
2295+getNamedSubAgg :: Object -> [Text] -> Maybe AggregationResults
2296+getNamedSubAgg o knownKeys = maggRes
2297+ where unknownKeys = HM.filterWithKey (\k _ -> k `notElem` knownKeys) o
2299+ | HM.null unknownKeys = Nothing
2300+ | otherwise = Just . M.fromList $ HM.toList unknownKeys
2253instance ToJSON GeoPoint where
2302instance ToJSON GeoPoint where
2254 toJSON (GeoPoint (FieldName geoPointField) geoPointLatLon) =
2303 toJSON (GeoPoint (FieldName geoPointField) geoPointLatLon) =
2255 object [ geoPointField .= geoPointLatLon ]
2304 object [ geoPointField .= geoPointLatLon ]