@@ -141,18 +141,26 @@ module Database.Bloodhound.Types
141 , EnablePositionIncrements(..)
141 , EnablePositionIncrements(..)
142 , SimpleQueryFlag(..)
142 , SimpleQueryFlag(..)
143 , FieldOrFields(..)
143 , FieldOrFields(..)
148+import Control.Applicative
146import Data.Aeson
149import Data.Aeson
147import qualified Data.ByteString.Lazy.Char8 as L
150import qualified Data.ByteString.Lazy.Char8 as L
148−import Data.List.NonEmpty (NonEmpty)
151+import Data.List (nub)
152+import Data.List.NonEmpty (NonEmpty(..))
153+import Data.Maybe (catMaybes)
149import Data.Text (Text)
155import Data.Text (Text)
150import qualified Data.Text as T
156import qualified Data.Text as T
151import Data.Time.Clock (UTCTime)
157import Data.Time.Clock (UTCTime)
152−import Database.Bloodhound.Types.Class
153import GHC.Generics (Generic)
158import GHC.Generics (Generic)
154import Network.HTTP.Client
159import Network.HTTP.Client
155import qualified Network.HTTP.Types.Method as NHTM
160import qualified Network.HTTP.Types.Method as NHTM
161+import Prelude hiding (filter)
163+import Database.Bloodhound.Types.Class
158{-| 'Version' is embedded in 'Status' -}
166{-| 'Version' is embedded in 'Status' -}
@@ -942,3 +950,781 @@ maybeJsonF _ _ = []
943showText :: Show a => a -> Text
951showText :: Show a => a -> Text
944showText = T.pack . show
952showText = T.pack . show
955+instance Monoid Filter where
956+ mempty = IdentityFilter
957+ mappend a b = AndFilter [a, b] defaultCache
959+instance Seminearring Filter where
960+ a <||> b = OrFilter [a, b] defaultCache
962+instance ToJSON Filter where
963+ toJSON (AndFilter filters cache) =
964+ object ["and" .= fmap toJSON filters
965+ , "_cache" .= cache]
967+ toJSON (OrFilter filters cache) =
968+ object ["or" .= fmap toJSON filters
969+ , "_cache" .= cache]
971+ toJSON (NotFilter filter cache) =
973+ object ["filter" .= toJSON filter
974+ , "_cache" .= cache]]
976+ toJSON (IdentityFilter) =
977+ object ["match_all" .= object []]
979+ toJSON (TermFilter (Term termFilterField termFilterValue) cache) =
980+ object ["term" .= object base]
981+ where base = [termFilterField .= termFilterValue,
984+ toJSON (ExistsFilter (FieldName fieldName)) =
985+ object ["exists" .= object
986+ ["field" .= fieldName]]
988+ toJSON (BoolFilter boolMatch) =
989+ object ["bool" .= toJSON boolMatch]
991+ toJSON (GeoBoundingBoxFilter bbConstraint filterType) =
992+ object ["geo_bounding_box" .= toJSON bbConstraint
993+ , "type" .= toJSON filterType]
995+ toJSON (GeoDistanceFilter (GeoPoint (FieldName distanceGeoField) geoDistLatLon)
996+ distance distanceType optimizeBbox cache) =
997+ object ["geo_distance" .=
998+ object ["distance" .= toJSON distance
999+ , "distance_type" .= toJSON distanceType
1000+ , "optimize_bbox" .= optimizeBbox
1001+ , distanceGeoField .= toJSON geoDistLatLon
1002+ , "_cache" .= cache]]
1004+ toJSON (GeoDistanceRangeFilter (GeoPoint (FieldName gddrField) drLatLon)
1005+ (DistanceRange geoDistRangeDistFrom drDistanceTo)) =
1006+ object ["geo_distance_range" .=
1007+ object ["from" .= toJSON geoDistRangeDistFrom
1008+ , "to" .= toJSON drDistanceTo
1009+ , gddrField .= toJSON drLatLon]]
1011+ toJSON (GeoPolygonFilter (FieldName geoPolygonFilterField) latLons) =
1012+ object ["geo_polygon" .=
1013+ object [geoPolygonFilterField .=
1014+ object ["points" .= fmap toJSON latLons]]]
1016+ toJSON (IdsFilter (MappingName mappingName) values) =
1018+ object ["type" .= mappingName
1019+ , "values" .= fmap (T.pack . unpackId) values]]
1021+ toJSON (LimitFilter limit) =
1022+ object ["limit" .= object ["value" .= limit]]
1024+ toJSON (MissingFilter (FieldName fieldName) (Existence existence) (NullValue nullValue)) =
1025+ object ["missing" .=
1026+ object ["field" .= fieldName
1027+ , "existence" .= existence
1028+ , "null_value" .= nullValue]]
1030+ toJSON (PrefixFilter (FieldName fieldName) fieldValue cache) =
1031+ object ["prefix" .=
1032+ object [fieldName .= fieldValue
1033+ , "_cache" .= cache]]
1035+ toJSON (RangeFilter (FieldName fieldName) (Left halfRange) rangeExecution cache) =
1037+ object [fieldName .=
1038+ object [key .= val]
1039+ , "execution" .= toJSON rangeExecution
1040+ , "_cache" .= cache]]
1042+ (key, val) = halfRangeToKV halfRange
1044+ toJSON (RangeFilter (FieldName fieldName) (Right range) rangeExecution cache) =
1046+ object [fieldName .=
1047+ object [lessKey .= lessVal
1048+ , greaterKey .= greaterVal]
1049+ , "execution" .= toJSON rangeExecution
1050+ , "_cache" .= cache]]
1052+ (lessKey, lessVal, greaterKey, greaterVal) = rangeToKV range
1054+ toJSON (RegexpFilter (FieldName fieldName)
1055+ (Regexp regexText) flags (CacheName cacheName) cache (CacheKey cacheKey)) =
1056+ object ["regexp" .=
1057+ object [fieldName .=
1058+ object ["value" .= regexText
1059+ , "flags" .= toJSON flags]
1060+ , "_name" .= cacheName
1061+ , "_cache" .= cache
1062+ , "_cache_key" .= cacheKey]]
1064+instance ToJSON GeoPoint where
1065+ toJSON (GeoPoint (FieldName geoPointField) geoPointLatLon) =
1066+ object [ geoPointField .= toJSON geoPointLatLon ]
1069+instance ToJSON Query where
1070+ toJSON (TermQuery (Term termQueryField termQueryValue) boost) =
1072+ object [termQueryField .= object merged]]
1074+ base = [ "value" .= termQueryValue ]
1075+ boosted = maybe [] (return . ("boost" .=)) boost
1076+ merged = mappend base boosted
1078+ toJSON (TermsQuery terms termsQueryMinimumMatch) =
1079+ object [ "terms" .= object conjoined ]
1081+ [ "tags" .= fmap toJSON terms
1082+ , "minimum_should_match" .= toJSON termsQueryMinimumMatch ]
1084+ toJSON (IdsQuery idsQueryMappingName docIds) =
1085+ object [ "ids" .= object conjoined ]
1086+ where conjoined = [ "type" .= toJSON idsQueryMappingName
1087+ , "values" .= fmap toJSON docIds ]
1089+ toJSON (QueryQueryStringQuery qQueryStringQuery) =
1090+ object [ "query_string" .= toJSON qQueryStringQuery ]
1092+ toJSON (QueryMatchQuery matchQuery) =
1093+ object [ "match" .= toJSON matchQuery ]
1095+ toJSON (QueryMultiMatchQuery multiMatchQuery) =
1096+ object [ "multi_match" .= toJSON multiMatchQuery ]
1098+ toJSON (QueryBoolQuery boolQuery) =
1099+ object [ "bool" .= toJSON boolQuery ]
1101+ toJSON (QueryBoostingQuery boostingQuery) =
1102+ object [ "boosting" .= toJSON boostingQuery ]
1104+ toJSON (QueryCommonTermsQuery commonTermsQuery) =
1105+ object [ "common" .= toJSON commonTermsQuery ]
1107+ toJSON (ConstantScoreFilter filter boost) =
1108+ object [ "constant_score" .= toJSON filter
1109+ , "boost" .= toJSON boost]
1111+ toJSON (ConstantScoreQuery query boost) =
1112+ object [ "constant_score" .= toJSON query
1113+ , "boost" .= toJSON boost]
1115+ toJSON (QueryDisMaxQuery disMaxQuery) =
1116+ object [ "dis_max" .= toJSON disMaxQuery ]
1118+ toJSON (QueryFilteredQuery qFilteredQuery) =
1119+ object [ "filtered" .= toJSON qFilteredQuery ]
1121+ toJSON (QueryFuzzyLikeThisQuery fuzzyQuery) =
1122+ object [ "fuzzy_like_this" .= toJSON fuzzyQuery ]
1124+ toJSON (QueryFuzzyLikeFieldQuery fuzzyFieldQuery) =
1125+ object [ "fuzzy_like_this_field" .= toJSON fuzzyFieldQuery ]
1127+ toJSON (QueryFuzzyQuery fuzzyQuery) =
1128+ object [ "fuzzy" .= toJSON fuzzyQuery ]
1130+ toJSON (QueryHasChildQuery childQuery) =
1131+ object [ "has_child" .= toJSON childQuery ]
1133+ toJSON (QueryHasParentQuery parentQuery) =
1134+ object [ "has_parent" .= toJSON parentQuery ]
1136+ toJSON (QueryIndicesQuery qIndicesQuery) =
1137+ object [ "indices" .= toJSON qIndicesQuery ]
1139+ toJSON (MatchAllQuery boost) =
1140+ object [ "match_all" .= object maybeAdd ]
1141+ where maybeAdd = catMaybes [ mField "boost" boost ]
1143+ toJSON (QueryMoreLikeThisQuery query) =
1144+ object [ "more_like_this" .= toJSON query ]
1146+ toJSON (QueryMoreLikeThisFieldQuery query) =
1147+ object [ "more_like_this_field" .= toJSON query ]
1149+ toJSON (QueryNestedQuery query) =
1150+ object [ "nested" .= toJSON query ]
1152+ toJSON (QueryPrefixQuery query) =
1153+ object [ "prefix" .= toJSON query ]
1155+ toJSON (QueryRangeQuery query) =
1156+ object [ "range" .= toJSON query ]
1158+ toJSON (QueryRegexpQuery query) =
1159+ object [ "regexp" .= toJSON query ]
1161+ toJSON (QuerySimpleQueryStringQuery query) =
1162+ object [ "simple_query_string" .= toJSON query ]
1165+mField :: (ToJSON a, Functor f) => T.Text -> f a -> f (T.Text, Value)
1166+mField field = fmap ((field .=) . toJSON)
1168+instance ToJSON SimpleQueryStringQuery where
1169+ toJSON (SimpleQueryStringQuery sqsQueryString
1170+ sqsFields sqsBoolean sqsAnalyzer
1171+ sqsFlags sqsLowercaseExpanded sqsLocale) =
1173+ where base = [ "query" .= toJSON sqsQueryString ]
1175+ catMaybes [ mField "fields" sqsFields
1176+ , mField "default_operator" sqsBoolean
1177+ , mField "analyzer" sqsAnalyzer
1178+ , mField "flags" sqsFlags
1179+ , mField "lowercase_expanded_terms" sqsLowercaseExpanded
1180+ , mField "locale" sqsLocale ]
1181+ conjoined = base ++ maybeAdd
1183+instance ToJSON FieldOrFields where
1184+ toJSON (FofField fieldName) =
1186+ toJSON (FofFields fieldNames) =
1189+instance ToJSON SimpleQueryFlag where
1190+ toJSON SimpleQueryAll = "ALL"
1191+ toJSON SimpleQueryNone = "NONE"
1192+ toJSON SimpleQueryAnd = "AND"
1193+ toJSON SimpleQueryOr = "OR"
1194+ toJSON SimpleQueryPrefix = "PREFIX"
1195+ toJSON SimpleQueryPhrase = "PHRASE"
1196+ toJSON SimpleQueryPrecedence = "PRECEDENCE"
1197+ toJSON SimpleQueryEscape = "ESCAPE"
1198+ toJSON SimpleQueryWhitespace = "WHITESPACE"
1199+ toJSON SimpleQueryFuzzy = "FUZZY"
1200+ toJSON SimpleQueryNear = "NEAR"
1201+ toJSON SimpleQuerySlop = "SLOP"
1203+instance ToJSON RegexpQuery where
1204+ toJSON (RegexpQuery (FieldName rqQueryField)
1205+ (Regexp regexpQueryQuery) rqQueryFlags
1207+ object [ rqQueryField .= object conjoined ]
1208+ where base = [ "value" .= regexpQueryQuery
1209+ , "flags" .= toJSON rqQueryFlags ]
1210+ maybeAdd = catMaybes [ mField "boost" rqQueryBoost ]
1211+ conjoined = base ++ maybeAdd
1213+instance ToJSON QueryStringQuery where
1214+ toJSON (QueryStringQuery qsQueryString
1215+ qsDefaultField qsOperator
1216+ qsAnalyzer qsAllowWildcard
1217+ qsLowercaseExpanded qsEnablePositionIncrements
1218+ qsFuzzyMaxExpansions qsFuzziness
1219+ qsFuzzyPrefixLength qsPhraseSlop
1220+ qsBoost qsAnalyzeWildcard
1221+ qsGeneratePhraseQueries qsMinimumShouldMatch
1222+ qsLenient qsLocale) =
1225+ base = [ "query" .= toJSON qsQueryString ]
1227+ catMaybes [ mField "default_field" qsDefaultField
1228+ , mField "default_operator" qsOperator
1229+ , mField "analyzer" qsAnalyzer
1230+ , mField "allow_leading_wildcard" qsAllowWildcard
1231+ , mField "lowercase_expanded_terms" qsLowercaseExpanded
1232+ , mField "enable_position_increments" qsEnablePositionIncrements
1233+ , mField "fuzzy_max_expansions" qsFuzzyMaxExpansions
1234+ , mField "fuzziness" qsFuzziness
1235+ , mField "fuzzy_prefix_length" qsFuzzyPrefixLength
1236+ , mField "phrase_slop" qsPhraseSlop
1237+ , mField "boost" qsBoost
1238+ , mField "analyze_wildcard" qsAnalyzeWildcard
1239+ , mField "auto_generate_phrase_queries" qsGeneratePhraseQueries
1240+ , mField "minimum_should_match" qsMinimumShouldMatch
1241+ , mField "lenient" qsLenient
1242+ , mField "locale" qsLocale ]
1243+ conjoined = base ++ maybeAdd
1245+instance ToJSON RangeQuery where
1246+ toJSON (RangeQuery (FieldName fieldName) (Right range) boost) =
1247+ object [ fieldName .= conjoined ]
1248+ where conjoined = [ "boost" .= toJSON boost
1249+ , lessKey .= lessVal
1250+ , greaterKey .= greaterVal ]
1251+ (lessKey, lessVal, greaterKey, greaterVal) = rangeToKV range
1253+ toJSON (RangeQuery (FieldName fieldName) (Left halfRange) boost) =
1254+ object [ fieldName .= conjoined ]
1255+ where conjoined = [ "boost" .= toJSON boost
1257+ (key, val) = halfRangeToKV halfRange
1259+instance ToJSON PrefixQuery where
1260+ toJSON (PrefixQuery (FieldName fieldName) queryValue boost) =
1261+ object [ fieldName .= object conjoined ]
1262+ where base = [ "value" .= toJSON queryValue ]
1263+ maybeAdd = catMaybes [ mField "boost" boost ]
1264+ conjoined = base ++ maybeAdd
1267+instance ToJSON NestedQuery where
1268+ toJSON (NestedQuery nqPath nqScoreType nqQuery) =
1269+ object [ "path" .= toJSON nqPath
1270+ , "score_mode" .= toJSON nqScoreType
1271+ , "query" .= toJSON nqQuery ]
1274+instance ToJSON MoreLikeThisFieldQuery where
1275+ toJSON (MoreLikeThisFieldQuery text (FieldName fieldName)
1276+ percent mtf mqt stopwords mindf maxdf
1277+ minwl maxwl boostTerms boost analyzer) =
1278+ object [ fieldName .= object conjoined ]
1279+ where base = [ "like_text" .= toJSON text ]
1280+ maybeAdd = catMaybes [ mField "percent_terms_to_match" percent
1281+ , mField "min_term_freq" mtf
1282+ , mField "max_query_terms" mqt
1283+ , mField "stop_words" stopwords
1284+ , mField "min_doc_freq" mindf
1285+ , mField "max_doc_freq" maxdf
1286+ , mField "min_word_length" minwl
1287+ , mField "max_word_length" maxwl
1288+ , mField "boost_terms" boostTerms
1289+ , mField "boost" boost
1290+ , mField "analyzer" analyzer ]
1291+ conjoined = base ++ maybeAdd
1294+instance ToJSON MoreLikeThisQuery where
1295+ toJSON (MoreLikeThisQuery text fields percent
1296+ mtf mqt stopwords mindf maxdf
1297+ minwl maxwl boostTerms boost analyzer) =
1299+ where base = [ "like_text" .= toJSON text ]
1300+ maybeAdd = catMaybes [ mField "fields" fields
1301+ , mField "percent_terms_to_match" percent
1302+ , mField "min_term_freq" mtf
1303+ , mField "max_query_terms" mqt
1304+ , mField "stop_words" stopwords
1305+ , mField "min_doc_freq" mindf
1306+ , mField "max_doc_freq" maxdf
1307+ , mField "min_word_length" minwl
1308+ , mField "max_word_length" maxwl
1309+ , mField "boost_terms" boostTerms
1310+ , mField "boost" boost
1311+ , mField "analyzer" analyzer ]
1312+ conjoined = base ++ maybeAdd
1315+instance ToJSON IndicesQuery where
1316+ toJSON (IndicesQuery indices query noMatch) =
1317+ object $ [ "indices" .= toJSON indices
1318+ , "query" .= toJSON query ] ++ maybeAdd
1319+ where maybeAdd = catMaybes [ mField "no_match_query" noMatch ]
1322+instance ToJSON HasParentQuery where
1323+ toJSON (HasParentQuery queryType query scoreType) =
1324+ object $ [ "parent_type" .= toJSON queryType
1325+ , "query" .= toJSON query ] ++ maybeAdd
1326+ where maybeAdd = catMaybes [ mField "score_type" scoreType ]
1329+instance ToJSON HasChildQuery where
1330+ toJSON (HasChildQuery queryType query scoreType) =
1331+ object $ [ "query" .= toJSON query
1332+ , "type" .= toJSON queryType ] ++ maybeAdd
1333+ where maybeAdd = catMaybes [ mField "score_type" scoreType ]
1336+instance ToJSON FuzzyQuery where
1337+ toJSON (FuzzyQuery (FieldName fieldName) queryText
1338+ prefixLength maxEx fuzziness boost) =
1339+ object [ fieldName .= object conjoined ]
1340+ where base = [ "value" .= toJSON queryText
1341+ , "fuzziness" .= toJSON fuzziness
1342+ , "prefix_length" .= toJSON prefixLength
1343+ , "max_expansions" .= toJSON maxEx ]
1344+ maybeAdd = catMaybes [ mField "boost" boost ]
1345+ conjoined = base ++ maybeAdd
1348+instance ToJSON FuzzyLikeFieldQuery where
1349+ toJSON (FuzzyLikeFieldQuery (FieldName fieldName)
1350+ fieldText maxTerms ignoreFreq fuzziness prefixLength
1352+ object $ [ fieldName .=
1353+ object [ "like_text" .= toJSON fieldText
1354+ , "max_query_terms" .= toJSON maxTerms
1355+ , "ignore_tf" .= toJSON ignoreFreq
1356+ , "fuzziness" .= toJSON fuzziness
1357+ , "prefix_length" .= toJSON prefixLength
1358+ , "boost" .= toJSON boost ]] ++ maybeAdd
1359+ where maybeAdd = catMaybes [ mField "analyzer" analyzer ]
1362+instance ToJSON FuzzyLikeThisQuery where
1363+ toJSON (FuzzyLikeThisQuery fields text maxTerms
1364+ ignoreFreq fuzziness prefixLength boost analyzer) =
1366+ where base = [ "fields" .= toJSON fields
1367+ , "like_text" .= toJSON text
1368+ , "max_query_terms" .= toJSON maxTerms
1369+ , "ignore_tf" .= toJSON ignoreFreq
1370+ , "fuzziness" .= toJSON fuzziness
1371+ , "prefix_length" .= toJSON prefixLength
1372+ , "boost" .= toJSON boost ]
1373+ maybeAdd = catMaybes [ mField "analyzer" analyzer ]
1374+ conjoined = base ++ maybeAdd
1377+instance ToJSON FilteredQuery where
1378+ toJSON (FilteredQuery query filter) =
1379+ object [ "query" .= toJSON query
1380+ , "filter" .= toJSON filter ]
1383+instance ToJSON DisMaxQuery where
1384+ toJSON (DisMaxQuery queries tiebreaker boost) =
1386+ where maybeAdd = catMaybes [mField "boost" boost]
1387+ base = [ "queries" .= toJSON queries
1388+ , "tie_breaker" .= toJSON tiebreaker ]
1389+ conjoined = base ++ maybeAdd
1392+instance ToJSON CommonTermsQuery where
1393+ toJSON (CommonTermsQuery (FieldName fieldName)
1394+ (QueryString query) cf lfo hfo msm
1395+ boost analyzer disableCoord) =
1396+ object [fieldName .= object conjoined]
1397+ where base = [ "query" .= query
1398+ , "cutoff_frequency" .= toJSON cf
1399+ , "low_freq_operator" .= toJSON lfo
1400+ , "high_freq_operator" .= toJSON hfo ]
1401+ extension = catMaybes
1402+ [ mField "minimum_should_match" msm
1403+ , mField "boost" boost
1404+ , mField "analyzer" analyzer
1405+ , mField "disable_coord" disableCoord ]
1406+ conjoined = base ++ extension
1409+instance ToJSON CommonMinimumMatch where
1410+ toJSON (CommonMinimumMatch mm) = toJSON mm
1411+ toJSON (CommonMinimumMatchHighLow (MinimumMatchHighLow lowF highF)) =
1412+ object [ "low_freq" .= toJSON lowF
1413+ , "high_freq" .= toJSON highF ]
1415+instance ToJSON BoostingQuery where
1416+ toJSON (BoostingQuery bqPositiveQuery bqNegativeQuery bqNegativeBoost) =
1417+ object [ "positive" .= toJSON bqPositiveQuery
1418+ , "negative" .= toJSON bqNegativeQuery
1419+ , "negative_boost" .= toJSON bqNegativeBoost ]
1422+instance ToJSON BoolQuery where
1423+ toJSON (BoolQuery mustM notM shouldM bqMin boost disableCoord) =
1425+ where filtered = catMaybes
1426+ [ mField "must" mustM
1427+ , mField "must_not" notM
1428+ , mField "should" shouldM
1429+ , mField "minimum_should_match" bqMin
1430+ , mField "boost" boost
1431+ , mField "disable_coord" disableCoord ]
1434+instance ToJSON MatchQuery where
1435+ toJSON (MatchQuery (FieldName fieldName)
1436+ (QueryString mqQueryString) booleanOperator
1437+ zeroTermsQuery cutoffFrequency matchQueryType
1438+ analyzer maxExpansions lenient) =
1439+ object [ fieldName .= object conjoined ]
1440+ where conjoined = [ "query" .= mqQueryString
1441+ , "operator" .= toJSON booleanOperator
1442+ , "zero_terms_query" .= toJSON zeroTermsQuery]
1444+ maybeAdd = catMaybes [ mField "cutoff_frequency" cutoffFrequency
1445+ , mField "type" matchQueryType
1446+ , mField "analyzer" analyzer
1447+ , mField "max_expansions" maxExpansions
1448+ , mField "lenient" lenient ]
1451+instance ToJSON MultiMatchQuery where
1452+ toJSON (MultiMatchQuery fields (QueryString query) boolOp
1453+ ztQ tb mmqt cf analyzer maxEx lenient) =
1454+ object ["multi_match" .= object conjoined]
1455+ where baseQuery = [ "fields" .= fmap toJSON fields
1457+ , "operator" .= toJSON boolOp
1458+ , "zero_terms_query" .= toJSON ztQ ]
1459+ maybeAdd = catMaybes [ mField "tiebreaker" tb
1460+ , mField "type" mmqt
1461+ , mField "cutoff_frequency" cf
1462+ , mField "analyzer" analyzer
1463+ , mField "max_expansions" maxEx
1464+ , mField "lenient" lenient ]
1465+ conjoined = baseQuery ++ maybeAdd
1468+instance ToJSON MultiMatchQueryType where
1469+ toJSON MultiMatchBestFields = "best_fields"
1470+ toJSON MultiMatchMostFields = "most_fields"
1471+ toJSON MultiMatchCrossFields = "cross_fields"
1472+ toJSON MultiMatchPhrase = "phrase"
1473+ toJSON MultiMatchPhrasePrefix = "phrase_prefix"
1475+instance ToJSON BooleanOperator where
1476+ toJSON And = String "and"
1477+ toJSON Or = String "or"
1479+instance ToJSON ZeroTermsQuery where
1480+ toJSON ZeroTermsNone = String "none"
1481+ toJSON ZeroTermsAll = String "all"
1483+instance ToJSON MatchQueryType where
1484+ toJSON MatchPhrase = "phrase"
1485+ toJSON MatchPhrasePrefix = "phrase_prefix"
1487+instance ToJSON FieldName where
1488+ toJSON (FieldName fieldName) = String fieldName
1490+instance ToJSON ReplicaCount
1491+instance ToJSON ShardCount
1492+instance ToJSON CutoffFrequency
1493+instance ToJSON Analyzer
1494+instance ToJSON MaxExpansions
1495+instance ToJSON Lenient
1496+instance ToJSON Boost
1497+instance ToJSON Version
1498+instance ToJSON Tiebreaker
1499+instance ToJSON MinimumMatch
1500+instance ToJSON DisableCoord
1501+instance ToJSON PrefixLength
1502+instance ToJSON Fuzziness
1503+instance ToJSON IgnoreTermFrequency
1504+instance ToJSON MaxQueryTerms
1505+instance ToJSON TypeName
1506+instance ToJSON IndexName
1507+instance ToJSON BoostTerms
1508+instance ToJSON MaxWordLength
1509+instance ToJSON MinWordLength
1510+instance ToJSON MaxDocFrequency
1511+instance ToJSON MinDocFrequency
1512+instance ToJSON PhraseSlop
1513+instance ToJSON StopWord
1514+instance ToJSON QueryPath
1515+instance ToJSON MinimumTermFrequency
1516+instance ToJSON PercentMatch
1517+instance ToJSON MappingName
1518+instance ToJSON DocId
1519+instance ToJSON QueryString
1520+instance ToJSON AllowLeadingWildcard
1521+instance ToJSON LowercaseExpanded
1522+instance ToJSON AnalyzeWildcard
1523+instance ToJSON GeneratePhraseQueries
1524+instance ToJSON Locale
1525+instance ToJSON EnablePositionIncrements
1526+instance FromJSON Version
1527+instance FromJSON IndexName
1528+instance FromJSON MappingName
1529+instance FromJSON DocId
1532+instance FromJSON Status where
1533+ parseJSON (Object v) = Status <$>
1539+ parseJSON _ = empty
1542+instance ToJSON IndexSettings where
1543+ toJSON (IndexSettings s r) = object ["settings" .= object ["shards" .= s, "replicas" .= r]]
1546+instance (FromJSON a) => FromJSON (EsResult a) where
1547+ parseJSON (Object v) = EsResult <$>
1551+ v .: "_version" <*>
1554+ parseJSON _ = empty
1557+instance ToJSON Search where
1558+ toJSON (Search query filter sort sTrackSortScores sFrom sSize) =
1559+ object merged where
1560+ lQuery = maybeJson "query" query
1561+ lFilter = maybeJson "filter" filter
1562+ lSort = maybeJsonF "sort" sort
1563+ merged = mconcat [[ "from" .= sFrom
1565+ , "track_scores" .= sTrackSortScores]
1571+instance ToJSON SortSpec where
1572+ toJSON (DefaultSortSpec
1573+ (DefaultSort (FieldName dsSortFieldName) dsSortOrder dsIgnoreUnmapped
1574+ dsSortMode dsMissingSort dsNestedFilter)) =
1575+ object [dsSortFieldName .= object merged] where
1576+ base = ["order" .= toJSON dsSortOrder
1577+ , "ignore_unmapped" .= dsIgnoreUnmapped]
1578+ lSortMode = maybeJson "mode" dsSortMode
1579+ lMissingSort = maybeJson "missing" dsMissingSort
1580+ lNestedFilter = maybeJson "nested_filter" dsNestedFilter
1581+ merged = mconcat [base, lSortMode, lMissingSort, lNestedFilter]
1583+ toJSON (GeoDistanceSortSpec gdsSortOrder (GeoPoint (FieldName field) gdsLatLon) units) =
1584+ object [ "unit" .= toJSON units
1585+ , field .= toJSON gdsLatLon
1586+ , "order" .= toJSON gdsSortOrder ]
1589+instance ToJSON SortOrder where
1590+ toJSON Ascending = String "asc"
1591+ toJSON Descending = String "desc"
1594+instance ToJSON SortMode where
1595+ toJSON SortMin = String "min"
1596+ toJSON SortMax = String "max"
1597+ toJSON SortSum = String "sum"
1598+ toJSON SortAvg = String "avg"
1601+instance ToJSON Missing where
1602+ toJSON LastMissing = String "_last"
1603+ toJSON FirstMissing = String "_first"
1604+ toJSON (CustomMissing txt) = String txt
1607+instance ToJSON ScoreType where
1608+ toJSON ScoreTypeMax = "max"
1609+ toJSON ScoreTypeAvg = "avg"
1610+ toJSON ScoreTypeSum = "sum"
1611+ toJSON ScoreTypeNone = "none"
1614+instance ToJSON Distance where
1615+ toJSON (Distance dCoefficient dUnit) =
1616+ String boltedTogether where
1617+ coefText = showText dCoefficient
1618+ (String unitText) = toJSON dUnit
1619+ boltedTogether = mappend coefText unitText
1622+instance ToJSON DistanceUnit where
1623+ toJSON Miles = String "mi"
1624+ toJSON Yards = String "yd"
1625+ toJSON Feet = String "ft"
1626+ toJSON Inches = String "in"
1627+ toJSON Kilometers = String "km"
1628+ toJSON Meters = String "m"
1629+ toJSON Centimeters = String "cm"
1630+ toJSON Millimeters = String "mm"
1631+ toJSON NauticalMiles = String "nmi"
1634+instance ToJSON DistanceType where
1635+ toJSON Arc = String "arc"
1636+ toJSON SloppyArc = String "sloppy_arc"
1637+ toJSON Plane = String "plane"
1640+instance ToJSON OptimizeBbox where
1641+ toJSON NoOptimizeBbox = String "none"
1642+ toJSON (OptimizeGeoFilterType gft) = toJSON gft
1645+instance ToJSON GeoBoundingBoxConstraint where
1646+ toJSON (GeoBoundingBoxConstraint
1647+ (FieldName gbbcGeoBBField) gbbcConstraintBox cache) =
1648+ object [gbbcGeoBBField .= toJSON gbbcConstraintBox
1649+ , "_cache" .= cache]
1652+instance ToJSON GeoFilterType where
1653+ toJSON GeoFilterMemory = String "memory"
1654+ toJSON GeoFilterIndexed = String "indexed"
1657+instance ToJSON GeoBoundingBox where
1658+ toJSON (GeoBoundingBox gbbTopLeft gbbBottomRight) =
1659+ object ["top_left" .= toJSON gbbTopLeft
1660+ , "bottom_right" .= toJSON gbbBottomRight]
1663+instance ToJSON LatLon where
1664+ toJSON (LatLon lLat lLon) =
1665+ object ["lat" .= lLat
1669+-- index for smaller ranges, fielddata for longer ranges
1670+instance ToJSON RangeExecution where
1671+ toJSON RangeExecutionIndex = "index"
1672+ toJSON RangeExecutionFielddata = "fielddata"
1675+instance ToJSON RegexpFlags where
1676+ toJSON AllRegexpFlags = String "ALL"
1677+ toJSON NoRegexpFlags = String "NONE"
1678+ toJSON (SomeRegexpFlags (h :| fs)) = String $ T.intercalate "|" flagStrs
1679+ where flagStrs = map flagStr . nub $ h:fs
1680+ flagStr AnyString = "ANYSTRING"
1681+ flagStr Automaton = "AUTOMATON"
1682+ flagStr Complement = "COMPLEMENT"
1683+ flagStr Empty = "EMPTY"
1684+ flagStr Intersection = "INTERSECTION"
1685+ flagStr Interval = "INTERVAL"
1687+instance ToJSON Term where
1688+ toJSON (Term field value) = object ["term" .= object
1692+instance ToJSON BoolMatch where
1693+ toJSON (MustMatch term cache) = object ["must" .= toJSON term,
1695+ toJSON (MustNotMatch term cache) = object ["must_not" .= toJSON term,
1697+ toJSON (ShouldMatch terms cache) = object ["should" .= fmap toJSON terms,
1701+instance (FromJSON a) => FromJSON (SearchResult a) where
1702+ parseJSON (Object v) = SearchResult <$>
1704+ v .: "timed_out" <*>
1707+ parseJSON _ = empty
1709+instance (FromJSON a) => FromJSON (SearchHits a) where
1710+ parseJSON (Object v) = SearchHits <$>
1712+ v .: "max_score" <*>
1714+ parseJSON _ = empty
1716+instance (FromJSON a) => FromJSON (Hit a) where
1717+ parseJSON (Object v) = Hit <$>
1723+ parseJSON _ = empty
1725+instance FromJSON ShardResult where
1726+ parseJSON (Object v) = ShardResult <$>
1728+ v .: "successful" <*>
1730+ parseJSON _ = empty