git.iko.soy/typeable/bloodhound
on master help sign in
Merge pull request #56 from centromere/fields-support
6844fd4Chris Allen
README.md+15−2
@@ -383,7 +383,7 @@ Search
383
383
384-- exported by the Client module, just defaults some stuff.
384-- exported by the Client module, just defaults some stuff.
385-- mkSearch :: Maybe Query -> Maybe Filter -> Search
385-- mkSearch :: Maybe Query -> Maybe Filter -> Search
386−-- mkSearch query filter = Search query filter Nothing False (From 0) (Size 10)
386+-- mkSearch query filter = Search query filter Nothing False (From 0) (Size 10) Nothing
387
387
388let query = TermQuery (Term "user" "bitemyapp") Nothing
388let query = TermQuery (Term "user" "bitemyapp") Nothing
389
389
@@ -486,9 +486,22 @@ let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending
486-- -> Maybe Sort
486-- -> Maybe Sort
487-- -> TrackSortScores
487-- -> TrackSortScores
488-- -> From -> Size
488-- -> From -> Size
489+-- -> Maybe [FieldName]
489
490
490-- just add more sortspecs to the list if you want tie-breakers.
491-- just add more sortspecs to the list if you want tie-breakers.
491−let search = Search Nothing (Just IdentityFilter) (Just [sortSpec]) False (From 0) (Size 10)
492+let search = Search Nothing (Just IdentityFilter) (Just [sortSpec]) False (From 0) (Size 10) Nothing
493+
494+```
495+
496+### Field selection
497+
498+If you only want certain fields from the source document returned, you can
499+set the "fields" field of the Search record.
500+
501+``` {.haskell}
502+
503+let search' = mkSearch (Just (MatchAllQuery Nothing)) Nothing
504+ search = search' { fields = Just [FieldName "updated"] }
492
505
493```
506```
494
507
src/Database/Bloodhound/Client.hs+6−6
@@ -537,9 +537,9 @@ searchByType (IndexName indexName)
537--
537--
538-- >>> let query = TermQuery (Term "user" "bitemyapp") Nothing
538-- >>> let query = TermQuery (Term "user" "bitemyapp") Nothing
539-- >>> mkSearch (Just query) Nothing
539-- >>> mkSearch (Just query) Nothing
540−-- Search {queryBody = Just (TermQuery (Term {termField = "user", termValue = "bitemyapp"}) Nothing), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 0, size = Size 10}
540+-- Search {queryBody = Just (TermQuery (Term {termField = "user", termValue = "bitemyapp"}) Nothing), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 0, size = Size 10, fields = Nothing}
541mkSearch :: Maybe Query -> Maybe Filter -> Search
541mkSearch :: Maybe Query -> Maybe Filter -> Search
542−mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10)
542+mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) Nothing
543
543
544-- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for
544-- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for
545-- the 'Query' and the 'Aggregation'.
545-- the 'Query' and the 'Aggregation'.
@@ -549,7 +549,7 @@ mkSearch query filter = Search query filter Nothing Nothing Nothing False (From
549-- TermsAgg (TermsAggregation {term = Left "user", termInclude = Nothing, termExclude = Nothing, termOrder = Nothing, termMinDocCount = Nothing, termSize = Nothing, termShardSize = Nothing, termCollectMode = Just BreadthFirst, termExecutionHint = Nothing, termAggs = Nothing})
549-- TermsAgg (TermsAggregation {term = Left "user", termInclude = Nothing, termExclude = Nothing, termOrder = Nothing, termMinDocCount = Nothing, termSize = Nothing, termShardSize = Nothing, termCollectMode = Just BreadthFirst, termExecutionHint = Nothing, termAggs = Nothing})
550-- >>> let myAggregation = mkAggregateSearch Nothing $ mkAggregations "users" terms
550-- >>> let myAggregation = mkAggregateSearch Nothing $ mkAggregations "users" terms
551mkAggregateSearch :: Maybe Query -> Aggregations -> Search
551mkAggregateSearch :: Maybe Query -> Aggregations -> Search
552−mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0)
552+mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0) Nothing
553
553
554-- | 'mkHighlightSearch' is a helper function that defaults everything in a 'Search' except for
554-- | 'mkHighlightSearch' is a helper function that defaults everything in a 'Search' except for
555-- the 'Query' and the 'Aggregation'.
555-- the 'Query' and the 'Aggregation'.
@@ -558,7 +558,7 @@ mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSear
558-- >>> let testHighlight = Highlights Nothing [FieldHighlight (FieldName "message") Nothing]
558-- >>> let testHighlight = Highlights Nothing [FieldHighlight (FieldName "message") Nothing]
559-- >>> let search = mkHighlightSearch (Just query) testHighlight
559-- >>> let search = mkHighlightSearch (Just query) testHighlight
560mkHighlightSearch :: Maybe Query -> Highlights -> Search
560mkHighlightSearch :: Maybe Query -> Highlights -> Search
561−mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10)
561+mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10) Nothing
562
562
563-- | 'pageSearch' is a helper function that takes a search and assigns the from
563-- | 'pageSearch' is a helper function that takes a search and assigns the from
564-- and size fields for the search. The from parameter defines the offset
564-- and size fields for the search. The from parameter defines the offset
@@ -568,9 +568,9 @@ mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing
568-- >>> let query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString "haskell")
568-- >>> let query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString "haskell")
569-- >>> let search = mkSearch (Just query) Nothing
569-- >>> let search = mkSearch (Just query) Nothing
570-- >>> search
570-- >>> search
571−-- Search {queryBody = Just (QueryMatchQuery (MatchQuery {matchQueryField = FieldName "_all", matchQueryQueryString = QueryString "haskell", matchQueryOperator = Or, matchQueryZeroTerms = ZeroTermsNone, matchQueryCutoffFrequency = Nothing, matchQueryMatchType = Nothing, matchQueryAnalyzer = Nothing, matchQueryMaxExpansions = Nothing, matchQueryLenient = Nothing})), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 0, size = Size 10}
571+-- Search {queryBody = Just (QueryMatchQuery (MatchQuery {matchQueryField = FieldName "_all", matchQueryQueryString = QueryString "haskell", matchQueryOperator = Or, matchQueryZeroTerms = ZeroTermsNone, matchQueryCutoffFrequency = Nothing, matchQueryMatchType = Nothing, matchQueryAnalyzer = Nothing, matchQueryMaxExpansions = Nothing, matchQueryLenient = Nothing})), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 0, size = Size 10, fields = Nothing}
572-- >>> pageSearch (From 10) (Size 100) search
572-- >>> pageSearch (From 10) (Size 100) search
573−-- Search {queryBody = Just (QueryMatchQuery (MatchQuery {matchQueryField = FieldName "_all", matchQueryQueryString = QueryString "haskell", matchQueryOperator = Or, matchQueryZeroTerms = ZeroTermsNone, matchQueryCutoffFrequency = Nothing, matchQueryMatchType = Nothing, matchQueryAnalyzer = Nothing, matchQueryMaxExpansions = Nothing, matchQueryLenient = Nothing})), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 10, size = Size 100}
573+-- Search {queryBody = Just (QueryMatchQuery (MatchQuery {matchQueryField = FieldName "_all", matchQueryQueryString = QueryString "haskell", matchQueryOperator = Or, matchQueryZeroTerms = ZeroTermsNone, matchQueryCutoffFrequency = Nothing, matchQueryMatchType = Nothing, matchQueryAnalyzer = Nothing, matchQueryMaxExpansions = Nothing, matchQueryLenient = Nothing})), filterBody = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 10, size = Size 100, fields = Nothing}
574pageSearch :: From -- ^ The result offset
574pageSearch :: From -- ^ The result offset
575 -> Size -- ^ The number of results to return
575 -> Size -- ^ The number of results to return
576 -> Search -- ^ The current seach
576 -> Search -- ^ The current seach
src/Database/Bloodhound/Types.hs+5−3
@@ -714,7 +714,8 @@ data Search = Search { queryBody :: Maybe Query
714 -- default False
714 -- default False
715 , trackSortScores :: TrackSortScores
715 , trackSortScores :: TrackSortScores
716 , from :: From
716 , from :: From
717− , size :: Size } deriving (Eq, Show)
717+ , size :: Size
718+ , fields :: Maybe [FieldName] } deriving (Eq, Show)
718
719
719data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
720data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
720 , highlightFields :: [FieldHighlight]
721 , highlightFields :: [FieldHighlight]
@@ -2049,7 +2050,7 @@ instance (FromJSON a) => FromJSON (EsResult a) where
2049
2050
2050
2051
2051instance ToJSON Search where
2052instance ToJSON Search where
2052− toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize) =
2053+ toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize sFields) =
2053 omitNulls [ "query" .= query
2054 omitNulls [ "query" .= query
2054 , "filter" .= sFilter
2055 , "filter" .= sFilter
2055 , "sort" .= sort
2056 , "sort" .= sort
@@ -2057,7 +2058,8 @@ instance ToJSON Search where
2057 , "highlight" .= highlight
2058 , "highlight" .= highlight
2058 , "from" .= sFrom
2059 , "from" .= sFrom
2059 , "size" .= sSize
2060 , "size" .= sSize
2060− , "track_scores" .= sTrackSortScores]
2061+ , "track_scores" .= sTrackSortScores
2062+ , "fields" .= sFields]
2061
2063
2062
2064
2063instance ToJSON FieldHighlight where
2065instance ToJSON FieldHighlight where
tests/tests.hs+1−1
@@ -425,7 +425,7 @@ main = hspec $ do
425 let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending
425 let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending
426 let search = Search Nothing
426 let search = Search Nothing
427 (Just IdentityFilter) (Just [sortSpec]) Nothing Nothing
427 (Just IdentityFilter) (Just [sortSpec]) Nothing Nothing
428− False (From 0) (Size 10)
428+ False (From 0) (Size 10) Nothing
429 reply <- searchByIndex testIndex search
429 reply <- searchByIndex testIndex search
430 let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet)
430 let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet)
431 let myTweet = fmap (hitSource . head . hits . searchHits) result
431 let myTweet = fmap (hitSource . head . hits . searchHits) result