git.iko.soy/typeable/bloodhound
on master help sign in
more query support
commit11b3b3e6296bfcc5083a6bf64024fcbec3e4eee2 authorChris Allen <cma@bitemyapp.com> dateMon 28 Apr 2014, 00:15 (12 years ago) parentff2e480
2 files changed+172−13
Database/Bloodhound/Types.hs+2−11 Database/Bloodhound/Types/Instances.hs+170−2
Database/Bloodhound/Types.hs+2−11
@@ -283,7 +283,7 @@ data Query = TermQuery Term (Maybe Boost)
283283 | QueryHasChildQuery HasChildQuery
284284 | QueryHasParentQuery HasParentQuery
285285 | IdsQuery MappingName [DocId]
286− | QueryIndicesQuery AnIndicesQuery
286+ | QueryIndicesQuery IndicesQuery
287287 | MatchAllQuery (Maybe Boost)
288288 | QueryMoreLikeThisQuery MoreLikeThisQuery
289289 | QueryMoreLikeThisFieldQuery MoreLikeThisFieldQuery
@@ -399,22 +399,13 @@ data MoreLikeThisQuery =
399399 , moreLikeThisAnalyzer :: Maybe Analyzer
400400 } deriving (Eq, Show)
401401
402−data AnIndicesQuery = AnIndexQuery IndexQuery
403− | AnIndicesQuery IndicesQuery deriving (Eq, Show)
404−
405402
406403data IndicesQuery =
407− IndicesQuery { indicesQueryIndexName :: IndexName
404+ IndicesQuery { indicesQueryIndices :: [IndexName]
408405 , indicesQuery :: Query
409406 -- default "all"
410407 , indicesQueryNoMatch :: Maybe Query } deriving (Eq, Show)
411408
412−data IndexQuery =
413− IndexQuery { indexQueryIndexName :: IndexName
414− , indexQuery :: Query
415− -- default "all"
416− , indexQueryNoMatch :: Maybe Query } deriving (Eq, Show)
417−
418409data HasParentQuery =
419410 HasParentQuery { hasParentQueryType :: TypeName
420411 , hasParentQuery :: Query
Database/Bloodhound/Types/Instances.hs+170−2
@@ -143,15 +143,183 @@ instance ToJSON Query where
143143 object [ "bool" .= toJSON boolQuery ]
144144
145145 toJSON (QueryBoostingQuery boostingQuery) =
146− object ["boosting" .= toJSON boostingQuery]
146+ object [ "boosting" .= toJSON boostingQuery ]
147147
148148 toJSON (QueryCommonTermsQuery commonTermsQuery) =
149− object ["common" .= toJSON commonTermsQuery]
149+ object [ "common" .= toJSON commonTermsQuery ]
150+
151+ toJSON (ConstantScoreFilter filter boost) =
152+ object [ "constant_score" .= toJSON filter
153+ , "boost" .= toJSON boost]
154+
155+ toJSON (ConstantScoreQuery query boost) =
156+ object [ "constant_score" .= toJSON filter
157+ , "boost" .= toJSON boost]
158+
159+ toJSON (QueryDisMaxQuery disMaxQuery) =
160+ object [ "dis_max" .= toJSON disMaxQuery ]
161+
162+ toJSON (QueryFilteredQuery filteredQuery) =
163+ object [ "filtered" .= toJSON filteredQuery ]
164+
165+ toJSON (QueryFuzzyLikeThisQuery fuzzyQuery) =
166+ object [ "fuzzy_like_this" .= toJSON fuzzyQuery ]
167+
168+ toJSON (QueryFuzzyLikeFieldQuery fuzzyFieldQuery) =
169+ object [ "fuzzy_like_this_field" .= toJSON fuzzyFieldQuery ]
170+
171+ toJSON (QueryFuzzyQuery fuzzyQuery) =
172+ object [ "fuzzy" .= toJSON fuzzyQuery ]
173+
174+ toJSON (QueryHasChildQuery childQuery) =
175+ object [ "has_child" .= toJSON childQuery ]
176+
177+ toJSON (QueryHasParentQuery parentQuery) =
178+ object [ "has_parent" .= toJSON parentQuery ]
179+
180+ toJSON (QueryIndicesQuery indicesQuery) =
181+ object [ "indices" .= toJSON indicesQuery ]
182+
183+ toJSON (MatchAllQuery boost) =
184+ object [ "match_all" .= object maybeAdd ]
185+ where maybeAdd catMaybes [ mField "boost" boost ]
186+
187+ toJSON (QueryMoreLikeThisQuery query) =
188+ object [ "more_like_this" .= toJSON query ]
189+
190+ toJSON (QueryMoreLikeThisFieldQuery query) =
191+ object [ "more_like_this_field" .= toJSON query ]
192+
193+ toJSON (QueryNestedQuery query) =
194+ object [ "nested" .= toJSON query ]
195+
150196
151197mField :: (ToJSON a, Functor f) => T.Text -> f a -> f (T.Text, Value)
152198mField field = fmap ((field .=) . toJSON)
153199
154200
201+instance NestedQuery where
202+ toJSON (NestedQuery path scoreType query) =
203+ object [ "path" .= toJSON path
204+ , "score_mode" .= toJSON scoreType
205+ , "query" .= toJSON query ]
206+
207+
208+instance MoreLikeThisFieldQuery where
209+ toJSON (MoreLikeThisFieldQuery text (FieldName fieldName)
210+ percent mtf mqt stopwords mindf maxdf
211+ minwl maxwl boostTerms boost analyzer) =
212+ object [ fieldName .= object conjoined ]
213+ where base = [ "like_text" .= toJSON text ]
214+ maybeAdd = catMaybes [ mField "fields" fields
215+ , mField "percent_terms_to_match" percent
216+ , mField "min_term_freq" mtf
217+ , mField "max_query_terms" mqt
218+ , mField "stop_words" stopwords
219+ , mField "min_doc_freq" mindf
220+ , mField "max_doc_freq" maxdf
221+ , mField "min_word_length" minwl
222+ , mField "max_word_length" maxwl
223+ , mField "boost_terms" boostTerms
224+ , mField "boost" boost
225+ , mField "analyzer" analyzer ]
226+ conjoined = base ++ maybeAdd
227+
228+
229+instance ToJSON MoreLikeThisQuery where
230+ toJSON (MoreLikeThisQuery text fields percent
231+ mtf mqt stopwords mindf maxdf
232+ minwl maxwl boostTerms boost analyzer) =
233+ object conjoined
234+ where base = [ "like_text" .= toJSON text ]
235+ maybeAdd = catMaybes [ mField "fields" fields
236+ , mField "percent_terms_to_match" percent
237+ , mField "min_term_freq" mtf
238+ , mField "max_query_terms" mqt
239+ , mField "stop_words" stopwords
240+ , mField "min_doc_freq" mindf
241+ , mField "max_doc_freq" maxdf
242+ , mField "min_word_length" minwl
243+ , mField "max_word_length" maxwl
244+ , mField "boost_terms" boostTerms
245+ , mField "boost" boost
246+ , mField "analyzer" analyzer ]
247+ conjoined = base ++ maybeAdd
248+
249+
250+instance ToJSON IndicesQuery where
251+ toJSON (IndicesQuery indices query noMatch) =
252+ object [ "indices" .= toJSON indices
253+ , "query" .= toJSON query ] ++ maybeAdd
254+ where maybeAdd = catMaybes [ mField "no_match_query" noMatch ]
255+
256+
257+instance ToJSON HasParentQuery where
258+ toJSON (HasParentQuery queryType query scoreType) =
259+ object [ "parent_type" .= toJSON queryType
260+ , "query" .= toJSON query ] ++ maybeAdd
261+ where maybeAdd = catMaybes [ mField "score_type" scoreType ]
262+
263+
264+instance ToJSON HasChildQuery where
265+ toJSON (HasChildQuery queryType query scoreType) =
266+ object [ "query" .= toJSON query
267+ , "type" .= toJSON queryType ] ++ maybeAdd
268+ where maybeAdd = catMaybes [ mField "score_type" scoreType ]
269+
270+
271+instance ToJSON FuzzyQuery where
272+ toJSON (FuzzyQuery (FieldName fieldName) queryText
273+ prefixLength maxEx fuzziness boost) =
274+ object [ fieldName .= object conjoined ]
275+ where base = [ "value" .= toJSON queryText
276+ , "fuzziness" .= toJSON fuzziness
277+ , "prefix_length" .= toJSON prefixLength
278+ , "max_expansions" .= toJSON maxEx ]
279+ maybeAdd = catMaybes [ mField "boost" boost ]
280+ conjoined = base ++ maybeAdd
281+
282+
283+instance ToJSON FuzzyLikeFieldQuery where
284+ toJSON (FuzzyLikeFieldQuery (FieldName fieldName)
285+ fieldText maxTerms ignoreFreq fuzziness prefixLength
286+ boost analyzer) =
287+ object [ fieldName .=
288+ object [ "like_text" .= toJSON fieldText
289+ , "max_query_terms" .= toJSON maxTerms
290+ , "ignore_tf" .= toJSON ignoreFreq
291+ , "fuzziness" .= toJSON fuzziness
292+ , "prefix_length" .= toJSON prefixLength
293+ , "boost" .= toJSON boost ]] ++ maybeAdd
294+ where maybeAdd = catMaybes [ mField "analyzer" analyzer ]
295+
296+
297+instance ToJSON FuzzyLikeThisQuery where
298+ toJSON (FuzzyLikeThisQuery fields text maxTerms
299+ ignoreFreq fuzziness prefixLength boost analyzer) =
300+ object [ "fields" .= toJSON fields
301+ , "like_text" .= toJSON text
302+ , "max_query_terms" .= toJSON maxTerms
303+ , "ignore_tf" .= toJSON ignoreFreq
304+ , "fuzziness" .= toJSON fuzziness
305+ , "prefix_length" .= toJSON prefixLength
306+ , "boost" .= toJSON boost ] ++ maybeAdd
307+ where maybeAdd = catMaybes [ mField "analyzer" analyzer ]
308+
309+
310+instance ToJSON FilteredQuery where
311+ toJSON (FilteredQuery query filter) =
312+ object [ "query" .= toJSON query
313+ , "filter" .= toJSON filter ]
314+
315+
316+instance ToJSON DisMaxQuery where
317+ toJSON (DisMaxQuery queries tiebreaker boost) =
318+ object [ "queries" .= toJSON queries
319+ , "tie_breaker" .= toJSON tiebreaker ] ++ maybeAdd
320+ where maybeAdd = catMaybes [mField "boost" boost]
321+
322+
155323instance ToJSON CommonTermsQuery where
156324 toJSON (CommonTermsQuery (FieldName fieldName)
157325 (QueryString query) cf lfo hfo msm