git.iko.soy/typeable/bloodhound
on fc6fe99 help sign in
71 files changed+15363−9686
since splitsnapshots
unifiedsplit
.gitignore−3 .hlint.yaml−66 .travis.yml+18−18 DSL.org+1−1 Makefile+14−117 README.md+7−7 bloodhound.cabal+97 changelog.md−107 docker-compose.yml−35 elasticsearch/Dockerfile−3 elasticsearch/config/elasticsearch.yml−3 examples/Makefile−5 examples/README.md+1−4 examples/Tweet.hs+22−5 examples/package.yaml+1 examples/stack.yaml−20 package.yaml−74 src/Bloodhound/Import.hs−85 src/Database/Bloodhound.hs−10 src/Database/Bloodhound/Common/Script.hs−220 src/Database/Bloodhound/Internal/Aggregation.hs−479 src/Database/Bloodhound/Internal/Analysis.hs−379 src/Database/Bloodhound/Internal/Client.hs−2431 src/Database/Bloodhound/Internal/Highlight.hs−164 src/Database/Bloodhound/Internal/Newtypes.hs−237 src/Database/Bloodhound/Internal/Query.hs−1640 src/Database/Bloodhound/Internal/Sort.hs−107 src/Database/Bloodhound/Internal/StringlyTyped.hs−51 src/Database/Bloodhound/Internal/Suggest.hs−270 src/Database/Bloodhound/Types.hs−542 src/Database/V1/Bloodhound.hs+10 src/Database/V1/Bloodhound/Client.hs+917 src/Database/V1/Bloodhound/Types.hs+5319 src/Database/V1/Bloodhound/Types/Class.hs+17 src/Database/V1/Bloodhound/Types/Internal.hs+57 src/Database/V5/Bloodhound.hs+10 src/Database/Bloodhound/Client.hs → src/Database/V5/Bloodhound/Client.hs+56−148 src/Database/V5/Bloodhound/Types.hs+5097 src/Database/V5/Bloodhound/Types/Class.hs+17 src/Database/V5/Bloodhound/Types/Internal.hs+57 stack-7.10.yaml+17 stack-7.8.yaml+21 stack-8.0.yaml−12 stack-8.2.yaml−15 stack-8.2.yaml.lock−40 stack-8.4.yaml−15 stack-8.4.yaml.lock−12 stack-8.6.yaml−14 stack-8.6.yaml.lock−33 stack.yaml+13−1 stack.yaml.lock−12 tests/Test/Aggregation.hs−147 tests/Test/ApproxEq.hs−63 tests/Test/BulkAPI.hs−95 tests/Test/Common.hs−292 tests/Test/Documents.hs−51 tests/Test/Generators.hs−563 tests/Test/Highlights.hs−32 tests/Test/Import.hs−58 tests/Test/Indices.hs−173 tests/Test/JSON.hs−213 tests/Test/Query.hs−132 tests/Test/Script.hs−36 tests/Test/Snapshots.hs−201 tests/Test/Sorting.hs−22 tests/Test/SourceFiltering.hs−38 tests/Test/Suggest.hs−23 tests/Test/Templates.hs−26 tests/V1/tests.hs+1905 tests/V5/tests.hs+1689 tests/tests.hs−136
.gitignore−3
@@ -17,6 +17,3 @@ bloodhound.iml
1717.hg
1818.hgignore
1919examples/bloodhound-examples.cabal
20−/.ghc.environment.*
21−.hspec-failures
22−bloodhound.cabal
.hlint.yamldeleted−66
@@ -1,66 +0,0 @@
1−# HLint configuration file
2−# https://github.com/ndmitchell/hlint
3−##########################
4−
5−# This file contains a template configuration file, which is typically
6−# placed as .hlint.yaml in the root of your project
7−
8−
9−# Specify additional command line arguments
10−#
11−# - arguments: [--color, --cpp-simple, -XQuasiQuotes]
12−
13−
14−# Control which extensions/flags/modules/functions can be used
15−#
16−# - extensions:
17−# - default: false # all extension are banned by default
18−# - name: [PatternGuards, ViewPatterns] # only these listed extensions can be used
19−# - {name: CPP, within: CrossPlatform} # CPP can only be used in a given module
20−#
21−# - flags:
22−# - {name: -w, within: []} # -w is allowed nowhere
23−#
24−# - modules:
25−# - {name: [Data.Set, Data.HashSet], as: Set} # if you import Data.Set qualified, it must be as 'Set'
26−# - {name: Control.Arrow, within: []} # Certain modules are banned entirely
27−#
28−# - functions:
29−# - {name: unsafePerformIO, within: []} # unsafePerformIO can only appear in no modules
30−
31−
32−# Add custom hints for this project
33−#
34−# Will suggest replacing "wibbleMany [myvar]" with "wibbleOne myvar"
35−# - error: {lhs: "wibbleMany [x]", rhs: wibbleOne x}
36−
37−
38−# Turn on hints that are off by default
39−#
40−# Ban "module X(module X) where", to require a real export list
41−# - warn: {name: Use explicit module export list}
42−#
43−# Replace a $ b $ c with a . b $ c
44−# - group: {name: dollar, enabled: true}
45−#
46−# Generalise map to fmap, ++ to <>
47−# - group: {name: generalise, enabled: true}
48−
49−
50−# Ignore some builtin hints
51−- ignore: {name: "Use <$>"}
52−- ignore: {name: "Use lambda-case"}
53−# When we don't use camelCase it's to match ES.
54−# Possibly this is a mistake.
55−- ignore: {name: "Use camelCase"}
56−- ignore: {name: "Eta reduce"}
57−# - ignore: {name: Use let}
58−# - ignore: {name: Use const, within: SpecialModule} # Only within certain modules
59−
60−
61−# Define some custom infix operators
62−# - fixity: infixr 3 ~^#^~
63−
64−
65−# To generate a suitable file for HLint do:
66−# $ hlint --default > .hlint.yaml
.travis.yml+18−18
@@ -1,24 +1,30 @@
11sudo: false
2−language: generic
3−
4−jdk:
5− - oraclejdk11
62
73addons:
84 apt:
95 packages:
106 - libgmp-dev
7+ - oracle-java8-installer
118
129env:
13− matrix:
14− - GHCVER=8.6 ESVER=5.6.0 STACK_YAML=stack.yaml DLINK=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch ES_JAVA_OPTS="-Xms500m -Xmx750m"
10+ global:
11+ - JAVA_HOME=/usr/lib/jvm/java-8-oracle
12+ matrix:
13+ - GHCVER=7.8 ESVER=1.3.6 STACK_YAML=stack-7.8.yaml ESFLAG=ES1 DLINK=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch
14+ - GHCVER=7.8 ESVER=1.4.1 STACK_YAML=stack-7.8.yaml ESFLAG=ES1 DLINK=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch
15+ - GHCVER=7.10 ESVER=1.5.2 STACK_YAML=stack-7.10.yaml ESFLAG=ES1 DLINK=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch
16+ - GHCVER=7.10 ESVER=1.6.0 STACK_YAML=stack-7.10.yaml ESFLAG=ES1 DLINK=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch
17+ - GHCVER=7.10 ESVER=1.7.2 STACK_YAML=stack-7.10.yaml ESFLAG=ES1 DLINK=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch
18+ - GHCVER=8.0 ESVER=1.7.2 STACK_YAML=stack.yaml ESFLAG=ES1 DLINK=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch
19+ - GHCVER=8.0 ESVER=5.0.2 STACK_YAML=stack.yaml ESFLAG=ES5 DLINK=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch ES_JAVA_OPTS="-Xms500m -Xmx750m"
1520
1621install:
1722 # stack
1823 - mkdir -p ~/.local/bin
19− - travis_retry curl -L https://github.com/commercialhaskell/stack/releases/download/v2.1.3/stack-2.1.3-linux-x86_64.tar.gz | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'
24+ - travis_retry curl -L https://github.com/commercialhaskell/stack/releases/download/v1.2.0/stack-1.2.0-linux-x86_64.tar.gz | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'
2025 - export PATH=~/.local/bin:$PATH
21− - stack --no-terminal --version
26+ - stack --version
27+
2228 # elasticsearch
2329 - wget --no-check-certificate $DLINK-$ESVER.tar.gz
2430 - tar xzf elasticsearch-$ESVER.tar.gz
@@ -27,17 +33,11 @@ install:
2733 - ./elasticsearch-$ESVER/bin/elasticsearch &
2834
2935script:
30− - stack setup --no-terminal
31− - stack update --no-terminal
32− - stack build -j2 --fast --no-terminal
33− - travis_wait 60 sleep 1800 &
34− - stack test --fast --no-terminal bloodhound:bloodhound-tests --test-arguments="--qc-max-success 500"
36+ - stack setup
37+ - stack update
38+ - stack build -j1
39+ - stack test bloodhound:tests --test-arguments="--qc-max-success 500" --flag bloodhound:$ESFLAG
3540
3641cache:
37− timeout: 1500
3842 directories:
39− - $HOME/.cabal
40− - $HOME/.ghc
4143 - $HOME/.stack
42− - .stack-work/
43− apt: true
DSL.org+1−1
@@ -1,4 +1,4 @@
1−* Elasticsearch DSL
1+* ElasticSearch DSL
22
33** Grouping
44
Makefile+14−117
@@ -1,129 +1,26 @@
1−.PHONY : build build-validate ghci test test-rerun test-ghci ghcid ghcid-validate \
2− weeder hlint hlint-watch mod-build
3−
4−.DEFAULT_GOAL = help
5−
6−stack = STACK_YAML='stack.yaml' stack
7−ghc_perf_options = --ghc-options '+RTS -A128M -RTS'
8−build = build $(ghc_perf_options)
9−ghci = ghci $(ghc_perf_options)
10−test = test $(ghc_perf_options)
11−stack-8.0 = STACK_YAML="stack-8.0.yaml" stack
12−stack-8.2 = STACK_YAML="stack-8.2.yaml" stack
13−stack-8.4 = STACK_YAML="stack-8.4.yaml" stack
14−stack-8.6 = STACK_YAML="stack-8.6.yaml" stack
15−elasticsearch_directory = elasticsearch
16−
17−# stack build --ghc-options '+RTS -A128M -RTS'
18−
19−## run build
201build:
21− $(stack) $(build)
22−
23−## build with validation options (Wall, Werror)
24−build-validate:
25− $(stack) build --fast --ghc-options '-Wall -Werror +RTS -A128M -RTS'
26−
27−## run ghci
28−ghci:
29− $(stack) $(ghci)
30−
31−## run tests
32−test: echo-warn
33− $(stack) $(test)
34−
35−## run tests with forced re-run via "-r"
36−test-rerun: echo-warn
37− $(stack) $(test) --test-arguments "-r"
38−
39−## run ghci with test stanza
40−test-ghci:
41− $(stack) $(ghci) bloodhound:test:bloodhound-tests
42−
43−## run ghcid
44−ghcid:
45− ghcid -c "$(stack) ghci bloodhound:lib --test --ghci-options='-fobject-code -fno-warn-unused-do-bind' --main-is bloodhound:test:bloodhound-tests"
46−
47−## run ghcid with validate options (Werror, etc.)
48−ghcid-validate:
49− ghcid -c "$(stack) ghci bloodhound:lib --test --ghci-options='-Werror -fobject-code -fno-warn-unused-do-bind' --main-is bloodhound:test:bloodhound-tests"
50−
51−## run weeder
52−weeder:
53− weeder . --build
54−
55−## run hlint
56−hlint:
57− hlint .
58−
59−## hlint watch with `sos`
60−hlint-watch:
61− sos src/ -c "hlint ." -p "src/(.*)\.hs"
62−
63−# mod-build:
64−# stack build --ghc-options '+RTS -A128M -RTS'
2+ stack build
653
664echo-warn:
67− @echo "Make certain you have an elasticsearch instance on localhost:9200 !"
5+ echo "Make certain you have an elasticsearch instance on localhost:9200 !"
686
69−## Test with GHC 8.0 and ES 5.x
70−test-8.0:
71− STACK_YAML="stack-8.0.yaml" stack test --fast bloodhound:test:bloodhound-tests --test-arguments="--qc-max-success 500"
7+test: echo-warn
8+ stack test
729
73−## Test with GHC 8.2 and ES 5.x
74−test-8.2:
75− STACK_YAML="stack.yaml" stack test --fast bloodhound:test:bloodhound-tests --test-arguments="--qc-max-success 500"
10+7.8-build:
11+ STACK_YAML="stack-7.8.yaml" stack build
7612
77−## Build with the GHC 8.0 Stack YAML
78−build-8.0:
79− $(stack-8.0) $(build)
13+7.8-test: echo-warn
14+ STACK_YAML="stack-7.8.yaml" stack test
8015
81−## Build with the GHC 8.2 Stack YAML
82−build-8.2:
83− $(stack-8.2) $(build)
16+7.10-build:
17+ STACK_YAML="stack-8=7.10.yaml" stack build
8418
85−## Build with the GHC 8.4 Stack YAML
86−build-8.4:
87− $(stack-8.4) $(build)
19+7.10-test: echo-warn
20+ STACK_YAML="stack-7.10.yaml" stack test
8821
89−## Build with the GHC 8.6 Stack YAML
90−build-8.6:
91− $(stack-8.6) $(build)
22+ghci:
23+ stack ghci
9224
93−## Upload the package to Hackage
9425upload:
9526 stack upload --no-signature .
96−
97−# Create ES5 instance
98−
99−## Run test environment
100−compose-ES5:
101− @LOCAL_USER_ID=${LOCAL_USER_ID} docker-compose -f docker-compose.yml --project-directory $(elasticsearch_directory) up
102−
103−## Run test environment in detach mode
104−compose-ES5-detach-up:
105− @LOCAL_USER_ID=${LOCAL_USER_ID} docker-compose -f docker-compose.yml --project-directory $(elasticsearch_directory) up -d
106−
107−## Close test environment if run on detach mode
108−compose-ES5-detach-down:
109− @LOCAL_USER_ID=${LOCAL_USER_ID} docker-compose -f docker-compose.yml --project-directory $(elasticsearch_directory) down
110−
111−## build the docker compose images
112−compose-build:
113− @LOCAL_USER_ID=${LOCAL_USER_ID} docker-compose -f docker-compose.yml --project-directory $(elasticsearch_directory) build
114−
115−## Spawn bash shell in ES5 test container
116−ES5-shell:
117− @LOCAL_USER_ID=${LOCAL_USER_ID} docker-compose -f docker-compose.yml --project-directory $(elasticsearch_directory) exec elasticsearch1 bash
118−
119−help:
120− @echo "Please use \`make <target>' where <target> is one of\n\n"
121− @awk '/^[a-zA-Z\-\_0-9]+:/ { \
122− helpMessage = match(lastLine, /^## (.*)/); \
123− if (helpMessage) { \
124− helpCommand = substr($$1, 0, index($$1, ":")); \
125− helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
126− printf "%-30s %s\n", helpCommand, helpMessage; \
127− } \
128− } \
129− { lastLine = $$0 }' $(MAKEFILE_LIST)
README.md+7−7
@@ -22,17 +22,17 @@ Version compatibility
2222---------------------
2323
2424As of version 0.13.0.0, Bloodhound has 2 separate module trees for
25−Elasticsearch versions 1 and 5. Import the module that is appropriate
25+ElasticSearch versions 1 and 5. Import the module that is appropriate
2626for your use case. If you would like to add support for another major
2727version, open a ticket expressing your intend and follow the pattern
2828used for other versions. We weighed the idea of sharing code between
2929versions but it just got too messy, especially considering the
30−instability of the Elasticsearch API. We switched to a model which
30+instability of the ElasticSearch API. We switched to a model which
3131would allow the persons responsible for a particular protocol version
3232to maintain that version while avoiding conflict with other versions.
3333
3434See our [TravisCI](https://travis-ci.org/bitemyapp/bloodhound) for a
35−listing of Elasticsearch version we test against.
35+listing of ElasticSearch version we test against.
3636
3737
3838Stability
@@ -43,15 +43,15 @@ Bloodhound is stable for production use. I will strive to avoid breaking API com
4343Testing
4444---------
4545
46−The TravisCI tests are run using [Stack](http://docs.haskellstack.org/en/stable/README.html). You should use Stack instead of `cabal` to build and test Bloodhound to avoid compatibility problems. You will also need to have an Elasticsearch instance running at `localhost:9200` in order to execute some of the tests. See the "Version compatibility" section above for a list of Elasticsearch versions that are officially validated against in TravisCI.
46+The TravisCI tests are run using [Stack](http://docs.haskellstack.org/en/stable/README.html). You should use Stack instead of `cabal` to build and test Bloodhound to avoid compatibility problems. You will also need to have an ElasticSearch instance running at `localhost:9200` in order to execute some of the tests. See the "Version compatibility" section above for a list of ElasticSearch versions that are officially validated against in TravisCI.
4747
4848Steps to run the tests locally:
49− 1. Dig through the [past releases] (https://www.elastic.co/downloads/past-releases) section of the Elasticsearch download page and install the desired Elasticsearch versions.
49+ 1. Dig through the [past releases] (https://www.elastic.co/downloads/past-releases) section of the ElasticSearch download page and install the desired ElasticSearch versions.
5050 2. Install [Stack] (http://docs.haskellstack.org/en/stable/README.html#how-to-install)
5151 3. In your local Bloodhound directory, run `stack setup && stack build`
52− 4. Start the desired version of Elasticsearch at `localhost:9200`, which should be the default.
52+ 4. Start the desired version of ElasticSearch at `localhost:9200`, which should be the default.
5353 5. Run `stack test` in your local Bloodhound directory.
54− 6. The unit tests will pass if you re-execute `stack test`. If you want to start with a clean slate, stop your Elasticsearch instance, delete the `data/` folder in the Elasticsearch installation, restart Elasticsearch, and re-run `stack test`.
54+ 6. The unit tests will pass if you re-execute `stack test`. If you want to start with a clean slate, stop your ElasticSearch instance, delete the `data/` folder in the ElasticSearch installation, restart ElasticSearch, and re-run `stack test`.
5555
5656
5757Hackage page and Haddock documentation
bloodhound.cabalnew file+97
@@ -0,0 +1,97 @@
1+name: bloodhound
2+version: 0.13.0.0
3+synopsis: ElasticSearch client library for Haskell
4+description: ElasticSearch made awesome for Haskell hackers
5+homepage: https://github.com/bitemyapp/bloodhound
6+license: BSD3
7+license-file: LICENSE
8+author: Chris Allen
9+maintainer: cma@bitemyapp.com
10+copyright: 2015, Chris Allen
11+category: Database, Search
12+build-type: Simple
13+cabal-version: >=1.10
14+
15+extra-source-files:
16+ README.md
17+ changelog.md
18+
19+source-repository head
20+ type: git
21+ location: https://github.com/bitemyapp/bloodhound.git
22+
23+Flag ES1
24+ Description: Run the test suite against an Elasticsearch 1.x server
25+ Default: True
26+
27+Flag ES5
28+ Description: Run the test suite against an Elasticsearch 5.x server
29+ Default: False
30+
31+library
32+ ghc-options: -Wall
33+ exposed-modules: Database.V1.Bloodhound
34+ Database.V1.Bloodhound.Client
35+ Database.V1.Bloodhound.Types
36+ Database.V1.Bloodhound.Types.Class
37+ Database.V1.Bloodhound.Types.Internal
38+ Database.V5.Bloodhound
39+ Database.V5.Bloodhound.Client
40+ Database.V5.Bloodhound.Types
41+ Database.V5.Bloodhound.Types.Class
42+ Database.V5.Bloodhound.Types.Internal
43+ hs-source-dirs: src
44+ build-depends: base >= 4.3 && <5,
45+ bytestring >= 0.10.0 && <0.11,
46+ containers >= 0.5.0.0 && <0.6,
47+ aeson >= 0.11.1 && <1.2,
48+ http-client >= 0.4.30 && <0.6,
49+ network-uri >= 2.6 && <2.7,
50+ semigroups >= 0.15 && <0.19,
51+ time >= 1.4 && <1.7,
52+ text >= 0.11 && <1.3,
53+ mtl >= 1.0 && <2.3,
54+ transformers >= 0.2 && <0.6,
55+ http-types >= 0.8 && <0.10,
56+ vector >= 0.10.9 && <0.13,
57+ scientific >= 0.3.0.0 && <0.4.0.0,
58+ exceptions,
59+ data-default-class,
60+ blaze-builder,
61+ unordered-containers,
62+ mtl-compat,
63+ hashable
64+ default-language: Haskell2010
65+
66+test-suite tests
67+ ghc-options: -Wall -fno-warn-orphans
68+ type: exitcode-stdio-1.0
69+ main-is: tests.hs
70+ if flag(ES5)
71+ hs-source-dirs: tests/V5
72+ else
73+ hs-source-dirs: tests/V1
74+
75+ build-depends: base,
76+ bloodhound,
77+ bytestring,
78+ http-client,
79+ http-types,
80+ containers,
81+ hspec >= 1.8,
82+ text,
83+ time,
84+ aeson,
85+ semigroups,
86+ QuickCheck,
87+ vector,
88+ unordered-containers >= 0.2.5.0 && <0.3,
89+ mtl,
90+ quickcheck-properties,
91+ generics-sop >=0.2 && <0.3,
92+ errors,
93+ exceptions,
94+ temporary,
95+ unix-compat,
96+ network-uri
97+ default-language: Haskell2010
changelog.md−107
@@ -1,110 +1,3 @@
1−0.17.0.0
2−========
3−- @bitemyapp
4− - Removed V1 modules and support.
5− - Removed the cabal file, replaced with an Hpack `package.yaml`.
6−- @JoseD92
7− - Removed the disused `Seminearring` class.
8− - Added Docker Compose support.
9−
10−0.16.0.0
11−========
12−- @bitemyapp
13− - Reorganized V5 modules internally, ripped out Generic,
14− rewrote part of the test suite.
15− - Added message field to EsProtocolException
16− - Switch from Data.Version to Data.SemVer to support parsing broader version format.
17− - Drop support for GHC < 8.0
18− - Remove upper bound on aeson.
19−- @mxavier
20− - Reorganized V1 modules, mirroring the work done on V5.
21−- @andrewthad
22− - Added support for autogenerated elasticsearch ids in the bulk API
23− - Added support for token filters
24− - Added action for searching multiple indices
25−- @lunaris
26− - Added support for scripts fields and function score queries
27−- @bermanjosh
28− - Added support for direct generators
29−- @rvl
30− - Add [character filters][] to the `AnalyzerDefinition` type.
31− - Add the "stemmer" and "stop" [token filters][] to `TokenFilterDefinition`.
32−- @ahodgen
33− - Add support for wildcard queries
34−- @ashutoshrishi
35− - Added [fuzziness][] option to a Match Query
36− - Added support for "AUTO" Fuzziness alongside a numeric value.
37−
38−[Character Filters]: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/analysis-charfilters.html
39−[Token Filters]: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/analysis-tokenfilters.html
40−[Fuzziness]: https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness
41−
42−0.15.0.2
43−========
44−- @michaelxavier
45− - Allow http-types 0.12
46−
47−0.15.0.1
48−========
49−- @bitemyapp
50− - Allow http-types 0.11
51− - Removed unused `oPath` function
52−
53−0.15.0.0
54−===================
55−
56−- @jagare
57− - #204 Fix bug where Location was used instad of LatLon
58− - #207 Add instructions on how to run example code
59−
60−- @andrewthad
61− - #200 Add function for deleting alias
62− - #201 Add BulkCreateEncoding, which allows use of aeson's Encoding for faster bulk creates
63− - #203 Add support for total fields limit
64−
65−- @chrissound
66− - #196 Add missing import to example
67−
68−- @tmcgilchrist
69− - #186 Fix warnings and drop redundant constraints
70−
71−- @23Skidoo
72− - #183 Bump dependencies to support GHC 8.2
73−
74−- @bermanjosh
75− - #182 Support for Suggesters and Template Queries
76−
77−- @dawei-dev
78− - #181 Fix typo
79−
80−
81−0.14.0.0
82−===================
83−
84−- @bitemyapp
85− - Aeson 1.2
86−
87−- @shmish111
88− - #177 Added stats aggregation
89−
90−- @justinwhite
91− - #176 Fixed typo
92−
93−- @WraithM
94− - #175 Sorted version of getInitialScroll for V5 client
95−
96−0.13.0.0
97−===================
98−
99−- @michaelxavier
100− - #174 doctests are bad
101− - #171 Added Semigroup instances
102− - #165 Aeson 1.1
103−
104−- @bermanjosh
105− - #168 Sub-aggregation support
106− - #167 Limit QuickCheck to 500 checks
107−
10810.12.1.0
1092===================
1103* @michaelxavier
docker-compose.ymldeleted−35
@@ -1,35 +0,0 @@
1−version: "2"
2−services:
3− elasticsearch1:
4− build: .
5− container_name: elasticsearch1
6− environment:
7− - cluster.name=docker-cluster
8− - bootstrap.memory_lock=true
9− - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
10− ulimits:
11− memlock:
12− soft: -1
13− hard: -1
14− mem_limit: 1g
15− ports:
16− - 9200:9200
17− networks:
18− - esnet
19− elasticsearch2:
20− build: .
21− environment:
22− - cluster.name=docker-cluster
23− - bootstrap.memory_lock=true
24− - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
25− - "discovery.zen.ping.unicast.hosts=elasticsearch1"
26− ulimits:
27− memlock:
28− soft: -1
29− hard: -1
30− mem_limit: 1g
31− networks:
32− - esnet
33−
34−networks:
35− esnet: {}
elasticsearch/Dockerfiledeleted−3
@@ -1,3 +0,0 @@
1−FROM docker.elastic.co/elasticsearch/elasticsearch:5.6.0
2−
3−COPY config /usr/share/elasticsearch/config/
elasticsearch/config/elasticsearch.ymldeleted−3
@@ -1,3 +0,0 @@
1−xpack.security.enabled: false
2−cluster.name: "docker-cluster"
3−network.host: 0.0.0.0
examples/Makefiledeleted−5
@@ -1,5 +0,0 @@
1−build:
2− stack build
3−
4−ghci:
5− stack ghci
examples/README.md+1−4
@@ -1,6 +1,3 @@
11# Bloodhound Examples
22
3−These examples can be built via `stack build`.
4−To run them start an Elasticsearch 5.x server at `localhost:9200`
5−
6−Run with `stack exec tweet-example`
3+These examples can be build via `stack build`.
examples/Tweet.hs+22−5
@@ -5,6 +5,7 @@ module Main
55 ) where
66
77
8+-------------------------------------------------------------------------------
89import Control.Monad.IO.Class (liftIO)
910import Data.Aeson (FromJSON (..), defaultOptions,
1011 genericParseJSON, genericToJSON,
@@ -14,9 +15,10 @@ import Data.Text (Text)
1415import Data.Time.Calendar (Day (..))
1516import Data.Time.Clock (UTCTime (..), secondsToDiffTime)
1617import qualified Data.Vector as V
17−import Database.Bloodhound
18+import Database.V1.Bloodhound
1819import GHC.Generics (Generic)
1920import Network.HTTP.Client (defaultManagerSettings)
21+-------------------------------------------------------------------------------
2022
2123
2224data TweetMapping = TweetMapping deriving (Eq, Show)
@@ -28,15 +30,25 @@ instance ToJSON TweetMapping where
2830 object ["location" .= object ["type" .= ("geo_point" :: Text)]]
2931 ]
3032
33+
34+-------------------------------------------------------------------------------
35+data Location = Location
36+ { locLat :: Double
37+ , locLon :: Double
38+ } deriving (Eq, Generic, Show)
39+
40+
41+-------------------------------------------------------------------------------
3142data Tweet = Tweet
3243 { user :: Text
3344 , postDate :: UTCTime
3445 , message :: Text
3546 , age :: Int
36− , location :: LatLon
47+ , location :: Location
3748 } deriving (Eq, Generic, Show)
3849
3950
51+-------------------------------------------------------------------------------
4052exampleTweet :: Tweet
4153exampleTweet =
4254 Tweet
@@ -47,14 +59,19 @@ exampleTweet =
4759 , location = loc
4860 }
4961 where
50− loc = LatLon {lat = 40.12, lon = -71.3}
62+ loc = Location {locLat = 40.12, locLon = (-71.34)}
5163
5264instance ToJSON Tweet where
5365 toJSON = genericToJSON defaultOptions
5466instance FromJSON Tweet where
5567 parseJSON = genericParseJSON defaultOptions
68+instance ToJSON Location where
69+ toJSON = genericToJSON defaultOptions
70+instance FromJSON Location where
71+ parseJSON = genericParseJSON defaultOptions
5672
5773
74+-------------------------------------------------------------------------------
5875main :: IO ()
5976main = runBH' $ do
6077 -- set up index
@@ -83,7 +100,7 @@ main = runBH' $ do
83100 True <- indexExists aliasName
84101
85102 -- create a template so that if we just write into an index named tweet-2017-01-02, for instance, the index will be automatically created with the given mapping. This is a great idea for any ongoing indices because it makes them much easier to manage and rotate.
86− let idxTpl = IndexTemplate (TemplatePattern "tweet-*") (Just (IndexSettings (ShardCount 1) (ReplicaCount 1))) [object ["tweet" .= toJSON TweetMapping]]
103+ let idxTpl = IndexTemplate (TemplatePattern "tweet-*") (Just (IndexSettings (ShardCount 1) (ReplicaCount 1))) [toJSON TweetMapping]
87104 let templateName = TemplateName "tweet-tpl"
88105 _ <- putTemplate idxTpl templateName
89106 True <- templateExists templateName
@@ -101,7 +118,7 @@ main = runBH' $ do
101118
102119 return ()
103120 where
104− testServer = Server "http://localhost:9200"
121+ testServer = (Server "http://localhost:9200")
105122 runBH' = withBH defaultManagerSettings testServer
106123 testIndex = IndexName "twitter"
107124 testMapping = MappingName "tweet"
examples/package.yaml+1
@@ -17,6 +17,7 @@ dependencies:
1717- http-client
1818- vector
1919- semigroups
20+- transformers
2021ghc-options:
2122- -Wall
2223- -threaded
examples/stack.yamldeleted−20
@@ -1,20 +0,0 @@
1−flags:
2− bloodhound-examples:
3− werror: false
4−
5−packages:
6−- '.'
7−- '..'
8−# extra-deps:
9−# - http-client-0.5.0
10−# - aeson-1.2.0.0
11−# - fail-4.9.0.0
12−# - http-types-0.9
13−# - attoparsec-0.13.0.1
14−# - quickcheck-properties-0.1
15−# - semigroups-0.18.3
16−# - uri-bytestring-0.1.9
17−# - temporary-resourcet-0.1.0.0
18−# - transformers-compat-0.5.1.4
19−
20−resolver: lts-8.14
package.yamldeleted−74
@@ -1,74 +0,0 @@
1−name: bloodhound
2−version: '0.17.0.0'
3−synopsis: Elasticsearch client library for Haskell
4−description: Elasticsearch made awesome for Haskell hackers
5−category: Database, Search
6−author: Chris Allen
7−maintainer: cma@bitemyapp.com
8−copyright: 2018 Chris Allen
9−license: BSD3
10−github: bitemyapp/bloodhound.git
11−
12−extra-source-files:
13− - README.md
14− - changelog.md
15− - tests/tests.hs
16−
17−dependencies:
18− - aeson >=0.11.1
19− - base >=4.3 && <5
20− - blaze-builder
21− - bytestring >=0.10.0
22− - containers >=0.5.0.0
23− - exceptions
24− - hashable
25− - http-client >=0.4.30
26− - http-types >=0.8
27− - mtl >=1.0
28− - network-uri >=2.6
29− - scientific >=0.3.0.0
30− - semigroups >=0.15
31− - semver
32− - text >=0.11
33− - time >=1.4
34− - transformers >=0.2
35− - unordered-containers
36− - vector >=0.10.9
37−
38−library:
39− source-dirs: src
40− ghc-options: -Wall
41− exposed-modules:
42− - Database.Bloodhound
43− - Database.Bloodhound.Client
44− - Database.Bloodhound.Types
45− - Database.Bloodhound.Internal.Aggregation
46− - Database.Bloodhound.Internal.Analysis
47− - Database.Bloodhound.Internal.Client
48− - Database.Bloodhound.Internal.Highlight
49− - Database.Bloodhound.Internal.Newtypes
50− - Database.Bloodhound.Internal.Query
51− - Database.Bloodhound.Internal.Sort
52− - Database.Bloodhound.Internal.StringlyTyped
53− - Database.Bloodhound.Internal.Suggest
54−
55−tests:
56− bloodhound-tests:
57− source-dirs: tests
58− main: tests.hs
59− ghc-options:
60− - -Wall
61− - -fno-warn-orphans
62− dependencies:
63− - QuickCheck
64− - base
65− - bloodhound
66− - errors
67− - hspec >=1.8
68− - microlens
69− - microlens-aeson
70− - pretty-simple
71− - quickcheck-arbitrary-template
72− - quickcheck-properties
73− - temporary
74− - unix-compat
src/Bloodhound/Import.hsdeleted−85
@@ -1,85 +0,0 @@
1−module Bloodhound.Import
2− ( module X
3− , LByteString
4− , Method
5− , omitNulls
6− , parseNEJSON
7− , parseReadText
8− , readMay
9− , showText
10− , deleteSeveral
11− , oPath
12− , tshow
13− ) where
14−
15−import Control.Applicative as X (Alternative (..), optional)
16−import Control.Exception as X (Exception)
17−import Control.Monad as X (MonadPlus (..), forM, (<=<))
18−import Control.Monad.Catch as X (MonadCatch, MonadMask,
19− MonadThrow)
20−import Control.Monad.Except as X (MonadError)
21−import Control.Monad.Fix as X (MonadFix)
22−import Control.Monad.IO.Class as X (MonadIO (..))
23−import Control.Monad.Reader as X (MonadReader (..),
24− MonadTrans (..), ReaderT (..))
25−import Control.Monad.State as X (MonadState)
26−import Control.Monad.Writer as X (MonadWriter)
27−import Data.Aeson as X
28−import Data.Aeson.Types as X (Pair, Parser, emptyObject,
29− parseEither, parseMaybe,
30− typeMismatch)
31−import Data.Bifunctor as X (first)
32−import Data.Char as X (isNumber)
33−import Data.Hashable as X (Hashable)
34−import qualified Data.HashMap.Strict as HM
35−import Data.List as X (foldl', intercalate, nub)
36−import Data.List.NonEmpty as X (NonEmpty (..), toList)
37−import Data.Maybe as X (catMaybes, fromMaybe,
38− isNothing, maybeToList)
39−import Data.Scientific as X (Scientific)
40−import Data.Semigroup as X (Semigroup (..))
41−import Data.Text as X (Text)
42−import Data.Time.Calendar as X (Day (..), showGregorian)
43−import Data.Time.Clock as X (NominalDiffTime, UTCTime)
44−import Data.Time.Clock.POSIX as X
45−
46−import qualified Data.ByteString.Lazy as BL
47−import qualified Data.Text as T
48−import qualified Data.Traversable as DT
49−import qualified Data.Vector as V
50−import qualified Network.HTTP.Types.Method as NHTM
51−
52−type LByteString = BL.ByteString
53−
54−type Method = NHTM.Method
55−
56−readMay :: Read a => String -> Maybe a
57−readMay s = case reads s of
58− (a, ""):_ -> Just a
59− _ -> Nothing
60−
61−parseReadText :: Read a => Text -> Parser a
62−parseReadText = maybe mzero return . readMay . T.unpack
63−
64−showText :: Show a => a -> Text
65−showText = T.pack . show
66−
67−omitNulls :: [(Text, Value)] -> Value
68−omitNulls = object . filter notNull where
69− notNull (_, Null) = False
70− notNull (_, Array a) = (not . V.null) a
71− notNull _ = True
72−
73−parseNEJSON :: (FromJSON a) => [Value] -> Parser (NonEmpty a)
74−parseNEJSON [] = fail "Expected non-empty list"
75−parseNEJSON (x:xs) = DT.mapM parseJSON (x :| xs)
76−
77−deleteSeveral :: (Eq k, Hashable k) => [k] -> HM.HashMap k v -> HM.HashMap k v
78−deleteSeveral ks hm = foldr HM.delete hm ks
79−
80−oPath :: ToJSON a => NonEmpty Text -> a -> Value
81−oPath (k :| []) v = object [k .= v]
82−oPath (k:| (h:t)) v = object [k .= oPath (h :| t) v]
83−
84−tshow :: Show a => a -> Text
85−tshow = T.pack . show
src/Database/Bloodhound.hsdeleted−10
@@ -1,10 +0,0 @@
1−module Database.Bloodhound
2− ( -- module Data.Aeson.Types
3− -- ,
4− module Database.Bloodhound.Client
5− , module Database.Bloodhound.Types
6− ) where
7−
8−-- import Data.Aeson.Types
9−import Database.Bloodhound.Client
10−import Database.Bloodhound.Types
src/Database/Bloodhound/Common/Script.hsdeleted−220
@@ -1,220 +0,0 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−
4−module Database.Bloodhound.Common.Script where
5−
6−import Bloodhound.Import
7−
8−import qualified Data.HashMap.Strict as HM
9−
10−import Database.Bloodhound.Internal.Newtypes
11−
12−newtype ScriptFields =
13− ScriptFields (HM.HashMap ScriptFieldName ScriptFieldValue)
14− deriving (Eq, Show)
15−
16−type ScriptFieldName = Text
17−type ScriptFieldValue = Value
18−
19−data Script =
20− Script { scriptLanguage :: Maybe ScriptLanguage
21− , scriptInline :: Maybe ScriptInline
22− , scriptStored :: Maybe ScriptId
23− , scriptParams :: Maybe ScriptParams
24− } deriving (Eq, Show)
25−
26−newtype ScriptLanguage =
27− ScriptLanguage Text deriving (Eq, Show, FromJSON, ToJSON)
28−
29−newtype ScriptInline =
30− ScriptInline Text deriving (Eq, Show, FromJSON, ToJSON)
31−
32−newtype ScriptId =
33− ScriptId Text deriving (Eq, Show, FromJSON, ToJSON)
34−
35−newtype ScriptParams =
36− ScriptParams (HM.HashMap ScriptParamName ScriptParamValue)
37− deriving (Eq, Show)
38−
39−type ScriptParamName = Text
40−type ScriptParamValue = Value
41−
42−data BoostMode =
43− BoostModeMultiply
44− | BoostModeReplace
45− | BoostModeSum
46− | BoostModeAvg
47− | BoostModeMax
48− | BoostModeMin deriving (Eq, Show)
49−
50−data ScoreMode =
51− ScoreModeMultiply
52− | ScoreModeSum
53− | ScoreModeAvg
54− | ScoreModeFirst
55− | ScoreModeMax
56− | ScoreModeMin deriving (Eq, Show)
57−
58−data FunctionScoreFunction =
59− FunctionScoreFunctionScript Script
60− | FunctionScoreFunctionRandom Seed
61− | FunctionScoreFunctionFieldValueFactor FieldValueFactor
62− deriving (Eq, Show)
63−
64−newtype Weight =
65− Weight Float deriving (Eq, Show, FromJSON, ToJSON)
66−
67−newtype Seed =
68− Seed Float deriving (Eq, Show, FromJSON, ToJSON)
69−
70−data FieldValueFactor =
71− FieldValueFactor { fieldValueFactorField :: FieldName
72− , fieldValueFactor :: Maybe Factor
73− , fieldValueFactorModifier :: Maybe FactorModifier
74− , fieldValueFactorMissing :: Maybe FactorMissingFieldValue
75− } deriving (Eq, Show)
76−
77−newtype Factor =
78− Factor Float deriving (Eq, Show, FromJSON, ToJSON)
79−
80−data FactorModifier =
81− FactorModifierNone
82− | FactorModifierLog
83− | FactorModifierLog1p
84− | FactorModifierLog2p
85− | FactorModifierLn
86− | FactorModifierLn1p
87− | FactorModifierLn2p
88− | FactorModifierSquare
89− | FactorModifierSqrt
90− | FactorModifierReciprocal deriving (Eq, Show)
91−
92−newtype FactorMissingFieldValue =
93− FactorMissingFieldValue Float deriving (Eq, Show, FromJSON, ToJSON)
94−
95−instance ToJSON BoostMode where
96− toJSON BoostModeMultiply = "multiply"
97− toJSON BoostModeReplace = "replace"
98− toJSON BoostModeSum = "sum"
99− toJSON BoostModeAvg = "avg"
100− toJSON BoostModeMax = "max"
101− toJSON BoostModeMin = "min"
102−
103−instance FromJSON BoostMode where
104− parseJSON = withText "BoostMode" parse
105− where parse "multiply" = pure BoostModeMultiply
106− parse "replace" = pure BoostModeReplace
107− parse "sum" = pure BoostModeSum
108− parse "avg" = pure BoostModeAvg
109− parse "max" = pure BoostModeMax
110− parse "min" = pure BoostModeMin
111− parse bm = fail ("Unexpected BoostMode: " <> show bm)
112−
113−instance ToJSON ScoreMode where
114− toJSON ScoreModeMultiply = "multiply"
115− toJSON ScoreModeSum = "sum"
116− toJSON ScoreModeFirst = "first"
117− toJSON ScoreModeAvg = "avg"
118− toJSON ScoreModeMax = "max"
119− toJSON ScoreModeMin = "min"
120−
121−instance FromJSON ScoreMode where
122− parseJSON = withText "ScoreMode" parse
123− where parse "multiply" = pure ScoreModeMultiply
124− parse "sum" = pure ScoreModeSum
125− parse "first" = pure ScoreModeFirst
126− parse "avg" = pure ScoreModeAvg
127− parse "max" = pure ScoreModeMax
128− parse "min" = pure ScoreModeMin
129− parse sm = fail ("Unexpected ScoreMode: " <> show sm)
130−
131−functionScoreFunctionPair :: FunctionScoreFunction -> (Text, Value)
132−functionScoreFunctionPair (FunctionScoreFunctionScript functionScoreScript) =
133− ("script_score", toJSON functionScoreScript)
134−functionScoreFunctionPair (FunctionScoreFunctionRandom seed) =
135− ("random_score", omitNulls [ "seed" .= seed ])
136−functionScoreFunctionPair (FunctionScoreFunctionFieldValueFactor fvf) =
137− ("field_value_factor", toJSON fvf)
138−
139−parseFunctionScoreFunction :: Object -> Parser FunctionScoreFunction
140−parseFunctionScoreFunction o =
141− singleScript `taggedWith` "script_score"
142− <|> singleRandom `taggedWith` "random_score"
143− <|> singleFieldValueFactor `taggedWith` "field_value_factor"
144− where taggedWith parser k = parser =<< o .: k
145− singleScript = pure . FunctionScoreFunctionScript
146− singleRandom o' = FunctionScoreFunctionRandom <$> o' .: "seed"
147− singleFieldValueFactor = pure . FunctionScoreFunctionFieldValueFactor
148−
149−instance ToJSON ScriptFields where
150− toJSON (ScriptFields x) = Object x
151−
152−instance FromJSON ScriptFields where
153− parseJSON (Object o) = pure (ScriptFields o)
154− parseJSON _ = fail "error parsing ScriptFields"
155−
156−instance ToJSON Script where
157− toJSON (Script lang inline stored params) =
158− object [ "script" .= omitNulls base ]
159− where base = [ "lang" .= lang
160− , "inline" .= inline
161− , "stored" .= stored
162− , "params" .= params ]
163−
164−instance FromJSON Script where
165− parseJSON = withObject "Script" parse
166− where parse o = o .: "script" >>= \o' ->
167− Script
168− <$> o' .:? "lang"
169− <*> o' .:? "inline"
170− <*> o' .:? "stored"
171− <*> o' .:? "params"
172−
173−instance ToJSON ScriptParams where
174− toJSON (ScriptParams x) = Object x
175−
176−instance FromJSON ScriptParams where
177− parseJSON (Object o) = pure (ScriptParams o)
178− parseJSON _ = fail "error parsing ScriptParams"
179−
180−instance ToJSON FieldValueFactor where
181− toJSON (FieldValueFactor field factor modifier missing) =
182− omitNulls base
183− where base = [ "field" .= field
184− , "factor" .= factor
185− , "modifier" .= modifier
186− , "missing" .= missing ]
187−
188−instance FromJSON FieldValueFactor where
189− parseJSON = withObject "FieldValueFactor" parse
190− where parse o = FieldValueFactor
191− <$> o .: "field"
192− <*> o .:? "factor"
193− <*> o .:? "modifier"
194− <*> o .:? "missing"
195−
196−instance ToJSON FactorModifier where
197− toJSON FactorModifierNone = "none"
198− toJSON FactorModifierLog = "log"
199− toJSON FactorModifierLog1p = "log1p"
200− toJSON FactorModifierLog2p = "log2p"
201− toJSON FactorModifierLn = "ln"
202− toJSON FactorModifierLn1p = "ln1p"
203− toJSON FactorModifierLn2p = "ln2p"
204− toJSON FactorModifierSquare = "square"
205− toJSON FactorModifierSqrt = "sqrt"
206− toJSON FactorModifierReciprocal = "reciprocal"
207−
208−instance FromJSON FactorModifier where
209− parseJSON = withText "FactorModifier" parse
210− where parse "none" = pure FactorModifierNone
211− parse "log" = pure FactorModifierLog
212− parse "log1p" = pure FactorModifierLog1p
213− parse "log2p" = pure FactorModifierLog2p
214− parse "ln" = pure FactorModifierLn
215− parse "ln1p" = pure FactorModifierLn1p
216− parse "ln2p" = pure FactorModifierLn2p
217− parse "square" = pure FactorModifierSquare
218− parse "sqrt" = pure FactorModifierSqrt
219− parse "reciprocal" = pure FactorModifierReciprocal
220− parse fm = fail ("Unexpected FactorModifier: " <> show fm)
src/Database/Bloodhound/Internal/Aggregation.hsdeleted−479
@@ -1,479 +0,0 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−{-# LANGUAGE RecordWildCards #-}
4−
5−module Database.Bloodhound.Internal.Aggregation where
6−
7−import Bloodhound.Import
8−
9−import qualified Data.Aeson as Aeson
10−import qualified Data.HashMap.Strict as HM
11−import qualified Data.Map.Strict as M
12−import qualified Data.Text as T
13−
14−import Database.Bloodhound.Internal.Client
15−import Database.Bloodhound.Internal.Highlight (HitHighlight)
16−import Database.Bloodhound.Internal.Newtypes
17−import Database.Bloodhound.Internal.Query
18−import Database.Bloodhound.Internal.Sort
19−
20−type Aggregations = M.Map Text Aggregation
21−
22−emptyAggregations :: Aggregations
23−emptyAggregations = M.empty
24−
25−mkAggregations :: Text -> Aggregation -> Aggregations
26−mkAggregations name aggregation = M.insert name aggregation emptyAggregations
27−
28−data Aggregation = TermsAgg TermsAggregation
29− | CardinalityAgg CardinalityAggregation
30− | MaxAgg MaxAggregation
31− | DateHistogramAgg DateHistogramAggregation
32− | ValueCountAgg ValueCountAggregation
33− | FilterAgg FilterAggregation
34− | DateRangeAgg DateRangeAggregation
35− | MissingAgg MissingAggregation
36− | TopHitsAgg TopHitsAggregation
37− | StatsAgg StatisticsAggregation
38− deriving (Eq, Show)
39−
40−instance ToJSON Aggregation where
41− toJSON (TermsAgg (TermsAggregation term include exclude order minDocCount size shardSize collectMode executionHint termAggs)) =
42− omitNulls ["terms" .= omitNulls [ toJSON' term,
43− "include" .= include,
44− "exclude" .= exclude,
45− "order" .= order,
46− "min_doc_count" .= minDocCount,
47− "size" .= size,
48− "shard_size" .= shardSize,
49− "collect_mode" .= collectMode,
50− "execution_hint" .= executionHint
51− ],
52− "aggs" .= termAggs ]
53− where
54− toJSON' x = case x of { Left y -> "field" .= y; Right y -> "script" .= y }
55−
56− toJSON (CardinalityAgg (CardinalityAggregation field precisionThreshold)) =
57− object ["cardinality" .= omitNulls [ "field" .= field,
58− "precisionThreshold" .= precisionThreshold
59− ]
60− ]
61−
62− toJSON (MaxAgg (MaxAggregation field)) =
63− object ["max" .= omitNulls [ "field" .= field ]]
64−
65− toJSON (DateHistogramAgg
66− (DateHistogramAggregation field interval format
67− preZone postZone preOffset postOffset dateHistoAggs)) =
68− omitNulls ["date_histogram" .= omitNulls [ "field" .= field,
69− "interval" .= interval,
70− "format" .= format,
71− "pre_zone" .= preZone,
72− "post_zone" .= postZone,
73− "pre_offset" .= preOffset,
74− "post_offset" .= postOffset
75− ],
76− "aggs" .= dateHistoAggs ]
77− toJSON (ValueCountAgg a) = object ["value_count" .= v]
78− where v = case a of
79− (FieldValueCount (FieldName n)) ->
80− object ["field" .= n]
81− (ScriptValueCount s) ->
82− object ["script" .= s]
83− toJSON (FilterAgg (FilterAggregation filt ags)) =
84− omitNulls [ "filter" .= filt
85− , "aggs" .= ags]
86− toJSON (DateRangeAgg a) = object [ "date_range" .= a
87− ]
88− toJSON (MissingAgg (MissingAggregation{..})) =
89− object ["missing" .= object ["field" .= maField]]
90−
91− toJSON (TopHitsAgg (TopHitsAggregation mfrom msize msort)) =
92− omitNulls ["top_hits" .= omitNulls [ "size" .= msize
93− , "from" .= mfrom
94− , "sort" .= msort
95− ]
96− ]
97−
98− toJSON (StatsAgg (StatisticsAggregation typ field)) =
99− object [stType .= omitNulls [ "field" .= field ]]
100− where
101− stType | typ == Basic = "stats"
102− | otherwise = "extended_stats"
103−
104−data TopHitsAggregation = TopHitsAggregation
105− { taFrom :: Maybe From
106− , taSize :: Maybe Size
107− , taSort :: Maybe Sort
108− } deriving (Eq, Show)
109−
110−data MissingAggregation = MissingAggregation
111− { maField :: Text
112− } deriving (Eq, Show)
113−
114−data TermsAggregation = TermsAggregation
115− { term :: Either Text Text
116− , termInclude :: Maybe TermInclusion
117− , termExclude :: Maybe TermInclusion
118− , termOrder :: Maybe TermOrder
119− , termMinDocCount :: Maybe Int
120− , termSize :: Maybe Int
121− , termShardSize :: Maybe Int
122− , termCollectMode :: Maybe CollectionMode
123− , termExecutionHint :: Maybe ExecutionHint
124− , termAggs :: Maybe Aggregations
125− } deriving (Eq, Show)
126−
127−data CardinalityAggregation = CardinalityAggregation
128− { cardinalityField :: FieldName,
129− precisionThreshold :: Maybe Int
130− } deriving (Eq, Show)
131−
132−data MaxAggregation = MaxAggregation
133− { maxField :: FieldName
134− } deriving (Eq, Show)
135−
136−data DateHistogramAggregation = DateHistogramAggregation
137− { dateField :: FieldName
138− , dateInterval :: Interval
139− , dateFormat :: Maybe Text
140− -- pre and post deprecated in 1.5
141− , datePreZone :: Maybe Text
142− , datePostZone :: Maybe Text
143− , datePreOffset :: Maybe Text
144− , datePostOffset :: Maybe Text
145− , dateAggs :: Maybe Aggregations
146− } deriving (Eq, Show)
147−
148−data DateRangeAggregation = DateRangeAggregation
149− { draField :: FieldName
150− , draFormat :: Maybe Text
151− , draRanges :: NonEmpty DateRangeAggRange
152− } deriving (Eq, Show)
153−
154−instance ToJSON DateRangeAggregation where
155− toJSON DateRangeAggregation {..} =
156− omitNulls [ "field" .= draField
157− , "format" .= draFormat
158− , "ranges" .= toList draRanges
159− ]
160−
161−data DateRangeAggRange =
162− DateRangeFrom DateMathExpr
163− | DateRangeTo DateMathExpr
164− | DateRangeFromAndTo DateMathExpr DateMathExpr
165− deriving (Eq, Show)
166−
167−instance ToJSON DateRangeAggRange where
168− toJSON (DateRangeFrom e) = object [ "from" .= e ]
169− toJSON (DateRangeTo e) = object [ "to" .= e ]
170− toJSON (DateRangeFromAndTo f t) = object [ "from" .= f, "to" .= t ]
171−
172−-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html> for more information.
173−data ValueCountAggregation =
174− FieldValueCount FieldName
175− | ScriptValueCount Script
176− deriving (Eq, Show)
177−
178−-- | Single-bucket filter aggregations. See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html#search-aggregations-bucket-filter-aggregation> for more information.
179−data FilterAggregation = FilterAggregation
180− { faFilter :: Filter
181− , faAggs :: Maybe Aggregations }
182− deriving (Eq, Show)
183−
184−data StatisticsAggregation = StatisticsAggregation
185− { statsType :: StatsType
186− , statsField :: FieldName }
187− deriving (Eq, Show)
188−
189−data StatsType
190− = Basic
191− | Extended
192− deriving (Eq, Show)
193−
194−mkTermsAggregation :: Text -> TermsAggregation
195−mkTermsAggregation t =
196− TermsAggregation (Left t)
197− Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
198−
199−mkTermsScriptAggregation :: Text -> TermsAggregation
200−mkTermsScriptAggregation t = TermsAggregation (Right t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
201−
202−mkDateHistogram :: FieldName -> Interval -> DateHistogramAggregation
203−mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothing Nothing Nothing
204−
205−mkCardinalityAggregation :: FieldName -> CardinalityAggregation
206−mkCardinalityAggregation t = CardinalityAggregation t Nothing
207−
208−mkStatsAggregation :: FieldName -> StatisticsAggregation
209−mkStatsAggregation = StatisticsAggregation Basic
210−
211−mkExtendedStatsAggregation :: FieldName -> StatisticsAggregation
212−mkExtendedStatsAggregation = StatisticsAggregation Extended
213−
214−type AggregationResults = M.Map Text Value
215−
216−class BucketAggregation a where
217− key :: a -> BucketValue
218− docCount :: a -> Int
219− aggs :: a -> Maybe AggregationResults
220−
221−data Bucket a = Bucket
222− { buckets :: [a]
223− } deriving (Read, Show)
224−
225−instance (FromJSON a) => FromJSON (Bucket a) where
226− parseJSON (Object v) = Bucket <$>
227− v .: "buckets"
228− parseJSON _ = mempty
229−
230−data BucketValue = TextValue Text
231− | ScientificValue Scientific
232− | BoolValue Bool deriving (Read, Show)
233−
234−instance FromJSON BucketValue where
235− parseJSON (String t) = return $ TextValue t
236− parseJSON (Number s) = return $ ScientificValue s
237− parseJSON (Bool b) = return $ BoolValue b
238− parseJSON _ = mempty
239−
240−data TermInclusion = TermInclusion Text
241− | TermPattern Text Text deriving (Eq, Show)
242−
243−instance ToJSON TermInclusion where
244− toJSON (TermInclusion x) = toJSON x
245− toJSON (TermPattern pattern flags) =
246− omitNulls [ "pattern" .= pattern
247− , "flags" .= flags]
248−
249−data TermOrder = TermOrder
250− { termSortField :: Text
251− , termSortOrder :: SortOrder } deriving (Eq, Show)
252−
253−instance ToJSON TermOrder where
254− toJSON (TermOrder termSortField termSortOrder) =
255− object [termSortField .= termSortOrder]
256−
257−data CollectionMode = BreadthFirst
258− | DepthFirst deriving (Eq, Show)
259−
260−instance ToJSON CollectionMode where
261− toJSON BreadthFirst = "breadth_first"
262− toJSON DepthFirst = "depth_first"
263−
264−data ExecutionHint = Ordinals
265− | GlobalOrdinals
266− | GlobalOrdinalsHash
267− | GlobalOrdinalsLowCardinality
268− | Map deriving (Eq, Show)
269−
270−instance ToJSON ExecutionHint where
271− toJSON Ordinals = "ordinals"
272− toJSON GlobalOrdinals = "global_ordinals"
273− toJSON GlobalOrdinalsHash = "global_ordinals_hash"
274− toJSON GlobalOrdinalsLowCardinality = "global_ordinals_low_cardinality"
275− toJSON Map = "map"
276−
277−-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math> for more information.
278−data DateMathExpr =
279− DateMathExpr DateMathAnchor [DateMathModifier]
280− deriving (Eq, Show)
281−
282−instance ToJSON DateMathExpr where
283− toJSON (DateMathExpr a mods) = String (fmtA a <> mconcat (fmtMod <$> mods))
284− where fmtA DMNow = "now"
285− fmtA (DMDate date) = (T.pack $ showGregorian date) <> "||"
286− fmtMod (AddTime n u) = "+" <> showText n <> fmtU u
287− fmtMod (SubtractTime n u) = "-" <> showText n <> fmtU u
288− fmtMod (RoundDownTo u) = "/" <> fmtU u
289− fmtU DMYear = "y"
290− fmtU DMMonth = "M"
291− fmtU DMWeek = "w"
292− fmtU DMDay = "d"
293− fmtU DMHour = "h"
294− fmtU DMMinute = "m"
295− fmtU DMSecond = "s"
296−
297−-- | Starting point for a date range. This along with the 'DateMathModifiers' gets you the date ES will start from.
298−data DateMathAnchor =
299− DMNow
300− | DMDate Day
301− deriving (Eq, Show)
302−
303−data DateMathModifier =
304− AddTime Int DateMathUnit
305− | SubtractTime Int DateMathUnit
306− | RoundDownTo DateMathUnit
307− deriving (Eq, Show)
308−
309−data DateMathUnit =
310− DMYear
311− | DMMonth
312− | DMWeek
313− | DMDay
314− | DMHour
315− | DMMinute
316− | DMSecond
317− deriving (Eq, Show)
318−
319−data TermsResult = TermsResult
320− { termKey :: BucketValue
321− , termsDocCount :: Int
322− , termsAggs :: Maybe AggregationResults
323− } deriving (Read, Show)
324−
325−instance FromJSON TermsResult where
326− parseJSON (Object v) = TermsResult <$>
327− v .: "key" <*>
328− v .: "doc_count" <*>
329− (pure $ getNamedSubAgg v ["key", "doc_count"])
330− parseJSON _ = mempty
331−
332−instance BucketAggregation TermsResult where
333− key = termKey
334− docCount = termsDocCount
335− aggs = termsAggs
336−
337−data DateHistogramResult = DateHistogramResult
338− { dateKey :: Int
339− , dateKeyStr :: Maybe Text
340− , dateDocCount :: Int
341− , dateHistogramAggs :: Maybe AggregationResults
342− } deriving (Show)
343−
344−instance FromJSON DateHistogramResult where
345− parseJSON (Object v) = DateHistogramResult <$>
346− v .: "key" <*>
347− v .:? "key_as_string" <*>
348− v .: "doc_count" <*>
349− (pure $ getNamedSubAgg v [ "key"
350− , "doc_count"
351− , "key_as_string"
352− ]
353− )
354− parseJSON _ = mempty
355−
356−instance BucketAggregation DateHistogramResult where
357− key = TextValue . showText . dateKey
358− docCount = dateDocCount
359− aggs = dateHistogramAggs
360−
361−data DateRangeResult = DateRangeResult
362− { dateRangeKey :: Text
363− , dateRangeFrom :: Maybe UTCTime
364− , dateRangeFromAsString :: Maybe Text
365− , dateRangeTo :: Maybe UTCTime
366− , dateRangeToAsString :: Maybe Text
367− , dateRangeDocCount :: Int
368− , dateRangeAggs :: Maybe AggregationResults
369− } deriving (Eq, Show)
370−
371−instance FromJSON DateRangeResult where
372− parseJSON = withObject "DateRangeResult" parse
373− where parse v = DateRangeResult <$>
374− v .: "key" <*>
375− (fmap posixMS <$> v .:? "from") <*>
376− v .:? "from_as_string" <*>
377− (fmap posixMS <$> v .:? "to") <*>
378− v .:? "to_as_string" <*>
379− v .: "doc_count" <*>
380− (pure $ getNamedSubAgg v [ "key"
381− , "from"
382− , "from_as_string"
383− , "to"
384− , "to_as_string"
385− , "doc_count"
386− ]
387− )
388−
389−instance BucketAggregation DateRangeResult where
390− key = TextValue . dateRangeKey
391− docCount = dateRangeDocCount
392− aggs = dateRangeAggs
393−
394−toTerms :: Text -> AggregationResults -> Maybe (Bucket TermsResult)
395−toTerms = toAggResult
396−
397−toDateHistogram :: Text -> AggregationResults -> Maybe (Bucket DateHistogramResult)
398−toDateHistogram = toAggResult
399−
400−toMissing :: Text -> AggregationResults -> Maybe MissingResult
401−toMissing = toAggResult
402−
403−toTopHits :: (FromJSON a) => Text -> AggregationResults -> Maybe (TopHitResult a)
404−toTopHits = toAggResult
405−
406−toAggResult :: (FromJSON a) => Text -> AggregationResults -> Maybe a
407−toAggResult t a = M.lookup t a >>= deserialize
408− where deserialize = parseMaybe parseJSON
409−
410−-- Try to get an AggregationResults when we don't know the
411−-- field name. We filter out the known keys to try to minimize the noise.
412−getNamedSubAgg :: Object -> [Text] -> Maybe AggregationResults
413−getNamedSubAgg o knownKeys = maggRes
414− where unknownKeys = HM.filterWithKey (\k _ -> k `notElem` knownKeys) o
415− maggRes
416− | HM.null unknownKeys = Nothing
417− | otherwise = Just . M.fromList $ HM.toList unknownKeys
418−
419−data MissingResult = MissingResult
420− { missingDocCount :: Int
421− } deriving (Show)
422−
423−instance FromJSON MissingResult where
424− parseJSON = withObject "MissingResult" parse
425− where parse v = MissingResult <$> v .: "doc_count"
426−
427−data TopHitResult a = TopHitResult
428− { tarHits :: (SearchHits a)
429− } deriving Show
430−
431−instance (FromJSON a) => FromJSON (TopHitResult a) where
432− parseJSON (Object v) = TopHitResult <$>
433− v .: "hits"
434− parseJSON _ = fail "Failure in FromJSON (TopHitResult a)"
435−
436−data SearchHits a =
437− SearchHits { hitsTotal :: Int
438− , maxScore :: Score
439− , hits :: [Hit a] } deriving (Eq, Show)
440−
441−
442−instance (FromJSON a) => FromJSON (SearchHits a) where
443− parseJSON (Object v) = SearchHits <$>
444− v .: "total" <*>
445− v .: "max_score" <*>
446− v .: "hits"
447− parseJSON _ = empty
448−
449−instance Semigroup (SearchHits a) where
450− (SearchHits ta ma ha) <> (SearchHits tb mb hb) =
451− SearchHits (ta + tb) (max ma mb) (ha <> hb)
452−
453−instance Monoid (SearchHits a) where
454− mempty = SearchHits 0 Nothing mempty
455− mappend = (<>)
456−
457−type SearchAfterKey = [Aeson.Value]
458−
459−data Hit a =
460− Hit { hitIndex :: IndexName
461− , hitType :: MappingName
462− , hitDocId :: DocId
463− , hitScore :: Score
464− , hitSource :: Maybe a
465− , hitSort :: Maybe SearchAfterKey
466− , hitFields :: Maybe HitFields
467− , hitHighlight :: Maybe HitHighlight } deriving (Eq, Show)
468−
469−instance (FromJSON a) => FromJSON (Hit a) where
470− parseJSON (Object v) = Hit <$>
471− v .: "_index" <*>
472− v .: "_type" <*>
473− v .: "_id" <*>
474− v .: "_score" <*>
475− v .:? "_source" <*>
476− v .:? "sort" <*>
477− v .:? "fields" <*>
478− v .:? "highlight"
479− parseJSON _ = empty
src/Database/Bloodhound/Internal/Analysis.hsdeleted−379
@@ -1,379 +0,0 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−
4−module Database.Bloodhound.Internal.Analysis where
5−
6−import Bloodhound.Import
7−
8−import qualified Data.Map.Strict as M
9−import qualified Data.Text as T
10−
11−import Database.Bloodhound.Internal.Newtypes
12−import Database.Bloodhound.Internal.StringlyTyped
13−
14−data Analysis = Analysis
15− { analysisAnalyzer :: M.Map Text AnalyzerDefinition
16− , analysisTokenizer :: M.Map Text TokenizerDefinition
17− , analysisTokenFilter :: M.Map Text TokenFilterDefinition
18− , analysisCharFilter :: M.Map Text CharFilterDefinition
19− } deriving (Eq, Show)
20−
21−instance ToJSON Analysis where
22− toJSON (Analysis analyzer tokenizer tokenFilter charFilter) = object
23− [ "analyzer" .= analyzer
24− , "tokenizer" .= tokenizer
25− , "filter" .= tokenFilter
26− , "char_filter" .= charFilter
27− ]
28−
29−instance FromJSON Analysis where
30− parseJSON = withObject "Analysis" $ \m -> Analysis
31− <$> m .: "analyzer"
32− <*> m .:? "tokenizer" .!= M.empty
33− <*> m .:? "filter" .!= M.empty
34− <*> m .:? "char_filter" .!= M.empty
35−
36−newtype Tokenizer =
37− Tokenizer Text
38− deriving (Eq, Show, ToJSON, FromJSON)
39−
40−data AnalyzerDefinition = AnalyzerDefinition
41− { analyzerDefinitionTokenizer :: Maybe Tokenizer
42− , analyzerDefinitionFilter :: [TokenFilter]
43− , analyzerDefinitionCharFilter :: [CharFilter]
44− } deriving (Eq,Show)
45−
46−instance ToJSON AnalyzerDefinition where
47− toJSON (AnalyzerDefinition tokenizer tokenFilter charFilter) =
48− object $ catMaybes
49− [ fmap ("tokenizer" .=) tokenizer
50− , Just $ "filter" .= tokenFilter
51− , Just $ "char_filter" .= charFilter
52− ]
53−
54−instance FromJSON AnalyzerDefinition where
55− parseJSON = withObject "AnalyzerDefinition" $ \m -> AnalyzerDefinition
56− <$> m .:? "tokenizer"
57− <*> m .:? "filter" .!= []
58− <*> m .:? "char_filter" .!= []
59−
60−-- | Character filters are used to preprocess the stream of characters
61−-- before it is passed to the tokenizer.
62−data CharFilterDefinition
63− = CharFilterDefinitionMapping (M.Map Text Text)
64− | CharFilterDefinitionPatternReplace
65− { charFilterDefinitionPatternReplacePattern :: Text
66− , charFilterDefinitionPatternReplaceReplacement :: Text
67− , charFilterDefinitionPatternReplaceFlags :: Maybe Text
68− }
69− deriving (Eq, Show)
70−
71−instance ToJSON CharFilterDefinition where
72− toJSON (CharFilterDefinitionMapping ms) = object
73− [ "type" .= ("mapping" :: Text)
74− , "mappings" .= [a <> " => " <> b | (a, b) <- M.toList ms] ]
75− toJSON (CharFilterDefinitionPatternReplace pat repl flags) = object $
76− [ "type" .= ("pattern_replace" :: Text)
77− , "pattern" .= pat
78− , "replacement" .= repl
79− ] ++ maybe [] (\f -> ["flags" .= f]) flags
80−
81−instance FromJSON CharFilterDefinition where
82− parseJSON = withObject "CharFilterDefinition" $ \m -> do
83− t <- m .: "type"
84− case (t :: Text) of
85− "mapping" -> CharFilterDefinitionMapping . M.fromList <$> ms
86− where
87− ms = m .: "mappings" >>= mapM parseMapping
88− parseMapping kv = case T.splitOn "=>" kv of
89− (k:vs) -> pure (T.strip k, T.strip $ T.concat vs)
90− _ -> fail "mapping is not of the format key => value"
91− "pattern_replace" -> CharFilterDefinitionPatternReplace
92− <$> m .: "pattern" <*> m .: "replacement" <*> m .:? "flags"
93− _ -> fail ("unrecognized character filter type: " ++ T.unpack t)
94−
95−newtype TokenizerDefinition =
96− TokenizerDefinitionNgram Ngram
97− deriving (Eq,Show)
98−
99−instance ToJSON TokenizerDefinition where
100− toJSON x = case x of
101− TokenizerDefinitionNgram (Ngram minGram maxGram tokenChars) -> object
102− [ "type" .= ("ngram" :: Text)
103− , "min_gram" .= minGram
104− , "max_gram" .= maxGram
105− , "token_chars" .= tokenChars
106− ]
107−
108−instance FromJSON TokenizerDefinition where
109− parseJSON = withObject "TokenizerDefinition" $ \m -> do
110− typ <- m .: "type" :: Parser Text
111− case typ of
112− "ngram" -> fmap TokenizerDefinitionNgram $ Ngram
113− <$> fmap unStringlyTypedInt (m .: "min_gram")
114− <*> fmap unStringlyTypedInt (m .: "max_gram")
115− <*> m .: "token_chars"
116− _ -> fail "invalid TokenizerDefinition"
117−
118−data Ngram = Ngram
119− { ngramMinGram :: Int
120− , ngramMaxGram :: Int
121− , ngramTokenChars :: [TokenChar]
122− } deriving (Eq,Show)
123−
124−data TokenChar =
125− TokenLetter
126− | TokenDigit
127− | TokenWhitespace
128− | TokenPunctuation
129− | TokenSymbol
130− deriving (Eq,Show)
131−
132−instance ToJSON TokenChar where
133− toJSON t = String $ case t of
134− TokenLetter -> "letter"
135− TokenDigit -> "digit"
136− TokenWhitespace -> "whitespace"
137− TokenPunctuation -> "punctuation"
138− TokenSymbol -> "symbol"
139−
140−instance FromJSON TokenChar where
141− parseJSON = withText "TokenChar" $ \t -> case t of
142− "letter" -> return TokenLetter
143− "digit" -> return TokenDigit
144− "whitespace" -> return TokenWhitespace
145− "punctuation" -> return TokenPunctuation
146− "symbol" -> return TokenSymbol
147− _ -> fail "invalid TokenChar"
148−
149−-- | Token filters are used to create custom analyzers.
150−data TokenFilterDefinition
151− = TokenFilterDefinitionLowercase (Maybe Language)
152− | TokenFilterDefinitionUppercase (Maybe Language)
153− | TokenFilterDefinitionApostrophe
154− | TokenFilterDefinitionReverse
155− | TokenFilterDefinitionSnowball Language
156− | TokenFilterDefinitionShingle Shingle
157− | TokenFilterDefinitionStemmer Language
158− | TokenFilterDefinitionStop (Either Language [StopWord])
159− deriving (Eq, Show)
160−
161−instance ToJSON TokenFilterDefinition where
162− toJSON x = case x of
163− TokenFilterDefinitionLowercase mlang -> object $ catMaybes
164− [ Just $ "type" .= ("lowercase" :: Text)
165− , fmap (\lang -> "language" .= languageToText lang) mlang
166− ]
167− TokenFilterDefinitionUppercase mlang -> object $ catMaybes
168− [ Just $ "type" .= ("uppercase" :: Text)
169− , fmap (\lang -> "language" .= languageToText lang) mlang
170− ]
171− TokenFilterDefinitionApostrophe -> object
172− [ "type" .= ("apostrophe" :: Text)
173− ]
174− TokenFilterDefinitionReverse -> object
175− [ "type" .= ("reverse" :: Text)
176− ]
177− TokenFilterDefinitionSnowball lang -> object
178− [ "type" .= ("snowball" :: Text)
179− , "language" .= languageToText lang
180− ]
181− TokenFilterDefinitionShingle s -> object
182− [ "type" .= ("shingle" :: Text)
183− , "max_shingle_size" .= shingleMaxSize s
184− , "min_shingle_size" .= shingleMinSize s
185− , "output_unigrams" .= shingleOutputUnigrams s
186− , "output_unigrams_if_no_shingles" .= shingleOutputUnigramsIfNoShingles s
187− , "token_separator" .= shingleTokenSeparator s
188− , "filler_token" .= shingleFillerToken s
189− ]
190− TokenFilterDefinitionStemmer lang -> object
191− [ "type" .= ("stemmer" :: Text)
192− , "language" .= languageToText lang
193− ]
194− TokenFilterDefinitionStop stop -> object
195− [ "type" .= ("stop" :: Text)
196− , "stopwords" .= case stop of
197− Left lang -> String $ "_" <> languageToText lang <> "_"
198− Right stops -> toJSON stops
199− ]
200−
201−instance FromJSON TokenFilterDefinition where
202− parseJSON = withObject "TokenFilterDefinition" $ \m -> do
203− t <- m .: "type"
204− case (t :: Text) of
205− "reverse" -> return TokenFilterDefinitionReverse
206− "apostrophe" -> return TokenFilterDefinitionApostrophe
207− "lowercase" -> TokenFilterDefinitionLowercase
208− <$> m .:? "language"
209− "uppercase" -> TokenFilterDefinitionUppercase
210− <$> m .:? "language"
211− "snowball" -> TokenFilterDefinitionSnowball
212− <$> m .: "language"
213− "shingle" -> fmap TokenFilterDefinitionShingle $ Shingle
214− <$> (fmap.fmap) unStringlyTypedInt (m .:? "max_shingle_size") .!= 2
215− <*> (fmap.fmap) unStringlyTypedInt (m .:? "min_shingle_size") .!= 2
216− <*> (fmap.fmap) unStringlyTypedBool (m .:? "output_unigrams") .!= True
217− <*> (fmap.fmap) unStringlyTypedBool (m .:? "output_unigrams_if_no_shingles") .!= False
218− <*> m .:? "token_separator" .!= " "
219− <*> m .:? "filler_token" .!= "_"
220− "stemmer" -> TokenFilterDefinitionStemmer
221− <$> m .: "language"
222− "stop" -> do
223− stop <- m .: "stopwords"
224− stop' <- case stop of
225− String lang ->
226− fmap Left
227− . parseJSON
228− . String
229− . T.drop 1
230− . T.dropEnd 1 $ lang
231− _ -> Right <$> parseJSON stop
232− return (TokenFilterDefinitionStop stop')
233− _ -> fail ("unrecognized token filter type: " ++ T.unpack t)
234−
235−-- | The set of languages that can be passed to various analyzers,
236−-- filters, etc. in Elasticsearch. Most data types in this module
237−-- that have a 'Language' field are actually only actually to
238−-- handle a subset of these languages. Consult the official
239−-- Elasticsearch documentation to see what is actually supported.
240−data Language
241− = Arabic
242− | Armenian
243− | Basque
244− | Bengali
245− | Brazilian
246− | Bulgarian
247− | Catalan
248− | Cjk
249− | Czech
250− | Danish
251− | Dutch
252− | English
253− | Finnish
254− | French
255− | Galician
256− | German
257− | German2
258− | Greek
259− | Hindi
260− | Hungarian
261− | Indonesian
262− | Irish
263− | Italian
264− | Kp
265− | Latvian
266− | Lithuanian
267− | Lovins
268− | Norwegian
269− | Persian
270− | Porter
271− | Portuguese
272− | Romanian
273− | Russian
274− | Sorani
275− | Spanish
276− | Swedish
277− | Thai
278− | Turkish
279− deriving (Eq, Show)
280−
281−instance ToJSON Language where
282− toJSON = String . languageToText
283−
284−instance FromJSON Language where
285− parseJSON = withText "Language" $ \t -> case languageFromText t of
286− Nothing -> fail "not a supported Elasticsearch language"
287− Just lang -> return lang
288−
289−languageToText :: Language -> Text
290−languageToText x = case x of
291− Arabic -> "arabic"
292− Armenian -> "armenian"
293− Basque -> "basque"
294− Bengali -> "bengali"
295− Brazilian -> "brazilian"
296− Bulgarian -> "bulgarian"
297− Catalan -> "catalan"
298− Cjk -> "cjk"
299− Czech -> "czech"
300− Danish -> "danish"
301− Dutch -> "dutch"
302− English -> "english"
303− Finnish -> "finnish"
304− French -> "french"
305− Galician -> "galician"
306− German -> "german"
307− German2 -> "german2"
308− Greek -> "greek"
309− Hindi -> "hindi"
310− Hungarian -> "hungarian"
311− Indonesian -> "indonesian"
312− Irish -> "irish"
313− Italian -> "italian"
314− Kp -> "kp"
315− Latvian -> "latvian"
316− Lithuanian -> "lithuanian"
317− Lovins -> "lovins"
318− Norwegian -> "norwegian"
319− Persian -> "persian"
320− Porter -> "porter"
321− Portuguese -> "portuguese"
322− Romanian -> "romanian"
323− Russian -> "russian"
324− Sorani -> "sorani"
325− Spanish -> "spanish"
326− Swedish -> "swedish"
327− Thai -> "thai"
328− Turkish -> "turkish"
329−
330−languageFromText :: Text -> Maybe Language
331−languageFromText x = case x of
332− "arabic" -> Just Arabic
333− "armenian" -> Just Armenian
334− "basque" -> Just Basque
335− "bengali" -> Just Bengali
336− "brazilian" -> Just Brazilian
337− "bulgarian" -> Just Bulgarian
338− "catalan" -> Just Catalan
339− "cjk" -> Just Cjk
340− "czech" -> Just Czech
341− "danish" -> Just Danish
342− "dutch" -> Just Dutch
343− "english" -> Just English
344− "finnish" -> Just Finnish
345− "french" -> Just French
346− "galician" -> Just Galician
347− "german" -> Just German
348− "german2" -> Just German2
349− "greek" -> Just Greek
350− "hindi" -> Just Hindi
351− "hungarian" -> Just Hungarian
352− "indonesian" -> Just Indonesian
353− "irish" -> Just Irish
354− "italian" -> Just Italian
355− "kp" -> Just Kp
356− "latvian" -> Just Latvian
357− "lithuanian" -> Just Lithuanian
358− "lovins" -> Just Lovins
359− "norwegian" -> Just Norwegian
360− "persian" -> Just Persian
361− "porter" -> Just Porter
362− "portuguese" -> Just Portuguese
363− "romanian" -> Just Romanian
364− "russian" -> Just Russian
365− "sorani" -> Just Sorani
366− "spanish" -> Just Spanish
367− "swedish" -> Just Swedish
368− "thai" -> Just Thai
369− "turkish" -> Just Turkish
370− _ -> Nothing
371−
372−data Shingle = Shingle
373− { shingleMaxSize :: Int
374− , shingleMinSize :: Int
375− , shingleOutputUnigrams :: Bool
376− , shingleOutputUnigramsIfNoShingles :: Bool
377− , shingleTokenSeparator :: Text
378− , shingleFillerToken :: Text
379− } deriving (Eq, Show)
src/Database/Bloodhound/Internal/Client.hsdeleted−2431
@@ -1,2431 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE FlexibleInstances #-}
3−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4−{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE RecordWildCards #-}
7−{-# LANGUAGE UndecidableInstances #-}
8−
9−module Database.Bloodhound.Internal.Client where
10−
11−import Bloodhound.Import
12−
13−#if defined(MIN_VERSION_GLASGOW_HASKELL)
14−#if MIN_VERSION_GLASGOW_HASKELL(8,6,0,0)
15−import Control.Monad.Fail (MonadFail)
16−#endif
17−#endif
18−import qualified Data.Text as T
19−import qualified Data.Traversable as DT
20−import qualified Data.HashMap.Strict as HM
21−import qualified Data.SemVer as SemVer
22−import qualified Data.Vector as V
23−import GHC.Enum
24−import Network.HTTP.Client
25−import Text.Read (Read(..))
26−import qualified Text.Read as TR
27−
28−import Database.Bloodhound.Internal.Analysis
29−import Database.Bloodhound.Internal.Newtypes
30−import Database.Bloodhound.Internal.Query
31−import Database.Bloodhound.Internal.StringlyTyped
32−
33−{-| Common environment for Elasticsearch calls. Connections will be
34− pipelined according to the provided HTTP connection manager.
35−-}
36−data BHEnv = BHEnv { bhServer :: Server
37− , bhManager :: Manager
38− , bhRequestHook :: Request -> IO Request
39− -- ^ Low-level hook that is run before every request is sent. Used to implement custom authentication strategies. Defaults to 'return' with 'mkBHEnv'.
40− }
41−
42−instance (Functor m, Applicative m, MonadIO m) => MonadBH (ReaderT BHEnv m) where
43− getBHEnv = ask
44−
45−{-| 'Server' is used with the client functions to point at the ES instance
46−-}
47−newtype Server = Server Text deriving (Eq, Show, FromJSON)
48−
49−{-| All API calls to Elasticsearch operate within
50− MonadBH
51− . The idea is that it can be easily embedded in your
52− own monad transformer stack. A default instance for a ReaderT and
53− alias 'BH' is provided for the simple case.
54−-}
55−class (Functor m, Applicative m, MonadIO m) => MonadBH m where
56− getBHEnv :: m BHEnv
57−
58−-- | Create a 'BHEnv' with all optional fields defaulted. HTTP hook
59−-- will be a noop. You can use the exported fields to customize
60−-- it further, e.g.:
61−--
62−-- >> (mkBHEnv myServer myManager) { bhRequestHook = customHook }
63−mkBHEnv :: Server -> Manager -> BHEnv
64−mkBHEnv s m = BHEnv s m return
65−
66−newtype BH m a = BH {
67− unBH :: ReaderT BHEnv m a
68− } deriving ( Functor
69− , Applicative
70− , Monad
71− , MonadIO
72− , MonadState s
73− , MonadWriter w
74− , MonadError e
75− , Alternative
76− , MonadPlus
77− , MonadFix
78− , MonadThrow
79− , MonadCatch
80−#if defined(MIN_VERSION_GLASGOW_HASKELL)
81−#if MIN_VERSION_GLASGOW_HASKELL(8,6,0,0)
82− , MonadFail
83−#endif
84−#endif
85− , MonadMask)
86−
87−instance MonadTrans BH where
88− lift = BH . lift
89−
90−instance (MonadReader r m) => MonadReader r (BH m) where
91− ask = lift ask
92− local f (BH (ReaderT m)) = BH $ ReaderT $ \r ->
93− local f (m r)
94−
95−instance (Functor m, Applicative m, MonadIO m) => MonadBH (BH m) where
96− getBHEnv = BH getBHEnv
97−
98−runBH :: BHEnv -> BH m a -> m a
99−runBH e f = runReaderT (unBH f) e
100−
101−{-| 'Version' is embedded in 'Status' -}
102−data Version = Version { number :: VersionNumber
103− , build_hash :: BuildHash
104− , build_date :: UTCTime
105− , build_snapshot :: Bool
106− , lucene_version :: VersionNumber }
107− deriving (Eq, Show)
108−
109−-- | Traditional software versioning number
110−newtype VersionNumber = VersionNumber
111− { versionNumber :: SemVer.Version }
112− deriving (Eq, Ord, Show)
113−
114−{-| 'Status' is a data type for describing the JSON body returned by
115− Elasticsearch when you query its status. This was deprecated in 1.2.0.
116−
117− <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html#indices-status>
118−-}
119−
120−data Status = Status
121− { name :: Text
122− , cluster_name :: Text
123− , cluster_uuid :: Text
124− , version :: Version
125− , tagline :: Text }
126− deriving (Eq, Show)
127−
128−instance FromJSON Status where
129− parseJSON (Object v) = Status <$>
130− v .: "name" <*>
131− v .: "cluster_name" <*>
132− v .: "cluster_uuid" <*>
133− v .: "version" <*>
134− v .: "tagline"
135− parseJSON _ = empty
136−
137−{-| 'IndexSettings' is used to configure the shards and replicas when
138− you create an Elasticsearch Index.
139−
140− <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html>
141−-}
142−
143−data IndexSettings = IndexSettings
144− { indexShards :: ShardCount
145− , indexReplicas :: ReplicaCount }
146− deriving (Eq, Show)
147−
148−instance ToJSON IndexSettings where
149− toJSON (IndexSettings s r) = object ["settings" .=
150− object ["index" .=
151− object ["number_of_shards" .= s, "number_of_replicas" .= r]
152− ]
153− ]
154−
155−instance FromJSON IndexSettings where
156− parseJSON = withObject "IndexSettings" parse
157− where parse o = do s <- o .: "settings"
158− i <- s .: "index"
159− IndexSettings <$> i .: "number_of_shards"
160− <*> i .: "number_of_replicas"
161−
162−{-| 'defaultIndexSettings' is an 'IndexSettings' with 3 shards and
163− 2 replicas. -}
164−defaultIndexSettings :: IndexSettings
165−defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
166−-- defaultIndexSettings is exported by Database.Bloodhound as well
167−-- no trailing slashes in servers, library handles building the path.
168−
169−
170−{-| 'ForceMergeIndexSettings' is used to configure index optimization. See
171− <https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html>
172− for more info.
173−-}
174−data ForceMergeIndexSettings =
175− ForceMergeIndexSettings { maxNumSegments :: Maybe Int
176− -- ^ Number of segments to optimize to. 1 will fully optimize the index. If omitted, the default behavior is to only optimize if the server deems it necessary.
177− , onlyExpungeDeletes :: Bool
178− -- ^ Should the optimize process only expunge segments with deletes in them? If the purpose of the optimization is to free disk space, this should be set to True.
179− , flushAfterOptimize :: Bool
180− -- ^ Should a flush be performed after the optimize.
181− } deriving (Eq, Show)
182−
183−
184−{-| 'defaultForceMergeIndexSettings' implements the default settings that
185− Elasticsearch uses for index optimization. 'maxNumSegments' is Nothing,
186− 'onlyExpungeDeletes' is False, and flushAfterOptimize is True.
187−-}
188−defaultForceMergeIndexSettings :: ForceMergeIndexSettings
189−defaultForceMergeIndexSettings = ForceMergeIndexSettings Nothing False True
190−
191−{-| 'UpdatableIndexSetting' are settings which may be updated after an index is created.
192−
193− <https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html>
194−-}
195−data UpdatableIndexSetting = NumberOfReplicas ReplicaCount
196− -- ^ The number of replicas each shard has.
197− | AutoExpandReplicas ReplicaBounds
198− | BlocksReadOnly Bool
199− -- ^ Set to True to have the index read only. False to allow writes and metadata changes.
200− | BlocksRead Bool
201− -- ^ Set to True to disable read operations against the index.
202− | BlocksWrite Bool
203− -- ^ Set to True to disable write operations against the index.
204− | BlocksMetaData Bool
205− -- ^ Set to True to disable metadata operations against the index.
206− | RefreshInterval NominalDiffTime
207− -- ^ The async refresh interval of a shard
208− | IndexConcurrency Int
209− | FailOnMergeFailure Bool
210− | TranslogFlushThresholdOps Int
211− -- ^ When to flush on operations.
212− | TranslogFlushThresholdSize Bytes
213− -- ^ When to flush based on translog (bytes) size.
214− | TranslogFlushThresholdPeriod NominalDiffTime
215− -- ^ When to flush based on a period of not flushing.
216− | TranslogDisableFlush Bool
217− -- ^ Disables flushing. Note, should be set for a short interval and then enabled.
218− | CacheFilterMaxSize (Maybe Bytes)
219− -- ^ The maximum size of filter cache (per segment in shard).
220− | CacheFilterExpire (Maybe NominalDiffTime)
221− -- ^ The expire after access time for filter cache.
222− | GatewaySnapshotInterval NominalDiffTime
223− -- ^ The gateway snapshot interval (only applies to shared gateways).
224− | RoutingAllocationInclude (NonEmpty NodeAttrFilter)
225− -- ^ A node matching any rule will be allowed to host shards from the index.
226− | RoutingAllocationExclude (NonEmpty NodeAttrFilter)
227− -- ^ A node matching any rule will NOT be allowed to host shards from the index.
228− | RoutingAllocationRequire (NonEmpty NodeAttrFilter)
229− -- ^ Only nodes matching all rules will be allowed to host shards from the index.
230− | RoutingAllocationEnable AllocationPolicy
231− -- ^ Enables shard allocation for a specific index.
232− | RoutingAllocationShardsPerNode ShardCount
233− -- ^ Controls the total number of shards (replicas and primaries) allowed to be allocated on a single node.
234− | RecoveryInitialShards InitialShardCount
235− -- ^ When using local gateway a particular shard is recovered only if there can be allocated quorum shards in the cluster.
236− | GCDeletes NominalDiffTime
237− | TTLDisablePurge Bool
238− -- ^ Disables temporarily the purge of expired docs.
239− | TranslogFSType FSType
240− | CompressionSetting Compression
241− | IndexCompoundFormat CompoundFormat
242− | IndexCompoundOnFlush Bool
243− | WarmerEnabled Bool
244− | MappingTotalFieldsLimit Int
245− | AnalysisSetting Analysis
246− -- ^ Analysis is not a dynamic setting and can only be performed on a closed index.
247− deriving (Eq, Show)
248−
249−attrFilterJSON :: NonEmpty NodeAttrFilter -> Value
250−attrFilterJSON fs = object [ n .= T.intercalate "," (toList vs)
251− | NodeAttrFilter (NodeAttrName n) vs <- toList fs]
252−
253−parseAttrFilter :: Value -> Parser (NonEmpty NodeAttrFilter)
254−parseAttrFilter = withObject "NonEmpty NodeAttrFilter" parse
255− where parse o = case HM.toList o of
256− [] -> fail "Expected non-empty list of NodeAttrFilters"
257− x:xs -> DT.mapM (uncurry parse') (x :| xs)
258− parse' n = withText "Text" $ \t ->
259− case T.splitOn "," t of
260− fv:fvs -> return (NodeAttrFilter (NodeAttrName n) (fv :| fvs))
261− [] -> fail "Expected non-empty list of filter values"
262−
263−instance ToJSON UpdatableIndexSetting where
264− toJSON (NumberOfReplicas x) = oPath ("index" :| ["number_of_replicas"]) x
265− toJSON (AutoExpandReplicas x) = oPath ("index" :| ["auto_expand_replicas"]) x
266− toJSON (RefreshInterval x) = oPath ("index" :| ["refresh_interval"]) (NominalDiffTimeJSON x)
267− toJSON (IndexConcurrency x) = oPath ("index" :| ["concurrency"]) x
268− toJSON (FailOnMergeFailure x) = oPath ("index" :| ["fail_on_merge_failure"]) x
269− toJSON (TranslogFlushThresholdOps x) = oPath ("index" :| ["translog", "flush_threshold_ops"]) x
270− toJSON (TranslogFlushThresholdSize x) = oPath ("index" :| ["translog", "flush_threshold_size"]) x
271− toJSON (TranslogFlushThresholdPeriod x) = oPath ("index" :| ["translog", "flush_threshold_period"]) (NominalDiffTimeJSON x)
272− toJSON (TranslogDisableFlush x) = oPath ("index" :| ["translog", "disable_flush"]) x
273− toJSON (CacheFilterMaxSize x) = oPath ("index" :| ["cache", "filter", "max_size"]) x
274− toJSON (CacheFilterExpire x) = oPath ("index" :| ["cache", "filter", "expire"]) (NominalDiffTimeJSON <$> x)
275− toJSON (GatewaySnapshotInterval x) = oPath ("index" :| ["gateway", "snapshot_interval"]) (NominalDiffTimeJSON x)
276− toJSON (RoutingAllocationInclude fs) = oPath ("index" :| ["routing", "allocation", "include"]) (attrFilterJSON fs)
277− toJSON (RoutingAllocationExclude fs) = oPath ("index" :| ["routing", "allocation", "exclude"]) (attrFilterJSON fs)
278− toJSON (RoutingAllocationRequire fs) = oPath ("index" :| ["routing", "allocation", "require"]) (attrFilterJSON fs)
279− toJSON (RoutingAllocationEnable x) = oPath ("index" :| ["routing", "allocation", "enable"]) x
280− toJSON (RoutingAllocationShardsPerNode x) = oPath ("index" :| ["routing", "allocation", "total_shards_per_node"]) x
281− toJSON (RecoveryInitialShards x) = oPath ("index" :| ["recovery", "initial_shards"]) x
282− toJSON (GCDeletes x) = oPath ("index" :| ["gc_deletes"]) (NominalDiffTimeJSON x)
283− toJSON (TTLDisablePurge x) = oPath ("index" :| ["ttl", "disable_purge"]) x
284− toJSON (TranslogFSType x) = oPath ("index" :| ["translog", "fs", "type"]) x
285− toJSON (CompressionSetting x) = oPath ("index" :| ["codec"]) x
286− toJSON (IndexCompoundFormat x) = oPath ("index" :| ["compound_format"]) x
287− toJSON (IndexCompoundOnFlush x) = oPath ("index" :| ["compound_on_flush"]) x
288− toJSON (WarmerEnabled x) = oPath ("index" :| ["warmer", "enabled"]) x
289− toJSON (BlocksReadOnly x) = oPath ("blocks" :| ["read_only"]) x
290− toJSON (BlocksRead x) = oPath ("blocks" :| ["read"]) x
291− toJSON (BlocksWrite x) = oPath ("blocks" :| ["write"]) x
292− toJSON (BlocksMetaData x) = oPath ("blocks" :| ["metadata"]) x
293− toJSON (MappingTotalFieldsLimit x) = oPath ("index" :| ["mapping","total_fields","limit"]) x
294− toJSON (AnalysisSetting x) = oPath ("index" :| ["analysis"]) x
295−
296−instance FromJSON UpdatableIndexSetting where
297− parseJSON = withObject "UpdatableIndexSetting" parse
298− where parse o = numberOfReplicas `taggedAt` ["index", "number_of_replicas"]
299− <|> autoExpandReplicas `taggedAt` ["index", "auto_expand_replicas"]
300− <|> refreshInterval `taggedAt` ["index", "refresh_interval"]
301− <|> indexConcurrency `taggedAt` ["index", "concurrency"]
302− <|> failOnMergeFailure `taggedAt` ["index", "fail_on_merge_failure"]
303− <|> translogFlushThresholdOps `taggedAt` ["index", "translog", "flush_threshold_ops"]
304− <|> translogFlushThresholdSize `taggedAt` ["index", "translog", "flush_threshold_size"]
305− <|> translogFlushThresholdPeriod `taggedAt` ["index", "translog", "flush_threshold_period"]
306− <|> translogDisableFlush `taggedAt` ["index", "translog", "disable_flush"]
307− <|> cacheFilterMaxSize `taggedAt` ["index", "cache", "filter", "max_size"]
308− <|> cacheFilterExpire `taggedAt` ["index", "cache", "filter", "expire"]
309− <|> gatewaySnapshotInterval `taggedAt` ["index", "gateway", "snapshot_interval"]
310− <|> routingAllocationInclude `taggedAt` ["index", "routing", "allocation", "include"]
311− <|> routingAllocationExclude `taggedAt` ["index", "routing", "allocation", "exclude"]
312− <|> routingAllocationRequire `taggedAt` ["index", "routing", "allocation", "require"]
313− <|> routingAllocationEnable `taggedAt` ["index", "routing", "allocation", "enable"]
314− <|> routingAllocationShardsPerNode `taggedAt` ["index", "routing", "allocation", "total_shards_per_node"]
315− <|> recoveryInitialShards `taggedAt` ["index", "recovery", "initial_shards"]
316− <|> gcDeletes `taggedAt` ["index", "gc_deletes"]
317− <|> ttlDisablePurge `taggedAt` ["index", "ttl", "disable_purge"]
318− <|> translogFSType `taggedAt` ["index", "translog", "fs", "type"]
319− <|> compressionSetting `taggedAt` ["index", "codec"]
320− <|> compoundFormat `taggedAt` ["index", "compound_format"]
321− <|> compoundOnFlush `taggedAt` ["index", "compound_on_flush"]
322− <|> warmerEnabled `taggedAt` ["index", "warmer", "enabled"]
323− <|> blocksReadOnly `taggedAt` ["blocks", "read_only"]
324− <|> blocksRead `taggedAt` ["blocks", "read"]
325− <|> blocksWrite `taggedAt` ["blocks", "write"]
326− <|> blocksMetaData `taggedAt` ["blocks", "metadata"]
327− <|> mappingTotalFieldsLimit `taggedAt` ["index", "mapping", "total_fields", "limit"]
328− <|> analysisSetting `taggedAt` ["index", "analysis"]
329− where taggedAt f ks = taggedAt' f (Object o) ks
330− taggedAt' f v [] =
331− f =<< (parseJSON v <|> parseJSON (unStringlyTypeJSON v))
332− taggedAt' f v (k:ks) =
333− withObject "Object" (\o -> do v' <- o .: k
334− taggedAt' f v' ks) v
335− numberOfReplicas = pure . NumberOfReplicas
336− autoExpandReplicas = pure . AutoExpandReplicas
337− refreshInterval = pure . RefreshInterval . ndtJSON
338− indexConcurrency = pure . IndexConcurrency
339− failOnMergeFailure = pure . FailOnMergeFailure
340− translogFlushThresholdOps = pure . TranslogFlushThresholdOps
341− translogFlushThresholdSize = pure . TranslogFlushThresholdSize
342− translogFlushThresholdPeriod = pure . TranslogFlushThresholdPeriod . ndtJSON
343− translogDisableFlush = pure . TranslogDisableFlush
344− cacheFilterMaxSize = pure . CacheFilterMaxSize
345− cacheFilterExpire = pure . CacheFilterExpire . fmap ndtJSON
346− gatewaySnapshotInterval = pure . GatewaySnapshotInterval . ndtJSON
347− routingAllocationInclude = fmap RoutingAllocationInclude . parseAttrFilter
348− routingAllocationExclude = fmap RoutingAllocationExclude . parseAttrFilter
349− routingAllocationRequire = fmap RoutingAllocationRequire . parseAttrFilter
350− routingAllocationEnable = pure . RoutingAllocationEnable
351− routingAllocationShardsPerNode = pure . RoutingAllocationShardsPerNode
352− recoveryInitialShards = pure . RecoveryInitialShards
353− gcDeletes = pure . GCDeletes . ndtJSON
354− ttlDisablePurge = pure . TTLDisablePurge
355− translogFSType = pure . TranslogFSType
356− compressionSetting = pure . CompressionSetting
357− compoundFormat = pure . IndexCompoundFormat
358− compoundOnFlush = pure . IndexCompoundOnFlush
359− warmerEnabled = pure . WarmerEnabled
360− blocksReadOnly = pure . BlocksReadOnly
361− blocksRead = pure . BlocksRead
362− blocksWrite = pure . BlocksWrite
363− blocksMetaData = pure . BlocksMetaData
364− mappingTotalFieldsLimit = pure . MappingTotalFieldsLimit
365− analysisSetting = pure . AnalysisSetting
366−
367−data ReplicaBounds = ReplicasBounded Int Int
368− | ReplicasLowerBounded Int
369− | ReplicasUnbounded
370− deriving (Eq, Show)
371−
372−
373−instance ToJSON ReplicaBounds where
374− toJSON (ReplicasBounded a b) = String (showText a <> "-" <> showText b)
375− toJSON (ReplicasLowerBounded a) = String (showText a <> "-all")
376− toJSON ReplicasUnbounded = Bool False
377−
378−instance FromJSON ReplicaBounds where
379− parseJSON v = withText "ReplicaBounds" parseText v
380− <|> withBool "ReplicaBounds" parseBool v
381− where parseText t = case T.splitOn "-" t of
382− [a, "all"] -> ReplicasLowerBounded <$> parseReadText a
383− [a, b] -> ReplicasBounded <$> parseReadText a
384− <*> parseReadText b
385− _ -> fail ("Could not parse ReplicaBounds: " <> show t)
386− parseBool False = pure ReplicasUnbounded
387− parseBool _ = fail "ReplicasUnbounded cannot be represented with True"
388−
389−data Compression
390− = CompressionDefault
391− -- ^ Compress with LZ4
392− | CompressionBest
393− -- ^ Compress with DEFLATE. Elastic
394− -- <https://www.elastic.co/blog/elasticsearch-storage-the-true-story-2.0 blogs>
395− -- that this can reduce disk use by 15%-25%.
396− deriving (Eq,Show)
397−
398−instance ToJSON Compression where
399− toJSON x = case x of
400− CompressionDefault -> toJSON ("default" :: Text)
401− CompressionBest -> toJSON ("best_compression" :: Text)
402−
403−instance FromJSON Compression where
404− parseJSON = withText "Compression" $ \t -> case t of
405− "default" -> return CompressionDefault
406− "best_compression" -> return CompressionBest
407− _ -> fail "invalid compression codec"
408−
409−-- | A measure of bytes used for various configurations. You may want
410−-- to use smart constructors like 'gigabytes' for larger values.
411−--
412−-- >>> gigabytes 9
413−-- Bytes 9000000000
414−--
415−-- >>> megabytes 9
416−-- Bytes 9000000
417−--
418−-- >>> kilobytes 9
419−-- Bytes 9000
420−newtype Bytes =
421− Bytes Int
422− deriving (Eq, Show, Ord, ToJSON, FromJSON)
423−
424−gigabytes :: Int -> Bytes
425−gigabytes n = megabytes (1000 * n)
426−
427−
428−megabytes :: Int -> Bytes
429−megabytes n = kilobytes (1000 * n)
430−
431−
432−kilobytes :: Int -> Bytes
433−kilobytes n = Bytes (1000 * n)
434−
435−
436−data FSType = FSSimple
437− | FSBuffered deriving (Eq, Show)
438−
439−instance ToJSON FSType where
440− toJSON FSSimple = "simple"
441− toJSON FSBuffered = "buffered"
442−
443−instance FromJSON FSType where
444− parseJSON = withText "FSType" parse
445− where parse "simple" = pure FSSimple
446− parse "buffered" = pure FSBuffered
447− parse t = fail ("Invalid FSType: " <> show t)
448−
449−data InitialShardCount = QuorumShards
450− | QuorumMinus1Shards
451− | FullShards
452− | FullMinus1Shards
453− | ExplicitShards Int
454− deriving (Eq, Show)
455−
456−instance FromJSON InitialShardCount where
457− parseJSON v = withText "InitialShardCount" parseText v
458− <|> ExplicitShards <$> parseJSON v
459− where parseText "quorum" = pure QuorumShards
460− parseText "quorum-1" = pure QuorumMinus1Shards
461− parseText "full" = pure FullShards
462− parseText "full-1" = pure FullMinus1Shards
463− parseText _ = mzero
464−
465−instance ToJSON InitialShardCount where
466− toJSON QuorumShards = String "quorum"
467− toJSON QuorumMinus1Shards = String "quorum-1"
468− toJSON FullShards = String "full"
469− toJSON FullMinus1Shards = String "full-1"
470− toJSON (ExplicitShards x) = toJSON x
471−
472−data NodeAttrFilter = NodeAttrFilter
473− { nodeAttrFilterName :: NodeAttrName
474− , nodeAttrFilterValues :: NonEmpty Text }
475− deriving (Eq, Ord, Show)
476−
477−newtype NodeAttrName = NodeAttrName Text deriving (Eq, Ord, Show)
478−
479−data CompoundFormat = CompoundFileFormat Bool
480− | MergeSegmentVsTotalIndex Double
481− -- ^ percentage between 0 and 1 where 0 is false, 1 is true
482− deriving (Eq, Show)
483−
484−instance ToJSON CompoundFormat where
485− toJSON (CompoundFileFormat x) = Bool x
486− toJSON (MergeSegmentVsTotalIndex x) = toJSON x
487−
488−instance FromJSON CompoundFormat where
489− parseJSON v = CompoundFileFormat <$> parseJSON v
490− <|> MergeSegmentVsTotalIndex <$> parseJSON v
491−
492−newtype NominalDiffTimeJSON =
493− NominalDiffTimeJSON { ndtJSON :: NominalDiffTime }
494−
495−instance ToJSON NominalDiffTimeJSON where
496− toJSON (NominalDiffTimeJSON t) = String (showText (round t :: Integer) <> "s")
497−
498−instance FromJSON NominalDiffTimeJSON where
499− parseJSON = withText "NominalDiffTime" parse
500− where parse t = case T.takeEnd 1 t of
501− "s" -> NominalDiffTimeJSON . fromInteger <$> parseReadText (T.dropEnd 1 t)
502− _ -> fail "Invalid or missing NominalDiffTime unit (expected s)"
503−
504−data IndexSettingsSummary = IndexSettingsSummary
505− { sSummaryIndexName :: IndexName
506− , sSummaryFixedSettings :: IndexSettings
507− , sSummaryUpdateable :: [UpdatableIndexSetting]}
508− deriving (Eq, Show)
509−
510−parseSettings :: Object -> Parser [UpdatableIndexSetting]
511−parseSettings o = do
512− o' <- o .: "index"
513− -- slice the index object into singleton hashmaps and try to parse each
514− parses <- forM (HM.toList o') $ \(k, v) -> do
515− -- blocks are now nested into the "index" key, which is not how they're serialized
516− let atRoot = Object (HM.singleton k v)
517− let atIndex = Object (HM.singleton "index" atRoot)
518− optional (parseJSON atRoot <|> parseJSON atIndex)
519− return (catMaybes parses)
520−
521−instance FromJSON IndexSettingsSummary where
522− parseJSON = withObject "IndexSettingsSummary" parse
523− where parse o = case HM.toList o of
524− [(ixn, v@(Object o'))] -> IndexSettingsSummary (IndexName ixn)
525− <$> parseJSON v
526− <*> (fmap (filter (not . redundant)) . parseSettings =<< o' .: "settings")
527− _ -> fail "Expected single-key object with index name"
528− redundant (NumberOfReplicas _) = True
529− redundant _ = False
530−
531−{-| 'Reply' and 'Method' are type synonyms from 'Network.HTTP.Types.Method.Method' -}
532−type Reply = Network.HTTP.Client.Response LByteString
533−
534−{-| 'OpenCloseIndex' is a sum type for opening and closing indices.
535−
536− <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html>
537−-}
538−data OpenCloseIndex = OpenIndex | CloseIndex deriving (Eq, Show)
539−
540−data FieldType = GeoPointType
541− | GeoShapeType
542− | FloatType
543− | IntegerType
544− | LongType
545− | ShortType
546− | ByteType deriving (Eq, Show)
547−
548−newtype FieldDefinition = FieldDefinition
549− { fieldType :: FieldType
550− } deriving (Eq, Show)
551−
552−{-| An 'IndexTemplate' defines a template that will automatically be
553− applied to new indices created. The templates include both
554− 'IndexSettings' and mappings, and a simple 'TemplatePattern' that
555− controls if the template will be applied to the index created.
556− Specify mappings as follows: @[toJSON TweetMapping, ...]@
557−
558− https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html
559−-}
560−data IndexTemplate =
561− IndexTemplate { templatePattern :: TemplatePattern
562− , templateSettings :: Maybe IndexSettings
563− , templateMappings :: [Value]
564− }
565−
566−instance ToJSON IndexTemplate where
567− toJSON (IndexTemplate p s m) = merge
568− (object [ "template" .= p
569− , "mappings" .= foldl' merge (object []) m
570− ])
571− (toJSON s)
572− where
573− merge (Object o1) (Object o2) = toJSON $ HM.union o1 o2
574− merge o Null = o
575− merge _ _ = undefined
576−
577−data MappingField =
578− MappingField { mappingFieldName :: FieldName
579− , fieldDefinition :: FieldDefinition }
580− deriving (Eq, Show)
581−
582−{-| Support for type reification of 'Mapping's is currently incomplete, for
583− now the mapping API verbiage expects a 'ToJSON'able blob.
584−
585− Indexes have mappings, mappings are schemas for the documents contained
586− in the index. I'd recommend having only one mapping per index, always
587− having a mapping, and keeping different kinds of documents separated
588− if possible.
589−-}
590−data Mapping =
591− Mapping { typeName :: TypeName
592− , mappingFields :: [MappingField] }
593− deriving (Eq, Show)
594−
595−data AllocationPolicy = AllocAll
596− -- ^ Allows shard allocation for all shards.
597− | AllocPrimaries
598− -- ^ Allows shard allocation only for primary shards.
599− | AllocNewPrimaries
600− -- ^ Allows shard allocation only for primary shards for new indices.
601− | AllocNone
602− -- ^ No shard allocation is allowed
603− deriving (Eq, Show)
604−
605−instance ToJSON AllocationPolicy where
606− toJSON AllocAll = String "all"
607− toJSON AllocPrimaries = String "primaries"
608− toJSON AllocNewPrimaries = String "new_primaries"
609− toJSON AllocNone = String "none"
610−
611−instance FromJSON AllocationPolicy where
612− parseJSON = withText "AllocationPolicy" parse
613− where parse "all" = pure AllocAll
614− parse "primaries" = pure AllocPrimaries
615− parse "new_primaries" = pure AllocNewPrimaries
616− parse "none" = pure AllocNone
617− parse t = fail ("Invlaid AllocationPolicy: " <> show t)
618−
619−{-| 'BulkOperation' is a sum type for expressing the four kinds of bulk
620− operation index, create, delete, and update. 'BulkIndex' behaves like an
621− "upsert", 'BulkCreate' will fail if a document already exists at the DocId.
622− Consult the <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk Bulk API documentation>
623− for further explanation.
624− Warning: Bulk operations suffixed with @Auto@ rely on Elasticsearch to
625− generate the id. Often, people use auto-generated identifiers when
626− Elasticsearch is the only place that their data is stored. Do not let
627− Elasticsearch be the only place your data is stored. It does not guarantee
628− durability, and it may silently discard data.
629− This <https://github.com/elastic/elasticsearch/issues/10708 issue> is
630− discussed further on github.
631−-}
632−data BulkOperation =
633− BulkIndex IndexName MappingName DocId Value
634− -- ^ Create the document, replacing it if it already exists.
635− | BulkIndexAuto IndexName MappingName Value
636− -- ^ Create a document with an autogenerated id.
637− | BulkIndexEncodingAuto IndexName MappingName Encoding
638− -- ^ Create a document with an autogenerated id. Use fast JSON encoding.
639− | BulkCreate IndexName MappingName DocId Value
640− -- ^ Create a document, failing if it already exists.
641− | BulkCreateEncoding IndexName MappingName DocId Encoding
642− -- ^ Create a document, failing if it already exists. Use fast JSON encoding.
643− | BulkDelete IndexName MappingName DocId
644− -- ^ Delete the document
645− | BulkUpdate IndexName MappingName DocId Value
646− -- ^ Update the document, merging the new value with the existing one.
647− deriving (Eq, Show)
648−
649−{-| 'EsResult' describes the standard wrapper JSON document that you see in
650− successful Elasticsearch lookups or lookups that couldn't find the document.
651−-}
652−data EsResult a = EsResult { _index :: Text
653− , _type :: Text
654− , _id :: Text
655− , foundResult :: Maybe (EsResultFound a)} deriving (Eq, Show)
656−
657−{-| 'EsResultFound' contains the document and its metadata inside of an
658− 'EsResult' when the document was successfully found.
659−-}
660−data EsResultFound a =
661− EsResultFound { _version :: DocVersion
662− , _source :: a }
663− deriving (Eq, Show)
664−
665−instance (FromJSON a) => FromJSON (EsResult a) where
666− parseJSON jsonVal@(Object v) = do
667− found <- v .:? "found" .!= False
668− fr <- if found
669− then parseJSON jsonVal
670− else return Nothing
671− EsResult <$> v .: "_index" <*>
672− v .: "_type" <*>
673− v .: "_id" <*>
674− pure fr
675− parseJSON _ = empty
676−
677−instance (FromJSON a) => FromJSON (EsResultFound a) where
678− parseJSON (Object v) = EsResultFound <$>
679− v .: "_version" <*>
680− v .: "_source"
681− parseJSON _ = empty
682−
683−{-| 'EsError' is the generic type that will be returned when there was a
684− problem. If you can't parse the expected response, its a good idea to
685− try parsing this.
686−-}
687−data EsError =
688− EsError { errorStatus :: Int
689− , errorMessage :: Text }
690− deriving (Eq, Show)
691−
692−instance FromJSON EsError where
693− parseJSON (Object v) = EsError <$>
694− v .: "status" <*>
695− (v .: "error" <|> (v .: "error" >>= (.: "reason")))
696− parseJSON _ = empty
697−
698−{-| 'EsProtocolException' will be thrown if Bloodhound cannot parse a response
699−returned by the Elasticsearch server. If you encounter this error, please
700−verify that your domain data types and FromJSON instances are working properly
701−(for example, the 'a' of '[Hit a]' in 'SearchResult.searchHits.hits'). If you're
702−sure that your mappings are correct, then this error may be an indication of an
703−incompatibility between Bloodhound and Elasticsearch. Please open a bug report
704−and be sure to include the exception body.
705−-}
706−data EsProtocolException = EsProtocolException
707− { esProtoExMessage :: !Text
708− , esProtoExBody :: !LByteString
709− } deriving (Eq, Show)
710−
711−instance Exception EsProtocolException
712−
713−data IndexAlias = IndexAlias { srcIndex :: IndexName
714− , indexAlias :: IndexAliasName } deriving (Eq, Show)
715−
716−data IndexAliasAction =
717− AddAlias IndexAlias IndexAliasCreate
718− | RemoveAlias IndexAlias
719− deriving (Eq, Show)
720−
721−data IndexAliasCreate =
722− IndexAliasCreate { aliasCreateRouting :: Maybe AliasRouting
723− , aliasCreateFilter :: Maybe Filter}
724− deriving (Eq, Show)
725−
726−data AliasRouting =
727− AllAliasRouting RoutingValue
728− | GranularAliasRouting (Maybe SearchAliasRouting) (Maybe IndexAliasRouting)
729− deriving (Eq, Show)
730−
731−newtype SearchAliasRouting =
732− SearchAliasRouting (NonEmpty RoutingValue)
733− deriving (Eq, Show)
734−
735−instance ToJSON SearchAliasRouting where
736− toJSON (SearchAliasRouting rvs) = toJSON (T.intercalate "," (routingValue <$> toList rvs))
737−
738−instance FromJSON SearchAliasRouting where
739− parseJSON = withText "SearchAliasRouting" parse
740− where parse t = SearchAliasRouting <$> parseNEJSON (String <$> T.splitOn "," t)
741−
742−newtype IndexAliasRouting =
743− IndexAliasRouting RoutingValue
744− deriving (Eq, Show, ToJSON, FromJSON)
745−
746−newtype RoutingValue =
747− RoutingValue { routingValue :: Text }
748− deriving (Eq, Show, ToJSON, FromJSON)
749−
750−newtype IndexAliasesSummary =
751− IndexAliasesSummary { indexAliasesSummary :: [IndexAliasSummary] }
752− deriving (Eq, Show)
753−
754−instance FromJSON IndexAliasesSummary where
755− parseJSON = withObject "IndexAliasesSummary" parse
756− where parse o = IndexAliasesSummary . mconcat <$> mapM (uncurry go) (HM.toList o)
757− go ixn = withObject "index aliases" $ \ia -> do
758− aliases <- ia .:? "aliases" .!= mempty
759− forM (HM.toList aliases) $ \(aName, v) -> do
760− let indexAlias = IndexAlias (IndexName ixn) (IndexAliasName (IndexName aName))
761− IndexAliasSummary indexAlias <$> parseJSON v
762−
763−
764−instance ToJSON IndexAliasAction where
765− toJSON (AddAlias ia opts) = object ["add" .= (iaObj <> optsObj)]
766− where Object iaObj = toJSON ia
767− Object optsObj = toJSON opts
768− toJSON (RemoveAlias ia) = object ["remove" .= iaObj]
769− where Object iaObj = toJSON ia
770−
771−instance ToJSON IndexAlias where
772− toJSON IndexAlias {..} = object ["index" .= srcIndex
773− , "alias" .= indexAlias
774− ]
775−
776−instance ToJSON IndexAliasCreate where
777− toJSON IndexAliasCreate {..} = Object (filterObj <> routingObj)
778− where filterObj = maybe mempty (HM.singleton "filter" . toJSON) aliasCreateFilter
779− Object routingObj = maybe (Object mempty) toJSON aliasCreateRouting
780−
781−instance ToJSON AliasRouting where
782− toJSON (AllAliasRouting v) = object ["routing" .= v]
783− toJSON (GranularAliasRouting srch idx) = object (catMaybes prs)
784− where prs = [("search_routing" .=) <$> srch
785− ,("index_routing" .=) <$> idx]
786−
787−instance FromJSON AliasRouting where
788− parseJSON = withObject "AliasRouting" parse
789− where parse o = parseAll o <|> parseGranular o
790− parseAll o = AllAliasRouting <$> o .: "routing"
791− parseGranular o = do
792− sr <- o .:? "search_routing"
793− ir <- o .:? "index_routing"
794− if isNothing sr && isNothing ir
795− then fail "Both search_routing and index_routing can't be blank"
796− else return (GranularAliasRouting sr ir)
797−
798−instance FromJSON IndexAliasCreate where
799− parseJSON v = withObject "IndexAliasCreate" parse v
800− where parse o = IndexAliasCreate <$> optional (parseJSON v)
801− <*> o .:? "filter"
802−
803−{-| 'IndexAliasSummary' is a summary of an index alias configured for a server. -}
804−data IndexAliasSummary = IndexAliasSummary
805− { indexAliasSummaryAlias :: IndexAlias
806− , indexAliasSummaryCreate :: IndexAliasCreate }
807− deriving (Eq, Show)
808−
809−{-| 'DocVersion' is an integer version number for a document between 1
810−and 9.2e+18 used for <<https://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html optimistic concurrency control>>.
811−-}
812−newtype DocVersion = DocVersion {
813− docVersionNumber :: Int
814− } deriving (Eq, Show, Ord, ToJSON)
815−
816−-- | Smart constructor for in-range doc version
817−mkDocVersion :: Int -> Maybe DocVersion
818−mkDocVersion i
819− | i >= docVersionNumber minBound
820− && i <= docVersionNumber maxBound =
821− Just $ DocVersion i
822− | otherwise = Nothing
823−
824−instance Bounded DocVersion where
825− minBound = DocVersion 1
826− maxBound = DocVersion 9200000000000000000 -- 9.2e+18
827−
828−instance Enum DocVersion where
829− succ x
830− | x /= maxBound = DocVersion (succ $ docVersionNumber x)
831− | otherwise = succError "DocVersion"
832− pred x
833− | x /= minBound = DocVersion (pred $ docVersionNumber x)
834− | otherwise = predError "DocVersion"
835− toEnum i =
836− fromMaybe (error $ show i ++ " out of DocVersion range") $ mkDocVersion i
837− fromEnum = docVersionNumber
838− enumFrom = boundedEnumFrom
839− enumFromThen = boundedEnumFromThen
840−
841−instance FromJSON DocVersion where
842− parseJSON v = do
843− i <- parseJSON v
844− maybe (fail "DocVersion out of range") return $ mkDocVersion i
845−
846−{-| 'ExternalDocVersion' is a convenience wrapper if your code uses its
847−own version numbers instead of ones from ES.
848−-}
849−newtype ExternalDocVersion = ExternalDocVersion DocVersion
850− deriving (Eq, Show, Ord, Bounded, Enum, ToJSON)
851−
852−{-| 'VersionControl' is specified when indexing documents as a
853−optimistic concurrency control.
854−-}
855−data VersionControl = NoVersionControl
856− -- ^ Don't send a version. This is a pure overwrite.
857− | InternalVersion DocVersion
858− -- ^ Use the default ES versioning scheme. Only
859− -- index the document if the version is the same
860− -- as the one specified. Only applicable to
861− -- updates, as you should be getting Version from
862− -- a search result.
863− | ExternalGT ExternalDocVersion
864− -- ^ Use your own version numbering. Only index
865− -- the document if the version is strictly higher
866− -- OR the document doesn't exist. The given
867− -- version will be used as the new version number
868− -- for the stored document. N.B. All updates must
869− -- increment this number, meaning there is some
870− -- global, external ordering of updates.
871− | ExternalGTE ExternalDocVersion
872− -- ^ Use your own version numbering. Only index
873− -- the document if the version is equal or higher
874− -- than the stored version. Will succeed if there
875− -- is no existing document. The given version will
876− -- be used as the new version number for the
877− -- stored document. Use with care, as this could
878− -- result in data loss.
879− | ForceVersion ExternalDocVersion
880− -- ^ The document will always be indexed and the
881− -- given version will be the new version. This is
882− -- typically used for correcting errors. Use with
883− -- care, as this could result in data loss.
884− deriving (Eq, Show, Ord)
885−
886−{-| 'DocumentParent' is used to specify a parent document.
887−-}
888−newtype DocumentParent = DocumentParent DocId
889− deriving (Eq, Show)
890−
891−{-| 'IndexDocumentSettings' are special settings supplied when indexing
892−a document. For the best backwards compatiblity when new fields are
893−added, you should probably prefer to start with 'defaultIndexDocumentSettings'
894−-}
895−data IndexDocumentSettings =
896− IndexDocumentSettings { idsVersionControl :: VersionControl
897− , idsParent :: Maybe DocumentParent
898− } deriving (Eq, Show)
899−
900−{-| Reasonable default settings. Chooses no version control and no parent.
901−-}
902−defaultIndexDocumentSettings :: IndexDocumentSettings
903−defaultIndexDocumentSettings = IndexDocumentSettings NoVersionControl Nothing
904−
905−{-| 'IndexSelection' is used for APIs which take a single index, a list of
906− indexes, or the special @_all@ index.
907−-}
908−--TODO: this does not fully support <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/multi-index.html multi-index syntax>. It wouldn't be too hard to implement but you'd have to add the optional parameters (ignore_unavailable, allow_no_indices, expand_wildcards) to any APIs using it. Also would be a breaking API.
909−data IndexSelection =
910− IndexList (NonEmpty IndexName)
911− | AllIndexes
912− deriving (Eq, Show)
913−
914−{-| 'NodeSelection' is used for most cluster APIs. See <https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html#cluster-nodes here> for more details.
915−-}
916−data NodeSelection =
917− LocalNode
918− -- ^ Whatever node receives this request
919− | NodeList (NonEmpty NodeSelector)
920− | AllNodes
921− deriving (Eq, Show)
922−
923−
924−-- | An exact match or pattern to identify a node. Note that All of
925−-- these options support wildcarding, so your node name, server, attr
926−-- name can all contain * characters to be a fuzzy match.
927−data NodeSelector =
928− NodeByName NodeName
929− | NodeByFullNodeId FullNodeId
930− | NodeByHost Server
931− -- ^ e.g. 10.0.0.1 or even 10.0.0.*
932− | NodeByAttribute NodeAttrName Text
933− -- ^ NodeAttrName can be a pattern, e.g. rack*. The value can too.
934− deriving (Eq, Show)
935−
936−{-| 'TemplateName' is used to describe which template to query/create/delete
937−-}
938−newtype TemplateName = TemplateName Text deriving (Eq, Show, ToJSON, FromJSON)
939−
940−{-| 'TemplatePattern' represents a pattern which is matched against index names
941−-}
942−newtype TemplatePattern = TemplatePattern Text deriving (Eq, Show, ToJSON, FromJSON)
943−
944−-- | Username type used for HTTP Basic authentication. See 'basicAuthHook'.
945−newtype EsUsername = EsUsername { esUsername :: Text } deriving (Read, Show, Eq)
946−
947−-- | Password type used for HTTP Basic authentication. See 'basicAuthHook'.
948−newtype EsPassword = EsPassword { esPassword :: Text } deriving (Read, Show, Eq)
949−
950−
951−data SnapshotRepoSelection =
952− SnapshotRepoList (NonEmpty SnapshotRepoPattern)
953− | AllSnapshotRepos
954− deriving (Eq, Show)
955−
956−
957−-- | Either specifies an exact repo name or one with globs in it,
958−-- e.g. @RepoPattern "foo*"@ __NOTE__: Patterns are not supported on ES < 1.7
959−data SnapshotRepoPattern =
960− ExactRepo SnapshotRepoName
961− | RepoPattern Text
962− deriving (Eq, Show)
963−
964−-- | The unique name of a snapshot repository.
965−newtype SnapshotRepoName =
966− SnapshotRepoName { snapshotRepoName :: Text }
967− deriving (Eq, Ord, Show, ToJSON, FromJSON)
968−
969−
970−-- | A generic representation of a snapshot repo. This is what gets
971−-- sent to and parsed from the server. For repo types enabled by
972−-- plugins that aren't exported by this library, consider making a
973−-- custom type which implements 'SnapshotRepo'. If it is a common repo
974−-- type, consider submitting a pull request to have it included in the
975−-- library proper
976−data GenericSnapshotRepo = GenericSnapshotRepo {
977− gSnapshotRepoName :: SnapshotRepoName
978− , gSnapshotRepoType :: SnapshotRepoType
979− , gSnapshotRepoSettings :: GenericSnapshotRepoSettings
980− } deriving (Eq, Show)
981−
982−
983−instance SnapshotRepo GenericSnapshotRepo where
984− toGSnapshotRepo = id
985− fromGSnapshotRepo = Right
986−
987−
988−newtype SnapshotRepoType =
989− SnapshotRepoType { snapshotRepoType :: Text }
990− deriving (Eq, Ord, Show, ToJSON, FromJSON)
991−
992−
993−-- | Opaque representation of snapshot repo settings. Instances of
994−-- 'SnapshotRepo' will produce this.
995−newtype GenericSnapshotRepoSettings =
996− GenericSnapshotRepoSettings { gSnapshotRepoSettingsObject :: Object }
997− deriving (Eq, Show, ToJSON)
998−
999−
1000− -- Regardless of whether you send strongly typed json, my version of
1001− -- ES sends back stringly typed json in the settings, e.g. booleans
1002− -- as strings, so we'll try to convert them.
1003−instance FromJSON GenericSnapshotRepoSettings where
1004− parseJSON = fmap (GenericSnapshotRepoSettings . fmap unStringlyTypeJSON). parseJSON
1005−
1006−-- | The result of running 'verifySnapshotRepo'.
1007−newtype SnapshotVerification =
1008− SnapshotVerification {
1009− snapshotNodeVerifications :: [SnapshotNodeVerification]
1010− } deriving (Eq, Show)
1011−
1012−
1013−instance FromJSON SnapshotVerification where
1014− parseJSON = withObject "SnapshotVerification" parse
1015− where
1016− parse o = do
1017− o2 <- o .: "nodes"
1018− SnapshotVerification <$> mapM (uncurry parse') (HM.toList o2)
1019− parse' rawFullId = withObject "SnapshotNodeVerification" $ \o ->
1020− SnapshotNodeVerification (FullNodeId rawFullId) <$> o .: "name"
1021−
1022−
1023−-- | A node that has verified a snapshot
1024−data SnapshotNodeVerification = SnapshotNodeVerification {
1025− snvFullId :: FullNodeId
1026− , snvNodeName :: NodeName
1027− } deriving (Eq, Show)
1028−
1029−
1030−-- | Unique, automatically-generated name assigned to nodes that are
1031−-- usually returned in node-oriented APIs.
1032−newtype FullNodeId = FullNodeId { fullNodeId :: Text }
1033− deriving (Eq, Ord, Show, FromJSON)
1034−
1035−
1036−-- | A human-readable node name that is supplied by the user in the
1037−-- node config or automatically generated by Elasticsearch.
1038−newtype NodeName = NodeName { nodeName :: Text }
1039− deriving (Eq, Ord, Show, FromJSON)
1040−
1041−newtype ClusterName = ClusterName { clusterName :: Text }
1042− deriving (Eq, Ord, Show, FromJSON)
1043−
1044−data NodesInfo = NodesInfo {
1045− nodesInfo :: [NodeInfo]
1046− , nodesClusterName :: ClusterName
1047− } deriving (Eq, Show)
1048−
1049−data NodesStats = NodesStats {
1050− nodesStats :: [NodeStats]
1051− , nodesStatsClusterName :: ClusterName
1052− } deriving (Eq, Show)
1053−
1054−data NodeStats = NodeStats {
1055− nodeStatsName :: NodeName
1056− , nodeStatsFullId :: FullNodeId
1057− , nodeStatsBreakersStats :: Maybe NodeBreakersStats
1058− , nodeStatsHTTP :: NodeHTTPStats
1059− , nodeStatsTransport :: NodeTransportStats
1060− , nodeStatsFS :: NodeFSStats
1061− , nodeStatsNetwork :: Maybe NodeNetworkStats
1062− , nodeStatsThreadPool :: NodeThreadPoolsStats
1063− , nodeStatsJVM :: NodeJVMStats
1064− , nodeStatsProcess :: NodeProcessStats
1065− , nodeStatsOS :: NodeOSStats
1066− , nodeStatsIndices :: NodeIndicesStats
1067− } deriving (Eq, Show)
1068−
1069−data NodeBreakersStats = NodeBreakersStats {
1070− nodeStatsParentBreaker :: NodeBreakerStats
1071− , nodeStatsRequestBreaker :: NodeBreakerStats
1072− , nodeStatsFieldDataBreaker :: NodeBreakerStats
1073− } deriving (Eq, Show)
1074−
1075−data NodeBreakerStats = NodeBreakerStats {
1076− nodeBreakersTripped :: Int
1077− , nodeBreakersOverhead :: Double
1078− , nodeBreakersEstSize :: Bytes
1079− , nodeBreakersLimitSize :: Bytes
1080− } deriving (Eq, Show)
1081−
1082−data NodeHTTPStats = NodeHTTPStats {
1083− nodeHTTPTotalOpened :: Int
1084− , nodeHTTPCurrentOpen :: Int
1085− } deriving (Eq, Show)
1086−
1087−data NodeTransportStats = NodeTransportStats {
1088− nodeTransportTXSize :: Bytes
1089− , nodeTransportCount :: Int
1090− , nodeTransportRXSize :: Bytes
1091− , nodeTransportRXCount :: Int
1092− , nodeTransportServerOpen :: Int
1093− } deriving (Eq, Show)
1094−
1095−data NodeFSStats = NodeFSStats {
1096− nodeFSDataPaths :: [NodeDataPathStats]
1097− , nodeFSTotal :: NodeFSTotalStats
1098− , nodeFSTimestamp :: UTCTime
1099− } deriving (Eq, Show)
1100−
1101−data NodeDataPathStats = NodeDataPathStats {
1102− nodeDataPathDiskServiceTime :: Maybe Double
1103− , nodeDataPathDiskQueue :: Maybe Double
1104− , nodeDataPathIOSize :: Maybe Bytes
1105− , nodeDataPathWriteSize :: Maybe Bytes
1106− , nodeDataPathReadSize :: Maybe Bytes
1107− , nodeDataPathIOOps :: Maybe Int
1108− , nodeDataPathWrites :: Maybe Int
1109− , nodeDataPathReads :: Maybe Int
1110− , nodeDataPathAvailable :: Bytes
1111− , nodeDataPathFree :: Bytes
1112− , nodeDataPathTotal :: Bytes
1113− , nodeDataPathType :: Maybe Text
1114− , nodeDataPathDevice :: Maybe Text
1115− , nodeDataPathMount :: Text
1116− , nodeDataPathPath :: Text
1117− } deriving (Eq, Show)
1118−
1119−data NodeFSTotalStats = NodeFSTotalStats {
1120− nodeFSTotalDiskServiceTime :: Maybe Double
1121− , nodeFSTotalDiskQueue :: Maybe Double
1122− , nodeFSTotalIOSize :: Maybe Bytes
1123− , nodeFSTotalWriteSize :: Maybe Bytes
1124− , nodeFSTotalReadSize :: Maybe Bytes
1125− , nodeFSTotalIOOps :: Maybe Int
1126− , nodeFSTotalWrites :: Maybe Int
1127− , nodeFSTotalReads :: Maybe Int
1128− , nodeFSTotalAvailable :: Bytes
1129− , nodeFSTotalFree :: Bytes
1130− , nodeFSTotalTotal :: Bytes
1131− } deriving (Eq, Show)
1132−
1133−data NodeNetworkStats = NodeNetworkStats {
1134− nodeNetTCPOutRSTs :: Int
1135− , nodeNetTCPInErrs :: Int
1136− , nodeNetTCPAttemptFails :: Int
1137− , nodeNetTCPEstabResets :: Int
1138− , nodeNetTCPRetransSegs :: Int
1139− , nodeNetTCPOutSegs :: Int
1140− , nodeNetTCPInSegs :: Int
1141− , nodeNetTCPCurrEstab :: Int
1142− , nodeNetTCPPassiveOpens :: Int
1143− , nodeNetTCPActiveOpens :: Int
1144− } deriving (Eq, Show)
1145−
1146−data NodeThreadPoolsStats = NodeThreadPoolsStats {
1147− nodeThreadPoolsStatsSnapshot :: NodeThreadPoolStats
1148− , nodeThreadPoolsStatsBulk :: NodeThreadPoolStats
1149− , nodeThreadPoolsStatsMerge :: NodeThreadPoolStats
1150− , nodeThreadPoolsStatsGet :: NodeThreadPoolStats
1151− , nodeThreadPoolsStatsManagement :: NodeThreadPoolStats
1152− , nodeThreadPoolsStatsFetchShardStore :: Maybe NodeThreadPoolStats
1153− , nodeThreadPoolsStatsOptimize :: Maybe NodeThreadPoolStats
1154− , nodeThreadPoolsStatsFlush :: NodeThreadPoolStats
1155− , nodeThreadPoolsStatsSearch :: NodeThreadPoolStats
1156− , nodeThreadPoolsStatsWarmer :: NodeThreadPoolStats
1157− , nodeThreadPoolsStatsGeneric :: NodeThreadPoolStats
1158− , nodeThreadPoolsStatsSuggest :: Maybe NodeThreadPoolStats
1159− , nodeThreadPoolsStatsRefresh :: NodeThreadPoolStats
1160− , nodeThreadPoolsStatsIndex :: NodeThreadPoolStats
1161− , nodeThreadPoolsStatsListener :: Maybe NodeThreadPoolStats
1162− , nodeThreadPoolsStatsFetchShardStarted :: Maybe NodeThreadPoolStats
1163− , nodeThreadPoolsStatsPercolate :: Maybe NodeThreadPoolStats
1164− } deriving (Eq, Show)
1165−
1166−data NodeThreadPoolStats = NodeThreadPoolStats {
1167− nodeThreadPoolCompleted :: Int
1168− , nodeThreadPoolLargest :: Int
1169− , nodeThreadPoolRejected :: Int
1170− , nodeThreadPoolActive :: Int
1171− , nodeThreadPoolQueue :: Int
1172− , nodeThreadPoolThreads :: Int
1173− } deriving (Eq, Show)
1174−
1175−data NodeJVMStats = NodeJVMStats {
1176− nodeJVMStatsMappedBufferPool :: JVMBufferPoolStats
1177− , nodeJVMStatsDirectBufferPool :: JVMBufferPoolStats
1178− , nodeJVMStatsGCOldCollector :: JVMGCStats
1179− , nodeJVMStatsGCYoungCollector :: JVMGCStats
1180− , nodeJVMStatsPeakThreadsCount :: Int
1181− , nodeJVMStatsThreadsCount :: Int
1182− , nodeJVMStatsOldPool :: JVMPoolStats
1183− , nodeJVMStatsSurvivorPool :: JVMPoolStats
1184− , nodeJVMStatsYoungPool :: JVMPoolStats
1185− , nodeJVMStatsNonHeapCommitted :: Bytes
1186− , nodeJVMStatsNonHeapUsed :: Bytes
1187− , nodeJVMStatsHeapMax :: Bytes
1188− , nodeJVMStatsHeapCommitted :: Bytes
1189− , nodeJVMStatsHeapUsedPercent :: Int
1190− , nodeJVMStatsHeapUsed :: Bytes
1191− , nodeJVMStatsUptime :: NominalDiffTime
1192− , nodeJVMStatsTimestamp :: UTCTime
1193− } deriving (Eq, Show)
1194−
1195−data JVMBufferPoolStats = JVMBufferPoolStats {
1196− jvmBufferPoolStatsTotalCapacity :: Bytes
1197− , jvmBufferPoolStatsUsed :: Bytes
1198− , jvmBufferPoolStatsCount :: Int
1199− } deriving (Eq, Show)
1200−
1201−data JVMGCStats = JVMGCStats {
1202− jvmGCStatsCollectionTime :: NominalDiffTime
1203− , jvmGCStatsCollectionCount :: Int
1204− } deriving (Eq, Show)
1205−
1206−data JVMPoolStats = JVMPoolStats {
1207− jvmPoolStatsPeakMax :: Bytes
1208− , jvmPoolStatsPeakUsed :: Bytes
1209− , jvmPoolStatsMax :: Bytes
1210− , jvmPoolStatsUsed :: Bytes
1211− } deriving (Eq, Show)
1212−
1213−data NodeProcessStats = NodeProcessStats {
1214− nodeProcessTimestamp :: UTCTime
1215− , nodeProcessOpenFDs :: Int
1216− , nodeProcessMaxFDs :: Int
1217− , nodeProcessCPUPercent :: Int
1218− , nodeProcessCPUTotal :: NominalDiffTime
1219− , nodeProcessMemTotalVirtual :: Bytes
1220− } deriving (Eq, Show)
1221−
1222−data NodeOSStats = NodeOSStats {
1223− nodeOSTimestamp :: UTCTime
1224− , nodeOSCPUPercent :: Int
1225− , nodeOSLoad :: Maybe LoadAvgs
1226− , nodeOSMemTotal :: Bytes
1227− , nodeOSMemFree :: Bytes
1228− , nodeOSMemFreePercent :: Int
1229− , nodeOSMemUsed :: Bytes
1230− , nodeOSMemUsedPercent :: Int
1231− , nodeOSSwapTotal :: Bytes
1232− , nodeOSSwapFree :: Bytes
1233− , nodeOSSwapUsed :: Bytes
1234− } deriving (Eq, Show)
1235−
1236−data LoadAvgs = LoadAvgs {
1237− loadAvg1Min :: Double
1238− , loadAvg5Min :: Double
1239− , loadAvg15Min :: Double
1240− } deriving (Eq, Show)
1241−
1242−data NodeIndicesStats = NodeIndicesStats {
1243− nodeIndicesStatsRecoveryThrottleTime :: Maybe NominalDiffTime
1244− , nodeIndicesStatsRecoveryCurrentAsTarget :: Maybe Int
1245− , nodeIndicesStatsRecoveryCurrentAsSource :: Maybe Int
1246− , nodeIndicesStatsQueryCacheMisses :: Maybe Int
1247− , nodeIndicesStatsQueryCacheHits :: Maybe Int
1248− , nodeIndicesStatsQueryCacheEvictions :: Maybe Int
1249− , nodeIndicesStatsQueryCacheSize :: Maybe Bytes
1250− , nodeIndicesStatsSuggestCurrent :: Maybe Int
1251− , nodeIndicesStatsSuggestTime :: Maybe NominalDiffTime
1252− , nodeIndicesStatsSuggestTotal :: Maybe Int
1253− , nodeIndicesStatsTranslogSize :: Bytes
1254− , nodeIndicesStatsTranslogOps :: Int
1255− , nodeIndicesStatsSegFixedBitSetMemory :: Maybe Bytes
1256− , nodeIndicesStatsSegVersionMapMemory :: Bytes
1257− , nodeIndicesStatsSegIndexWriterMaxMemory :: Maybe Bytes
1258− , nodeIndicesStatsSegIndexWriterMemory :: Bytes
1259− , nodeIndicesStatsSegMemory :: Bytes
1260− , nodeIndicesStatsSegCount :: Int
1261− , nodeIndicesStatsCompletionSize :: Bytes
1262− , nodeIndicesStatsPercolateQueries :: Maybe Int
1263− , nodeIndicesStatsPercolateMemory :: Maybe Bytes
1264− , nodeIndicesStatsPercolateCurrent :: Maybe Int
1265− , nodeIndicesStatsPercolateTime :: Maybe NominalDiffTime
1266− , nodeIndicesStatsPercolateTotal :: Maybe Int
1267− , nodeIndicesStatsFieldDataEvictions :: Int
1268− , nodeIndicesStatsFieldDataMemory :: Bytes
1269− , nodeIndicesStatsWarmerTotalTime :: NominalDiffTime
1270− , nodeIndicesStatsWarmerTotal :: Int
1271− , nodeIndicesStatsWarmerCurrent :: Int
1272− , nodeIndicesStatsFlushTotalTime :: NominalDiffTime
1273− , nodeIndicesStatsFlushTotal :: Int
1274− , nodeIndicesStatsRefreshTotalTime :: NominalDiffTime
1275− , nodeIndicesStatsRefreshTotal :: Int
1276− , nodeIndicesStatsMergesTotalSize :: Bytes
1277− , nodeIndicesStatsMergesTotalDocs :: Int
1278− , nodeIndicesStatsMergesTotalTime :: NominalDiffTime
1279− , nodeIndicesStatsMergesTotal :: Int
1280− , nodeIndicesStatsMergesCurrentSize :: Bytes
1281− , nodeIndicesStatsMergesCurrentDocs :: Int
1282− , nodeIndicesStatsMergesCurrent :: Int
1283− , nodeIndicesStatsSearchFetchCurrent :: Int
1284− , nodeIndicesStatsSearchFetchTime :: NominalDiffTime
1285− , nodeIndicesStatsSearchFetchTotal :: Int
1286− , nodeIndicesStatsSearchQueryCurrent :: Int
1287− , nodeIndicesStatsSearchQueryTime :: NominalDiffTime
1288− , nodeIndicesStatsSearchQueryTotal :: Int
1289− , nodeIndicesStatsSearchOpenContexts :: Int
1290− , nodeIndicesStatsGetCurrent :: Int
1291− , nodeIndicesStatsGetMissingTime :: NominalDiffTime
1292− , nodeIndicesStatsGetMissingTotal :: Int
1293− , nodeIndicesStatsGetExistsTime :: NominalDiffTime
1294− , nodeIndicesStatsGetExistsTotal :: Int
1295− , nodeIndicesStatsGetTime :: NominalDiffTime
1296− , nodeIndicesStatsGetTotal :: Int
1297− , nodeIndicesStatsIndexingThrottleTime :: Maybe NominalDiffTime
1298− , nodeIndicesStatsIndexingIsThrottled :: Maybe Bool
1299− , nodeIndicesStatsIndexingNoopUpdateTotal :: Maybe Int
1300− , nodeIndicesStatsIndexingDeleteCurrent :: Int
1301− , nodeIndicesStatsIndexingDeleteTime :: NominalDiffTime
1302− , nodeIndicesStatsIndexingDeleteTotal :: Int
1303− , nodeIndicesStatsIndexingIndexCurrent :: Int
1304− , nodeIndicesStatsIndexingIndexTime :: NominalDiffTime
1305− , nodeIndicesStatsIndexingTotal :: Int
1306− , nodeIndicesStatsStoreThrottleTime :: NominalDiffTime
1307− , nodeIndicesStatsStoreSize :: Bytes
1308− , nodeIndicesStatsDocsDeleted :: Int
1309− , nodeIndicesStatsDocsCount :: Int
1310− } deriving (Eq, Show)
1311−
1312−-- | A quirky address format used throughout Elasticsearch. An example
1313−-- would be inet[/1.1.1.1:9200]. inet may be a placeholder for a
1314−-- <https://en.wikipedia.org/wiki/Fully_qualified_domain_name FQDN>.
1315−newtype EsAddress = EsAddress { esAddress :: Text }
1316− deriving (Eq, Ord, Show, FromJSON)
1317−
1318−-- | Typically a 7 character hex string.
1319−newtype BuildHash = BuildHash { buildHash :: Text }
1320− deriving (Eq, Ord, Show, FromJSON, ToJSON)
1321−
1322−newtype PluginName = PluginName { pluginName :: Text }
1323− deriving (Eq, Ord, Show, FromJSON)
1324−
1325−data NodeInfo = NodeInfo {
1326− nodeInfoHTTPAddress :: Maybe EsAddress
1327− , nodeInfoBuild :: BuildHash
1328− , nodeInfoESVersion :: VersionNumber
1329− , nodeInfoIP :: Server
1330− , nodeInfoHost :: Server
1331− , nodeInfoTransportAddress :: EsAddress
1332− , nodeInfoName :: NodeName
1333− , nodeInfoFullId :: FullNodeId
1334− , nodeInfoPlugins :: [NodePluginInfo]
1335− , nodeInfoHTTP :: NodeHTTPInfo
1336− , nodeInfoTransport :: NodeTransportInfo
1337− , nodeInfoNetwork :: Maybe NodeNetworkInfo
1338− , nodeInfoThreadPool :: NodeThreadPoolsInfo
1339− , nodeInfoJVM :: NodeJVMInfo
1340− , nodeInfoProcess :: NodeProcessInfo
1341− , nodeInfoOS :: NodeOSInfo
1342− , nodeInfoSettings :: Object
1343− -- ^ The members of the settings objects are not consistent,
1344− -- dependent on plugins, etc.
1345− } deriving (Eq, Show)
1346−
1347−data NodePluginInfo = NodePluginInfo {
1348− nodePluginSite :: Maybe Bool
1349− -- ^ Is this a site plugin?
1350− , nodePluginJVM :: Maybe Bool
1351− -- ^ Is this plugin running on the JVM
1352− , nodePluginDescription :: Text
1353− , nodePluginVersion :: MaybeNA VersionNumber
1354− , nodePluginName :: PluginName
1355− } deriving (Eq, Show)
1356−
1357−data NodeHTTPInfo = NodeHTTPInfo {
1358− nodeHTTPMaxContentLength :: Bytes
1359− , nodeHTTPTransportAddress :: BoundTransportAddress
1360− } deriving (Eq, Show)
1361−
1362−data NodeTransportInfo = NodeTransportInfo {
1363− nodeTransportProfiles :: [BoundTransportAddress]
1364− , nodeTransportAddress :: BoundTransportAddress
1365− } deriving (Eq, Show)
1366−
1367−data BoundTransportAddress = BoundTransportAddress {
1368− publishAddress :: EsAddress
1369− , boundAddress :: [EsAddress]
1370− } deriving (Eq, Show)
1371−
1372−data NodeNetworkInfo = NodeNetworkInfo {
1373− nodeNetworkPrimaryInterface :: NodeNetworkInterface
1374− , nodeNetworkRefreshInterval :: NominalDiffTime
1375− } deriving (Eq, Show)
1376−
1377−newtype MacAddress = MacAddress { macAddress :: Text }
1378− deriving (Eq, Ord, Show, FromJSON)
1379−
1380−newtype NetworkInterfaceName = NetworkInterfaceName { networkInterfaceName :: Text }
1381− deriving (Eq, Ord, Show, FromJSON)
1382−
1383−data NodeNetworkInterface = NodeNetworkInterface {
1384− nodeNetIfaceMacAddress :: MacAddress
1385− , nodeNetIfaceName :: NetworkInterfaceName
1386− , nodeNetIfaceAddress :: Server
1387− } deriving (Eq, Show)
1388−
1389−data NodeThreadPoolsInfo = NodeThreadPoolsInfo {
1390− nodeThreadPoolsRefresh :: NodeThreadPoolInfo
1391− , nodeThreadPoolsManagement :: NodeThreadPoolInfo
1392− , nodeThreadPoolsPercolate :: Maybe NodeThreadPoolInfo
1393− , nodeThreadPoolsListener :: Maybe NodeThreadPoolInfo
1394− , nodeThreadPoolsFetchShardStarted :: Maybe NodeThreadPoolInfo
1395− , nodeThreadPoolsSearch :: NodeThreadPoolInfo
1396− , nodeThreadPoolsFlush :: NodeThreadPoolInfo
1397− , nodeThreadPoolsWarmer :: NodeThreadPoolInfo
1398− , nodeThreadPoolsOptimize :: Maybe NodeThreadPoolInfo
1399− , nodeThreadPoolsBulk :: NodeThreadPoolInfo
1400− , nodeThreadPoolsSuggest :: Maybe NodeThreadPoolInfo
1401− , nodeThreadPoolsMerge :: NodeThreadPoolInfo
1402− , nodeThreadPoolsSnapshot :: NodeThreadPoolInfo
1403− , nodeThreadPoolsGet :: NodeThreadPoolInfo
1404− , nodeThreadPoolsFetchShardStore :: Maybe NodeThreadPoolInfo
1405− , nodeThreadPoolsIndex :: NodeThreadPoolInfo
1406− , nodeThreadPoolsGeneric :: NodeThreadPoolInfo
1407− } deriving (Eq, Show)
1408−
1409−data NodeThreadPoolInfo = NodeThreadPoolInfo {
1410− nodeThreadPoolQueueSize :: ThreadPoolSize
1411− , nodeThreadPoolKeepalive :: Maybe NominalDiffTime
1412− , nodeThreadPoolMin :: Maybe Int
1413− , nodeThreadPoolMax :: Maybe Int
1414− , nodeThreadPoolType :: ThreadPoolType
1415− } deriving (Eq, Show)
1416−
1417−data ThreadPoolSize = ThreadPoolBounded Int
1418− | ThreadPoolUnbounded
1419− deriving (Eq, Show)
1420−
1421−data ThreadPoolType = ThreadPoolScaling
1422− | ThreadPoolFixed
1423− | ThreadPoolCached
1424− deriving (Eq, Show)
1425−
1426−data NodeJVMInfo = NodeJVMInfo {
1427− nodeJVMInfoMemoryPools :: [JVMMemoryPool]
1428− , nodeJVMInfoMemoryPoolsGCCollectors :: [JVMGCCollector]
1429− , nodeJVMInfoMemoryInfo :: JVMMemoryInfo
1430− , nodeJVMInfoStartTime :: UTCTime
1431− , nodeJVMInfoVMVendor :: Text
1432− , nodeJVMVMVersion :: VMVersion
1433− -- ^ JVM doesn't seme to follow normal version conventions
1434− , nodeJVMVMName :: Text
1435− , nodeJVMVersion :: JVMVersion
1436− , nodeJVMPID :: PID
1437− } deriving (Eq, Show)
1438−
1439−-- | We cannot parse JVM version numbers and we're not going to try.
1440−newtype JVMVersion =
1441− JVMVersion { unJVMVersion :: Text }
1442− deriving (Eq, Show)
1443−
1444−instance FromJSON JVMVersion where
1445− parseJSON = withText "JVMVersion" (pure . JVMVersion)
1446−
1447−data JVMMemoryInfo = JVMMemoryInfo {
1448− jvmMemoryInfoDirectMax :: Bytes
1449− , jvmMemoryInfoNonHeapMax :: Bytes
1450− , jvmMemoryInfoNonHeapInit :: Bytes
1451− , jvmMemoryInfoHeapMax :: Bytes
1452− , jvmMemoryInfoHeapInit :: Bytes
1453− } deriving (Eq, Show)
1454−
1455−-- VM version numbers don't appear to be SemVer
1456−-- so we're special casing this jawn.
1457−newtype VMVersion =
1458− VMVersion { unVMVersion :: Text }
1459− deriving (Eq, Show)
1460−
1461−instance ToJSON VMVersion where
1462− toJSON = toJSON . unVMVersion
1463−
1464−instance FromJSON VMVersion where
1465− parseJSON = withText "VMVersion" (pure . VMVersion)
1466−
1467−newtype JVMMemoryPool = JVMMemoryPool {
1468− jvmMemoryPool :: Text
1469− } deriving (Eq, Show, FromJSON)
1470−
1471−newtype JVMGCCollector = JVMGCCollector {
1472− jvmGCCollector :: Text
1473− } deriving (Eq, Show, FromJSON)
1474−
1475−newtype PID = PID {
1476− pid :: Int
1477− } deriving (Eq, Show, FromJSON)
1478−
1479−data NodeOSInfo = NodeOSInfo {
1480− nodeOSRefreshInterval :: NominalDiffTime
1481− , nodeOSName :: Text
1482− , nodeOSArch :: Text
1483− , nodeOSVersion :: VersionNumber
1484− , nodeOSAvailableProcessors :: Int
1485− , nodeOSAllocatedProcessors :: Int
1486− } deriving (Eq, Show)
1487−
1488−data CPUInfo = CPUInfo {
1489− cpuCacheSize :: Bytes
1490− , cpuCoresPerSocket :: Int
1491− , cpuTotalSockets :: Int
1492− , cpuTotalCores :: Int
1493− , cpuMHZ :: Int
1494− , cpuModel :: Text
1495− , cpuVendor :: Text
1496− } deriving (Eq, Show)
1497−
1498−data NodeProcessInfo = NodeProcessInfo {
1499− nodeProcessMLockAll :: Bool
1500− -- ^ See <https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
1501− , nodeProcessMaxFileDescriptors :: Maybe Int
1502− , nodeProcessId :: PID
1503− , nodeProcessRefreshInterval :: NominalDiffTime
1504− } deriving (Eq, Show)
1505−
1506−data ShardResult =
1507− ShardResult { shardTotal :: Int
1508− , shardsSuccessful :: Int
1509− , shardsFailed :: Int } deriving (Eq, Show)
1510−
1511−instance FromJSON ShardResult where
1512− parseJSON (Object v) = ShardResult <$>
1513− v .: "total" <*>
1514− v .: "successful" <*>
1515− v .: "failed"
1516− parseJSON _ = empty
1517−
1518−data SnapshotState = SnapshotInit
1519− | SnapshotStarted
1520− | SnapshotSuccess
1521− | SnapshotFailed
1522− | SnapshotAborted
1523− | SnapshotMissing
1524− | SnapshotWaiting
1525− deriving (Eq, Show)
1526−
1527−instance FromJSON SnapshotState where
1528− parseJSON = withText "SnapshotState" parse
1529− where
1530− parse "INIT" = return SnapshotInit
1531− parse "STARTED" = return SnapshotStarted
1532− parse "SUCCESS" = return SnapshotSuccess
1533− parse "FAILED" = return SnapshotFailed
1534− parse "ABORTED" = return SnapshotAborted
1535− parse "MISSING" = return SnapshotMissing
1536− parse "WAITING" = return SnapshotWaiting
1537− parse t = fail ("Invalid snapshot state " <> T.unpack t)
1538−
1539−
1540−data SnapshotRestoreSettings = SnapshotRestoreSettings {
1541− snapRestoreWaitForCompletion :: Bool
1542− -- ^ Should the API call return immediately after initializing
1543− -- the restore or wait until completed? Note that if this is
1544− -- enabled, it could wait a long time, so you should adjust your
1545− -- 'ManagerSettings' accordingly to set long timeouts or
1546− -- explicitly handle timeouts.
1547− , snapRestoreIndices :: Maybe IndexSelection
1548− -- ^ Nothing will restore all indices in the snapshot. Just [] is
1549− -- permissable and will essentially be a no-op restore.
1550− , snapRestoreIgnoreUnavailable :: Bool
1551− -- ^ If set to True, any indices that do not exist will be ignored
1552− -- during snapshot rather than failing the restore.
1553− , snapRestoreIncludeGlobalState :: Bool
1554− -- ^ If set to false, will ignore any global state in the snapshot
1555− -- and will not restore it.
1556− , snapRestoreRenamePattern :: Maybe RestoreRenamePattern
1557− -- ^ A regex pattern for matching indices. Used with
1558− -- 'snapRestoreRenameReplacement', the restore can reference the
1559− -- matched index and create a new index name upon restore.
1560− , snapRestoreRenameReplacement :: Maybe (NonEmpty RestoreRenameToken)
1561− -- ^ Expression of how index renames should be constructed.
1562− , snapRestorePartial :: Bool
1563− -- ^ If some indices fail to restore, should the process proceed?
1564− , snapRestoreIncludeAliases :: Bool
1565− -- ^ Should the restore also restore the aliases captured in the
1566− -- snapshot.
1567− , snapRestoreIndexSettingsOverrides :: Maybe RestoreIndexSettings
1568− -- ^ Settings to apply during the restore process. __NOTE:__ This
1569− -- option is not supported in ES < 1.5 and should be set to
1570− -- Nothing in that case.
1571− , snapRestoreIgnoreIndexSettings :: Maybe (NonEmpty Text)
1572− -- ^ This type could be more rich but it isn't clear which
1573− -- settings are allowed to be ignored during restore, so we're
1574− -- going with including this feature in a basic form rather than
1575− -- omitting it. One example here would be
1576− -- "index.refresh_interval". Any setting specified here will
1577− -- revert back to the server default during the restore process.
1578− } deriving (Eq, Show)
1579−
1580−newtype SnapshotRepoUpdateSettings = SnapshotRepoUpdateSettings
1581− { repoUpdateVerify :: Bool
1582− -- ^ After creation/update, synchronously check that nodes can
1583− -- write to this repo. Defaults to True. You may use False if you
1584− -- need a faster response and plan on verifying manually later
1585− -- with 'verifySnapshotRepo'.
1586− } deriving (Eq, Show)
1587−
1588−
1589−-- | Reasonable defaults for repo creation/update
1590−--
1591−-- * repoUpdateVerify True
1592−defaultSnapshotRepoUpdateSettings :: SnapshotRepoUpdateSettings
1593−defaultSnapshotRepoUpdateSettings = SnapshotRepoUpdateSettings True
1594−
1595−
1596−-- | A filesystem-based snapshot repo that ships with
1597−-- Elasticsearch. This is an instance of 'SnapshotRepo' so it can be
1598−-- used with 'updateSnapshotRepo'
1599−data FsSnapshotRepo = FsSnapshotRepo {
1600− fsrName :: SnapshotRepoName
1601− , fsrLocation :: FilePath
1602− , fsrCompressMetadata :: Bool
1603− , fsrChunkSize :: Maybe Bytes
1604− -- ^ Size by which to split large files during snapshotting.
1605− , fsrMaxRestoreBytesPerSec :: Maybe Bytes
1606− -- ^ Throttle node restore rate. If not supplied, defaults to 40mb/sec
1607− , fsrMaxSnapshotBytesPerSec :: Maybe Bytes
1608− -- ^ Throttle node snapshot rate. If not supplied, defaults to 40mb/sec
1609− } deriving (Eq, Show)
1610−
1611−
1612−instance SnapshotRepo FsSnapshotRepo where
1613− toGSnapshotRepo FsSnapshotRepo {..} =
1614− GenericSnapshotRepo fsrName fsRepoType (GenericSnapshotRepoSettings settings)
1615− where
1616− Object settings = object $ [ "location" .= fsrLocation
1617− , "compress" .= fsrCompressMetadata
1618− ] ++ optionalPairs
1619− optionalPairs = catMaybes [ ("chunk_size" .=) <$> fsrChunkSize
1620− , ("max_restore_bytes_per_sec" .=) <$> fsrMaxRestoreBytesPerSec
1621− , ("max_snapshot_bytes_per_sec" .=) <$> fsrMaxSnapshotBytesPerSec
1622− ]
1623− fromGSnapshotRepo GenericSnapshotRepo {..}
1624− | gSnapshotRepoType == fsRepoType = do
1625− let o = gSnapshotRepoSettingsObject gSnapshotRepoSettings
1626− parseRepo $
1627− FsSnapshotRepo gSnapshotRepoName <$> o .: "location"
1628− <*> o .:? "compress" .!= False
1629− <*> o .:? "chunk_size"
1630− <*> o .:? "max_restore_bytes_per_sec"
1631− <*> o .:? "max_snapshot_bytes_per_sec"
1632− | otherwise = Left (RepoTypeMismatch fsRepoType gSnapshotRepoType)
1633−
1634−
1635−parseRepo :: Parser a -> Either SnapshotRepoConversionError a
1636−parseRepo parser = case parseEither (const parser) () of
1637− Left e -> Left (OtherRepoConversionError (T.pack e))
1638− Right a -> Right a
1639−
1640−
1641−fsRepoType :: SnapshotRepoType
1642−fsRepoType = SnapshotRepoType "fs"
1643−
1644−-- | Law: fromGSnapshotRepo (toGSnapshotRepo r) == Right r
1645−class SnapshotRepo r where
1646− toGSnapshotRepo :: r -> GenericSnapshotRepo
1647− fromGSnapshotRepo :: GenericSnapshotRepo -> Either SnapshotRepoConversionError r
1648−
1649−
1650−data SnapshotRepoConversionError = RepoTypeMismatch SnapshotRepoType SnapshotRepoType
1651− -- ^ Expected type and actual type
1652− | OtherRepoConversionError Text
1653− deriving (Show, Eq)
1654−
1655−
1656−instance Exception SnapshotRepoConversionError
1657−
1658−
1659−data SnapshotCreateSettings = SnapshotCreateSettings {
1660− snapWaitForCompletion :: Bool
1661− -- ^ Should the API call return immediately after initializing
1662− -- the snapshot or wait until completed? Note that if this is
1663− -- enabled it could wait a long time, so you should adjust your
1664− -- 'ManagerSettings' accordingly to set long timeouts or
1665− -- explicitly handle timeouts.
1666− , snapIndices :: Maybe IndexSelection
1667− -- ^ Nothing will snapshot all indices. Just [] is permissable and
1668− -- will essentially be a no-op snapshot.
1669− , snapIgnoreUnavailable :: Bool
1670− -- ^ If set to True, any matched indices that don't exist will be
1671− -- ignored. Otherwise it will be an error and fail.
1672− , snapIncludeGlobalState :: Bool
1673− , snapPartial :: Bool
1674− -- ^ If some indices failed to snapshot (e.g. if not all primary
1675− -- shards are available), should the process proceed?
1676− } deriving (Eq, Show)
1677−
1678−
1679−-- | Reasonable defaults for snapshot creation
1680−--
1681−-- * snapWaitForCompletion False
1682−-- * snapIndices Nothing
1683−-- * snapIgnoreUnavailable False
1684−-- * snapIncludeGlobalState True
1685−-- * snapPartial False
1686−defaultSnapshotCreateSettings :: SnapshotCreateSettings
1687−defaultSnapshotCreateSettings = SnapshotCreateSettings {
1688− snapWaitForCompletion = False
1689− , snapIndices = Nothing
1690− , snapIgnoreUnavailable = False
1691− , snapIncludeGlobalState = True
1692− , snapPartial = False
1693− }
1694−
1695−
1696−data SnapshotSelection =
1697− SnapshotList (NonEmpty SnapshotPattern)
1698− | AllSnapshots
1699− deriving (Eq, Show)
1700−
1701−
1702−-- | Either specifies an exact snapshot name or one with globs in it,
1703−-- e.g. @SnapPattern "foo*"@ __NOTE__: Patterns are not supported on
1704−-- ES < 1.7
1705−data SnapshotPattern =
1706− ExactSnap SnapshotName
1707− | SnapPattern Text
1708− deriving (Eq, Show)
1709−
1710−
1711−-- | General information about the state of a snapshot. Has some
1712−-- redundancies with 'SnapshotStatus'
1713−data SnapshotInfo = SnapshotInfo {
1714− snapInfoShards :: ShardResult
1715− , snapInfoFailures :: [SnapshotShardFailure]
1716− , snapInfoDuration :: NominalDiffTime
1717− , snapInfoEndTime :: UTCTime
1718− , snapInfoStartTime :: UTCTime
1719− , snapInfoState :: SnapshotState
1720− , snapInfoIndices :: [IndexName]
1721− , snapInfoName :: SnapshotName
1722− } deriving (Eq, Show)
1723−
1724−
1725−instance FromJSON SnapshotInfo where
1726− parseJSON = withObject "SnapshotInfo" parse
1727− where
1728− parse o = SnapshotInfo <$> o .: "shards"
1729− <*> o .: "failures"
1730− <*> (unMS <$> o .: "duration_in_millis")
1731− <*> (posixMS <$> o .: "end_time_in_millis")
1732− <*> (posixMS <$> o .: "start_time_in_millis")
1733− <*> o .: "state"
1734− <*> o .: "indices"
1735− <*> o .: "snapshot"
1736−
1737−data SnapshotShardFailure = SnapshotShardFailure {
1738− snapShardFailureIndex :: IndexName
1739− , snapShardFailureNodeId :: Maybe NodeName -- I'm not 100% sure this isn't actually 'FullNodeId'
1740− , snapShardFailureReason :: Text
1741− , snapShardFailureShardId :: ShardId
1742− } deriving (Eq, Show)
1743−
1744−
1745−instance FromJSON SnapshotShardFailure where
1746− parseJSON = withObject "SnapshotShardFailure" parse
1747− where
1748− parse o = SnapshotShardFailure <$> o .: "index"
1749− <*> o .:? "node_id"
1750− <*> o .: "reason"
1751− <*> o .: "shard_id"
1752−
1753−-- | Regex-stype pattern, e.g. "index_(.+)" to match index names
1754−newtype RestoreRenamePattern =
1755− RestoreRenamePattern { rrPattern :: Text }
1756− deriving (Eq, Show, Ord, ToJSON)
1757−
1758−
1759−-- | A single token in a index renaming scheme for a restore. These
1760−-- are concatenated into a string before being sent to
1761−-- Elasticsearch. Check out these Java
1762−-- <https://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html docs> to find out more if you're into that sort of thing.
1763−data RestoreRenameToken = RRTLit Text
1764− -- ^ Just a literal string of characters
1765− | RRSubWholeMatch
1766− -- ^ Equivalent to $0. The entire matched pattern, not any subgroup
1767− | RRSubGroup RRGroupRefNum
1768− -- ^ A specific reference to a group number
1769− deriving (Eq, Show)
1770−
1771−
1772−-- | A group number for regex matching. Only values from 1-9 are
1773−-- supported. Construct with 'mkRRGroupRefNum'
1774−newtype RRGroupRefNum =
1775− RRGroupRefNum { rrGroupRefNum :: Int }
1776− deriving (Eq, Ord, Show)
1777−
1778−instance Bounded RRGroupRefNum where
1779− minBound = RRGroupRefNum 1
1780− maxBound = RRGroupRefNum 9
1781−
1782−
1783−-- | Only allows valid group number references (1-9).
1784−mkRRGroupRefNum :: Int -> Maybe RRGroupRefNum
1785−mkRRGroupRefNum i
1786− | i >= rrGroupRefNum minBound
1787− && i <= rrGroupRefNum maxBound =
1788− Just $ RRGroupRefNum i
1789− | otherwise = Nothing
1790−
1791−-- | Reasonable defaults for snapshot restores
1792−--
1793−-- * snapRestoreWaitForCompletion False
1794−-- * snapRestoreIndices Nothing
1795−-- * snapRestoreIgnoreUnavailable False
1796−-- * snapRestoreIncludeGlobalState True
1797−-- * snapRestoreRenamePattern Nothing
1798−-- * snapRestoreRenameReplacement Nothing
1799−-- * snapRestorePartial False
1800−-- * snapRestoreIncludeAliases True
1801−-- * snapRestoreIndexSettingsOverrides Nothing
1802−-- * snapRestoreIgnoreIndexSettings Nothing
1803−defaultSnapshotRestoreSettings :: SnapshotRestoreSettings
1804−defaultSnapshotRestoreSettings = SnapshotRestoreSettings {
1805− snapRestoreWaitForCompletion = False
1806− , snapRestoreIndices = Nothing
1807− , snapRestoreIgnoreUnavailable = False
1808− , snapRestoreIncludeGlobalState = True
1809− , snapRestoreRenamePattern = Nothing
1810− , snapRestoreRenameReplacement = Nothing
1811− , snapRestorePartial = False
1812− , snapRestoreIncludeAliases = True
1813− , snapRestoreIndexSettingsOverrides = Nothing
1814− , snapRestoreIgnoreIndexSettings = Nothing
1815− }
1816−
1817−
1818−-- | Index settings that can be overridden. The docs only mention you
1819−-- can update number of replicas, but there may be more. You
1820−-- definitely cannot override shard count.
1821−newtype RestoreIndexSettings = RestoreIndexSettings
1822− { restoreOverrideReplicas :: Maybe ReplicaCount
1823− } deriving (Eq, Show)
1824−
1825−
1826−instance ToJSON RestoreIndexSettings where
1827− toJSON RestoreIndexSettings {..} = object prs
1828− where
1829− prs = catMaybes [("index.number_of_replicas" .=) <$> restoreOverrideReplicas]
1830−
1831−
1832−instance FromJSON NodesInfo where
1833− parseJSON = withObject "NodesInfo" parse
1834− where
1835− parse o = do
1836− nodes <- o .: "nodes"
1837− infos <- forM (HM.toList nodes) $ \(fullNID, v) -> do
1838− node <- parseJSON v
1839− parseNodeInfo (FullNodeId fullNID) node
1840− cn <- o .: "cluster_name"
1841− return (NodesInfo infos cn)
1842−
1843−instance FromJSON NodesStats where
1844− parseJSON = withObject "NodesStats" parse
1845− where
1846− parse o = do
1847− nodes <- o .: "nodes"
1848− stats <- forM (HM.toList nodes) $ \(fullNID, v) -> do
1849− node <- parseJSON v
1850− parseNodeStats (FullNodeId fullNID) node
1851− cn <- o .: "cluster_name"
1852− return (NodesStats stats cn)
1853−
1854−instance FromJSON NodeBreakerStats where
1855− parseJSON = withObject "NodeBreakerStats" parse
1856− where
1857− parse o = NodeBreakerStats <$> o .: "tripped"
1858− <*> o .: "overhead"
1859− <*> o .: "estimated_size_in_bytes"
1860− <*> o .: "limit_size_in_bytes"
1861−
1862−instance FromJSON NodeHTTPStats where
1863− parseJSON = withObject "NodeHTTPStats" parse
1864− where
1865− parse o = NodeHTTPStats <$> o .: "total_opened"
1866− <*> o .: "current_open"
1867−
1868−instance FromJSON NodeTransportStats where
1869− parseJSON = withObject "NodeTransportStats" parse
1870− where
1871− parse o = NodeTransportStats <$> o .: "tx_size_in_bytes"
1872− <*> o .: "tx_count"
1873− <*> o .: "rx_size_in_bytes"
1874− <*> o .: "rx_count"
1875− <*> o .: "server_open"
1876−
1877−instance FromJSON NodeFSStats where
1878− parseJSON = withObject "NodeFSStats" parse
1879− where
1880− parse o = NodeFSStats <$> o .: "data"
1881− <*> o .: "total"
1882− <*> (posixMS <$> o .: "timestamp")
1883−
1884−instance FromJSON NodeDataPathStats where
1885− parseJSON = withObject "NodeDataPathStats" parse
1886− where
1887− parse o =
1888− NodeDataPathStats <$> (fmap unStringlyTypedDouble <$> o .:? "disk_service_time")
1889− <*> (fmap unStringlyTypedDouble <$> o .:? "disk_queue")
1890− <*> o .:? "disk_io_size_in_bytes"
1891− <*> o .:? "disk_write_size_in_bytes"
1892− <*> o .:? "disk_read_size_in_bytes"
1893− <*> o .:? "disk_io_op"
1894− <*> o .:? "disk_writes"
1895− <*> o .:? "disk_reads"
1896− <*> o .: "available_in_bytes"
1897− <*> o .: "free_in_bytes"
1898− <*> o .: "total_in_bytes"
1899− <*> o .:? "type"
1900− <*> o .:? "dev"
1901− <*> o .: "mount"
1902− <*> o .: "path"
1903−
1904−instance FromJSON NodeFSTotalStats where
1905− parseJSON = withObject "NodeFSTotalStats" parse
1906− where
1907− parse o = NodeFSTotalStats <$> (fmap unStringlyTypedDouble <$> o .:? "disk_service_time")
1908− <*> (fmap unStringlyTypedDouble <$> o .:? "disk_queue")
1909− <*> o .:? "disk_io_size_in_bytes"
1910− <*> o .:? "disk_write_size_in_bytes"
1911− <*> o .:? "disk_read_size_in_bytes"
1912− <*> o .:? "disk_io_op"
1913− <*> o .:? "disk_writes"
1914− <*> o .:? "disk_reads"
1915− <*> o .: "available_in_bytes"
1916− <*> o .: "free_in_bytes"
1917− <*> o .: "total_in_bytes"
1918−
1919−instance FromJSON NodeNetworkStats where
1920− parseJSON = withObject "NodeNetworkStats" parse
1921− where
1922− parse o = do
1923− tcp <- o .: "tcp"
1924− NodeNetworkStats <$> tcp .: "out_rsts"
1925− <*> tcp .: "in_errs"
1926− <*> tcp .: "attempt_fails"
1927− <*> tcp .: "estab_resets"
1928− <*> tcp .: "retrans_segs"
1929− <*> tcp .: "out_segs"
1930− <*> tcp .: "in_segs"
1931− <*> tcp .: "curr_estab"
1932− <*> tcp .: "passive_opens"
1933− <*> tcp .: "active_opens"
1934−
1935−instance FromJSON NodeThreadPoolsStats where
1936− parseJSON = withObject "NodeThreadPoolsStats" parse
1937− where
1938− parse o = NodeThreadPoolsStats <$> o .: "snapshot"
1939− <*> o .: "bulk"
1940− <*> o .: "force_merge"
1941− <*> o .: "get"
1942− <*> o .: "management"
1943− <*> o .:? "fetch_shard_store"
1944− <*> o .:? "optimize"
1945− <*> o .: "flush"
1946− <*> o .: "search"
1947− <*> o .: "warmer"
1948− <*> o .: "generic"
1949− <*> o .:? "suggest"
1950− <*> o .: "refresh"
1951− <*> o .: "index"
1952− <*> o .:? "listener"
1953− <*> o .:? "fetch_shard_started"
1954− <*> o .:? "percolate"
1955−instance FromJSON NodeThreadPoolStats where
1956− parseJSON = withObject "NodeThreadPoolStats" parse
1957− where
1958− parse o = NodeThreadPoolStats <$> o .: "completed"
1959− <*> o .: "largest"
1960− <*> o .: "rejected"
1961− <*> o .: "active"
1962− <*> o .: "queue"
1963− <*> o .: "threads"
1964−
1965−instance FromJSON NodeJVMStats where
1966− parseJSON = withObject "NodeJVMStats" parse
1967− where
1968− parse o = do
1969− bufferPools <- o .: "buffer_pools"
1970− mapped <- bufferPools .: "mapped"
1971− direct <- bufferPools .: "direct"
1972− gc <- o .: "gc"
1973− collectors <- gc .: "collectors"
1974− oldC <- collectors .: "old"
1975− youngC <- collectors .: "young"
1976− threads <- o .: "threads"
1977− mem <- o .: "mem"
1978− pools <- mem .: "pools"
1979− oldM <- pools .: "old"
1980− survivorM <- pools .: "survivor"
1981− youngM <- pools .: "young"
1982− NodeJVMStats <$> pure mapped
1983− <*> pure direct
1984− <*> pure oldC
1985− <*> pure youngC
1986− <*> threads .: "peak_count"
1987− <*> threads .: "count"
1988− <*> pure oldM
1989− <*> pure survivorM
1990− <*> pure youngM
1991− <*> mem .: "non_heap_committed_in_bytes"
1992− <*> mem .: "non_heap_used_in_bytes"
1993− <*> mem .: "heap_max_in_bytes"
1994− <*> mem .: "heap_committed_in_bytes"
1995− <*> mem .: "heap_used_percent"
1996− <*> mem .: "heap_used_in_bytes"
1997− <*> (unMS <$> o .: "uptime_in_millis")
1998− <*> (posixMS <$> o .: "timestamp")
1999−
2000−instance FromJSON JVMBufferPoolStats where
2001− parseJSON = withObject "JVMBufferPoolStats" parse
2002− where
2003− parse o = JVMBufferPoolStats <$> o .: "total_capacity_in_bytes"
2004− <*> o .: "used_in_bytes"
2005− <*> o .: "count"
2006−
2007−instance FromJSON JVMGCStats where
2008− parseJSON = withObject "JVMGCStats" parse
2009− where
2010− parse o = JVMGCStats <$> (unMS <$> o .: "collection_time_in_millis")
2011− <*> o .: "collection_count"
2012−
2013−instance FromJSON JVMPoolStats where
2014− parseJSON = withObject "JVMPoolStats" parse
2015− where
2016− parse o = JVMPoolStats <$> o .: "peak_max_in_bytes"
2017− <*> o .: "peak_used_in_bytes"
2018− <*> o .: "max_in_bytes"
2019− <*> o .: "used_in_bytes"
2020−
2021−instance FromJSON NodeProcessStats where
2022− parseJSON = withObject "NodeProcessStats" parse
2023− where
2024− parse o = do
2025− mem <- o .: "mem"
2026− cpu <- o .: "cpu"
2027− NodeProcessStats <$> (posixMS <$> o .: "timestamp")
2028− <*> o .: "open_file_descriptors"
2029− <*> o .: "max_file_descriptors"
2030− <*> cpu .: "percent"
2031− <*> (unMS <$> cpu .: "total_in_millis")
2032− <*> mem .: "total_virtual_in_bytes"
2033−
2034−instance FromJSON NodeOSStats where
2035− parseJSON = withObject "NodeOSStats" parse
2036− where
2037− parse o = do
2038− swap <- o .: "swap"
2039− mem <- o .: "mem"
2040− cpu <- o .: "cpu"
2041− load <- o .:? "load_average"
2042− NodeOSStats <$> (posixMS <$> o .: "timestamp")
2043− <*> cpu .: "percent"
2044− <*> pure load
2045− <*> mem .: "total_in_bytes"
2046− <*> mem .: "free_in_bytes"
2047− <*> mem .: "free_percent"
2048− <*> mem .: "used_in_bytes"
2049− <*> mem .: "used_percent"
2050− <*> swap .: "total_in_bytes"
2051− <*> swap .: "free_in_bytes"
2052− <*> swap .: "used_in_bytes"
2053−
2054−instance FromJSON LoadAvgs where
2055− parseJSON = withArray "LoadAvgs" parse
2056− where
2057− parse v = case V.toList v of
2058− [one, five, fifteen] -> LoadAvgs <$> parseJSON one
2059− <*> parseJSON five
2060− <*> parseJSON fifteen
2061− _ -> fail "Expecting a triple of Doubles"
2062−
2063−instance FromJSON NodeIndicesStats where
2064− parseJSON = withObject "NodeIndicesStats" parse
2065− where
2066− parse o = do
2067− let (.::) mv k = case mv of
2068− Just v -> Just <$> v .: k
2069− Nothing -> pure Nothing
2070− mRecovery <- o .:? "recovery"
2071− mQueryCache <- o .:? "query_cache"
2072− mSuggest <- o .:? "suggest"
2073− translog <- o .: "translog"
2074− segments <- o .: "segments"
2075− completion <- o .: "completion"
2076− mPercolate <- o .:? "percolate"
2077− fielddata <- o .: "fielddata"
2078− warmer <- o .: "warmer"
2079− flush <- o .: "flush"
2080− refresh <- o .: "refresh"
2081− merges <- o .: "merges"
2082− search <- o .: "search"
2083− getStats <- o .: "get"
2084− indexing <- o .: "indexing"
2085− store <- o .: "store"
2086− docs <- o .: "docs"
2087− NodeIndicesStats <$> (fmap unMS <$> mRecovery .:: "throttle_time_in_millis")
2088− <*> mRecovery .:: "current_as_target"
2089− <*> mRecovery .:: "current_as_source"
2090− <*> mQueryCache .:: "miss_count"
2091− <*> mQueryCache .:: "hit_count"
2092− <*> mQueryCache .:: "evictions"
2093− <*> mQueryCache .:: "memory_size_in_bytes"
2094− <*> mSuggest .:: "current"
2095− <*> (fmap unMS <$> mSuggest .:: "time_in_millis")
2096− <*> mSuggest .:: "total"
2097− <*> translog .: "size_in_bytes"
2098− <*> translog .: "operations"
2099− <*> segments .:? "fixed_bit_set_memory_in_bytes"
2100− <*> segments .: "version_map_memory_in_bytes"
2101− <*> segments .:? "index_writer_max_memory_in_bytes"
2102− <*> segments .: "index_writer_memory_in_bytes"
2103− <*> segments .: "memory_in_bytes"
2104− <*> segments .: "count"
2105− <*> completion .: "size_in_bytes"
2106− <*> mPercolate .:: "queries"
2107− <*> mPercolate .:: "memory_size_in_bytes"
2108− <*> mPercolate .:: "current"
2109− <*> (fmap unMS <$> mPercolate .:: "time_in_millis")
2110− <*> mPercolate .:: "total"
2111− <*> fielddata .: "evictions"
2112− <*> fielddata .: "memory_size_in_bytes"
2113− <*> (unMS <$> warmer .: "total_time_in_millis")
2114− <*> warmer .: "total"
2115− <*> warmer .: "current"
2116− <*> (unMS <$> flush .: "total_time_in_millis")
2117− <*> flush .: "total"
2118− <*> (unMS <$> refresh .: "total_time_in_millis")
2119− <*> refresh .: "total"
2120− <*> merges .: "total_size_in_bytes"
2121− <*> merges .: "total_docs"
2122− <*> (unMS <$> merges .: "total_time_in_millis")
2123− <*> merges .: "total"
2124− <*> merges .: "current_size_in_bytes"
2125− <*> merges .: "current_docs"
2126− <*> merges .: "current"
2127− <*> search .: "fetch_current"
2128− <*> (unMS <$> search .: "fetch_time_in_millis")
2129− <*> search .: "fetch_total"
2130− <*> search .: "query_current"
2131− <*> (unMS <$> search .: "query_time_in_millis")
2132− <*> search .: "query_total"
2133− <*> search .: "open_contexts"
2134− <*> getStats .: "current"
2135− <*> (unMS <$> getStats .: "missing_time_in_millis")
2136− <*> getStats .: "missing_total"
2137− <*> (unMS <$> getStats .: "exists_time_in_millis")
2138− <*> getStats .: "exists_total"
2139− <*> (unMS <$> getStats .: "time_in_millis")
2140− <*> getStats .: "total"
2141− <*> (fmap unMS <$> indexing .:? "throttle_time_in_millis")
2142− <*> indexing .:? "is_throttled"
2143− <*> indexing .:? "noop_update_total"
2144− <*> indexing .: "delete_current"
2145− <*> (unMS <$> indexing .: "delete_time_in_millis")
2146− <*> indexing .: "delete_total"
2147− <*> indexing .: "index_current"
2148− <*> (unMS <$> indexing .: "index_time_in_millis")
2149− <*> indexing .: "index_total"
2150− <*> (unMS <$> store .: "throttle_time_in_millis")
2151− <*> store .: "size_in_bytes"
2152− <*> docs .: "deleted"
2153− <*> docs .: "count"
2154−
2155−instance FromJSON NodeBreakersStats where
2156− parseJSON = withObject "NodeBreakersStats" parse
2157− where
2158− parse o = NodeBreakersStats <$> o .: "parent"
2159− <*> o .: "request"
2160− <*> o .: "fielddata"
2161−
2162−parseNodeStats :: FullNodeId -> Object -> Parser NodeStats
2163−parseNodeStats fnid o =
2164− NodeStats <$> o .: "name"
2165− <*> pure fnid
2166− <*> o .:? "breakers"
2167− <*> o .: "http"
2168− <*> o .: "transport"
2169− <*> o .: "fs"
2170− <*> o .:? "network"
2171− <*> o .: "thread_pool"
2172− <*> o .: "jvm"
2173− <*> o .: "process"
2174− <*> o .: "os"
2175− <*> o .: "indices"
2176−
2177−parseNodeInfo :: FullNodeId -> Object -> Parser NodeInfo
2178−parseNodeInfo nid o =
2179− NodeInfo <$> o .:? "http_address"
2180− <*> o .: "build_hash"
2181− <*> o .: "version"
2182− <*> o .: "ip"
2183− <*> o .: "host"
2184− <*> o .: "transport_address"
2185− <*> o .: "name"
2186− <*> pure nid
2187− <*> o .: "plugins"
2188− <*> o .: "http"
2189− <*> o .: "transport"
2190− <*> o .:? "network"
2191− <*> o .: "thread_pool"
2192− <*> o .: "jvm"
2193− <*> o .: "process"
2194− <*> o .: "os"
2195− <*> o .: "settings"
2196−
2197−instance FromJSON NodePluginInfo where
2198− parseJSON = withObject "NodePluginInfo" parse
2199− where
2200− parse o = NodePluginInfo <$> o .:? "site"
2201− <*> o .:? "jvm"
2202− <*> o .: "description"
2203− <*> o .: "version"
2204− <*> o .: "name"
2205−
2206−instance FromJSON NodeHTTPInfo where
2207− parseJSON = withObject "NodeHTTPInfo" parse
2208− where
2209− parse o = NodeHTTPInfo <$> o .: "max_content_length_in_bytes"
2210− <*> parseJSON (Object o)
2211−
2212−instance FromJSON BoundTransportAddress where
2213− parseJSON = withObject "BoundTransportAddress" parse
2214− where
2215− parse o = BoundTransportAddress <$> o .: "publish_address"
2216− <*> o .: "bound_address"
2217−
2218−instance FromJSON NodeOSInfo where
2219− parseJSON = withObject "NodeOSInfo" parse
2220− where
2221− parse o =
2222− NodeOSInfo <$> (unMS <$> o .: "refresh_interval_in_millis")
2223− <*> o .: "name"
2224− <*> o .: "arch"
2225− <*> o .: "version"
2226− <*> o .: "available_processors"
2227− <*> o .: "allocated_processors"
2228−
2229−
2230−instance FromJSON CPUInfo where
2231− parseJSON = withObject "CPUInfo" parse
2232− where
2233− parse o = CPUInfo <$> o .: "cache_size_in_bytes"
2234− <*> o .: "cores_per_socket"
2235− <*> o .: "total_sockets"
2236− <*> o .: "total_cores"
2237− <*> o .: "mhz"
2238− <*> o .: "model"
2239− <*> o .: "vendor"
2240−
2241−instance FromJSON NodeProcessInfo where
2242− parseJSON = withObject "NodeProcessInfo" parse
2243− where
2244− parse o = NodeProcessInfo <$> o .: "mlockall"
2245− <*> o .:? "max_file_descriptors"
2246− <*> o .: "id"
2247− <*> (unMS <$> o .: "refresh_interval_in_millis")
2248−
2249−instance FromJSON NodeJVMInfo where
2250− parseJSON = withObject "NodeJVMInfo" parse
2251− where
2252− parse o = NodeJVMInfo <$> o .: "memory_pools"
2253− <*> o .: "gc_collectors"
2254− <*> o .: "mem"
2255− <*> (posixMS <$> o .: "start_time_in_millis")
2256− <*> o .: "vm_vendor"
2257− <*> o .: "vm_version"
2258− <*> o .: "vm_name"
2259− <*> o .: "version"
2260− <*> o .: "pid"
2261−
2262−instance FromJSON JVMMemoryInfo where
2263− parseJSON = withObject "JVMMemoryInfo" parse
2264− where
2265− parse o = JVMMemoryInfo <$> o .: "direct_max_in_bytes"
2266− <*> o .: "non_heap_max_in_bytes"
2267− <*> o .: "non_heap_init_in_bytes"
2268− <*> o .: "heap_max_in_bytes"
2269− <*> o .: "heap_init_in_bytes"
2270−
2271−instance FromJSON NodeThreadPoolsInfo where
2272− parseJSON = withObject "NodeThreadPoolsInfo" parse
2273− where
2274− parse o = NodeThreadPoolsInfo <$> o .: "refresh"
2275− <*> o .: "management"
2276− <*> o .:? "percolate"
2277− <*> o .:? "listener"
2278− <*> o .:? "fetch_shard_started"
2279− <*> o .: "search"
2280− <*> o .: "flush"
2281− <*> o .: "warmer"
2282− <*> o .:? "optimize"
2283− <*> o .: "bulk"
2284− <*> o .:? "suggest"
2285− <*> o .: "force_merge"
2286− <*> o .: "snapshot"
2287− <*> o .: "get"
2288− <*> o .:? "fetch_shard_store"
2289− <*> o .: "index"
2290− <*> o .: "generic"
2291−
2292−instance FromJSON NodeThreadPoolInfo where
2293− parseJSON = withObject "NodeThreadPoolInfo" parse
2294− where
2295− parse o = do
2296− ka <- maybe (return Nothing) (fmap Just . parseStringInterval) =<< o .:? "keep_alive"
2297− NodeThreadPoolInfo <$> (parseJSON . unStringlyTypeJSON =<< o .: "queue_size")
2298− <*> pure ka
2299− <*> o .:? "min"
2300− <*> o .:? "max"
2301− <*> o .: "type"
2302−
2303−data TimeInterval = Weeks
2304− | Days
2305− | Hours
2306− | Minutes
2307− | Seconds deriving Eq
2308−
2309−instance Show TimeInterval where
2310− show Weeks = "w"
2311− show Days = "d"
2312− show Hours = "h"
2313− show Minutes = "m"
2314− show Seconds = "s"
2315−
2316−instance Read TimeInterval where
2317− readPrec = f =<< TR.get
2318− where
2319− f 'w' = return Weeks
2320− f 'd' = return Days
2321− f 'h' = return Hours
2322− f 'm' = return Minutes
2323− f 's' = return Seconds
2324− f _ = fail "TimeInterval expected one of w, d, h, m, s"
2325−
2326−data Interval = Year
2327− | Quarter
2328− | Month
2329− | Week
2330− | Day
2331− | Hour
2332− | Minute
2333− | Second deriving (Eq, Show)
2334−
2335−instance ToJSON Interval where
2336− toJSON Year = "year"
2337− toJSON Quarter = "quarter"
2338− toJSON Month = "month"
2339− toJSON Week = "week"
2340− toJSON Day = "day"
2341− toJSON Hour = "hour"
2342− toJSON Minute = "minute"
2343− toJSON Second = "second"
2344−
2345−parseStringInterval :: (Monad m) => String -> m NominalDiffTime
2346−parseStringInterval s = case span isNumber s of
2347− ("", _) -> fail "Invalid interval"
2348− (nS, unitS) -> case (readMay nS, readMay unitS) of
2349− (Just n, Just unit) -> return (fromInteger (n * unitNDT unit))
2350− (Nothing, _) -> fail "Invalid interval number"
2351− (_, Nothing) -> fail "Invalid interval unit"
2352− where
2353− unitNDT Seconds = 1
2354− unitNDT Minutes = 60
2355− unitNDT Hours = 60 * 60
2356− unitNDT Days = 24 * 60 * 60
2357− unitNDT Weeks = 7 * 24 * 60 * 60
2358−
2359−instance FromJSON ThreadPoolSize where
2360− parseJSON v = parseAsNumber v <|> parseAsString v
2361− where
2362− parseAsNumber = parseAsInt <=< parseJSON
2363− parseAsInt (-1) = return ThreadPoolUnbounded
2364− parseAsInt n
2365− | n >= 0 = return (ThreadPoolBounded n)
2366− | otherwise = fail "Thread pool size must be >= -1."
2367− parseAsString = withText "ThreadPoolSize" $ \t ->
2368− case first (readMay . T.unpack) (T.span isNumber t) of
2369− (Just n, "k") -> return (ThreadPoolBounded (n * 1000))
2370− (Just n, "") -> return (ThreadPoolBounded n)
2371− _ -> fail ("Invalid thread pool size " <> T.unpack t)
2372−
2373−instance FromJSON ThreadPoolType where
2374− parseJSON = withText "ThreadPoolType" parse
2375− where
2376− parse "scaling" = return ThreadPoolScaling
2377− parse "fixed" = return ThreadPoolFixed
2378− parse "cached" = return ThreadPoolCached
2379− parse e = fail ("Unexpected thread pool type" <> T.unpack e)
2380−
2381−instance FromJSON NodeTransportInfo where
2382− parseJSON = withObject "NodeTransportInfo" parse
2383− where
2384− parse o = NodeTransportInfo <$> (maybe (return mempty) parseProfiles =<< o .:? "profiles")
2385− <*> parseJSON (Object o)
2386− parseProfiles (Object o) | HM.null o = return []
2387− parseProfiles v@(Array _) = parseJSON v
2388− parseProfiles Null = return []
2389− parseProfiles _ = fail "Could not parse profiles"
2390−
2391−instance FromJSON NodeNetworkInfo where
2392− parseJSON = withObject "NodeNetworkInfo" parse
2393− where
2394− parse o = NodeNetworkInfo <$> o .: "primary_interface"
2395− <*> (unMS <$> o .: "refresh_interval_in_millis")
2396−
2397−
2398−instance FromJSON NodeNetworkInterface where
2399− parseJSON = withObject "NodeNetworkInterface" parse
2400− where
2401− parse o = NodeNetworkInterface <$> o .: "mac_address"
2402− <*> o .: "name"
2403− <*> o .: "address"
2404−
2405−
2406−instance ToJSON Version where
2407− toJSON Version {..} = object ["number" .= number
2408− ,"build_hash" .= build_hash
2409− ,"build_date" .= build_date
2410− ,"build_snapshot" .= build_snapshot
2411− ,"lucene_version" .= lucene_version]
2412−
2413−instance FromJSON Version where
2414− parseJSON = withObject "Version" parse
2415− where parse o = Version
2416− <$> o .: "number"
2417− <*> o .: "build_hash"
2418− <*> o .: "build_date"
2419− <*> o .: "build_snapshot"
2420− <*> o .: "lucene_version"
2421−
2422−instance ToJSON VersionNumber where
2423− toJSON = toJSON . SemVer.toText . versionNumber
2424−
2425−instance FromJSON VersionNumber where
2426− parseJSON = withText "VersionNumber" parse
2427− where
2428− parse t =
2429− case SemVer.fromText t of
2430− (Left err) -> fail err
2431− (Right v) -> return (VersionNumber v)
src/Database/Bloodhound/Internal/Highlight.hsdeleted−164
@@ -1,164 +0,0 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−
4−module Database.Bloodhound.Internal.Highlight where
5−
6−import Bloodhound.Import
7−
8−import qualified Data.Map.Strict as M
9−
10−import Database.Bloodhound.Internal.Newtypes
11−import Database.Bloodhound.Internal.Query
12−
13−type HitHighlight = M.Map Text [Text]
14−
15−data Highlights = Highlights
16− { globalsettings :: Maybe HighlightSettings
17− , highlightFields :: [FieldHighlight]
18− } deriving (Eq, Show)
19−
20−instance ToJSON Highlights where
21− toJSON (Highlights global fields) =
22− omitNulls (("fields" .= fields)
23− : highlightSettingsPairs global)
24−
25−data FieldHighlight =
26− FieldHighlight FieldName (Maybe HighlightSettings)
27− deriving (Eq, Show)
28−
29−
30−instance ToJSON FieldHighlight where
31− toJSON (FieldHighlight (FieldName fName) (Just fSettings)) =
32− object [ fName .= fSettings ]
33− toJSON (FieldHighlight (FieldName fName) Nothing) =
34− object [ fName .= emptyObject ]
35−
36−data HighlightSettings =
37− Plain PlainHighlight
38− | Postings PostingsHighlight
39− | FastVector FastVectorHighlight
40− deriving (Eq, Show)
41−
42−
43−instance ToJSON HighlightSettings where
44− toJSON hs = omitNulls (highlightSettingsPairs (Just hs))
45−
46−data PlainHighlight =
47− PlainHighlight { plainCommon :: Maybe CommonHighlight
48− , plainNonPost :: Maybe NonPostings }
49− deriving (Eq, Show)
50−
51− -- This requires that index_options are set to 'offset' in the mapping.
52−data PostingsHighlight =
53− PostingsHighlight (Maybe CommonHighlight)
54− deriving (Eq, Show)
55−
56−-- This requires that term_vector is set to 'with_positions_offsets' in the mapping.
57−data FastVectorHighlight = FastVectorHighlight
58− { fvCommon :: Maybe CommonHighlight
59− , fvNonPostSettings :: Maybe NonPostings
60− , boundaryChars :: Maybe Text
61− , boundaryMaxScan :: Maybe Int
62− , fragmentOffset :: Maybe Int
63− , matchedFields :: [Text]
64− , phraseLimit :: Maybe Int
65− } deriving (Eq, Show)
66−
67−data CommonHighlight = CommonHighlight
68− { order :: Maybe Text
69− , forceSource :: Maybe Bool
70− , tag :: Maybe HighlightTag
71− , encoder :: Maybe HighlightEncoder
72− , noMatchSize :: Maybe Int
73− , highlightQuery :: Maybe Query
74− , requireFieldMatch :: Maybe Bool
75− } deriving (Eq, Show)
76−
77−-- Settings that are only applicable to FastVector and Plain highlighters.
78−data NonPostings =
79− NonPostings { fragmentSize :: Maybe Int
80− , numberOfFragments :: Maybe Int
81− } deriving (Eq, Show)
82−
83−data HighlightEncoder = DefaultEncoder
84− | HTMLEncoder
85− deriving (Eq, Show)
86−
87−instance ToJSON HighlightEncoder where
88− toJSON DefaultEncoder = String "default"
89− toJSON HTMLEncoder = String "html"
90−
91−-- NOTE: Should the tags use some kind of HTML type, rather than Text?
92−data HighlightTag =
93− TagSchema Text
94− -- Only uses more than the first value in the lists if fvh
95− | CustomTags ([Text], [Text])
96− deriving (Eq, Show)
97−
98−highlightSettingsPairs :: Maybe HighlightSettings -> [Pair]
99−highlightSettingsPairs Nothing = []
100−highlightSettingsPairs (Just (Plain plh)) = plainHighPairs (Just plh)
101−highlightSettingsPairs (Just (Postings ph)) = postHighPairs (Just ph)
102−highlightSettingsPairs (Just (FastVector fvh)) = fastVectorHighPairs (Just fvh)
103−
104−
105−plainHighPairs :: Maybe PlainHighlight -> [Pair]
106−plainHighPairs Nothing = []
107−plainHighPairs (Just (PlainHighlight plCom plNonPost)) =
108− [ "type" .= String "plain"]
109− ++ commonHighlightPairs plCom
110− ++ nonPostingsToPairs plNonPost
111−
112−postHighPairs :: Maybe PostingsHighlight -> [Pair]
113−postHighPairs Nothing = []
114−postHighPairs (Just (PostingsHighlight pCom)) =
115− ("type" .= String "postings")
116− : commonHighlightPairs pCom
117−
118−fastVectorHighPairs :: Maybe FastVectorHighlight -> [Pair]
119−fastVectorHighPairs Nothing = []
120−fastVectorHighPairs
121− (Just
122− (FastVectorHighlight fvCom fvNonPostSettings' fvBoundChars
123− fvBoundMaxScan fvFragOff fvMatchedFields
124− fvPhraseLim)) =
125− [ "type" .= String "fvh"
126− , "boundary_chars" .= fvBoundChars
127− , "boundary_max_scan" .= fvBoundMaxScan
128− , "fragment_offset" .= fvFragOff
129− , "matched_fields" .= fvMatchedFields
130− , "phraseLimit" .= fvPhraseLim]
131− ++ commonHighlightPairs fvCom
132− ++ nonPostingsToPairs fvNonPostSettings'
133−
134−commonHighlightPairs :: Maybe CommonHighlight -> [Pair]
135−commonHighlightPairs Nothing = []
136−commonHighlightPairs (Just (CommonHighlight chScore chForceSource
137− chTag chEncoder chNoMatchSize
138− chHighlightQuery chRequireFieldMatch)) =
139− [ "order" .= chScore
140− , "force_source" .= chForceSource
141− , "encoder" .= chEncoder
142− , "no_match_size" .= chNoMatchSize
143− , "highlight_query" .= chHighlightQuery
144− , "require_fieldMatch" .= chRequireFieldMatch
145− ]
146− ++ highlightTagToPairs chTag
147−
148−
149−nonPostingsToPairs :: Maybe NonPostings -> [Pair]
150−nonPostingsToPairs Nothing = []
151−nonPostingsToPairs (Just (NonPostings npFragSize npNumOfFrags)) =
152− [ "fragment_size" .= npFragSize
153− , "number_of_fragments" .= npNumOfFrags
154− ]
155−
156−highlightTagToPairs :: Maybe HighlightTag -> [Pair]
157−highlightTagToPairs (Just (TagSchema _)) =
158− [ "scheme" .= String "default"
159− ]
160−highlightTagToPairs (Just (CustomTags (pre, post))) =
161− [ "pre_tags" .= pre
162− , "post_tags" .= post
163− ]
164−highlightTagToPairs Nothing = []
src/Database/Bloodhound/Internal/Newtypes.hsdeleted−237
@@ -1,237 +0,0 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−
4−module Database.Bloodhound.Internal.Newtypes where
5−
6−import Bloodhound.Import
7−
8−import qualified Data.Map.Strict as M
9−
10−newtype From = From Int deriving (Eq, Show, ToJSON)
11−newtype Size = Size Int deriving (Eq, Show, ToJSON, FromJSON)
12−
13−-- Used with scripts
14−newtype HitFields =
15− HitFields (M.Map Text [Value])
16− deriving (Eq, Show)
17−
18−instance FromJSON HitFields where
19− parseJSON x
20− = HitFields <$> parseJSON x
21−
22−-- Slight misnomer.
23−type Score = Maybe Double
24−
25−{-| 'MappingName' is part of mappings which are how ES describes and schematizes
26− the data in the indices.
27−-}
28−newtype MappingName =
29− MappingName Text
30− deriving (Eq, Show, ToJSON, FromJSON)
31−
32−newtype ShardId = ShardId { shardId :: Int }
33− deriving (Eq, Show, FromJSON)
34−
35−{-| 'DocId' is a generic wrapper value for expressing unique Document IDs.
36− Can be set by the user or created by ES itself. Often used in client
37− functions for poking at specific documents.
38−-}
39−newtype DocId =
40− DocId Text
41− deriving (Eq, Show, ToJSON, FromJSON)
42−
43−{-| 'FieldName' is used all over the place wherever a specific field within
44− a document needs to be specified, usually in 'Query's or 'Filter's.
45−-}
46−newtype FieldName =
47− FieldName Text
48− deriving (Eq, Read, Show, ToJSON, FromJSON)
49−
50−newtype TypeName =
51− TypeName Text
52− deriving (Eq, Show, ToJSON, FromJSON)
53−
54−{-| 'QueryString' is used to wrap query text bodies, be they human written or not.
55−-}
56−newtype QueryString =
57− QueryString Text
58− deriving (Eq, Show, ToJSON, FromJSON)
59−
60−
61−-- {-| 'Script' is often used in place of 'FieldName' to specify more
62−-- complex ways of extracting a value from a document.
63−-- -}
64−-- newtype Script =
65−-- Script { scriptText :: Text }
66−-- deriving (Eq, Show)
67−
68−{-| 'CacheName' is used in 'RegexpFilter' for describing the
69− 'CacheKey' keyed caching behavior.
70−-}
71−newtype CacheName =
72− CacheName Text
73− deriving (Eq, Show, FromJSON, ToJSON)
74−
75−{-| 'CacheKey' is used in 'RegexpFilter' to key regex caching.
76−-}
77−newtype CacheKey =
78− CacheKey Text deriving (Eq, Show, FromJSON, ToJSON)
79−newtype Existence =
80− Existence Bool deriving (Eq, Show, FromJSON, ToJSON)
81−newtype NullValue =
82− NullValue Bool deriving (Eq, Show, FromJSON, ToJSON)
83−newtype CutoffFrequency =
84− CutoffFrequency Double deriving (Eq, Show, FromJSON, ToJSON)
85−newtype Analyzer =
86− Analyzer Text deriving (Eq, Show, FromJSON, ToJSON)
87−newtype MaxExpansions =
88− MaxExpansions Int deriving (Eq, Show, FromJSON, ToJSON)
89−
90−{-| 'Lenient', if set to true, will cause format based failures to be
91− ignored. I don't know what the bloody default is, Elasticsearch
92− documentation didn't say what it was. Let me know if you figure it out.
93−-}
94−newtype Lenient =
95− Lenient Bool deriving (Eq, Show, FromJSON, ToJSON)
96−newtype Tiebreaker =
97− Tiebreaker Double deriving (Eq, Show, FromJSON, ToJSON)
98−
99−{-| 'MinimumMatch' controls how many should clauses in the bool query should
100− match. Can be an absolute value (2) or a percentage (30%) or a
101− combination of both.
102−-}
103−newtype MinimumMatch =
104− MinimumMatch Int deriving (Eq, Show, FromJSON, ToJSON)
105−newtype DisableCoord =
106− DisableCoord Bool deriving (Eq, Show, FromJSON, ToJSON)
107−newtype IgnoreTermFrequency =
108− IgnoreTermFrequency Bool deriving (Eq, Show, FromJSON, ToJSON)
109−newtype MinimumTermFrequency =
110− MinimumTermFrequency Int deriving (Eq, Show, FromJSON, ToJSON)
111−newtype MaxQueryTerms =
112− MaxQueryTerms Int deriving (Eq, Show, FromJSON, ToJSON)
113−
114−{-| 'PrefixLength' is the prefix length used in queries, defaults to 0. -}
115−newtype PrefixLength =
116− PrefixLength Int deriving (Eq, Show, FromJSON, ToJSON)
117−newtype PercentMatch =
118− PercentMatch Double deriving (Eq, Show, FromJSON, ToJSON)
119−newtype StopWord =
120− StopWord Text deriving (Eq, Show, FromJSON, ToJSON)
121−newtype QueryPath =
122− QueryPath Text deriving (Eq, Show, FromJSON, ToJSON)
123−
124−{-| Allowing a wildcard at the beginning of a word (eg "*ing") is particularly
125− heavy, because all terms in the index need to be examined, just in case
126− they match. Leading wildcards can be disabled by setting
127− 'AllowLeadingWildcard' to false. -}
128−newtype AllowLeadingWildcard =
129− AllowLeadingWildcard Bool deriving (Eq, Show, FromJSON, ToJSON)
130−newtype LowercaseExpanded =
131− LowercaseExpanded Bool deriving (Eq, Show, FromJSON, ToJSON)
132−newtype EnablePositionIncrements =
133− EnablePositionIncrements Bool deriving (Eq, Show, FromJSON, ToJSON)
134−
135−{-| By default, wildcard terms in a query are not analyzed.
136− Setting 'AnalyzeWildcard' to true enables best-effort analysis.
137−-}
138−newtype AnalyzeWildcard = AnalyzeWildcard Bool deriving (Eq, Show, FromJSON, ToJSON)
139−
140−{-| 'GeneratePhraseQueries' defaults to false.
141−-}
142−newtype GeneratePhraseQueries =
143− GeneratePhraseQueries Bool deriving (Eq, Show, FromJSON, ToJSON)
144−
145−{-| 'Locale' is used for string conversions - defaults to ROOT.
146−-}
147−newtype Locale = Locale Text deriving (Eq, Show, FromJSON, ToJSON)
148−newtype MaxWordLength = MaxWordLength Int deriving (Eq, Show, FromJSON, ToJSON)
149−newtype MinWordLength = MinWordLength Int deriving (Eq, Show, FromJSON, ToJSON)
150−
151−{-| 'PhraseSlop' sets the default slop for phrases, 0 means exact
152− phrase matches. Default is 0.
153−-}
154−newtype PhraseSlop = PhraseSlop Int deriving (Eq, Show, FromJSON, ToJSON)
155−newtype MinDocFrequency = MinDocFrequency Int deriving (Eq, Show, FromJSON, ToJSON)
156−newtype MaxDocFrequency = MaxDocFrequency Int deriving (Eq, Show, FromJSON, ToJSON)
157−
158−-- | Newtype wrapper to parse ES's concerning tendency to in some APIs return a floating point number of milliseconds since epoch ಠ_ಠ
159−newtype POSIXMS = POSIXMS { posixMS :: UTCTime }
160−
161−instance FromJSON POSIXMS where
162− parseJSON = withScientific "POSIXMS" (return . parse)
163− where parse n =
164− let n' = truncate n :: Integer
165− in POSIXMS (posixSecondsToUTCTime (fromInteger (n' `div` 1000)))
166−
167−newtype Boost =
168− Boost Double
169− deriving (Eq, Show, ToJSON, FromJSON)
170−
171−newtype BoostTerms =
172− BoostTerms Double
173− deriving (Eq, Show, ToJSON, FromJSON)
174−
175−{-| 'ReplicaCount' is part of 'IndexSettings' -}
176−newtype ReplicaCount =
177− ReplicaCount Int
178− deriving (Eq, Show, ToJSON)
179−
180−{-| 'ShardCount' is part of 'IndexSettings' -}
181−newtype ShardCount =
182− ShardCount Int
183− deriving (Eq, Show, ToJSON)
184−
185−-- This insanity is because ES *sometimes* returns Replica/Shard counts as strings
186−instance FromJSON ReplicaCount where
187− parseJSON v = parseAsInt v
188− <|> parseAsString v
189− where parseAsInt = fmap ReplicaCount . parseJSON
190− parseAsString = withText "ReplicaCount" (fmap ReplicaCount . parseReadText)
191−
192−instance FromJSON ShardCount where
193− parseJSON v = parseAsInt v
194− <|> parseAsString v
195− where parseAsInt = fmap ShardCount . parseJSON
196− parseAsString = withText "ShardCount" (fmap ShardCount . parseReadText)
197−
198−{-| 'IndexName' is used to describe which index to query/create/delete -}
199−newtype IndexName =
200− IndexName Text
201− deriving (Eq, Show, ToJSON, FromJSON)
202−
203−newtype IndexAliasName =
204− IndexAliasName { indexAliasName :: IndexName }
205− deriving (Eq, Show, ToJSON)
206−
207−newtype MaybeNA a = MaybeNA { unMaybeNA :: Maybe a }
208− deriving (Show, Eq)
209−
210−instance FromJSON a => FromJSON (MaybeNA a) where
211− parseJSON (String "NA") = pure $ MaybeNA Nothing
212− parseJSON o = MaybeNA . Just <$> parseJSON o
213−
214−newtype SnapshotName =
215− SnapshotName { snapshotName :: Text }
216− deriving (Eq, Show, ToJSON, FromJSON)
217−
218−-- | Milliseconds
219−newtype MS = MS NominalDiffTime
220−
221−-- keeps the unexported constructor warnings at bay
222−unMS :: MS -> NominalDiffTime
223−unMS (MS t) = t
224−
225−
226−instance FromJSON MS where
227− parseJSON = withScientific "MS" (return . MS . parse)
228− where
229− parse n = fromInteger (truncate n * 1000)
230−
231−newtype TokenFilter =
232− TokenFilter Text
233− deriving (Eq, Show, FromJSON, ToJSON)
234−
235−newtype CharFilter =
236− CharFilter Text
237− deriving (Eq, Show, FromJSON, ToJSON)
src/Database/Bloodhound/Internal/Query.hsdeleted−1640
@@ -1,1640 +0,0 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−{-# LANGUAGE RecordWildCards #-}
4−
5−module Database.Bloodhound.Internal.Query
6− ( module X
7− , module Database.Bloodhound.Internal.Query
8− ) where
9−
10−import Bloodhound.Import
11−
12−import Data.Char (isNumber)
13−import qualified Data.HashMap.Strict as HM
14−import Data.List (nub)
15−import qualified Data.Text as T
16−
17−import Database.Bloodhound.Common.Script as X
18−import Database.Bloodhound.Internal.Newtypes
19−
20−data Query =
21− TermQuery Term (Maybe Boost)
22− | TermsQuery Text (NonEmpty Text)
23− | QueryMatchQuery MatchQuery
24− | QueryMultiMatchQuery MultiMatchQuery
25− | QueryBoolQuery BoolQuery
26− | QueryBoostingQuery BoostingQuery
27− | QueryCommonTermsQuery CommonTermsQuery
28− | ConstantScoreQuery Query Boost
29− | QueryFunctionScoreQuery FunctionScoreQuery
30− | QueryDisMaxQuery DisMaxQuery
31− | QueryFuzzyLikeThisQuery FuzzyLikeThisQuery
32− | QueryFuzzyLikeFieldQuery FuzzyLikeFieldQuery
33− | QueryFuzzyQuery FuzzyQuery
34− | QueryHasChildQuery HasChildQuery
35− | QueryHasParentQuery HasParentQuery
36− | IdsQuery MappingName [DocId]
37− | QueryIndicesQuery IndicesQuery
38− | MatchAllQuery (Maybe Boost)
39− | QueryMoreLikeThisQuery MoreLikeThisQuery
40− | QueryMoreLikeThisFieldQuery MoreLikeThisFieldQuery
41− | QueryNestedQuery NestedQuery
42− | QueryPrefixQuery PrefixQuery
43− | QueryQueryStringQuery QueryStringQuery
44− | QuerySimpleQueryStringQuery SimpleQueryStringQuery
45− | QueryRangeQuery RangeQuery
46− | QueryRegexpQuery RegexpQuery
47− | QueryExistsQuery FieldName
48− | QueryTemplateQueryInline TemplateQueryInline
49− | QueryMatchNoneQuery
50− | QueryWildcardQuery WildcardQuery
51− deriving (Eq, Show)
52−
53−instance ToJSON Query where
54− toJSON (TermQuery (Term termQueryField termQueryValue) boost) =
55− object [ "term" .=
56− object [termQueryField .= object merged]]
57− where
58− base = [ "value" .= termQueryValue ]
59− boosted = maybe [] (return . ("boost" .=)) boost
60− merged = mappend base boosted
61−
62− toJSON (TermsQuery fieldName terms) =
63− object [ "terms" .= object conjoined ]
64− where conjoined = [fieldName .= terms]
65−
66− toJSON (IdsQuery idsQueryMappingName docIds) =
67− object [ "ids" .= object conjoined ]
68− where conjoined = [ "type" .= idsQueryMappingName
69− , "values" .= fmap toJSON docIds ]
70−
71− toJSON (QueryQueryStringQuery qQueryStringQuery) =
72− object [ "query_string" .= qQueryStringQuery ]
73−
74− toJSON (QueryMatchQuery matchQuery) =
75− object [ "match" .= matchQuery ]
76−
77− toJSON (QueryMultiMatchQuery multiMatchQuery) =
78− toJSON multiMatchQuery
79−
80− toJSON (QueryBoolQuery boolQuery) =
81− object [ "bool" .= boolQuery ]
82−
83− toJSON (QueryBoostingQuery boostingQuery) =
84− object [ "boosting" .= boostingQuery ]
85−
86− toJSON (QueryCommonTermsQuery commonTermsQuery) =
87− object [ "common" .= commonTermsQuery ]
88−
89− toJSON (ConstantScoreQuery query boost) =
90− object ["constant_score" .= object ["query" .= query
91− , "boost" .= boost]]
92−
93− toJSON (QueryFunctionScoreQuery functionScoreQuery) =
94− object [ "function_score" .= functionScoreQuery ]
95−
96− toJSON (QueryDisMaxQuery disMaxQuery) =
97− object [ "dis_max" .= disMaxQuery ]
98−
99− toJSON (QueryFuzzyLikeThisQuery fuzzyQuery) =
100− object [ "fuzzy_like_this" .= fuzzyQuery ]
101−
102− toJSON (QueryFuzzyLikeFieldQuery fuzzyFieldQuery) =
103− object [ "fuzzy_like_this_field" .= fuzzyFieldQuery ]
104−
105− toJSON (QueryFuzzyQuery fuzzyQuery) =
106− object [ "fuzzy" .= fuzzyQuery ]
107−
108− toJSON (QueryHasChildQuery childQuery) =
109− object [ "has_child" .= childQuery ]
110−
111− toJSON (QueryHasParentQuery parentQuery) =
112− object [ "has_parent" .= parentQuery ]
113−
114− toJSON (QueryIndicesQuery qIndicesQuery) =
115− object [ "indices" .= qIndicesQuery ]
116−
117− toJSON (MatchAllQuery boost) =
118− object [ "match_all" .= omitNulls [ "boost" .= boost ] ]
119−
120− toJSON (QueryMoreLikeThisQuery query) =
121− object [ "more_like_this" .= query ]
122−
123− toJSON (QueryMoreLikeThisFieldQuery query) =
124− object [ "more_like_this_field" .= query ]
125−
126− toJSON (QueryNestedQuery query) =
127− object [ "nested" .= query ]
128−
129− toJSON (QueryPrefixQuery query) =
130− object [ "prefix" .= query ]
131−
132− toJSON (QueryRangeQuery query) =
133− object [ "range" .= query ]
134−
135− toJSON (QueryRegexpQuery query) =
136− object [ "regexp" .= query ]
137−
138− toJSON (QuerySimpleQueryStringQuery query) =
139− object [ "simple_query_string" .= query ]
140−
141− toJSON (QueryExistsQuery (FieldName fieldName)) =
142− object ["exists" .= object
143− ["field" .= fieldName]
144− ]
145− toJSON QueryMatchNoneQuery =
146− object ["match_none" .= object []]
147−
148− toJSON (QueryTemplateQueryInline templateQuery) =
149− object [ "template" .= templateQuery ]
150−
151− toJSON (QueryWildcardQuery query) =
152− object [ "wildcard" .= query ]
153−
154−instance FromJSON Query where
155− parseJSON v = withObject "Query" parse v
156− where parse o = termQuery `taggedWith` "term"
157− <|> termsQuery `taggedWith` "terms"
158− <|> idsQuery `taggedWith` "ids"
159− <|> queryQueryStringQuery `taggedWith` "query_string"
160− <|> queryMatchQuery `taggedWith` "match"
161− <|> queryMultiMatchQuery
162− <|> queryBoolQuery `taggedWith` "bool"
163− <|> queryBoostingQuery `taggedWith` "boosting"
164− <|> queryCommonTermsQuery `taggedWith` "common"
165− <|> constantScoreQuery `taggedWith` "constant_score"
166− <|> queryFunctionScoreQuery `taggedWith` "function_score"
167− <|> queryDisMaxQuery `taggedWith` "dis_max"
168− <|> queryFuzzyLikeThisQuery `taggedWith` "fuzzy_like_this"
169− <|> queryFuzzyLikeFieldQuery `taggedWith` "fuzzy_like_this_field"
170− <|> queryFuzzyQuery `taggedWith` "fuzzy"
171− <|> queryHasChildQuery `taggedWith` "has_child"
172− <|> queryHasParentQuery `taggedWith` "has_parent"
173− <|> queryIndicesQuery `taggedWith` "indices"
174− <|> matchAllQuery `taggedWith` "match_all"
175− <|> queryMoreLikeThisQuery `taggedWith` "more_like_this"
176− <|> queryMoreLikeThisFieldQuery `taggedWith` "more_like_this_field"
177− <|> queryNestedQuery `taggedWith` "nested"
178− <|> queryPrefixQuery `taggedWith` "prefix"
179− <|> queryRangeQuery `taggedWith` "range"
180− <|> queryRegexpQuery `taggedWith` "regexp"
181− <|> querySimpleQueryStringQuery `taggedWith` "simple_query_string"
182− <|> queryTemplateQueryInline `taggedWith` "template"
183− <|> queryWildcardQuery `taggedWith` "wildcard"
184− where taggedWith parser k = parser =<< o .: k
185− termQuery = fieldTagged $ \(FieldName fn) o ->
186− TermQuery <$> (Term fn <$> o .: "value") <*> o .:? "boost"
187− termsQuery o = case HM.toList o of
188− [(fn, vs)] -> do vals <- parseJSON vs
189− case vals of
190− x:xs -> return (TermsQuery fn (x :| xs))
191− _ -> fail "Expected non empty list of values"
192− _ -> fail "Expected object with 1 field-named key"
193− idsQuery o = IdsQuery <$> o .: "type"
194− <*> o .: "values"
195− queryQueryStringQuery = pure . QueryQueryStringQuery
196− queryMatchQuery = pure . QueryMatchQuery
197− queryMultiMatchQuery = QueryMultiMatchQuery <$> parseJSON v
198− queryBoolQuery = pure . QueryBoolQuery
199− queryBoostingQuery = pure . QueryBoostingQuery
200− queryCommonTermsQuery = pure . QueryCommonTermsQuery
201− constantScoreQuery o = case HM.lookup "query" o of
202− Just x -> ConstantScoreQuery <$> parseJSON x
203− <*> o .: "boost"
204− _ -> fail "Does not appear to be a ConstantScoreQuery"
205− queryFunctionScoreQuery = pure . QueryFunctionScoreQuery
206− queryDisMaxQuery = pure . QueryDisMaxQuery
207− queryFuzzyLikeThisQuery = pure . QueryFuzzyLikeThisQuery
208− queryFuzzyLikeFieldQuery = pure . QueryFuzzyLikeFieldQuery
209− queryFuzzyQuery = pure . QueryFuzzyQuery
210− queryHasChildQuery = pure . QueryHasChildQuery
211− queryHasParentQuery = pure . QueryHasParentQuery
212− queryIndicesQuery = pure . QueryIndicesQuery
213− matchAllQuery o = MatchAllQuery <$> o .:? "boost"
214− queryMoreLikeThisQuery = pure . QueryMoreLikeThisQuery
215− queryMoreLikeThisFieldQuery = pure . QueryMoreLikeThisFieldQuery
216− queryNestedQuery = pure . QueryNestedQuery
217− queryPrefixQuery = pure . QueryPrefixQuery
218− queryRangeQuery = pure . QueryRangeQuery
219− queryRegexpQuery = pure . QueryRegexpQuery
220− querySimpleQueryStringQuery = pure . QuerySimpleQueryStringQuery
221− -- queryExistsQuery o = QueryExistsQuery <$> o .: "field"
222− queryTemplateQueryInline = pure . QueryTemplateQueryInline
223− queryWildcardQuery = pure . QueryWildcardQuery
224−
225−-- | As of Elastic 2.0, 'Filters' are just 'Queries' housed in a
226−-- Bool Query, and flagged in a different context.
227−newtype Filter = Filter { unFilter :: Query }
228− deriving (Eq, Show)
229−
230−instance ToJSON Filter where
231− toJSON = toJSON . unFilter
232−
233−instance FromJSON Filter where
234− parseJSON v = Filter <$> parseJSON v
235−
236−data RegexpQuery =
237− RegexpQuery { regexpQueryField :: FieldName
238− , regexpQuery :: Regexp
239− , regexpQueryFlags :: RegexpFlags
240− , regexpQueryBoost :: Maybe Boost
241− } deriving (Eq, Show)
242−
243−instance ToJSON RegexpQuery where
244− toJSON (RegexpQuery (FieldName rqQueryField)
245− (Regexp regexpQueryQuery) rqQueryFlags
246− rqQueryBoost) =
247− object [ rqQueryField .= omitNulls base ]
248− where base = [ "value" .= regexpQueryQuery
249− , "flags" .= rqQueryFlags
250− , "boost" .= rqQueryBoost ]
251−
252−instance FromJSON RegexpQuery where
253− parseJSON = withObject "RegexpQuery" parse
254− where parse = fieldTagged $ \fn o ->
255− RegexpQuery fn
256− <$> o .: "value"
257− <*> o .: "flags"
258− <*> o .:? "boost"
259−
260−data WildcardQuery =
261− WildcardQuery { wildcardQueryField :: FieldName
262− , wildcardQuery :: Text
263− , wildcardQueryBoost :: Maybe Boost
264− } deriving (Eq, Show)
265−
266−instance ToJSON WildcardQuery where
267− toJSON (WildcardQuery (FieldName wcQueryField)
268− (wcQueryQuery) wcQueryBoost) =
269− object [ wcQueryField .= omitNulls base ]
270− where base = [ "value" .= wcQueryQuery
271− , "boost" .= wcQueryBoost ]
272−
273−instance FromJSON WildcardQuery where
274− parseJSON = withObject "WildcardQuery" parse
275− where parse = fieldTagged $ \fn o ->
276− WildcardQuery fn
277− <$> o .: "value"
278− <*> o .:? "boost"
279−
280−data RangeQuery =
281− RangeQuery { rangeQueryField :: FieldName
282− , rangeQueryRange :: RangeValue
283− , rangeQueryBoost :: Boost } deriving (Eq, Show)
284−
285−instance ToJSON RangeQuery where
286− toJSON (RangeQuery (FieldName fieldName) range boost) =
287− object [ fieldName .= object conjoined ]
288− where
289− conjoined = ("boost" .= boost) : rangeValueToPair range
290−
291−instance FromJSON RangeQuery where
292− parseJSON = withObject "RangeQuery" parse
293− where parse = fieldTagged $ \fn o ->
294− RangeQuery fn
295− <$> parseJSON (Object o)
296− <*> o .: "boost"
297−
298−mkRangeQuery :: FieldName -> RangeValue -> RangeQuery
299−mkRangeQuery f r = RangeQuery f r (Boost 1.0)
300−
301−data SimpleQueryStringQuery =
302− SimpleQueryStringQuery
303− { simpleQueryStringQuery :: QueryString
304− , simpleQueryStringField :: Maybe FieldOrFields
305− , simpleQueryStringOperator :: Maybe BooleanOperator
306− , simpleQueryStringAnalyzer :: Maybe Analyzer
307− , simpleQueryStringFlags :: Maybe (NonEmpty SimpleQueryFlag)
308− , simpleQueryStringLowercaseExpanded :: Maybe LowercaseExpanded
309− , simpleQueryStringLocale :: Maybe Locale
310− } deriving (Eq, Show)
311−
312−
313−instance ToJSON SimpleQueryStringQuery where
314− toJSON SimpleQueryStringQuery {..} =
315− omitNulls (base ++ maybeAdd)
316− where base = [ "query" .= simpleQueryStringQuery ]
317− maybeAdd = [ "fields" .= simpleQueryStringField
318− , "default_operator" .= simpleQueryStringOperator
319− , "analyzer" .= simpleQueryStringAnalyzer
320− , "flags" .= simpleQueryStringFlags
321− , "lowercase_expanded_terms" .= simpleQueryStringLowercaseExpanded
322− , "locale" .= simpleQueryStringLocale ]
323−
324−instance FromJSON SimpleQueryStringQuery where
325− parseJSON = withObject "SimpleQueryStringQuery" parse
326− where parse o = SimpleQueryStringQuery <$> o .: "query"
327− <*> o .:? "fields"
328− <*> o .:? "default_operator"
329− <*> o .:? "analyzer"
330− <*> (parseFlags <$> o .:? "flags")
331− <*> o .:? "lowercase_expanded_terms"
332− <*> o .:? "locale"
333− parseFlags (Just (x:xs)) = Just (x :| xs)
334− parseFlags _ = Nothing
335−
336−data SimpleQueryFlag =
337− SimpleQueryAll
338− | SimpleQueryNone
339− | SimpleQueryAnd
340− | SimpleQueryOr
341− | SimpleQueryPrefix
342− | SimpleQueryPhrase
343− | SimpleQueryPrecedence
344− | SimpleQueryEscape
345− | SimpleQueryWhitespace
346− | SimpleQueryFuzzy
347− | SimpleQueryNear
348− | SimpleQuerySlop deriving (Eq, Show)
349−
350−instance ToJSON SimpleQueryFlag where
351− toJSON SimpleQueryAll = "ALL"
352− toJSON SimpleQueryNone = "NONE"
353− toJSON SimpleQueryAnd = "AND"
354− toJSON SimpleQueryOr = "OR"
355− toJSON SimpleQueryPrefix = "PREFIX"
356− toJSON SimpleQueryPhrase = "PHRASE"
357− toJSON SimpleQueryPrecedence = "PRECEDENCE"
358− toJSON SimpleQueryEscape = "ESCAPE"
359− toJSON SimpleQueryWhitespace = "WHITESPACE"
360− toJSON SimpleQueryFuzzy = "FUZZY"
361− toJSON SimpleQueryNear = "NEAR"
362− toJSON SimpleQuerySlop = "SLOP"
363−
364−instance FromJSON SimpleQueryFlag where
365− parseJSON = withText "SimpleQueryFlag" parse
366− where parse "ALL" = pure SimpleQueryAll
367− parse "NONE" = pure SimpleQueryNone
368− parse "AND" = pure SimpleQueryAnd
369− parse "OR" = pure SimpleQueryOr
370− parse "PREFIX" = pure SimpleQueryPrefix
371− parse "PHRASE" = pure SimpleQueryPhrase
372− parse "PRECEDENCE" = pure SimpleQueryPrecedence
373− parse "ESCAPE" = pure SimpleQueryEscape
374− parse "WHITESPACE" = pure SimpleQueryWhitespace
375− parse "FUZZY" = pure SimpleQueryFuzzy
376− parse "NEAR" = pure SimpleQueryNear
377− parse "SLOP" = pure SimpleQuerySlop
378− parse f = fail ("Unexpected SimpleQueryFlag: " <> show f)
379−
380−-- use_dis_max and tie_breaker when fields are plural?
381−data QueryStringQuery =
382− QueryStringQuery
383− { queryStringQuery :: QueryString
384− , queryStringDefaultField :: Maybe FieldName
385− , queryStringOperator :: Maybe BooleanOperator
386− , queryStringAnalyzer :: Maybe Analyzer
387− , queryStringAllowLeadingWildcard :: Maybe AllowLeadingWildcard
388− , queryStringLowercaseExpanded :: Maybe LowercaseExpanded
389− , queryStringEnablePositionIncrements :: Maybe EnablePositionIncrements
390− , queryStringFuzzyMaxExpansions :: Maybe MaxExpansions
391− , queryStringFuzziness :: Maybe Fuzziness
392− , queryStringFuzzyPrefixLength :: Maybe PrefixLength
393− , queryStringPhraseSlop :: Maybe PhraseSlop
394− , queryStringBoost :: Maybe Boost
395− , queryStringAnalyzeWildcard :: Maybe AnalyzeWildcard
396− , queryStringGeneratePhraseQueries :: Maybe GeneratePhraseQueries
397− , queryStringMinimumShouldMatch :: Maybe MinimumMatch
398− , queryStringLenient :: Maybe Lenient
399− , queryStringLocale :: Maybe Locale
400− } deriving (Eq, Show)
401−
402−
403−instance ToJSON QueryStringQuery where
404− toJSON (QueryStringQuery qsQueryString
405− qsDefaultField qsOperator
406− qsAnalyzer qsAllowWildcard
407− qsLowercaseExpanded qsEnablePositionIncrements
408− qsFuzzyMaxExpansions qsFuzziness
409− qsFuzzyPrefixLength qsPhraseSlop
410− qsBoost qsAnalyzeWildcard
411− qsGeneratePhraseQueries qsMinimumShouldMatch
412− qsLenient qsLocale) =
413− omitNulls base
414− where
415− base = [ "query" .= qsQueryString
416− , "default_field" .= qsDefaultField
417− , "default_operator" .= qsOperator
418− , "analyzer" .= qsAnalyzer
419− , "allow_leading_wildcard" .= qsAllowWildcard
420− , "lowercase_expanded_terms" .= qsLowercaseExpanded
421− , "enable_position_increments" .= qsEnablePositionIncrements
422− , "fuzzy_max_expansions" .= qsFuzzyMaxExpansions
423− , "fuzziness" .= qsFuzziness
424− , "fuzzy_prefix_length" .= qsFuzzyPrefixLength
425− , "phrase_slop" .= qsPhraseSlop
426− , "boost" .= qsBoost
427− , "analyze_wildcard" .= qsAnalyzeWildcard
428− , "auto_generate_phrase_queries" .= qsGeneratePhraseQueries
429− , "minimum_should_match" .= qsMinimumShouldMatch
430− , "lenient" .= qsLenient
431− , "locale" .= qsLocale ]
432−
433−instance FromJSON QueryStringQuery where
434− parseJSON = withObject "QueryStringQuery" parse
435− where parse o = QueryStringQuery
436− <$> o .: "query"
437− <*> o .:? "default_field"
438− <*> o .:? "default_operator"
439− <*> o .:? "analyzer"
440− <*> o .:? "allow_leading_wildcard"
441− <*> o .:? "lowercase_expanded_terms"
442− <*> o .:? "enable_position_increments"
443− <*> o .:? "fuzzy_max_expansions"
444− <*> o .:? "fuzziness"
445− <*> o .:? "fuzzy_prefix_length"
446− <*> o .:? "phrase_slop"
447− <*> o .:? "boost"
448− <*> o .:? "analyze_wildcard"
449− <*> o .:? "auto_generate_phrase_queries"
450− <*> o .:? "minimum_should_match"
451− <*> o .:? "lenient"
452− <*> o .:? "locale"
453−
454−mkQueryStringQuery :: QueryString -> QueryStringQuery
455−mkQueryStringQuery qs =
456− QueryStringQuery qs Nothing Nothing
457− Nothing Nothing Nothing Nothing
458− Nothing Nothing Nothing Nothing
459− Nothing Nothing Nothing Nothing
460− Nothing Nothing
461−
462−data FieldOrFields = FofField FieldName
463− | FofFields (NonEmpty FieldName) deriving (Eq, Show)
464−
465−instance ToJSON FieldOrFields where
466− toJSON (FofField fieldName) =
467− toJSON fieldName
468− toJSON (FofFields fieldNames) =
469− toJSON fieldNames
470−
471−instance FromJSON FieldOrFields where
472− parseJSON v = FofField <$> parseJSON v
473− <|> FofFields <$> (parseNEJSON =<< parseJSON v)
474−
475−data PrefixQuery =
476− PrefixQuery
477− { prefixQueryField :: FieldName
478− , prefixQueryPrefixValue :: Text
479− , prefixQueryBoost :: Maybe Boost } deriving (Eq, Show)
480−
481−instance ToJSON PrefixQuery where
482− toJSON (PrefixQuery (FieldName fieldName) queryValue boost) =
483− object [ fieldName .= omitNulls base ]
484− where base = [ "value" .= queryValue
485− , "boost" .= boost ]
486−
487−instance FromJSON PrefixQuery where
488− parseJSON = withObject "PrefixQuery" parse
489− where parse = fieldTagged $ \fn o ->
490− PrefixQuery fn
491− <$> o .: "value"
492− <*> o .:? "boost"
493−
494−data NestedQuery =
495− NestedQuery
496− { nestedQueryPath :: QueryPath
497− , nestedQueryScoreType :: ScoreType
498− , nestedQuery :: Query } deriving (Eq, Show)
499−
500−instance ToJSON NestedQuery where
501− toJSON (NestedQuery nqPath nqScoreType nqQuery) =
502− object [ "path" .= nqPath
503− , "score_mode" .= nqScoreType
504− , "query" .= nqQuery ]
505−
506−instance FromJSON NestedQuery where
507− parseJSON = withObject "NestedQuery" parse
508− where parse o = NestedQuery
509− <$> o .: "path"
510− <*> o .: "score_mode"
511− <*> o .: "query"
512−
513−data MoreLikeThisFieldQuery =
514− MoreLikeThisFieldQuery
515− { moreLikeThisFieldText :: Text
516− , moreLikeThisFieldFields :: FieldName
517− -- default 0.3 (30%)
518− , moreLikeThisFieldPercentMatch :: Maybe PercentMatch
519− , moreLikeThisFieldMinimumTermFreq :: Maybe MinimumTermFrequency
520− , moreLikeThisFieldMaxQueryTerms :: Maybe MaxQueryTerms
521− , moreLikeThisFieldStopWords :: Maybe (NonEmpty StopWord)
522− , moreLikeThisFieldMinDocFrequency :: Maybe MinDocFrequency
523− , moreLikeThisFieldMaxDocFrequency :: Maybe MaxDocFrequency
524− , moreLikeThisFieldMinWordLength :: Maybe MinWordLength
525− , moreLikeThisFieldMaxWordLength :: Maybe MaxWordLength
526− , moreLikeThisFieldBoostTerms :: Maybe BoostTerms
527− , moreLikeThisFieldBoost :: Maybe Boost
528− , moreLikeThisFieldAnalyzer :: Maybe Analyzer
529− } deriving (Eq, Show)
530−
531−
532−instance ToJSON MoreLikeThisFieldQuery where
533− toJSON (MoreLikeThisFieldQuery text (FieldName fieldName)
534− percent mtf mqt stopwords mindf maxdf
535− minwl maxwl boostTerms boost analyzer) =
536− object [ fieldName .= omitNulls base ]
537− where base = [ "like_text" .= text
538− , "percent_terms_to_match" .= percent
539− , "min_term_freq" .= mtf
540− , "max_query_terms" .= mqt
541− , "stop_words" .= stopwords
542− , "min_doc_freq" .= mindf
543− , "max_doc_freq" .= maxdf
544− , "min_word_length" .= minwl
545− , "max_word_length" .= maxwl
546− , "boost_terms" .= boostTerms
547− , "boost" .= boost
548− , "analyzer" .= analyzer ]
549−
550−instance FromJSON MoreLikeThisFieldQuery where
551− parseJSON = withObject "MoreLikeThisFieldQuery" parse
552− where parse = fieldTagged $ \fn o ->
553− MoreLikeThisFieldQuery
554− <$> o .: "like_text"
555− <*> pure fn
556− <*> o .:? "percent_terms_to_match"
557− <*> o .:? "min_term_freq"
558− <*> o .:? "max_query_terms"
559− -- <*> (optionalNE =<< o .:? "stop_words")
560− <*> o .:? "stop_words"
561− <*> o .:? "min_doc_freq"
562− <*> o .:? "max_doc_freq"
563− <*> o .:? "min_word_length"
564− <*> o .:? "max_word_length"
565− <*> o .:? "boost_terms"
566− <*> o .:? "boost"
567− <*> o .:? "analyzer"
568− -- optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
569−
570−data MoreLikeThisQuery =
571− MoreLikeThisQuery
572− { moreLikeThisText :: Text
573− , moreLikeThisFields :: Maybe (NonEmpty FieldName)
574− -- default 0.3 (30%)
575− , moreLikeThisPercentMatch :: Maybe PercentMatch
576− , moreLikeThisMinimumTermFreq :: Maybe MinimumTermFrequency
577− , moreLikeThisMaxQueryTerms :: Maybe MaxQueryTerms
578− , moreLikeThisStopWords :: Maybe (NonEmpty StopWord)
579− , moreLikeThisMinDocFrequency :: Maybe MinDocFrequency
580− , moreLikeThisMaxDocFrequency :: Maybe MaxDocFrequency
581− , moreLikeThisMinWordLength :: Maybe MinWordLength
582− , moreLikeThisMaxWordLength :: Maybe MaxWordLength
583− , moreLikeThisBoostTerms :: Maybe BoostTerms
584− , moreLikeThisBoost :: Maybe Boost
585− , moreLikeThisAnalyzer :: Maybe Analyzer
586− } deriving (Eq, Show)
587−
588−
589−instance ToJSON MoreLikeThisQuery where
590− toJSON (MoreLikeThisQuery text fields percent
591− mtf mqt stopwords mindf maxdf
592− minwl maxwl boostTerms boost analyzer) =
593− omitNulls base
594− where base = [ "like_text" .= text
595− , "fields" .= fields
596− , "percent_terms_to_match" .= percent
597− , "min_term_freq" .= mtf
598− , "max_query_terms" .= mqt
599− , "stop_words" .= stopwords
600− , "min_doc_freq" .= mindf
601− , "max_doc_freq" .= maxdf
602− , "min_word_length" .= minwl
603− , "max_word_length" .= maxwl
604− , "boost_terms" .= boostTerms
605− , "boost" .= boost
606− , "analyzer" .= analyzer ]
607−
608−instance FromJSON MoreLikeThisQuery where
609− parseJSON = withObject "MoreLikeThisQuery" parse
610− where parse o = MoreLikeThisQuery
611− <$> o .: "like_text"
612− -- <*> (optionalNE =<< o .:? "fields")
613− <*> o .:? "fields"
614− <*> o .:? "percent_terms_to_match"
615− <*> o .:? "min_term_freq"
616− <*> o .:? "max_query_terms"
617− -- <*> (optionalNE =<< o .:? "stop_words")
618− <*> o .:? "stop_words"
619− <*> o .:? "min_doc_freq"
620− <*> o .:? "max_doc_freq"
621− <*> o .:? "min_word_length"
622− <*> o .:? "max_word_length"
623− <*> o .:? "boost_terms"
624− <*> o .:? "boost"
625− <*> o .:? "analyzer"
626− -- optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
627−
628−data IndicesQuery =
629− IndicesQuery
630− { indicesQueryIndices :: [IndexName]
631− , indicesQuery :: Query
632− -- default "all"
633− , indicesQueryNoMatch :: Maybe Query } deriving (Eq, Show)
634−
635−
636−instance ToJSON IndicesQuery where
637− toJSON (IndicesQuery indices query noMatch) =
638− omitNulls [ "indices" .= indices
639− , "no_match_query" .= noMatch
640− , "query" .= query ]
641−
642−instance FromJSON IndicesQuery where
643− parseJSON = withObject "IndicesQuery" parse
644− where parse o = IndicesQuery
645− <$> o .:? "indices" .!= []
646− <*> o .: "query"
647− <*> o .:? "no_match_query"
648−
649−data HasParentQuery =
650− HasParentQuery
651− { hasParentQueryType :: TypeName
652− , hasParentQuery :: Query
653− , hasParentQueryScoreType :: Maybe ScoreType } deriving (Eq, Show)
654−
655−instance ToJSON HasParentQuery where
656− toJSON (HasParentQuery queryType query scoreType) =
657− omitNulls [ "parent_type" .= queryType
658− , "score_type" .= scoreType
659− , "query" .= query ]
660−
661−instance FromJSON HasParentQuery where
662− parseJSON = withObject "HasParentQuery" parse
663− where parse o = HasParentQuery
664− <$> o .: "parent_type"
665− <*> o .: "query"
666− <*> o .:? "score_type"
667−
668−data HasChildQuery =
669− HasChildQuery
670− { hasChildQueryType :: TypeName
671− , hasChildQuery :: Query
672− , hasChildQueryScoreType :: Maybe ScoreType } deriving (Eq, Show)
673−
674−instance ToJSON HasChildQuery where
675− toJSON (HasChildQuery queryType query scoreType) =
676− omitNulls [ "query" .= query
677− , "score_type" .= scoreType
678− , "type" .= queryType ]
679−
680−instance FromJSON HasChildQuery where
681− parseJSON = withObject "HasChildQuery" parse
682− where parse o = HasChildQuery
683− <$> o .: "type"
684− <*> o .: "query"
685− <*> o .:? "score_type"
686−
687−data ScoreType =
688− ScoreTypeMax
689− | ScoreTypeSum
690− | ScoreTypeAvg
691− | ScoreTypeNone deriving (Eq, Show)
692−
693−instance ToJSON ScoreType where
694− toJSON ScoreTypeMax = "max"
695− toJSON ScoreTypeAvg = "avg"
696− toJSON ScoreTypeSum = "sum"
697− toJSON ScoreTypeNone = "none"
698−
699−instance FromJSON ScoreType where
700− parseJSON = withText "ScoreType" parse
701− where parse "max" = pure ScoreTypeMax
702− parse "avg" = pure ScoreTypeAvg
703− parse "sum" = pure ScoreTypeSum
704− parse "none" = pure ScoreTypeNone
705− parse t = fail ("Unexpected ScoreType: " <> show t)
706−
707−data FuzzyQuery =
708− FuzzyQuery { fuzzyQueryField :: FieldName
709− , fuzzyQueryValue :: Text
710− , fuzzyQueryPrefixLength :: PrefixLength
711− , fuzzyQueryMaxExpansions :: MaxExpansions
712− , fuzzyQueryFuzziness :: Fuzziness
713− , fuzzyQueryBoost :: Maybe Boost
714− } deriving (Eq, Show)
715−
716−
717−instance ToJSON FuzzyQuery where
718− toJSON (FuzzyQuery (FieldName fieldName) queryText
719− prefixLength maxEx fuzziness boost) =
720− object [ fieldName .= omitNulls base ]
721− where base = [ "value" .= queryText
722− , "fuzziness" .= fuzziness
723− , "prefix_length" .= prefixLength
724− , "boost" .= boost
725− , "max_expansions" .= maxEx ]
726−
727−instance FromJSON FuzzyQuery where
728− parseJSON = withObject "FuzzyQuery" parse
729− where parse = fieldTagged $ \fn o ->
730− FuzzyQuery fn
731− <$> o .: "value"
732− <*> o .: "prefix_length"
733− <*> o .: "max_expansions"
734− <*> o .: "fuzziness"
735− <*> o .:? "boost"
736−
737−data FuzzyLikeFieldQuery =
738− FuzzyLikeFieldQuery
739− { fuzzyLikeField :: FieldName
740− -- anaphora is good for the soul.
741− , fuzzyLikeFieldText :: Text
742− , fuzzyLikeFieldMaxQueryTerms :: MaxQueryTerms
743− , fuzzyLikeFieldIgnoreTermFrequency :: IgnoreTermFrequency
744− , fuzzyLikeFieldFuzziness :: Fuzziness
745− , fuzzyLikeFieldPrefixLength :: PrefixLength
746− , fuzzyLikeFieldBoost :: Boost
747− , fuzzyLikeFieldAnalyzer :: Maybe Analyzer
748− } deriving (Eq, Show)
749−
750−
751−instance ToJSON FuzzyLikeFieldQuery where
752− toJSON (FuzzyLikeFieldQuery (FieldName fieldName)
753− fieldText maxTerms ignoreFreq fuzziness prefixLength
754− boost analyzer) =
755− object [ fieldName .=
756− omitNulls [ "like_text" .= fieldText
757− , "max_query_terms" .= maxTerms
758− , "ignore_tf" .= ignoreFreq
759− , "fuzziness" .= fuzziness
760− , "prefix_length" .= prefixLength
761− , "analyzer" .= analyzer
762− , "boost" .= boost ]]
763−
764−instance FromJSON FuzzyLikeFieldQuery where
765− parseJSON = withObject "FuzzyLikeFieldQuery" parse
766− where parse = fieldTagged $ \fn o ->
767− FuzzyLikeFieldQuery fn
768− <$> o .: "like_text"
769− <*> o .: "max_query_terms"
770− <*> o .: "ignore_tf"
771− <*> o .: "fuzziness"
772− <*> o .: "prefix_length"
773− <*> o .: "boost"
774− <*> o .:? "analyzer"
775−
776−data FuzzyLikeThisQuery =
777− FuzzyLikeThisQuery
778− { fuzzyLikeFields :: [FieldName]
779− , fuzzyLikeText :: Text
780− , fuzzyLikeMaxQueryTerms :: MaxQueryTerms
781− , fuzzyLikeIgnoreTermFrequency :: IgnoreTermFrequency
782− , fuzzyLikeFuzziness :: Fuzziness
783− , fuzzyLikePrefixLength :: PrefixLength
784− , fuzzyLikeBoost :: Boost
785− , fuzzyLikeAnalyzer :: Maybe Analyzer
786− } deriving (Eq, Show)
787−
788−
789−instance ToJSON FuzzyLikeThisQuery where
790− toJSON (FuzzyLikeThisQuery fields text maxTerms
791− ignoreFreq fuzziness prefixLength boost analyzer) =
792− omitNulls base
793− where base = [ "fields" .= fields
794− , "like_text" .= text
795− , "max_query_terms" .= maxTerms
796− , "ignore_tf" .= ignoreFreq
797− , "fuzziness" .= fuzziness
798− , "prefix_length" .= prefixLength
799− , "analyzer" .= analyzer
800− , "boost" .= boost ]
801−
802−instance FromJSON FuzzyLikeThisQuery where
803− parseJSON = withObject "FuzzyLikeThisQuery" parse
804− where parse o = FuzzyLikeThisQuery
805− <$> o .:? "fields" .!= []
806− <*> o .: "like_text"
807− <*> o .: "max_query_terms"
808− <*> o .: "ignore_tf"
809− <*> o .: "fuzziness"
810− <*> o .: "prefix_length"
811− <*> o .: "boost"
812− <*> o .:? "analyzer"
813−
814−data DisMaxQuery =
815− DisMaxQuery { disMaxQueries :: [Query]
816− -- default 0.0
817− , disMaxTiebreaker :: Tiebreaker
818− , disMaxBoost :: Maybe Boost
819− } deriving (Eq, Show)
820−
821−
822−instance ToJSON DisMaxQuery where
823− toJSON (DisMaxQuery queries tiebreaker boost) =
824− omitNulls base
825− where base = [ "queries" .= queries
826− , "boost" .= boost
827− , "tie_breaker" .= tiebreaker ]
828−
829−instance FromJSON DisMaxQuery where
830− parseJSON = withObject "DisMaxQuery" parse
831− where parse o = DisMaxQuery
832− <$> o .:? "queries" .!= []
833− <*> o .: "tie_breaker"
834− <*> o .:? "boost"
835−
836−data MatchQuery = MatchQuery
837− { matchQueryField :: FieldName
838− , matchQueryQueryString :: QueryString
839− , matchQueryOperator :: BooleanOperator
840− , matchQueryZeroTerms :: ZeroTermsQuery
841− , matchQueryCutoffFrequency :: Maybe CutoffFrequency
842− , matchQueryMatchType :: Maybe MatchQueryType
843− , matchQueryAnalyzer :: Maybe Analyzer
844− , matchQueryMaxExpansions :: Maybe MaxExpansions
845− , matchQueryLenient :: Maybe Lenient
846− , matchQueryBoost :: Maybe Boost
847− , matchQueryMinimumShouldMatch :: Maybe Text
848− , matchQueryFuzziness :: Maybe Fuzziness
849− } deriving (Eq, Show)
850−
851−
852−instance ToJSON MatchQuery where
853− toJSON (MatchQuery (FieldName fieldName)
854− (QueryString mqQueryString) booleanOperator
855− zeroTermsQuery cutoffFrequency matchQueryType
856− analyzer maxExpansions lenient boost
857− minShouldMatch mqFuzziness
858− ) =
859− object [ fieldName .= omitNulls base ]
860− where base = [ "query" .= mqQueryString
861− , "operator" .= booleanOperator
862− , "zero_terms_query" .= zeroTermsQuery
863− , "cutoff_frequency" .= cutoffFrequency
864− , "type" .= matchQueryType
865− , "analyzer" .= analyzer
866− , "max_expansions" .= maxExpansions
867− , "lenient" .= lenient
868− , "boost" .= boost
869− , "minimum_should_match" .= minShouldMatch
870− , "fuzziness" .= mqFuzziness
871− ]
872−
873−instance FromJSON MatchQuery where
874− parseJSON = withObject "MatchQuery" parse
875− where parse = fieldTagged $ \fn o ->
876− MatchQuery fn
877− <$> o .: "query"
878− <*> o .: "operator"
879− <*> o .: "zero_terms_query"
880− <*> o .:? "cutoff_frequency"
881− <*> o .:? "type"
882− <*> o .:? "analyzer"
883− <*> o .:? "max_expansions"
884− <*> o .:? "lenient"
885− <*> o .:? "boost"
886− <*> o .:? "minimum_should_match"
887− <*> o .:? "fuzziness"
888−
889−{-| 'mkMatchQuery' is a convenience function that defaults the less common parameters,
890− enabling you to provide only the 'FieldName' and 'QueryString' to make a 'MatchQuery'
891−-}
892−mkMatchQuery :: FieldName -> QueryString -> MatchQuery
893−mkMatchQuery field query = MatchQuery field query Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
894−
895−data MatchQueryType =
896− MatchPhrase
897− | MatchPhrasePrefix deriving (Eq, Show)
898−
899−instance ToJSON MatchQueryType where
900− toJSON MatchPhrase = "phrase"
901− toJSON MatchPhrasePrefix = "phrase_prefix"
902−
903−instance FromJSON MatchQueryType where
904− parseJSON = withText "MatchQueryType" parse
905− where parse "phrase" = pure MatchPhrase
906− parse "phrase_prefix" = pure MatchPhrasePrefix
907− parse t = fail ("Unexpected MatchQueryType: " <> show t)
908−
909−data MultiMatchQuery = MultiMatchQuery
910− { multiMatchQueryFields :: [FieldName]
911− , multiMatchQueryString :: QueryString
912− , multiMatchQueryOperator :: BooleanOperator
913− , multiMatchQueryZeroTerms :: ZeroTermsQuery
914− , multiMatchQueryTiebreaker :: Maybe Tiebreaker
915− , multiMatchQueryType :: Maybe MultiMatchQueryType
916− , multiMatchQueryCutoffFrequency :: Maybe CutoffFrequency
917− , multiMatchQueryAnalyzer :: Maybe Analyzer
918− , multiMatchQueryMaxExpansions :: Maybe MaxExpansions
919− , multiMatchQueryLenient :: Maybe Lenient
920− } deriving (Eq, Show)
921−
922−instance ToJSON MultiMatchQuery where
923− toJSON (MultiMatchQuery fields (QueryString query) boolOp
924− ztQ tb mmqt cf analyzer maxEx lenient) =
925− object ["multi_match" .= omitNulls base]
926− where base = [ "fields" .= fmap toJSON fields
927− , "query" .= query
928− , "operator" .= boolOp
929− , "zero_terms_query" .= ztQ
930− , "tie_breaker" .= tb
931− , "type" .= mmqt
932− , "cutoff_frequency" .= cf
933− , "analyzer" .= analyzer
934− , "max_expansions" .= maxEx
935− , "lenient" .= lenient ]
936−
937−instance FromJSON MultiMatchQuery where
938− parseJSON = withObject "MultiMatchQuery" parse
939− where parse raw = do o <- raw .: "multi_match"
940− MultiMatchQuery
941− <$> o .:? "fields" .!= []
942− <*> o .: "query"
943− <*> o .: "operator"
944− <*> o .: "zero_terms_query"
945− <*> o .:? "tie_breaker"
946− <*> o .:? "type"
947− <*> o .:? "cutoff_frequency"
948− <*> o .:? "analyzer"
949− <*> o .:? "max_expansions"
950− <*> o .:? "lenient"
951−
952−{-| 'mkMultiMatchQuery' is a convenience function that defaults the less common parameters,
953− enabling you to provide only the list of 'FieldName's and 'QueryString' to
954− make a 'MultiMatchQuery'.
955−-}
956−
957−mkMultiMatchQuery :: [FieldName] -> QueryString -> MultiMatchQuery
958−mkMultiMatchQuery matchFields query =
959− MultiMatchQuery matchFields query
960− Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
961−
962−data MultiMatchQueryType =
963− MultiMatchBestFields
964− | MultiMatchMostFields
965− | MultiMatchCrossFields
966− | MultiMatchPhrase
967− | MultiMatchPhrasePrefix deriving (Eq, Show)
968−
969−instance ToJSON MultiMatchQueryType where
970− toJSON MultiMatchBestFields = "best_fields"
971− toJSON MultiMatchMostFields = "most_fields"
972− toJSON MultiMatchCrossFields = "cross_fields"
973− toJSON MultiMatchPhrase = "phrase"
974− toJSON MultiMatchPhrasePrefix = "phrase_prefix"
975−
976−instance FromJSON MultiMatchQueryType where
977− parseJSON = withText "MultiMatchPhrasePrefix" parse
978− where parse "best_fields" = pure MultiMatchBestFields
979− parse "most_fields" = pure MultiMatchMostFields
980− parse "cross_fields" = pure MultiMatchCrossFields
981− parse "phrase" = pure MultiMatchPhrase
982− parse "phrase_prefix" = pure MultiMatchPhrasePrefix
983− parse t = fail ("Unexpected MultiMatchPhrasePrefix: " <> show t)
984−
985−data BoolQuery =
986− BoolQuery { boolQueryMustMatch :: [Query]
987− , boolQueryFilter :: [Filter]
988− , boolQueryMustNotMatch :: [Query]
989− , boolQueryShouldMatch :: [Query]
990− , boolQueryMinimumShouldMatch :: Maybe MinimumMatch
991− , boolQueryBoost :: Maybe Boost
992− , boolQueryDisableCoord :: Maybe DisableCoord
993− } deriving (Eq, Show)
994−
995−
996−instance ToJSON BoolQuery where
997− toJSON (BoolQuery mustM filterM' notM shouldM bqMin boost disableCoord) =
998− omitNulls base
999− where base = [ "must" .= mustM
1000− , "filter" .= filterM'
1001− , "must_not" .= notM
1002− , "should" .= shouldM
1003− , "minimum_should_match" .= bqMin
1004− , "boost" .= boost
1005− , "disable_coord" .= disableCoord ]
1006−
1007−instance FromJSON BoolQuery where
1008− parseJSON = withObject "BoolQuery" parse
1009− where parse o = BoolQuery
1010− <$> o .:? "must" .!= []
1011− <*> o .:? "filter" .!= []
1012− <*> o .:? "must_not" .!= []
1013− <*> o .:? "should" .!= []
1014− <*> o .:? "minimum_should_match"
1015− <*> o .:? "boost"
1016− <*> o .:? "disable_coord"
1017−
1018−mkBoolQuery :: [Query] -> [Filter] -> [Query] -> [Query] -> BoolQuery
1019−mkBoolQuery must filt mustNot should =
1020− BoolQuery must filt mustNot should Nothing Nothing Nothing
1021−
1022−data BoostingQuery =
1023− BoostingQuery { positiveQuery :: Query
1024− , negativeQuery :: Query
1025− , negativeBoost :: Boost } deriving (Eq, Show)
1026−
1027−instance ToJSON BoostingQuery where
1028− toJSON (BoostingQuery bqPositiveQuery bqNegativeQuery bqNegativeBoost) =
1029− object [ "positive" .= bqPositiveQuery
1030− , "negative" .= bqNegativeQuery
1031− , "negative_boost" .= bqNegativeBoost ]
1032−
1033−instance FromJSON BoostingQuery where
1034− parseJSON = withObject "BoostingQuery" parse
1035− where parse o = BoostingQuery
1036− <$> o .: "positive"
1037− <*> o .: "negative"
1038− <*> o .: "negative_boost"
1039−
1040−data CommonTermsQuery =
1041− CommonTermsQuery { commonField :: FieldName
1042− , commonQuery :: QueryString
1043− , commonCutoffFrequency :: CutoffFrequency
1044− , commonLowFreqOperator :: BooleanOperator
1045− , commonHighFreqOperator :: BooleanOperator
1046− , commonMinimumShouldMatch :: Maybe CommonMinimumMatch
1047− , commonBoost :: Maybe Boost
1048− , commonAnalyzer :: Maybe Analyzer
1049− , commonDisableCoord :: Maybe DisableCoord
1050− } deriving (Eq, Show)
1051−
1052−
1053−instance ToJSON CommonTermsQuery where
1054− toJSON (CommonTermsQuery (FieldName fieldName)
1055− (QueryString query) cf lfo hfo msm
1056− boost analyzer disableCoord) =
1057− object [fieldName .= omitNulls base ]
1058− where base = [ "query" .= query
1059− , "cutoff_frequency" .= cf
1060− , "low_freq_operator" .= lfo
1061− , "minimum_should_match" .= msm
1062− , "boost" .= boost
1063− , "analyzer" .= analyzer
1064− , "disable_coord" .= disableCoord
1065− , "high_freq_operator" .= hfo ]
1066−
1067−instance FromJSON CommonTermsQuery where
1068− parseJSON = withObject "CommonTermsQuery" parse
1069− where parse = fieldTagged $ \fn o ->
1070− CommonTermsQuery fn
1071− <$> o .: "query"
1072− <*> o .: "cutoff_frequency"
1073− <*> o .: "low_freq_operator"
1074− <*> o .: "high_freq_operator"
1075− <*> o .:? "minimum_should_match"
1076− <*> o .:? "boost"
1077− <*> o .:? "analyzer"
1078− <*> o .:? "disable_coord"
1079−
1080−data CommonMinimumMatch =
1081− CommonMinimumMatchHighLow MinimumMatchHighLow
1082− | CommonMinimumMatch MinimumMatch
1083− deriving (Eq, Show)
1084−
1085−
1086−instance ToJSON CommonMinimumMatch where
1087− toJSON (CommonMinimumMatch mm) = toJSON mm
1088− toJSON (CommonMinimumMatchHighLow (MinimumMatchHighLow lowF highF)) =
1089− object [ "low_freq" .= lowF
1090− , "high_freq" .= highF ]
1091−
1092−instance FromJSON CommonMinimumMatch where
1093− parseJSON v = parseMinimum v
1094− <|> parseMinimumHighLow v
1095− where parseMinimum = fmap CommonMinimumMatch . parseJSON
1096− parseMinimumHighLow = fmap CommonMinimumMatchHighLow . withObject "CommonMinimumMatchHighLow" (\o ->
1097− MinimumMatchHighLow
1098− <$> o .: "low_freq"
1099− <*> o .: "high_freq")
1100−
1101−data MinimumMatchHighLow =
1102− MinimumMatchHighLow { lowFreq :: MinimumMatch
1103− , highFreq :: MinimumMatch } deriving (Eq, Show)
1104−
1105−data ZeroTermsQuery =
1106− ZeroTermsNone
1107− | ZeroTermsAll deriving (Eq, Show)
1108−
1109−instance ToJSON ZeroTermsQuery where
1110− toJSON ZeroTermsNone = String "none"
1111− toJSON ZeroTermsAll = String "all"
1112−
1113−instance FromJSON ZeroTermsQuery where
1114− parseJSON = withText "ZeroTermsQuery" parse
1115− where parse "none" = pure ZeroTermsNone
1116− parse "all" = pure ZeroTermsAll
1117− parse q = fail ("Unexpected ZeroTermsQuery: " <> show q)
1118−
1119−data RangeExecution = RangeExecutionIndex
1120− | RangeExecutionFielddata deriving (Eq, Show)
1121−
1122−-- index for smaller ranges, fielddata for longer ranges
1123−instance ToJSON RangeExecution where
1124− toJSON RangeExecutionIndex = "index"
1125− toJSON RangeExecutionFielddata = "fielddata"
1126−
1127−
1128−instance FromJSON RangeExecution where
1129− parseJSON = withText "RangeExecution" parse
1130− where parse "index" = pure RangeExecutionIndex
1131− parse "fielddata" = pure RangeExecutionFielddata
1132− parse t = error ("Unrecognized RangeExecution " <> show t)
1133−
1134−newtype Regexp = Regexp Text deriving (Eq, Show, FromJSON)
1135−
1136−data RegexpFlags = AllRegexpFlags
1137− | NoRegexpFlags
1138− | SomeRegexpFlags (NonEmpty RegexpFlag) deriving (Eq, Show)
1139−
1140−instance ToJSON RegexpFlags where
1141− toJSON AllRegexpFlags = String "ALL"
1142− toJSON NoRegexpFlags = String "NONE"
1143− toJSON (SomeRegexpFlags (h :| fs)) = String $ T.intercalate "|" flagStrs
1144− where flagStrs = map flagStr . nub $ h:fs
1145− flagStr AnyString = "ANYSTRING"
1146− flagStr Automaton = "AUTOMATON"
1147− flagStr Complement = "COMPLEMENT"
1148− flagStr Empty = "EMPTY"
1149− flagStr Intersection = "INTERSECTION"
1150− flagStr Interval = "INTERVAL"
1151−
1152−instance FromJSON RegexpFlags where
1153− parseJSON = withText "RegexpFlags" parse
1154− where parse "ALL" = pure AllRegexpFlags
1155− parse "NONE" = pure NoRegexpFlags
1156− parse t = SomeRegexpFlags <$> parseNEJSON (String <$> T.splitOn "|" t)
1157−
1158−data RegexpFlag = AnyString
1159− | Automaton
1160− | Complement
1161− | Empty
1162− | Intersection
1163− | Interval deriving (Eq, Show)
1164−
1165−instance FromJSON RegexpFlag where
1166− parseJSON = withText "RegexpFlag" parse
1167− where parse "ANYSTRING" = pure AnyString
1168− parse "AUTOMATON" = pure Automaton
1169− parse "COMPLEMENT" = pure Complement
1170− parse "EMPTY" = pure Empty
1171− parse "INTERSECTION" = pure Intersection
1172− parse "INTERVAL" = pure Interval
1173− parse f = fail ("Unknown RegexpFlag: " <> show f)
1174−
1175−newtype LessThan = LessThan Double deriving (Eq, Show)
1176−newtype LessThanEq = LessThanEq Double deriving (Eq, Show)
1177−newtype GreaterThan = GreaterThan Double deriving (Eq, Show)
1178−newtype GreaterThanEq = GreaterThanEq Double deriving (Eq, Show)
1179−
1180−newtype LessThanD = LessThanD UTCTime deriving (Eq, Show)
1181−newtype LessThanEqD = LessThanEqD UTCTime deriving (Eq, Show)
1182−newtype GreaterThanD = GreaterThanD UTCTime deriving (Eq, Show)
1183−newtype GreaterThanEqD = GreaterThanEqD UTCTime deriving (Eq, Show)
1184−
1185−data RangeValue = RangeDateLte LessThanEqD
1186− | RangeDateLt LessThanD
1187− | RangeDateGte GreaterThanEqD
1188− | RangeDateGt GreaterThanD
1189− | RangeDateGtLt GreaterThanD LessThanD
1190− | RangeDateGteLte GreaterThanEqD LessThanEqD
1191− | RangeDateGteLt GreaterThanEqD LessThanD
1192− | RangeDateGtLte GreaterThanD LessThanEqD
1193− | RangeDoubleLte LessThanEq
1194− | RangeDoubleLt LessThan
1195− | RangeDoubleGte GreaterThanEq
1196− | RangeDoubleGt GreaterThan
1197− | RangeDoubleGtLt GreaterThan LessThan
1198− | RangeDoubleGteLte GreaterThanEq LessThanEq
1199− | RangeDoubleGteLt GreaterThanEq LessThan
1200− | RangeDoubleGtLte GreaterThan LessThanEq
1201− deriving (Eq, Show)
1202−
1203−
1204−parseRangeValue :: ( FromJSON t4
1205− , FromJSON t3
1206− , FromJSON t2
1207− , FromJSON t1
1208− )
1209− => (t3 -> t5)
1210− -> (t1 -> t6)
1211− -> (t4 -> t7)
1212− -> (t2 -> t8)
1213− -> (t5 -> t6 -> b)
1214− -> (t7 -> t6 -> b)
1215− -> (t5 -> t8 -> b)
1216− -> (t7 -> t8 -> b)
1217− -> (t5 -> b)
1218− -> (t6 -> b)
1219− -> (t7 -> b)
1220− -> (t8 -> b)
1221− -> Parser b
1222− -> Object
1223− -> Parser b
1224−parseRangeValue mkGt mkLt mkGte mkLte
1225− fGtLt fGteLt fGtLte fGteLte
1226− fGt fLt fGte fLte nada o = do
1227− lt <- o .:? "lt"
1228− lte <- o .:? "lte"
1229− gt <- o .:? "gt"
1230− gte <- o .:? "gte"
1231− case (lt, lte, gt, gte) of
1232− (Just a, _, Just b, _) ->
1233− return (fGtLt (mkGt b) (mkLt a))
1234− (Just a, _, _, Just b) ->
1235− return (fGteLt (mkGte b) (mkLt a))
1236− (_, Just a, Just b, _) ->
1237− return (fGtLte (mkGt b) (mkLte a))
1238− (_, Just a, _, Just b) ->
1239− return (fGteLte (mkGte b) (mkLte a))
1240− (_, _, Just a, _) ->
1241− return (fGt (mkGt a))
1242− (Just a, _, _, _) ->
1243− return (fLt (mkLt a))
1244− (_, _, _, Just a) ->
1245− return (fGte (mkGte a))
1246− (_, Just a, _, _) ->
1247− return (fLte (mkLte a))
1248− (Nothing, Nothing, Nothing, Nothing) ->
1249− nada
1250−
1251−
1252−instance FromJSON RangeValue where
1253− parseJSON = withObject "RangeValue" parse
1254− where parse o = parseDate o
1255− <|> parseDouble o
1256− parseDate o =
1257− parseRangeValue
1258− GreaterThanD LessThanD
1259− GreaterThanEqD LessThanEqD
1260− RangeDateGtLt RangeDateGteLt
1261− RangeDateGtLte RangeDateGteLte
1262− RangeDateGt RangeDateLt
1263− RangeDateGte RangeDateLte
1264− mzero o
1265− parseDouble o =
1266− parseRangeValue
1267− GreaterThan LessThan
1268− GreaterThanEq LessThanEq
1269− RangeDoubleGtLt RangeDoubleGteLt
1270− RangeDoubleGtLte RangeDoubleGteLte
1271− RangeDoubleGt RangeDoubleLt
1272− RangeDoubleGte RangeDoubleLte
1273− mzero o
1274−
1275−rangeValueToPair :: RangeValue -> [Pair]
1276−rangeValueToPair rv = case rv of
1277− RangeDateLte (LessThanEqD t) -> ["lte" .= t]
1278− RangeDateGte (GreaterThanEqD t) -> ["gte" .= t]
1279− RangeDateLt (LessThanD t) -> ["lt" .= t]
1280− RangeDateGt (GreaterThanD t) -> ["gt" .= t]
1281− RangeDateGteLte (GreaterThanEqD l) (LessThanEqD g) -> ["gte" .= l, "lte" .= g]
1282− RangeDateGtLte (GreaterThanD l) (LessThanEqD g) -> ["gt" .= l, "lte" .= g]
1283− RangeDateGteLt (GreaterThanEqD l) (LessThanD g) -> ["gte" .= l, "lt" .= g]
1284− RangeDateGtLt (GreaterThanD l) (LessThanD g) -> ["gt" .= l, "lt" .= g]
1285− RangeDoubleLte (LessThanEq t) -> ["lte" .= t]
1286− RangeDoubleGte (GreaterThanEq t) -> ["gte" .= t]
1287− RangeDoubleLt (LessThan t) -> ["lt" .= t]
1288− RangeDoubleGt (GreaterThan t) -> ["gt" .= t]
1289− RangeDoubleGteLte (GreaterThanEq l) (LessThanEq g) -> ["gte" .= l, "lte" .= g]
1290− RangeDoubleGtLte (GreaterThan l) (LessThanEq g) -> ["gt" .= l, "lte" .= g]
1291− RangeDoubleGteLt (GreaterThanEq l) (LessThan g) -> ["gte" .= l, "lt" .= g]
1292− RangeDoubleGtLt (GreaterThan l) (LessThan g) -> ["gt" .= l, "lt" .= g]
1293−
1294−data Term = Term { termField :: Text
1295− , termValue :: Text } deriving (Eq, Show)
1296−
1297−instance ToJSON Term where
1298− toJSON (Term field value) = object ["term" .= object
1299− [field .= value]]
1300−
1301−instance FromJSON Term where
1302− parseJSON = withObject "Term" parse
1303− where parse o = do termObj <- o .: "term"
1304− case HM.toList termObj of
1305− [(fn, v)] -> Term fn <$> parseJSON v
1306− _ -> fail "Expected object with 1 field-named key"
1307−
1308−data BoolMatch = MustMatch Term Cache
1309− | MustNotMatch Term Cache
1310− | ShouldMatch [Term] Cache deriving (Eq, Show)
1311−
1312−
1313−instance ToJSON BoolMatch where
1314− toJSON (MustMatch term cache) = object ["must" .= term,
1315− "_cache" .= cache]
1316− toJSON (MustNotMatch term cache) = object ["must_not" .= term,
1317− "_cache" .= cache]
1318− toJSON (ShouldMatch terms cache) = object ["should" .= fmap toJSON terms,
1319− "_cache" .= cache]
1320−
1321−instance FromJSON BoolMatch where
1322− parseJSON = withObject "BoolMatch" parse
1323− where parse o = mustMatch `taggedWith` "must"
1324− <|> mustNotMatch `taggedWith` "must_not"
1325− <|> shouldMatch `taggedWith` "should"
1326− where taggedWith parser k = parser =<< o .: k
1327− mustMatch t = MustMatch t <$> o .:? "_cache" .!= defaultCache
1328− mustNotMatch t = MustNotMatch t <$> o .:? "_cache" .!= defaultCache
1329− shouldMatch t = ShouldMatch t <$> o .:? "_cache" .!= defaultCache
1330−
1331−-- "memory" or "indexed"
1332−data GeoFilterType = GeoFilterMemory
1333− | GeoFilterIndexed deriving (Eq, Show)
1334−
1335−instance ToJSON GeoFilterType where
1336− toJSON GeoFilterMemory = String "memory"
1337− toJSON GeoFilterIndexed = String "indexed"
1338−
1339−instance FromJSON GeoFilterType where
1340− parseJSON = withText "GeoFilterType" parse
1341− where parse "memory" = pure GeoFilterMemory
1342− parse "indexed" = pure GeoFilterIndexed
1343− parse t = fail ("Unrecognized GeoFilterType: " <> show t)
1344−
1345−data LatLon = LatLon { lat :: Double
1346− , lon :: Double } deriving (Eq, Show)
1347−
1348−instance ToJSON LatLon where
1349− toJSON (LatLon lLat lLon) =
1350− object ["lat" .= lLat
1351− , "lon" .= lLon]
1352−
1353−instance FromJSON LatLon where
1354− parseJSON = withObject "LatLon" parse
1355− where parse o = LatLon <$> o .: "lat"
1356− <*> o .: "lon"
1357−
1358−data GeoBoundingBox =
1359− GeoBoundingBox { topLeft :: LatLon
1360− , bottomRight :: LatLon } deriving (Eq, Show)
1361−
1362−instance ToJSON GeoBoundingBox where
1363− toJSON (GeoBoundingBox gbbTopLeft gbbBottomRight) =
1364− object ["top_left" .= gbbTopLeft
1365− , "bottom_right" .= gbbBottomRight]
1366−
1367−instance FromJSON GeoBoundingBox where
1368− parseJSON = withObject "GeoBoundingBox" parse
1369− where parse o = GeoBoundingBox
1370− <$> o .: "top_left"
1371− <*> o .: "bottom_right"
1372−
1373−data GeoBoundingBoxConstraint =
1374− GeoBoundingBoxConstraint { geoBBField :: FieldName
1375− , constraintBox :: GeoBoundingBox
1376− , bbConstraintcache :: Cache
1377− , geoType :: GeoFilterType
1378− } deriving (Eq, Show)
1379−
1380−instance ToJSON GeoBoundingBoxConstraint where
1381− toJSON (GeoBoundingBoxConstraint
1382− (FieldName gbbcGeoBBField) gbbcConstraintBox cache type') =
1383− object [gbbcGeoBBField .= gbbcConstraintBox
1384− , "_cache" .= cache
1385− , "type" .= type']
1386−
1387−instance FromJSON GeoBoundingBoxConstraint where
1388− parseJSON = withObject "GeoBoundingBoxConstraint" parse
1389− where parse o = case HM.toList (deleteSeveral ["type", "_cache"] o) of
1390− [(fn, v)] -> GeoBoundingBoxConstraint (FieldName fn)
1391− <$> parseJSON v
1392− <*> o .:? "_cache" .!= defaultCache
1393− <*> o .: "type"
1394− _ -> fail "Could not find field name for GeoBoundingBoxConstraint"
1395−
1396−data GeoPoint =
1397− GeoPoint { geoField :: FieldName
1398− , latLon :: LatLon} deriving (Eq, Show)
1399−
1400−instance ToJSON GeoPoint where
1401− toJSON (GeoPoint (FieldName geoPointField) geoPointLatLon) =
1402− object [ geoPointField .= geoPointLatLon ]
1403−
1404−data DistanceUnit = Miles
1405− | Yards
1406− | Feet
1407− | Inches
1408− | Kilometers
1409− | Meters
1410− | Centimeters
1411− | Millimeters
1412− | NauticalMiles deriving (Eq, Show)
1413−
1414−instance ToJSON DistanceUnit where
1415− toJSON Miles = String "mi"
1416− toJSON Yards = String "yd"
1417− toJSON Feet = String "ft"
1418− toJSON Inches = String "in"
1419− toJSON Kilometers = String "km"
1420− toJSON Meters = String "m"
1421− toJSON Centimeters = String "cm"
1422− toJSON Millimeters = String "mm"
1423− toJSON NauticalMiles = String "nmi"
1424−
1425−instance FromJSON DistanceUnit where
1426− parseJSON = withText "DistanceUnit" parse
1427− where parse "mi" = pure Miles
1428− parse "yd" = pure Yards
1429− parse "ft" = pure Feet
1430− parse "in" = pure Inches
1431− parse "km" = pure Kilometers
1432− parse "m" = pure Meters
1433− parse "cm" = pure Centimeters
1434− parse "mm" = pure Millimeters
1435− parse "nmi" = pure NauticalMiles
1436− parse u = fail ("Unrecognized DistanceUnit: " <> show u)
1437−
1438−data DistanceType = Arc
1439− | SloppyArc -- doesn't exist <1.0
1440− | Plane deriving (Eq, Show)
1441−
1442−instance ToJSON DistanceType where
1443− toJSON Arc = String "arc"
1444− toJSON SloppyArc = String "sloppy_arc"
1445− toJSON Plane = String "plane"
1446−
1447−instance FromJSON DistanceType where
1448− parseJSON = withText "DistanceType" parse
1449− where parse "arc" = pure Arc
1450− parse "sloppy_arc" = pure SloppyArc
1451− parse "plane" = pure Plane
1452− parse t = fail ("Unrecognized DistanceType: " <> show t)
1453−
1454−data OptimizeBbox = OptimizeGeoFilterType GeoFilterType
1455− | NoOptimizeBbox deriving (Eq, Show)
1456−
1457−
1458−instance ToJSON OptimizeBbox where
1459− toJSON NoOptimizeBbox = String "none"
1460− toJSON (OptimizeGeoFilterType gft) = toJSON gft
1461−
1462−instance FromJSON OptimizeBbox where
1463− parseJSON v = withText "NoOptimizeBbox" parseNoOptimize v
1464− <|> parseOptimize v
1465− where parseNoOptimize "none" = pure NoOptimizeBbox
1466− parseNoOptimize _ = mzero
1467− parseOptimize = fmap OptimizeGeoFilterType . parseJSON
1468−
1469−data Distance =
1470− Distance { coefficient :: Double
1471− , unit :: DistanceUnit } deriving (Eq, Show)
1472−
1473−
1474−instance ToJSON Distance where
1475− toJSON (Distance dCoefficient dUnit) =
1476− String boltedTogether where
1477− coefText = showText dCoefficient
1478− (String unitText) = toJSON dUnit
1479− boltedTogether = mappend coefText unitText
1480−
1481−instance FromJSON Distance where
1482− parseJSON = withText "Distance" parse
1483− where parse t = Distance <$> parseCoeff nT
1484− <*> parseJSON (String unitT)
1485− where (nT, unitT) = T.span validForNumber t
1486− -- may be a better way to do this
1487− validForNumber '-' = True
1488− validForNumber '.' = True
1489− validForNumber 'e' = True
1490− validForNumber c = isNumber c
1491− parseCoeff "" = fail "Empty string cannot be parsed as number"
1492− parseCoeff s = return (read (T.unpack s))
1493−
1494−data DistanceRange =
1495− DistanceRange { distanceFrom :: Distance
1496− , distanceTo :: Distance } deriving (Eq, Show)
1497−
1498−type TemplateQueryKey = Text
1499−type TemplateQueryValue = Text
1500−
1501−newtype TemplateQueryKeyValuePairs =
1502− TemplateQueryKeyValuePairs (HM.HashMap TemplateQueryKey TemplateQueryValue)
1503− deriving (Eq, Show)
1504−
1505−instance ToJSON TemplateQueryKeyValuePairs where
1506− toJSON (TemplateQueryKeyValuePairs x) =
1507− Object $ HM.map toJSON x
1508−
1509−instance FromJSON TemplateQueryKeyValuePairs where
1510− parseJSON (Object o) =
1511− pure . TemplateQueryKeyValuePairs $ HM.mapMaybe getValue o
1512− where getValue (String x) = Just x
1513− getValue _ = Nothing
1514− parseJSON _ =
1515− fail "error parsing TemplateQueryKeyValuePairs"
1516−
1517−data TemplateQueryInline =
1518− TemplateQueryInline { inline :: Query
1519− , params :: TemplateQueryKeyValuePairs
1520− }
1521− deriving (Eq, Show)
1522−
1523−instance ToJSON TemplateQueryInline where
1524− toJSON TemplateQueryInline{..} = object [ "inline" .= inline
1525− , "params" .= params
1526− ]
1527−
1528−instance FromJSON TemplateQueryInline where
1529− parseJSON = withObject "TemplateQueryInline" parse
1530− where parse o = TemplateQueryInline
1531− <$> o .: "inline"
1532− <*> o .: "params"
1533−
1534−{-| 'BooleanOperator' is the usual And/Or operators with an ES compatible
1535− JSON encoding baked in. Used all over the place.
1536−-}
1537−data BooleanOperator = And | Or deriving (Eq, Show)
1538−
1539−instance ToJSON BooleanOperator where
1540− toJSON And = String "and"
1541− toJSON Or = String "or"
1542−
1543−instance FromJSON BooleanOperator where
1544− parseJSON = withText "BooleanOperator" parse
1545− where parse "and" = pure And
1546− parse "or" = pure Or
1547− parse o = fail ("Unexpected BooleanOperator: " <> show o)
1548−
1549−{-| 'Cache' is for telling ES whether it should cache a 'Filter' not.
1550− 'Query's cannot be cached.
1551−-}
1552−type Cache = Bool -- caching on/off
1553−defaultCache :: Cache
1554−defaultCache = False
1555−
1556−
1557−data FunctionScoreQuery =
1558− FunctionScoreQuery { functionScoreQuery :: Maybe Query
1559− , functionScoreBoost :: Maybe Boost
1560− , functionScoreFunctions :: FunctionScoreFunctions
1561− , functionScoreMaxBoost :: Maybe Boost
1562− , functionScoreBoostMode :: Maybe BoostMode
1563− , functionScoreMinScore :: Score
1564− , functionScoreScoreMode :: Maybe ScoreMode
1565− } deriving (Eq, Show)
1566−
1567−instance ToJSON FunctionScoreQuery where
1568− toJSON (FunctionScoreQuery query boost fns maxBoost boostMode minScore scoreMode) =
1569− omitNulls base
1570− where base = functionScoreFunctionsPair fns :
1571− [ "query" .= query
1572− , "boost" .= boost
1573− , "max_boost" .= maxBoost
1574− , "boost_mode" .= boostMode
1575− , "min_score" .= minScore
1576− , "score_mode" .= scoreMode ]
1577−
1578−instance FromJSON FunctionScoreQuery where
1579− parseJSON = withObject "FunctionScoreQuery" parse
1580− where parse o = FunctionScoreQuery
1581− <$> o .:? "query"
1582− <*> o .:? "boost"
1583− <*> (singleFunction o
1584− <|> multipleFunctions `taggedWith` "functions")
1585− <*> o .:? "max_boost"
1586− <*> o .:? "boost_mode"
1587− <*> o .:? "min_score"
1588− <*> o .:? "score_mode"
1589− where taggedWith parser k = parser =<< o .: k
1590− singleFunction = fmap FunctionScoreSingle . parseFunctionScoreFunction
1591− multipleFunctions = pure . FunctionScoreMultiple
1592−
1593−data FunctionScoreFunctions =
1594− FunctionScoreSingle FunctionScoreFunction
1595− | FunctionScoreMultiple (NonEmpty ComponentFunctionScoreFunction) deriving (Eq, Show)
1596−
1597−data ComponentFunctionScoreFunction =
1598− ComponentFunctionScoreFunction { componentScoreFunctionFilter :: Maybe Filter
1599− , componentScoreFunction :: FunctionScoreFunction
1600− , componentScoreFunctionWeight :: Maybe Weight
1601− } deriving (Eq, Show)
1602−
1603−instance ToJSON ComponentFunctionScoreFunction where
1604− toJSON (ComponentFunctionScoreFunction filter' fn weight) =
1605− omitNulls base
1606− where base = functionScoreFunctionPair fn :
1607− [ "filter" .= filter'
1608− , "weight" .= weight ]
1609−
1610−instance FromJSON ComponentFunctionScoreFunction where
1611− parseJSON = withObject "ComponentFunctionScoreFunction" parse
1612− where parse o = ComponentFunctionScoreFunction
1613− <$> o .:? "filter"
1614− <*> parseFunctionScoreFunction o
1615− <*> o .:? "weight"
1616−
1617−functionScoreFunctionsPair :: FunctionScoreFunctions -> (Text, Value)
1618−functionScoreFunctionsPair (FunctionScoreSingle fn)
1619− = functionScoreFunctionPair fn
1620−functionScoreFunctionsPair (FunctionScoreMultiple componentFns) =
1621− ("functions", toJSON componentFns)
1622−
1623−fieldTagged :: Monad m => (FieldName -> Object -> m a) -> Object -> m a
1624−fieldTagged f o = case HM.toList o of
1625− [(k, Object o')] -> f (FieldName k) o'
1626− _ -> fail "Expected object with 1 field-named key"
1627−
1628−-- | Fuzziness value as a number or 'AUTO'.
1629−-- See:
1630−-- https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness
1631−data Fuzziness = Fuzziness Double | FuzzinessAuto
1632− deriving (Eq, Show)
1633−
1634−instance ToJSON Fuzziness where
1635− toJSON (Fuzziness n) = toJSON n
1636− toJSON FuzzinessAuto = String "AUTO"
1637−
1638−instance FromJSON Fuzziness where
1639− parseJSON (String "AUTO") = return FuzzinessAuto
1640− parseJSON v = Fuzziness <$> parseJSON v
src/Database/Bloodhound/Internal/Sort.hsdeleted−107
@@ -1,107 +0,0 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−{-# LANGUAGE RecordWildCards #-}
4−
5−module Database.Bloodhound.Internal.Sort where
6−
7−import Bloodhound.Import
8−
9−import Database.Bloodhound.Internal.Newtypes
10−import Database.Bloodhound.Internal.Query
11−
12−{-| 'SortMode' prescribes how to handle sorting array/multi-valued fields.
13−
14−http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_sort_mode_option
15−-}
16−data SortMode = SortMin
17− | SortMax
18− | SortSum
19− | SortAvg deriving (Eq, Show)
20−
21−instance ToJSON SortMode where
22− toJSON SortMin = String "min"
23− toJSON SortMax = String "max"
24− toJSON SortSum = String "sum"
25− toJSON SortAvg = String "avg"
26−
27−{-| 'mkSort' defaults everything but the 'FieldName' and the 'SortOrder' so
28− that you can concisely describe the usual kind of 'SortSpec's you want.
29−-}
30−mkSort :: FieldName -> SortOrder -> DefaultSort
31−mkSort fieldName sOrder = DefaultSort fieldName sOrder Nothing Nothing Nothing Nothing
32−
33−{-| 'Sort' is a synonym for a list of 'SortSpec's. Sort behavior is order
34− dependent with later sorts acting as tie-breakers for earlier sorts.
35−-}
36−type Sort = [SortSpec]
37−
38−{-| The two main kinds of 'SortSpec' are 'DefaultSortSpec' and
39− 'GeoDistanceSortSpec'. The latter takes a 'SortOrder', 'GeoPoint', and
40− 'DistanceUnit' to express "nearness" to a single geographical point as a
41− sort specification.
42−
43−<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
44−-}
45−data SortSpec =
46− DefaultSortSpec DefaultSort
47− | GeoDistanceSortSpec SortOrder GeoPoint DistanceUnit
48− deriving (Eq, Show)
49−
50−instance ToJSON SortSpec where
51− toJSON (DefaultSortSpec
52− (DefaultSort (FieldName dsSortFieldName) dsSortOrder dsIgnoreUnmapped
53− dsSortMode dsMissingSort dsNestedFilter)) =
54− object [dsSortFieldName .= omitNulls base] where
55− base = [ "order" .= dsSortOrder
56− , "unmapped_type" .= dsIgnoreUnmapped
57− , "mode" .= dsSortMode
58− , "missing" .= dsMissingSort
59− , "nested_filter" .= dsNestedFilter ]
60−
61− toJSON (GeoDistanceSortSpec gdsSortOrder (GeoPoint (FieldName field) gdsLatLon) units) =
62− object [ "unit" .= units
63− , field .= gdsLatLon
64− , "order" .= gdsSortOrder ]
65−
66−{-| 'DefaultSort' is usually the kind of 'SortSpec' you'll want. There's a
67− 'mkSort' convenience function for when you want to specify only the most
68− common parameters.
69−
70− The `ignoreUnmapped`, when `Just` field is used to set the elastic 'unmapped_type'
71−
72−<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
73−-}
74−data DefaultSort =
75− DefaultSort { sortFieldName :: FieldName
76− , sortOrder :: SortOrder
77− -- default False
78− , ignoreUnmapped :: Maybe Text
79− , sortMode :: Maybe SortMode
80− , missingSort :: Maybe Missing
81− , nestedFilter :: Maybe Filter } deriving (Eq, Show)
82−
83−{-| 'SortOrder' is 'Ascending' or 'Descending', as you might expect. These get
84− encoded into "asc" or "desc" when turned into JSON.
85−
86−<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
87−-}
88−data SortOrder = Ascending
89− | Descending deriving (Eq, Show)
90−
91−instance ToJSON SortOrder where
92− toJSON Ascending = String "asc"
93− toJSON Descending = String "desc"
94−
95−{-| 'Missing' prescribes how to handle missing fields. A missing field can be
96− sorted last, first, or using a custom value as a substitute.
97−
98−<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_missing_values>
99−-}
100−data Missing = LastMissing
101− | FirstMissing
102− | CustomMissing Text deriving (Eq, Show)
103−
104−instance ToJSON Missing where
105− toJSON LastMissing = String "_last"
106− toJSON FirstMissing = String "_first"
107− toJSON (CustomMissing txt) = String txt
src/Database/Bloodhound/Internal/StringlyTyped.hsdeleted−51
@@ -1,51 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−
3−module Database.Bloodhound.Internal.StringlyTyped where
4−
5−import Bloodhound.Import
6−
7−import qualified Data.Text as T
8−
9−
10−-- This whole module is a sin bucket to deal with Elasticsearch badness.
11−newtype StringlyTypedDouble = StringlyTypedDouble
12− { unStringlyTypedDouble :: Double }
13−
14−instance FromJSON StringlyTypedDouble where
15− parseJSON =
16− fmap StringlyTypedDouble
17− . parseJSON
18− . unStringlyTypeJSON
19−
20−newtype StringlyTypedInt = StringlyTypedInt
21− { unStringlyTypedInt :: Int }
22−
23−instance FromJSON StringlyTypedInt where
24− parseJSON =
25− fmap StringlyTypedInt
26− . parseJSON
27− . unStringlyTypeJSON
28−
29−newtype StringlyTypedBool = StringlyTypedBool { unStringlyTypedBool :: Bool }
30−
31−instance FromJSON StringlyTypedBool where
32− parseJSON =
33− fmap StringlyTypedBool
34− . parseJSON
35− . unStringlyTypeJSON
36−
37−-- | For some reason in several settings APIs, all leaf values get returned
38−-- as strings. This function attempts to recover from this for all
39−-- non-recursive JSON types. If nothing can be done, the value is left alone.
40−unStringlyTypeJSON :: Value -> Value
41−unStringlyTypeJSON (String "true") =
42− Bool True
43−unStringlyTypeJSON (String "false") =
44− Bool False
45−unStringlyTypeJSON (String "null") =
46− Null
47−unStringlyTypeJSON v@(String t) =
48− case readMay (T.unpack t) of
49− Just n -> Number n
50− Nothing -> v
51−unStringlyTypeJSON v = v
src/Database/Bloodhound/Internal/Suggest.hsdeleted−270
@@ -1,270 +0,0 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE RecordWildCards #-}
3−{-# LANGUAGE OverloadedStrings #-}
4−
5−module Database.Bloodhound.Internal.Suggest where
6−
7−import Bloodhound.Import
8−
9−import qualified Data.HashMap.Strict as HM
10−
11−import Database.Bloodhound.Internal.Newtypes
12−import Database.Bloodhound.Internal.Query (TemplateQueryInline(..), params)
13−
14−data Suggest = Suggest
15− { suggestText :: Text
16− , suggestName :: Text
17− , suggestType :: SuggestType
18− } deriving (Eq, Show)
19−
20−instance ToJSON Suggest where
21− toJSON Suggest{..} =
22− object [ "text" .= suggestText
23− , suggestName .= suggestType
24− ]
25−
26−instance FromJSON Suggest where
27− parseJSON (Object o) = do
28− suggestText' <- o .: "text"
29− let dropTextList =
30− HM.toList
31− $ HM.filterWithKey (\x _ -> x /= "text") o
32− suggestName' <-
33− case dropTextList of
34− [(x, _)] -> return x
35− _ -> fail "error parsing Suggest field name"
36− suggestType' <- o .: suggestName'
37− return $ Suggest suggestText' suggestName' suggestType'
38− parseJSON x = typeMismatch "Suggest" x
39−
40−data SuggestType =
41− SuggestTypePhraseSuggester PhraseSuggester
42− deriving (Eq, Show)
43−
44−instance ToJSON SuggestType where
45− toJSON (SuggestTypePhraseSuggester x) =
46− object [ "phrase" .= x ]
47−
48−instance FromJSON SuggestType where
49− parseJSON = withObject "SuggestType" parse
50− where parse o = phraseSuggester `taggedWith` "phrase"
51− where taggedWith parser k = parser =<< o .: k
52− phraseSuggester = pure . SuggestTypePhraseSuggester
53−
54−data PhraseSuggester = PhraseSuggester
55− { phraseSuggesterField :: FieldName
56− , phraseSuggesterGramSize :: Maybe Int
57− , phraseSuggesterRealWordErrorLikelihood :: Maybe Int
58− , phraseSuggesterConfidence :: Maybe Int
59− , phraseSuggesterMaxErrors :: Maybe Int
60− , phraseSuggesterSeparator :: Maybe Text
61− , phraseSuggesterSize :: Maybe Size
62− , phraseSuggesterAnalyzer :: Maybe Analyzer
63− , phraseSuggesterShardSize :: Maybe Int
64− , phraseSuggesterHighlight :: Maybe PhraseSuggesterHighlighter
65− , phraseSuggesterCollate :: Maybe PhraseSuggesterCollate
66− , phraseSuggesterCandidateGenerators :: [DirectGenerators]
67− } deriving (Eq, Show)
68−
69−instance ToJSON PhraseSuggester where
70− toJSON PhraseSuggester{..} =
71− omitNulls [ "field" .= phraseSuggesterField
72− , "gram_size" .= phraseSuggesterGramSize
73− , "real_word_error_likelihood" .=
74− phraseSuggesterRealWordErrorLikelihood
75− , "confidence" .= phraseSuggesterConfidence
76− , "max_errors" .= phraseSuggesterMaxErrors
77− , "separator" .= phraseSuggesterSeparator
78− , "size" .= phraseSuggesterSize
79− , "analyzer" .= phraseSuggesterAnalyzer
80− , "shard_size" .= phraseSuggesterShardSize
81− , "highlight" .= phraseSuggesterHighlight
82− , "collate" .= phraseSuggesterCollate
83− , "direct_generator" .=
84− phraseSuggesterCandidateGenerators
85− ]
86−
87−instance FromJSON PhraseSuggester where
88− parseJSON = withObject "PhraseSuggester" parse
89− where parse o = PhraseSuggester
90− <$> o .: "field"
91− <*> o .:? "gram_size"
92− <*> o .:? "real_word_error_likelihood"
93− <*> o .:? "confidence"
94− <*> o .:? "max_errors"
95− <*> o .:? "separator"
96− <*> o .:? "size"
97− <*> o .:? "analyzer"
98− <*> o .:? "shard_size"
99− <*> o .:? "highlight"
100− <*> o .:? "collate"
101− <*> o .:? "direct_generator" .!= []
102−
103−mkPhraseSuggester :: FieldName -> PhraseSuggester
104−mkPhraseSuggester fName =
105− PhraseSuggester fName Nothing Nothing Nothing Nothing Nothing Nothing
106− Nothing Nothing Nothing Nothing []
107−
108−data PhraseSuggesterHighlighter =
109− PhraseSuggesterHighlighter { phraseSuggesterHighlighterPreTag :: Text
110− , phraseSuggesterHighlighterPostTag :: Text
111− }
112− deriving (Eq, Show)
113−
114−instance ToJSON PhraseSuggesterHighlighter where
115− toJSON PhraseSuggesterHighlighter{..} =
116− object [ "pre_tag" .= phraseSuggesterHighlighterPreTag
117− , "post_tag" .= phraseSuggesterHighlighterPostTag
118− ]
119−
120−instance FromJSON PhraseSuggesterHighlighter where
121− parseJSON = withObject "PhraseSuggesterHighlighter" parse
122− where parse o = PhraseSuggesterHighlighter
123− <$> o .: "pre_tag"
124− <*> o .: "post_tag"
125−
126−data PhraseSuggesterCollate = PhraseSuggesterCollate
127− { phraseSuggesterCollateTemplateQuery :: TemplateQueryInline
128− , phraseSuggesterCollatePrune :: Bool
129− } deriving (Eq, Show)
130−
131−instance ToJSON PhraseSuggesterCollate where
132− toJSON PhraseSuggesterCollate{..} =
133− object [ "query" .= object
134− [ "inline" .= (inline phraseSuggesterCollateTemplateQuery)
135− ]
136− , "params" .= (params phraseSuggesterCollateTemplateQuery)
137− , "prune" .= phraseSuggesterCollatePrune
138− ]
139−
140−instance FromJSON PhraseSuggesterCollate where
141− parseJSON (Object o) = do
142− query' <- o .: "query"
143− inline' <- query' .: "inline"
144− params' <- o .: "params"
145− prune' <- o .:? "prune" .!= False
146− return $ PhraseSuggesterCollate
147− (TemplateQueryInline inline' params') prune'
148− parseJSON x = typeMismatch "PhraseSuggesterCollate" x
149−
150−data SuggestOptions =
151− SuggestOptions { suggestOptionsText :: Text
152− , suggestOptionsScore :: Double
153− , suggestOptionsFreq :: Maybe Int
154− , suggestOptionsHighlighted :: Maybe Text
155− }
156− deriving (Eq, Read, Show)
157−
158−instance FromJSON SuggestOptions where
159− parseJSON = withObject "SuggestOptions" parse
160− where
161− parse o = SuggestOptions
162− <$> o .: "text"
163− <*> o .: "score"
164− <*> o .:? "freq"
165− <*> o .:? "highlighted"
166−
167−data SuggestResponse =
168− SuggestResponse { suggestResponseText :: Text
169− , suggestResponseOffset :: Int
170− , suggestResponseLength :: Int
171− , suggestResponseOptions :: [SuggestOptions]
172− }
173− deriving (Eq, Read, Show)
174−
175−instance FromJSON SuggestResponse where
176− parseJSON = withObject "SuggestResponse" parse
177− where parse o = SuggestResponse
178− <$> o .: "text"
179− <*> o .: "offset"
180− <*> o .: "length"
181− <*> o .: "options"
182−
183−data NamedSuggestionResponse = NamedSuggestionResponse
184− { nsrName :: Text
185− , nsrResponses :: [SuggestResponse]
186− } deriving (Eq, Read, Show)
187−
188−instance FromJSON NamedSuggestionResponse where
189− parseJSON (Object o) = do
190− suggestionName' <- case HM.toList o of
191− [(x, _)] -> return x
192− _ -> fail "error parsing NamedSuggestionResponse name"
193− suggestionResponses' <- o .: suggestionName'
194− return $ NamedSuggestionResponse suggestionName' suggestionResponses'
195−
196− parseJSON x = typeMismatch "NamedSuggestionResponse" x
197−
198−data DirectGeneratorSuggestModeTypes = DirectGeneratorSuggestModeMissing
199− | DirectGeneratorSuggestModePopular
200− | DirectGeneratorSuggestModeAlways
201− deriving (Eq, Show)
202−
203−instance ToJSON DirectGeneratorSuggestModeTypes where
204− toJSON DirectGeneratorSuggestModeMissing = "missing"
205− toJSON DirectGeneratorSuggestModePopular = "popular"
206− toJSON DirectGeneratorSuggestModeAlways = "always"
207−
208−instance FromJSON DirectGeneratorSuggestModeTypes where
209− parseJSON = withText "DirectGeneratorSuggestModeTypes" parse
210− where
211− parse "missing" =
212− pure DirectGeneratorSuggestModeMissing
213− parse "popular" =
214− pure DirectGeneratorSuggestModePopular
215− parse "always" =
216− pure DirectGeneratorSuggestModeAlways
217− parse f =
218− fail ("Unexpected DirectGeneratorSuggestModeTypes: " <> show f)
219−
220−data DirectGenerators = DirectGenerators
221− { directGeneratorsField :: FieldName
222− , directGeneratorsSize :: Maybe Int
223− , directGeneratorSuggestMode :: DirectGeneratorSuggestModeTypes
224− , directGeneratorMaxEdits :: Maybe Double
225− , directGeneratorPrefixLength :: Maybe Int
226− , directGeneratorMinWordLength :: Maybe Int
227− , directGeneratorMaxInspections :: Maybe Int
228− , directGeneratorMinDocFreq :: Maybe Double
229− , directGeneratorMaxTermFreq :: Maybe Double
230− , directGeneratorPreFilter :: Maybe Text
231− , directGeneratorPostFilter :: Maybe Text
232− } deriving (Eq, Show)
233−
234−instance ToJSON DirectGenerators where
235− toJSON DirectGenerators{..} =
236− omitNulls [ "field" .= directGeneratorsField
237− , "size" .= directGeneratorsSize
238− , "suggest_mode" .= directGeneratorSuggestMode
239− , "max_edits" .= directGeneratorMaxEdits
240− , "prefix_length" .= directGeneratorPrefixLength
241− , "min_word_length" .= directGeneratorMinWordLength
242− , "max_inspections" .= directGeneratorMaxInspections
243− , "min_doc_freq" .= directGeneratorMinDocFreq
244− , "max_term_freq" .= directGeneratorMaxTermFreq
245− , "pre_filter" .= directGeneratorPreFilter
246− , "post_filter" .= directGeneratorPostFilter
247− ]
248−
249−instance FromJSON DirectGenerators where
250− parseJSON = withObject "DirectGenerators" parse
251− where parse o = DirectGenerators
252− <$> o .: "field"
253− <*> o .:? "size"
254− <*> o .: "suggest_mode"
255− <*> o .:? "max_edits"
256− <*> o .:? "prefix_length"
257− <*> o .:? "min_word_length"
258− <*> o .:? "max_inspections"
259− <*> o .:? "min_doc_freq"
260− <*> o .:? "max_term_freq"
261− <*> o .:? "pre_filter"
262− <*> o .:? "post_filter"
263−
264−mkDirectGenerators :: FieldName -> DirectGenerators
265−mkDirectGenerators fn =
266− DirectGenerators
267− fn
268− Nothing
269− DirectGeneratorSuggestModeMissing
270− Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
src/Database/Bloodhound/Types.hsdeleted−542
@@ -1,542 +0,0 @@
1−{-# LANGUAGE FlexibleContexts #-}
2−{-# LANGUAGE FlexibleInstances #-}
3−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4−{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE NamedFieldPuns #-}
6−{-# LANGUAGE OverloadedStrings #-}
7−{-# LANGUAGE RecordWildCards #-}
8−{-# LANGUAGE UndecidableInstances #-}
9−
10−-------------------------------------------------------------------------------
11−-- |
12−-- Module : Database.Bloodhound.Types
13−-- Copyright : (C) 2014, 2018 Chris Allen
14−-- License : BSD-style (see the file LICENSE)
15−-- Maintainer : Chris Allen <cma@bitemyapp.com
16−-- Stability : provisional
17−-- Portability : RecordWildCards
18−--
19−-- Data types for describing actions and data structures performed to interact
20−-- with Elasticsearch. The two main buckets your queries against Elasticsearch
21−-- will fall into are 'Query's and 'Filter's. 'Filter's are more like
22−-- traditional database constraints and often have preferable performance
23−-- properties. 'Query's support human-written textual queries, such as fuzzy
24−-- queries.
25−-------------------------------------------------------------------------------
26−
27−
28−
29−module Database.Bloodhound.Types
30− ( defaultCache
31− , defaultIndexSettings
32− , defaultIndexDocumentSettings
33− , mkSort
34− , showText
35− , unpackId
36− , mkMatchQuery
37− , mkMultiMatchQuery
38− , mkBoolQuery
39− , mkRangeQuery
40− , mkQueryStringQuery
41− , mkAggregations
42− , mkTermsAggregation
43− , mkTermsScriptAggregation
44− , mkDateHistogram
45− , mkCardinalityAggregation
46− , mkDocVersion
47− , mkStatsAggregation
48− , mkExtendedStatsAggregation
49− , docVersionNumber
50− , toMissing
51− , toTerms
52− , toDateHistogram
53− , toTopHits
54− , omitNulls
55− , BH(..)
56− , runBH
57− , BHEnv
58− , bhServer
59− , bhManager
60− , bhRequestHook
61− , mkBHEnv
62− , MonadBH(..)
63− , Version(..)
64− , VersionNumber(..)
65− , MaybeNA(..)
66− , BuildHash(..)
67− , Status(..)
68− , Existence(..)
69− , NullValue(..)
70− , IndexSettings(..)
71− , UpdatableIndexSetting(..)
72− , IndexSettingsSummary(..)
73− , AllocationPolicy(..)
74− , Compression(..)
75− , ReplicaBounds(..)
76− , Bytes(..)
77− , gigabytes
78− , megabytes
79− , kilobytes
80− , FSType(..)
81− , InitialShardCount(..)
82− , NodeAttrFilter(..)
83− , NodeAttrName(..)
84− , CompoundFormat(..)
85− , IndexTemplate(..)
86− , Server(..)
87− , Reply
88− , EsResult(..)
89− , EsResultFound(..)
90− , EsError(..)
91− , EsProtocolException(..)
92− , IndexAlias(..)
93− , IndexAliasName(..)
94− , IndexAliasAction(..)
95− , IndexAliasCreate(..)
96− , IndexAliasSummary(..)
97− , IndexAliasesSummary(..)
98− , AliasRouting(..)
99− , SearchAliasRouting(..)
100− , IndexAliasRouting(..)
101− , RoutingValue(..)
102− , DocVersion
103− , ExternalDocVersion(..)
104− , VersionControl(..)
105− , DocumentParent(..)
106− , IndexDocumentSettings(..)
107− , Query(..)
108− , Search(..)
109− , SearchType(..)
110− , SearchResult(..)
111− , ScrollId(..)
112− , SearchHits(..)
113− , TrackSortScores
114− , From(..)
115− , Size(..)
116− , Source(..)
117− , PatternOrPatterns(..)
118− , Include(..)
119− , Exclude(..)
120− , Pattern(..)
121− , ShardResult(..)
122− , Hit(..)
123− , HitFields(..)
124− , Filter(..)
125− , BoolMatch(..)
126− , Term(..)
127− , GeoPoint(..)
128− , GeoBoundingBoxConstraint(..)
129− , GeoBoundingBox(..)
130− , GeoFilterType(..)
131− , Distance(..)
132− , DistanceUnit(..)
133− , DistanceType(..)
134− , DistanceRange(..)
135− , OptimizeBbox(..)
136− , LatLon(..)
137− , RangeValue(..)
138− , RangeExecution(..)
139− , LessThan(..)
140− , LessThanEq(..)
141− , GreaterThan(..)
142− , GreaterThanEq(..)
143− , LessThanD(..)
144− , LessThanEqD(..)
145− , GreaterThanD(..)
146− , GreaterThanEqD(..)
147− , Regexp(..)
148− , RegexpFlags(..)
149− , RegexpFlag(..)
150− , FieldName(..)
151− , ScriptFields(..)
152− , ScriptFieldName
153− , ScriptFieldValue
154− , Script(..)
155− , ScriptLanguage(..)
156− , ScriptInline(..)
157− , ScriptId(..)
158− , ScriptParams(..)
159− , ScriptParamName
160− , ScriptParamValue
161− , IndexName(..)
162− , IndexSelection(..)
163− , NodeSelection(..)
164− , NodeSelector(..)
165− , ForceMergeIndexSettings(..)
166− , defaultForceMergeIndexSettings
167− , TemplateName(..)
168− , TemplatePattern(..)
169− , MappingName(..)
170− , DocId(..)
171− , CacheName(..)
172− , CacheKey(..)
173− , BulkOperation(..)
174− , ReplicaCount(..)
175− , ShardCount(..)
176− , Sort
177− , SortMode(..)
178− , SortOrder(..)
179− , SortSpec(..)
180− , DefaultSort(..)
181− , Missing(..)
182− , OpenCloseIndex(..)
183− , Method
184− , Boost(..)
185− , MatchQuery(..)
186− , MultiMatchQuery(..)
187− , BoolQuery(..)
188− , BoostingQuery(..)
189− , CommonTermsQuery(..)
190− , FunctionScoreQuery(..)
191− , BoostMode(..)
192− , ScoreMode(..)
193− , FunctionScoreFunctions(..)
194− , ComponentFunctionScoreFunction(..)
195− , FunctionScoreFunction(..)
196− , Weight(..)
197− , Seed(..)
198− , FieldValueFactor(..)
199− , Factor(..)
200− , FactorModifier(..)
201− , FactorMissingFieldValue(..)
202− , DisMaxQuery(..)
203− , FuzzyLikeThisQuery(..)
204− , FuzzyLikeFieldQuery(..)
205− , FuzzyQuery(..)
206− , HasChildQuery(..)
207− , HasParentQuery(..)
208− , IndicesQuery(..)
209− , MoreLikeThisQuery(..)
210− , MoreLikeThisFieldQuery(..)
211− , NestedQuery(..)
212− , PrefixQuery(..)
213− , QueryStringQuery(..)
214− , SimpleQueryStringQuery(..)
215− , RangeQuery(..)
216− , RegexpQuery(..)
217− , QueryString(..)
218− , TemplateQueryInline(..)
219− , TemplateQueryKeyValuePairs(..)
220− , WildcardQuery(..)
221− , BooleanOperator(..)
222− , ZeroTermsQuery(..)
223− , CutoffFrequency(..)
224− , Analyzer(..)
225− , Tokenizer(..)
226− , TokenFilter(..)
227− , CharFilter(..)
228− , MaxExpansions(..)
229− , Lenient(..)
230− , MatchQueryType(..)
231− , MultiMatchQueryType(..)
232− , Tiebreaker(..)
233− , MinimumMatch(..)
234− , DisableCoord(..)
235− , CommonMinimumMatch(..)
236− , MinimumMatchHighLow(..)
237− , PrefixLength(..)
238− , Fuzziness(..)
239− , IgnoreTermFrequency(..)
240− , MaxQueryTerms(..)
241− , ScoreType(..)
242− , Score
243− , Cache
244− , TypeName(..)
245− , BoostTerms(..)
246− , MaxWordLength(..)
247− , MinWordLength(..)
248− , MaxDocFrequency(..)
249− , MinDocFrequency(..)
250− , PhraseSlop(..)
251− , StopWord(..)
252− , QueryPath(..)
253− , MinimumTermFrequency(..)
254− , PercentMatch(..)
255− , FieldDefinition(..)
256− , MappingField(..)
257− , Mapping(..)
258− , AllowLeadingWildcard(..)
259− , LowercaseExpanded(..)
260− , GeneratePhraseQueries(..)
261− , Locale(..)
262− , AnalyzeWildcard(..)
263− , EnablePositionIncrements(..)
264− , SimpleQueryFlag(..)
265− , FieldOrFields(..)
266− , Monoid(..)
267− , ToJSON(..)
268− , Interval(..)
269− , TimeInterval(..)
270− , ExecutionHint(..)
271− , CollectionMode(..)
272− , TermOrder(..)
273− , TermInclusion(..)
274− , SnapshotRepoSelection(..)
275− , GenericSnapshotRepo(..)
276− , SnapshotRepo(..)
277− , SnapshotRepoConversionError(..)
278− , SnapshotRepoType(..)
279− , GenericSnapshotRepoSettings(..)
280− , SnapshotRepoUpdateSettings(..)
281− , defaultSnapshotRepoUpdateSettings
282− , SnapshotRepoName(..)
283− , SnapshotRepoPattern(..)
284− , SnapshotVerification(..)
285− , SnapshotNodeVerification(..)
286− , FullNodeId(..)
287− , NodeName(..)
288− , ClusterName(..)
289− , NodesInfo(..)
290− , NodesStats(..)
291− , NodeStats(..)
292− , NodeBreakersStats(..)
293− , NodeBreakerStats(..)
294− , NodeHTTPStats(..)
295− , NodeTransportStats(..)
296− , NodeFSStats(..)
297− , NodeDataPathStats(..)
298− , NodeFSTotalStats(..)
299− , NodeNetworkStats(..)
300− , NodeThreadPoolsStats(..)
301− , NodeThreadPoolStats(..)
302− , NodeJVMStats(..)
303− , JVMBufferPoolStats(..)
304− , JVMGCStats(..)
305− , JVMPoolStats(..)
306− , NodeProcessStats(..)
307− , NodeOSStats(..)
308− , LoadAvgs(..)
309− , NodeIndicesStats(..)
310− , EsAddress(..)
311− , PluginName(..)
312− , NodeInfo(..)
313− , NodePluginInfo(..)
314− , NodeHTTPInfo(..)
315− , NodeTransportInfo(..)
316− , BoundTransportAddress(..)
317− , NodeNetworkInfo(..)
318− , MacAddress(..)
319− , NetworkInterfaceName(..)
320− , NodeNetworkInterface(..)
321− , NodeThreadPoolsInfo(..)
322− , NodeThreadPoolInfo(..)
323− , ThreadPoolSize(..)
324− , ThreadPoolType(..)
325− , NodeJVMInfo(..)
326− , JVMMemoryPool(..)
327− , JVMGCCollector(..)
328− , JVMMemoryInfo(..)
329− , PID(..)
330− , NodeOSInfo(..)
331− , CPUInfo(..)
332− , NodeProcessInfo(..)
333− , FsSnapshotRepo(..)
334− , SnapshotCreateSettings(..)
335− , defaultSnapshotCreateSettings
336− , SnapshotSelection(..)
337− , SnapshotPattern(..)
338− , SnapshotInfo(..)
339− , SnapshotShardFailure(..)
340− , ShardId(..)
341− , SnapshotName(..)
342− , SnapshotState(..)
343− , SnapshotRestoreSettings(..)
344− , defaultSnapshotRestoreSettings
345− , RestoreRenamePattern(..)
346− , RestoreRenameToken(..)
347− , RRGroupRefNum
348− , rrGroupRefNum
349− , mkRRGroupRefNum
350− , RestoreIndexSettings(..)
351− , Suggest(..)
352− , SuggestType(..)
353− , PhraseSuggester(..)
354− , PhraseSuggesterHighlighter(..)
355− , PhraseSuggesterCollate(..)
356− , mkPhraseSuggester
357− , SuggestOptions(..)
358− , SuggestResponse(..)
359− , NamedSuggestionResponse(..)
360− , DirectGenerators(..)
361− , mkDirectGenerators
362− , DirectGeneratorSuggestModeTypes (..)
363−
364− , Aggregation(..)
365− , Aggregations
366− , AggregationResults
367− , BucketValue(..)
368− , Bucket(..)
369− , BucketAggregation(..)
370− , TermsAggregation(..)
371− , MissingAggregation(..)
372− , ValueCountAggregation(..)
373− , FilterAggregation(..)
374− , CardinalityAggregation(..)
375− , MaxAggregation(..)
376− , DateHistogramAggregation(..)
377− , DateRangeAggregation(..)
378− , DateRangeAggRange(..)
379− , DateMathExpr(..)
380− , DateMathAnchor(..)
381− , DateMathModifier(..)
382− , DateMathUnit(..)
383− , TopHitsAggregation(..)
384− , StatisticsAggregation(..)
385− , SearchAfterKey
386−
387− , Highlights(..)
388− , FieldHighlight(..)
389− , HighlightSettings(..)
390− , PlainHighlight(..)
391− , PostingsHighlight(..)
392− , FastVectorHighlight(..)
393− , CommonHighlight(..)
394− , NonPostings(..)
395− , HighlightEncoder(..)
396− , HighlightTag(..)
397− , HitHighlight
398−
399− , MissingResult(..)
400− , TermsResult(..)
401− , DateHistogramResult(..)
402− , DateRangeResult(..)
403− , TopHitResult(..)
404−
405− , EsUsername(..)
406− , EsPassword(..)
407−
408− , Analysis(..)
409− , AnalyzerDefinition(..)
410− , TokenizerDefinition(..)
411− , TokenFilterDefinition(..)
412− , CharFilterDefinition(..)
413− , Ngram(..)
414− , TokenChar(..)
415− , Shingle(..)
416− , Language(..)
417− ) where
418−
419−import Bloodhound.Import
420−
421−import Database.Bloodhound.Internal.Aggregation
422−import Database.Bloodhound.Internal.Analysis
423−import Database.Bloodhound.Internal.Client
424−import Database.Bloodhound.Internal.Highlight
425−import Database.Bloodhound.Internal.Newtypes
426−import Database.Bloodhound.Internal.Query
427−import Database.Bloodhound.Internal.Sort
428−import Database.Bloodhound.Internal.Suggest
429−
430−{-| 'unpackId' is a silly convenience function that gets used once.
431−-}
432−unpackId :: DocId -> Text
433−unpackId (DocId docId) = docId
434−
435−type TrackSortScores = Bool
436−
437−data Search = Search { queryBody :: Maybe Query
438− , filterBody :: Maybe Filter
439− , sortBody :: Maybe Sort
440− , aggBody :: Maybe Aggregations
441− , highlight :: Maybe Highlights
442− -- default False
443− , trackSortScores :: TrackSortScores
444− , from :: From
445− , size :: Size
446− , searchType :: SearchType
447− , searchAfterKey :: Maybe SearchAfterKey
448− , fields :: Maybe [FieldName]
449− , scriptFields :: Maybe ScriptFields
450− , source :: Maybe Source
451− , suggestBody :: Maybe Suggest -- ^ Only one Suggestion request / response per Search is supported.
452− } deriving (Eq, Show)
453−
454−
455−instance ToJSON Search where
456− toJSON (Search mquery sFilter sort searchAggs
457− highlight sTrackSortScores sFrom sSize _ sAfter sFields
458− sScriptFields sSource sSuggest) =
459− omitNulls [ "query" .= query'
460− , "sort" .= sort
461− , "aggregations" .= searchAggs
462− , "highlight" .= highlight
463− , "from" .= sFrom
464− , "size" .= sSize
465− , "track_scores" .= sTrackSortScores
466− , "search_after" .= sAfter
467− , "fields" .= sFields
468− , "script_fields" .= sScriptFields
469− , "_source" .= sSource
470− , "suggest" .= sSuggest]
471−
472− where query' = case sFilter of
473− Nothing -> mquery
474− Just x ->
475− Just
476− . QueryBoolQuery
477− $ mkBoolQuery (maybeToList mquery)
478− [x] [] []
479−
480−data SearchType = SearchTypeQueryThenFetch
481− | SearchTypeDfsQueryThenFetch
482− deriving (Eq, Show)
483−
484−data Source =
485− NoSource
486− | SourcePatterns PatternOrPatterns
487− | SourceIncludeExclude Include Exclude
488− deriving (Eq, Show)
489−
490−instance ToJSON Source where
491− toJSON NoSource = toJSON False
492− toJSON (SourcePatterns patterns) = toJSON patterns
493− toJSON (SourceIncludeExclude incl excl) = object [ "includes" .= incl, "excludes" .= excl ]
494−
495−data PatternOrPatterns = PopPattern Pattern
496− | PopPatterns [Pattern] deriving (Eq, Read, Show)
497−
498−instance ToJSON PatternOrPatterns where
499− toJSON (PopPattern pattern) = toJSON pattern
500− toJSON (PopPatterns patterns) = toJSON patterns
501−
502−data Include = Include [Pattern] deriving (Eq, Read, Show)
503−data Exclude = Exclude [Pattern] deriving (Eq, Read, Show)
504−
505−instance ToJSON Include where
506− toJSON (Include patterns) = toJSON patterns
507−
508−instance ToJSON Exclude where
509− toJSON (Exclude patterns) = toJSON patterns
510−
511−newtype Pattern = Pattern Text deriving (Eq, Read, Show)
512−
513−instance ToJSON Pattern where
514− toJSON (Pattern pattern) = toJSON pattern
515−
516−data SearchResult a =
517− SearchResult { took :: Int
518− , timedOut :: Bool
519− , shards :: ShardResult
520− , searchHits :: SearchHits a
521− , aggregations :: Maybe AggregationResults
522− , scrollId :: Maybe ScrollId
523− -- ^ Only one Suggestion request / response per
524− -- Search is supported.
525− , suggest :: Maybe NamedSuggestionResponse
526− }
527− deriving (Eq, Show)
528−
529−instance (FromJSON a) => FromJSON (SearchResult a) where
530− parseJSON (Object v) = SearchResult <$>
531− v .: "took" <*>
532− v .: "timed_out" <*>
533− v .: "_shards" <*>
534− v .: "hits" <*>
535− v .:? "aggregations" <*>
536− v .:? "_scroll_id" <*>
537− v .:? "suggest"
538− parseJSON _ = empty
539−
540−newtype ScrollId =
541− ScrollId Text
542− deriving (Eq, Show, Ord, ToJSON, FromJSON)
src/Database/V1/Bloodhound.hsnew file+10
@@ -0,0 +1,10 @@
1+module Database.V1.Bloodhound
2+ ( -- module Data.Aeson.Types
3+ -- ,
4+ module Database.V1.Bloodhound.Client
5+ , module Database.V1.Bloodhound.Types
6+ ) where
7+
8+-- import Data.Aeson.Types
9+import Database.V1.Bloodhound.Client
10+import Database.V1.Bloodhound.Types
src/Database/V1/Bloodhound/Client.hsnew file+917
@@ -0,0 +1,917 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE RecordWildCards #-}
3+{-# LANGUAGE TupleSections #-}
4+
5+-------------------------------------------------------------------------------
6+-- |
7+-- Module : Database.Bloodhound.Client
8+-- Copyright : (C) 2014 Chris Allen
9+-- License : BSD-style (see the file LICENSE)
10+-- Maintainer : Chris Allen <cma@bitemyapp.com
11+-- Stability : provisional
12+-- Portability : OverloadedStrings
13+--
14+-- Client side functions for talking to Elasticsearch servers.
15+--
16+-------------------------------------------------------------------------------
17+
18+module Database.V1.Bloodhound.Client
19+ ( -- * Bloodhound client functions
20+ -- | The examples in this module assume the following code has been run.
21+ -- The :{ and :} will only work in GHCi. You'll only need the data types
22+ -- and typeclass instances for the functions that make use of them.
23+
24+ -- $setup
25+ withBH
26+ -- ** Indices
27+ , createIndex
28+ , deleteIndex
29+ , updateIndexSettings
30+ , getIndexSettings
31+ , optimizeIndex
32+ , indexExists
33+ , openIndex
34+ , closeIndex
35+ , listIndices
36+ , waitForYellowIndex
37+ -- *** Index Aliases
38+ , updateIndexAliases
39+ , getIndexAliases
40+ -- *** Index Templates
41+ , putTemplate
42+ , templateExists
43+ , deleteTemplate
44+ -- ** Mapping
45+ , putMapping
46+ , deleteMapping
47+ -- ** Documents
48+ , indexDocument
49+ , updateDocument
50+ , getDocument
51+ , documentExists
52+ , deleteDocument
53+ -- ** Searching
54+ , searchAll
55+ , searchByIndex
56+ , searchByType
57+ , scanSearch
58+ , getInitialScroll
59+ , advanceScroll
60+ , refreshIndex
61+ , mkSearch
62+ , mkAggregateSearch
63+ , mkHighlightSearch
64+ , bulk
65+ , pageSearch
66+ , mkShardCount
67+ , mkReplicaCount
68+ , getStatus
69+ -- ** Snapshot/Restore
70+ -- *** Snapshot Repos
71+ , getSnapshotRepos
72+ , updateSnapshotRepo
73+ , verifySnapshotRepo
74+ , deleteSnapshotRepo
75+ -- *** Snapshots
76+ , createSnapshot
77+ , getSnapshots
78+ , deleteSnapshot
79+ -- *** Restoring Snapshots
80+ , restoreSnapshot
81+ -- ** Nodes
82+ , getNodesInfo
83+ , getNodesStats
84+ -- ** Request Utilities
85+ , encodeBulkOperations
86+ , encodeBulkOperation
87+ -- * Authentication
88+ , basicAuthHook
89+ -- * Reply-handling tools
90+ , isVersionConflict
91+ , isSuccess
92+ , isCreated
93+ , parseEsResponse
94+ )
95+ where
96+
97+import qualified Blaze.ByteString.Builder as BB
98+import Control.Applicative as A
99+import Control.Monad
100+import Control.Monad.Catch
101+import Control.Monad.IO.Class
102+import Data.Aeson
103+import Data.ByteString.Lazy.Builder
104+import qualified Data.ByteString.Lazy.Char8 as L
105+import Data.Foldable (toList)
106+import qualified Data.HashMap.Strict as HM
107+import Data.Ix
108+import qualified Data.List as LS (filter, foldl')
109+import Data.List.NonEmpty (NonEmpty (..))
110+import Data.Maybe (catMaybes, fromMaybe, isJust)
111+import Data.Monoid
112+import Data.Text (Text)
113+import qualified Data.Text as T
114+import qualified Data.Text.Encoding as T
115+import Data.Time.Clock
116+import qualified Data.Vector as V
117+import Network.HTTP.Client
118+import qualified Network.HTTP.Types.Method as NHTM
119+import qualified Network.HTTP.Types.Status as NHTS
120+import qualified Network.HTTP.Types.URI as NHTU
121+import qualified Network.URI as URI
122+import Prelude hiding (filter, head)
123+
124+import Database.V1.Bloodhound.Types
125+
126+-- | 'mkShardCount' is a straight-forward smart constructor for 'ShardCount'
127+-- which rejects 'Int' values below 1 and above 1000.
128+mkShardCount :: Int -> Maybe ShardCount
129+mkShardCount n
130+ | n < 1 = Nothing
131+ | n > 1000 = Nothing
132+ | otherwise = Just (ShardCount n)
133+
134+-- | 'mkReplicaCount' is a straight-forward smart constructor for 'ReplicaCount'
135+-- which rejects 'Int' values below 0 and above 1000.
136+mkReplicaCount :: Int -> Maybe ReplicaCount
137+mkReplicaCount n
138+ | n < 0 = Nothing
139+ | n > 1000 = Nothing -- ...
140+ | otherwise = Just (ReplicaCount n)
141+
142+emptyBody :: L.ByteString
143+emptyBody = L.pack ""
144+
145+dispatch :: MonadBH m
146+ => Method
147+ -> Text
148+ -> Maybe L.ByteString
149+ -> m Reply
150+dispatch dMethod url body = do
151+ initReq <- liftIO $ parseUrl' url
152+ reqHook <- bhRequestHook A.<$> getBHEnv
153+ let reqBody = RequestBodyLBS $ fromMaybe emptyBody body
154+ req <- liftIO $ reqHook $ setRequestIgnoreStatus $ initReq { method = dMethod
155+ , requestBody = reqBody }
156+ mgr <- bhManager <$> getBHEnv
157+ liftIO $ httpLbs req mgr
158+
159+joinPath' :: [Text] -> Text
160+joinPath' = T.intercalate "/"
161+
162+joinPath :: MonadBH m => [Text] -> m Text
163+joinPath ps = do
164+ Server s <- bhServer <$> getBHEnv
165+ return $ joinPath' (s:ps)
166+
167+appendSearchTypeParam :: Text -> SearchType -> Text
168+appendSearchTypeParam originalUrl st = addQuery params originalUrl
169+ where stText = "search_type"
170+ params
171+ | st == SearchTypeDfsQueryThenFetch = [(stText, Just "dfs_query_then_fetch")]
172+ | st == SearchTypeCount = [(stText, Just "count")]
173+ | st == SearchTypeScan = [(stText, Just "scan"), ("scroll", Just "1m")]
174+ | st == SearchTypeQueryAndFetch = [(stText, Just "query_and_fetch")]
175+ | st == SearchTypeDfsQueryAndFetch = [(stText, Just "dfs_query_and_fetch")]
176+ -- used to catch 'SearchTypeQueryThenFetch', which is also the default
177+ | otherwise = [(stText, Just "query_then_fetch")]
178+
179+-- | Severely dumbed down query renderer. Assumes your data doesn't
180+-- need any encoding
181+addQuery :: [(Text, Maybe Text)] -> Text -> Text
182+addQuery q u = u <> rendered
183+ where
184+ rendered =
185+ T.decodeUtf8 $ BB.toByteString $ NHTU.renderQueryText prependQuestionMark q
186+ prependQuestionMark = True
187+
188+bindM2 :: (Applicative m, Monad m) => (a -> b -> m c) -> m a -> m b -> m c
189+bindM2 f ma mb = join (f <$> ma <*> mb)
190+
191+-- | Convenience function that sets up a manager and BHEnv and runs
192+-- the given set of bloodhound operations. Connections will be
193+-- pipelined automatically in accordance with the given manager
194+-- settings in IO. If you've got your own monad transformer stack, you
195+-- should use 'runBH' directly.
196+withBH :: ManagerSettings -> Server -> BH IO a -> IO a
197+withBH ms s f = do
198+ mgr <- newManager ms
199+ let env = mkBHEnv s mgr
200+ runBH env f
201+
202+-- Shortcut functions for HTTP methods
203+delete :: MonadBH m => Text -> m Reply
204+delete = flip (dispatch NHTM.methodDelete) Nothing
205+get :: MonadBH m => Text -> m Reply
206+get = flip (dispatch NHTM.methodGet) Nothing
207+head :: MonadBH m => Text -> m Reply
208+head = flip (dispatch NHTM.methodHead) Nothing
209+put :: MonadBH m => Text -> Maybe L.ByteString -> m Reply
210+put = dispatch NHTM.methodPut
211+post :: MonadBH m => Text -> Maybe L.ByteString -> m Reply
212+post = dispatch NHTM.methodPost
213+
214+-- indexDocument s ix name doc = put (root </> s </> ix </> name </> doc) (Just encode doc)
215+-- http://hackage.haskell.org/package/http-client-lens-0.1.0/docs/Network-HTTP-Client-Lens.html
216+-- https://github.com/supki/libjenkins/blob/master/src/Jenkins/Rest/Internal.hs
217+
218+-- | 'getStatus' fetches the 'Status' of a 'Server'
219+getStatus :: MonadBH m => m (Maybe Status)
220+getStatus = do
221+ response <- get =<< url
222+ return $ decode (responseBody response)
223+ where url = joinPath []
224+
225+-- | 'getSnapshotRepos' gets the definitions of a subset of the
226+-- defined snapshot repos.
227+getSnapshotRepos
228+ :: ( MonadBH m
229+ , MonadThrow m
230+ )
231+ => SnapshotRepoSelection
232+ -> m (Either EsError [GenericSnapshotRepo])
233+getSnapshotRepos sel = fmap (fmap unGSRs) . parseEsResponse =<< get =<< url
234+ where
235+ url = joinPath ["_snapshot", selectorSeg]
236+ selectorSeg = case sel of
237+ AllSnapshotRepos -> "_all"
238+ SnapshotRepoList (p :| ps) -> T.intercalate "," (renderPat <$> (p:ps))
239+ renderPat (RepoPattern t) = t
240+ renderPat (ExactRepo (SnapshotRepoName t)) = t
241+
242+
243+-- | Wrapper to extract the list of 'GenericSnapshotRepo' in the
244+-- format they're returned in
245+newtype GSRs = GSRs { unGSRs :: [GenericSnapshotRepo] }
246+
247+
248+instance FromJSON GSRs where
249+ parseJSON = withObject "Collection of GenericSnapshotRepo" parse
250+ where
251+ parse = fmap GSRs . mapM (uncurry go) . HM.toList
252+ go rawName = withObject "GenericSnapshotRepo" $ \o -> do
253+ GenericSnapshotRepo (SnapshotRepoName rawName) <$> o .: "type"
254+ <*> o .: "settings"
255+
256+
257+-- | Create or update a snapshot repo
258+updateSnapshotRepo
259+ :: ( MonadBH m
260+ , SnapshotRepo repo
261+ )
262+ => SnapshotRepoUpdateSettings
263+ -- ^ Use 'defaultSnapshotRepoUpdateSettings' if unsure
264+ -> repo
265+ -> m Reply
266+updateSnapshotRepo SnapshotRepoUpdateSettings {..} repo =
267+ bindM2 put url (return (Just body))
268+ where
269+ url = addQuery params <$> joinPath ["_snapshot", snapshotRepoName gSnapshotRepoName]
270+ params
271+ | repoUpdateVerify = []
272+ | otherwise = [("verify", Just "false")]
273+ body = encode $ object [ "type" .= gSnapshotRepoType
274+ , "settings" .= gSnapshotRepoSettings
275+ ]
276+ GenericSnapshotRepo {..} = toGSnapshotRepo repo
277+
278+
279+
280+-- | Verify if a snapshot repo is working. __NOTE:__ this API did not
281+-- make it into ElasticSearch until 1.4. If you use an older version,
282+-- you will get an error here.
283+verifySnapshotRepo
284+ :: ( MonadBH m
285+ , MonadThrow m
286+ )
287+ => SnapshotRepoName
288+ -> m (Either EsError SnapshotVerification)
289+verifySnapshotRepo (SnapshotRepoName n) =
290+ parseEsResponse =<< bindM2 post url (return Nothing)
291+ where
292+ url = joinPath ["_snapshot", n, "_verify"]
293+
294+
295+deleteSnapshotRepo :: MonadBH m => SnapshotRepoName -> m Reply
296+deleteSnapshotRepo (SnapshotRepoName n) = delete =<< url
297+ where
298+ url = joinPath ["_snapshot", n]
299+
300+
301+-- | Create and start a snapshot
302+createSnapshot
303+ :: (MonadBH m)
304+ => SnapshotRepoName
305+ -> SnapshotName
306+ -> SnapshotCreateSettings
307+ -> m Reply
308+createSnapshot (SnapshotRepoName repoName)
309+ (SnapshotName snapName)
310+ SnapshotCreateSettings {..} =
311+ bindM2 put url (return (Just body))
312+ where
313+ url = addQuery params <$> joinPath ["_snapshot", repoName, snapName]
314+ params = [("wait_for_completion", Just (boolQP snapWaitForCompletion))]
315+ body = encode $ object prs
316+ prs = catMaybes [ ("indices" .=) . indexSelectionName <$> snapIndices
317+ , Just ("ignore_unavailable" .= snapIgnoreUnavailable)
318+ , Just ("ignore_global_state" .= snapIncludeGlobalState)
319+ , Just ("partial" .= snapPartial)
320+ ]
321+
322+
323+indexSelectionName :: IndexSelection -> Text
324+indexSelectionName AllIndexes = "_all"
325+indexSelectionName (IndexList (i :| is)) = T.intercalate "," (renderIndex <$> (i:is))
326+ where
327+ renderIndex (IndexName n) = n
328+
329+
330+-- | Get info about known snapshots given a pattern and repo name.
331+getSnapshots
332+ :: ( MonadBH m
333+ , MonadThrow m
334+ )
335+ => SnapshotRepoName
336+ -> SnapshotSelection
337+ -> m (Either EsError [SnapshotInfo])
338+getSnapshots (SnapshotRepoName repoName) sel =
339+ fmap (fmap unSIs) . parseEsResponse =<< get =<< url
340+ where
341+ url = joinPath ["_snapshot", repoName, snapPath]
342+ snapPath = case sel of
343+ AllSnapshots -> "_all"
344+ SnapshotList (s :| ss) -> T.intercalate "," (renderPath <$> (s:ss))
345+ renderPath (SnapPattern t) = t
346+ renderPath (ExactSnap (SnapshotName t)) = t
347+
348+
349+newtype SIs = SIs { unSIs :: [SnapshotInfo] }
350+
351+
352+instance FromJSON SIs where
353+ parseJSON = withObject "Collection of SnapshotInfo" parse
354+ where
355+ parse o = SIs <$> o .: "snapshots"
356+
357+
358+-- | Delete a snapshot. Cancels if it is running.
359+deleteSnapshot :: MonadBH m => SnapshotRepoName -> SnapshotName -> m Reply
360+deleteSnapshot (SnapshotRepoName repoName) (SnapshotName snapName) =
361+ delete =<< url
362+ where
363+ url = joinPath ["_snapshot", repoName, snapName]
364+
365+
366+-- | Restore a snapshot to the cluster See
367+-- <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/modules-snapshots.html#_restore>
368+-- for more details.
369+restoreSnapshot
370+ :: MonadBH m
371+ => SnapshotRepoName
372+ -> SnapshotName
373+ -> SnapshotRestoreSettings
374+ -- ^ Start with 'defaultSnapshotRestoreSettings' and customize
375+ -- from there for reasonable defaults.
376+ -> m Reply
377+restoreSnapshot (SnapshotRepoName repoName)
378+ (SnapshotName snapName)
379+ SnapshotRestoreSettings {..} = bindM2 post url (return (Just body))
380+ where
381+ url = addQuery params <$> joinPath ["_snapshot", repoName, snapName, "_restore"]
382+ params = [("wait_for_completion", Just (boolQP snapRestoreWaitForCompletion))]
383+ body = encode (object prs)
384+
385+
386+ prs = catMaybes [ ("indices" .=) . indexSelectionName <$> snapRestoreIndices
387+ , Just ("ignore_unavailable" .= snapRestoreIgnoreUnavailable)
388+ , Just ("include_global_state" .= snapRestoreIncludeGlobalState)
389+ , ("rename_pattern" .=) <$> snapRestoreRenamePattern
390+ , ("rename_replacement" .=) . renderTokens <$> snapRestoreRenameReplacement
391+ , Just ("include_aliases" .= snapRestoreIncludeAliases)
392+ , ("index_settings" .= ) <$> snapRestoreIndexSettingsOverrides
393+ , ("ignore_index_settings" .= ) <$> snapRestoreIgnoreIndexSettings
394+ ]
395+ renderTokens (t :| ts) = mconcat (renderToken <$> (t:ts))
396+ renderToken (RRTLit t) = t
397+ renderToken RRSubWholeMatch = "$0"
398+ renderToken (RRSubGroup g) = T.pack (show (rrGroupRefNum g))
399+
400+
401+getNodesInfo
402+ :: ( MonadBH m
403+ , MonadThrow m
404+ )
405+ => NodeSelection
406+ -> m (Either EsError NodesInfo)
407+getNodesInfo sel = parseEsResponse =<< get =<< url
408+ where
409+ url = joinPath ["_nodes", selectionSeg]
410+ selectionSeg = case sel of
411+ LocalNode -> "_local"
412+ NodeList (l :| ls) -> T.intercalate "," (selToSeg <$> (l:ls))
413+ AllNodes -> "_all"
414+ selToSeg (NodeByName (NodeName n)) = n
415+ selToSeg (NodeByFullNodeId (FullNodeId i)) = i
416+ selToSeg (NodeByHost (Server s)) = s
417+ selToSeg (NodeByAttribute (NodeAttrName a) v) = a <> ":" <> v
418+
419+getNodesStats
420+ :: ( MonadBH m
421+ , MonadThrow m
422+ )
423+ => NodeSelection
424+ -> m (Either EsError NodesStats)
425+getNodesStats sel = parseEsResponse =<< get =<< url
426+ where
427+ url = joinPath ["_nodes", selectionSeg, "stats"]
428+ selectionSeg = case sel of
429+ LocalNode -> "_local"
430+ NodeList (l :| ls) -> T.intercalate "," (selToSeg <$> (l:ls))
431+ AllNodes -> "_all"
432+ selToSeg (NodeByName (NodeName n)) = n
433+ selToSeg (NodeByFullNodeId (FullNodeId i)) = i
434+ selToSeg (NodeByHost (Server s)) = s
435+ selToSeg (NodeByAttribute (NodeAttrName a) v) = a <> ":" <> v
436+
437+-- | 'createIndex' will create an index given a 'Server', 'IndexSettings', and an 'IndexName'.
438+createIndex :: MonadBH m => IndexSettings -> IndexName -> m Reply
439+createIndex indexSettings (IndexName indexName) =
440+ bindM2 put url (return body)
441+ where url = joinPath [indexName]
442+ body = Just $ encode indexSettings
443+
444+
445+-- | 'deleteIndex' will delete an index given a 'Server', and an 'IndexName'.
446+deleteIndex :: MonadBH m => IndexName -> m Reply
447+deleteIndex (IndexName indexName) =
448+ delete =<< joinPath [indexName]
449+
450+-- | 'updateIndexSettings' will apply a non-empty list of setting updates to an index
451+updateIndexSettings :: MonadBH m => NonEmpty UpdatableIndexSetting -> IndexName -> m Reply
452+updateIndexSettings updates (IndexName indexName) =
453+ bindM2 put url (return body)
454+ where url = joinPath [indexName, "_settings"]
455+ body = Just (encode jsonBody)
456+ jsonBody = Object (deepMerge [u | Object u <- toJSON <$> toList updates])
457+
458+
459+getIndexSettings :: (MonadBH m, MonadThrow m) => IndexName
460+ -> m (Either EsError IndexSettingsSummary)
461+getIndexSettings (IndexName indexName) = do
462+ parseEsResponse =<< get =<< url
463+ where url = joinPath [indexName, "_settings"]
464+
465+
466+-- | 'optimizeIndex' will optimize a single index, list of indexes or
467+-- all indexes. Note that this call will block until finishing but
468+-- will continue even if the request times out. Concurrent requests to
469+-- optimize an index while another is performing will block until the
470+-- previous one finishes. For more information see
471+-- <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-optimize.html>. Nothing
472+-- worthwhile comes back in the reply body, so matching on the status
473+-- should suffice.
474+--
475+-- 'optimizeIndex' with a maxNumSegments of 1 and onlyExpungeDeletes
476+-- to True is the main way to release disk space back to the OS being
477+-- held by deleted documents.
478+--
479+-- Note that this API was deprecated in ElasticSearch 2.1 for the
480+-- almost completely identical forcemerge API. Adding support to that
481+-- API would be trivial but due to the significant breaking changes,
482+-- this library cannot currently be used with >= 2.0, so that feature was omitted.
483+optimizeIndex :: MonadBH m => IndexSelection -> IndexOptimizationSettings -> m Reply
484+optimizeIndex ixs IndexOptimizationSettings {..} =
485+ bindM2 post url (return body)
486+ where url = addQuery params <$> joinPath [indexName, "_optimize"]
487+ params = catMaybes [ ("max_num_segments",) . Just . showText <$> maxNumSegments
488+ , Just ("only_expunge_deletes", Just (boolQP onlyExpungeDeletes))
489+ , Just ("flush", Just (boolQP flushAfterOptimize))
490+ ]
491+ indexName = indexSelectionName ixs
492+ body = Nothing
493+
494+
495+deepMerge :: [Object] -> Object
496+deepMerge = LS.foldl' go mempty
497+ where go acc = LS.foldl' go' acc . HM.toList
498+ go' acc (k, v) = HM.insertWith merge k v acc
499+ merge (Object a) (Object b) = Object (deepMerge [a, b])
500+ merge _ b = b
501+
502+
503+statusCodeIs :: (Int, Int) -> Reply -> Bool
504+statusCodeIs r resp = inRange r $ NHTS.statusCode (responseStatus resp)
505+
506+respIsTwoHunna :: Reply -> Bool
507+respIsTwoHunna = statusCodeIs (200, 299)
508+
509+existentialQuery :: MonadBH m => Text -> m (Reply, Bool)
510+existentialQuery url = do
511+ reply <- head url
512+ return (reply, respIsTwoHunna reply)
513+
514+
515+-- | Tries to parse a response body as the expected type @a@ and
516+-- failing that tries to parse it as an EsError. All well-formed, JSON
517+-- responses from elasticsearch should fall into these two
518+-- categories. If they don't, a 'EsProtocolException' will be
519+-- thrown. If you encounter this, please report the full body it
520+-- reports along with your ElasticSearch verison.
521+parseEsResponse :: (MonadThrow m, FromJSON a) => Reply
522+ -> m (Either EsError a)
523+parseEsResponse reply
524+ | respIsTwoHunna reply = case eitherDecode body of
525+ Right a -> return (Right a)
526+ Left _ -> tryParseError
527+ | otherwise = tryParseError
528+ where body = responseBody reply
529+ tryParseError = case eitherDecode body of
530+ Right e -> return (Left e)
531+ -- this case should not be possible
532+ Left _ -> explode
533+ explode = throwM (EsProtocolException body)
534+
535+-- | 'indexExists' enables you to check if an index exists. Returns 'Bool'
536+-- in IO
537+indexExists :: MonadBH m => IndexName -> m Bool
538+indexExists (IndexName indexName) = do
539+ (_, exists) <- existentialQuery =<< joinPath [indexName]
540+ return exists
541+
542+-- | 'refreshIndex' will force a refresh on an index. You must
543+-- do this if you want to read what you wrote.
544+refreshIndex :: MonadBH m => IndexName -> m Reply
545+refreshIndex (IndexName indexName) =
546+ bindM2 post url (return Nothing)
547+ where url = joinPath [indexName, "_refresh"]
548+
549+-- | Block until the index becomes available for indexing
550+-- documents. This is useful for integration tests in which
551+-- indices are rapidly created and deleted.
552+waitForYellowIndex :: MonadBH m => IndexName -> m Reply
553+waitForYellowIndex (IndexName indexName) = get =<< url
554+ where url = addQuery q <$> joinPath ["_cluster","health",indexName]
555+ q = [("wait_for_status",Just "yellow"),("timeout",Just "10s")]
556+
557+stringifyOCIndex :: OpenCloseIndex -> Text
558+stringifyOCIndex oci = case oci of
559+ OpenIndex -> "_open"
560+ CloseIndex -> "_close"
561+
562+openOrCloseIndexes :: MonadBH m => OpenCloseIndex -> IndexName -> m Reply
563+openOrCloseIndexes oci (IndexName indexName) =
564+ bindM2 post url (return Nothing)
565+ where ociString = stringifyOCIndex oci
566+ url = joinPath [indexName, ociString]
567+
568+-- | 'openIndex' opens an index given a 'Server' and an 'IndexName'. Explained in further detail at
569+-- <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html>
570+openIndex :: MonadBH m => IndexName -> m Reply
571+openIndex = openOrCloseIndexes OpenIndex
572+
573+-- | 'closeIndex' closes an index given a 'Server' and an 'IndexName'. Explained in further detail at
574+-- <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html>
575+closeIndex :: MonadBH m => IndexName -> m Reply
576+closeIndex = openOrCloseIndexes CloseIndex
577+
578+-- | 'listIndices' returns a list of all index names on a given 'Server'
579+listIndices :: (MonadThrow m, MonadBH m) => m [IndexName]
580+listIndices =
581+ parse . responseBody =<< get =<< url
582+ where
583+ url = joinPath ["_cat/indices?v"]
584+ -- parses the tabular format the indices api provides
585+ parse body = case T.lines (T.decodeUtf8 (L.toStrict body)) of
586+ (hdr:rows) -> let ks = T.words hdr
587+ keyedRows = [ HM.fromList (zip ks (T.words row)) | row <- rows ]
588+ names = catMaybes (HM.lookup "index" <$> keyedRows)
589+ in return (IndexName <$> names)
590+ [] -> throwM (EsProtocolException body)
591+
592+-- | 'updateIndexAliases' updates the server's index alias
593+-- table. Operations are atomic. Explained in further detail at
594+-- <https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html>
595+updateIndexAliases :: MonadBH m => NonEmpty IndexAliasAction -> m Reply
596+updateIndexAliases actions = bindM2 post url (return body)
597+ where url = joinPath ["_aliases"]
598+ body = Just (encode bodyJSON)
599+ bodyJSON = object [ "actions" .= toList actions]
600+
601+-- | Get all aliases configured on the server.
602+getIndexAliases :: (MonadBH m, MonadThrow m)
603+ => m (Either EsError IndexAliasesSummary)
604+getIndexAliases = parseEsResponse =<< get =<< url
605+ where url = joinPath ["_aliases"]
606+
607+-- | 'putTemplate' creates a template given an 'IndexTemplate' and a 'TemplateName'.
608+-- Explained in further detail at
609+-- <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html>
610+putTemplate :: MonadBH m => IndexTemplate -> TemplateName -> m Reply
611+putTemplate indexTemplate (TemplateName templateName) =
612+ bindM2 put url (return body)
613+ where url = joinPath ["_template", templateName]
614+ body = Just $ encode indexTemplate
615+
616+-- | 'templateExists' checks to see if a template exists.
617+templateExists :: MonadBH m => TemplateName -> m Bool
618+templateExists (TemplateName templateName) = do
619+ (_, exists) <- existentialQuery =<< joinPath ["_template", templateName]
620+ return exists
621+
622+-- | 'deleteTemplate' is an HTTP DELETE and deletes a template.
623+deleteTemplate :: MonadBH m => TemplateName -> m Reply
624+deleteTemplate (TemplateName templateName) =
625+ delete =<< joinPath ["_template", templateName]
626+
627+-- | 'putMapping' is an HTTP PUT and has upsert semantics. Mappings are schemas
628+-- for documents in indexes.
629+putMapping :: (MonadBH m, ToJSON a) => IndexName
630+ -> MappingName -> a -> m Reply
631+putMapping (IndexName indexName) (MappingName mappingName) mapping =
632+ bindM2 put url (return body)
633+ where url = joinPath [indexName, "_mapping", mappingName]
634+ -- "_mapping" and mappingName above were originally transposed
635+ -- erroneously. The correct API call is: "/INDEX/_mapping/MAPPING_NAME"
636+ body = Just $ encode mapping
637+
638+-- | 'deleteMapping' is an HTTP DELETE and deletes a mapping for a given index.
639+-- Mappings are schemas for documents in indexes.
640+deleteMapping :: MonadBH m => IndexName -> MappingName -> m Reply
641+deleteMapping (IndexName indexName)
642+ (MappingName mappingName) =
643+ -- "_mapping" and mappingName below were originally transposed
644+ -- erroneously. The correct API call is: "/INDEX/_mapping/MAPPING_NAME"
645+ delete =<< joinPath [indexName, "_mapping", mappingName]
646+
647+versionCtlParams :: IndexDocumentSettings -> [(Text, Maybe Text)]
648+versionCtlParams cfg =
649+ case idsVersionControl cfg of
650+ NoVersionControl -> []
651+ InternalVersion v -> versionParams v "internal"
652+ ExternalGT (ExternalDocVersion v) -> versionParams v "external_gt"
653+ ExternalGTE (ExternalDocVersion v) -> versionParams v "external_gte"
654+ ForceVersion (ExternalDocVersion v) -> versionParams v "force"
655+ where
656+ vt = showText . docVersionNumber
657+ versionParams v t = [ ("version", Just $ vt v)
658+ , ("version_type", Just t)
659+ ]
660+
661+-- | 'indexDocument' is the primary way to save a single document in
662+-- Elasticsearch. The document itself is simply something we can
663+-- convert into a JSON 'Value'. The 'DocId' will function as the
664+-- primary key for the document.
665+indexDocument :: (ToJSON doc, MonadBH m) => IndexName -> MappingName
666+ -> IndexDocumentSettings -> doc -> DocId -> m Reply
667+indexDocument (IndexName indexName)
668+ (MappingName mappingName) cfg document (DocId docId) =
669+ bindM2 put url (return body)
670+ where url = addQuery params <$> joinPath [indexName, mappingName, docId]
671+ parentParams = case idsParent cfg of
672+ Nothing -> []
673+ Just (DocumentParent (DocId p)) -> [ ("parent", Just p) ]
674+ params = versionCtlParams cfg ++ parentParams
675+ body = Just (encode document)
676+
677+-- | 'updateDocument' provides a way to perform an partial update of a
678+-- an already indexed document.
679+updateDocument :: (ToJSON patch, MonadBH m) => IndexName -> MappingName
680+ -> IndexDocumentSettings -> patch -> DocId -> m Reply
681+updateDocument (IndexName indexName)
682+ (MappingName mappingName) cfg patch (DocId docId) =
683+ bindM2 post url (return body)
684+ where url = addQuery (versionCtlParams cfg) <$>
685+ joinPath [indexName, mappingName, docId, "_update"]
686+ body = Just (encode $ object ["doc" .= toJSON patch])
687+
688+-- | 'deleteDocument' is the primary way to delete a single document.
689+deleteDocument :: MonadBH m => IndexName -> MappingName
690+ -> DocId -> m Reply
691+deleteDocument (IndexName indexName)
692+ (MappingName mappingName) (DocId docId) =
693+ delete =<< joinPath [indexName, mappingName, docId]
694+
695+-- | 'bulk' uses
696+-- <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html Elasticsearch's bulk API>
697+-- to perform bulk operations. The 'BulkOperation' data type encodes the
698+-- index\/update\/delete\/create operations. You pass a 'V.Vector' of 'BulkOperation's
699+-- and a 'Server' to 'bulk' in order to send those operations up to your Elasticsearch
700+-- server to be performed. I changed from [BulkOperation] to a Vector due to memory overhead.
701+bulk :: MonadBH m => V.Vector BulkOperation -> m Reply
702+bulk bulkOps = bindM2 post url (return body)
703+ where url = joinPath ["_bulk"]
704+ body = Just $ encodeBulkOperations bulkOps
705+
706+-- | 'encodeBulkOperations' is a convenience function for dumping a vector of 'BulkOperation'
707+-- into an 'L.ByteString'
708+encodeBulkOperations :: V.Vector BulkOperation -> L.ByteString
709+encodeBulkOperations stream = collapsed where
710+ blobs = fmap encodeBulkOperation stream
711+ mashedTaters = mash (mempty :: Builder) blobs
712+ collapsed = toLazyByteString $ mappend mashedTaters (byteString "\n")
713+
714+mash :: Builder -> V.Vector L.ByteString -> Builder
715+mash = V.foldl' (\b x -> b `mappend` (byteString "\n") `mappend` (lazyByteString x))
716+
717+mkBulkStreamValue :: Text -> Text -> Text -> Text -> Value
718+mkBulkStreamValue operation indexName mappingName docId =
719+ object [operation .=
720+ object [ "_index" .= indexName
721+ , "_type" .= mappingName
722+ , "_id" .= docId]]
723+
724+-- | 'encodeBulkOperation' is a convenience function for dumping a single 'BulkOperation'
725+-- into an 'L.ByteString'
726+encodeBulkOperation :: BulkOperation -> L.ByteString
727+encodeBulkOperation (BulkIndex (IndexName indexName)
728+ (MappingName mappingName)
729+ (DocId docId) value) = blob
730+ where metadata = mkBulkStreamValue "index" indexName mappingName docId
731+ blob = encode metadata `mappend` "\n" `mappend` encode value
732+
733+encodeBulkOperation (BulkCreate (IndexName indexName)
734+ (MappingName mappingName)
735+ (DocId docId) value) = blob
736+ where metadata = mkBulkStreamValue "create" indexName mappingName docId
737+ blob = encode metadata `mappend` "\n" `mappend` encode value
738+
739+encodeBulkOperation (BulkDelete (IndexName indexName)
740+ (MappingName mappingName)
741+ (DocId docId)) = blob
742+ where metadata = mkBulkStreamValue "delete" indexName mappingName docId
743+ blob = encode metadata
744+
745+encodeBulkOperation (BulkUpdate (IndexName indexName)
746+ (MappingName mappingName)
747+ (DocId docId) value) = blob
748+ where metadata = mkBulkStreamValue "update" indexName mappingName docId
749+ doc = object ["doc" .= value]
750+ blob = encode metadata `mappend` "\n" `mappend` encode doc
751+
752+-- | 'getDocument' is a straight-forward way to fetch a single document from
753+-- Elasticsearch using a 'Server', 'IndexName', 'MappingName', and a 'DocId'.
754+-- The 'DocId' is the primary key for your Elasticsearch document.
755+getDocument :: MonadBH m => IndexName -> MappingName
756+ -> DocId -> m Reply
757+getDocument (IndexName indexName)
758+ (MappingName mappingName) (DocId docId) =
759+ get =<< joinPath [indexName, mappingName, docId]
760+
761+-- | 'documentExists' enables you to check if a document exists. Returns 'Bool'
762+-- in IO
763+documentExists :: MonadBH m => IndexName -> MappingName
764+ -> Maybe DocumentParent -> DocId -> m Bool
765+documentExists (IndexName indexName) (MappingName mappingName)
766+ parent (DocId docId) = do
767+ (_, exists) <- existentialQuery =<< url
768+ return exists
769+ where url = addQuery params <$> joinPath [indexName, mappingName, docId]
770+ parentParam = fmap (\(DocumentParent (DocId p)) -> p) parent
771+ params = LS.filter (\(_, v) -> isJust v) [("parent", parentParam)]
772+
773+dispatchSearch :: MonadBH m => Text -> Search -> m Reply
774+dispatchSearch url search = post url' (Just (encode search))
775+ where url' = appendSearchTypeParam url (searchType search)
776+
777+-- | 'searchAll', given a 'Search', will perform that search against all indexes
778+-- on an Elasticsearch server. Try to avoid doing this if it can be helped.
779+searchAll :: MonadBH m => Search -> m Reply
780+searchAll = bindM2 dispatchSearch url . return
781+ where url = joinPath ["_search"]
782+
783+-- | 'searchByIndex', given a 'Search' and an 'IndexName', will perform that search
784+-- against all mappings within an index on an Elasticsearch server.
785+searchByIndex :: MonadBH m => IndexName -> Search -> m Reply
786+searchByIndex (IndexName indexName) = bindM2 dispatchSearch url . return
787+ where url = joinPath [indexName, "_search"]
788+
789+-- | 'searchByType', given a 'Search', 'IndexName', and 'MappingName', will perform that
790+-- search against a specific mapping within an index on an Elasticsearch server.
791+searchByType :: MonadBH m => IndexName -> MappingName -> Search
792+ -> m Reply
793+searchByType (IndexName indexName)
794+ (MappingName mappingName) = bindM2 dispatchSearch url . return
795+ where url = joinPath [indexName, mappingName, "_search"]
796+
797+-- | For a given search, request a scroll for efficient streaming of
798+-- search results. Note that the search is put into 'SearchTypeScan'
799+-- mode and thus results will not be sorted. Combine this with
800+-- 'advanceScroll' to efficiently stream through the full result set
801+getInitialScroll :: MonadBH m => IndexName -> MappingName -> Search -> m (Maybe ScrollId)
802+getInitialScroll (IndexName indexName) (MappingName mappingName) search = do
803+ let url = joinPath [indexName, mappingName, "_search"]
804+ search' = search { searchType = SearchTypeScan }
805+ resp' <- bindM2 dispatchSearch url (return search')
806+ let msr = decode' $ responseBody resp' :: Maybe (SearchResult ())
807+ msid = maybe Nothing scrollId msr
808+ return msid
809+
810+scroll' :: (FromJSON a, MonadBH m, MonadThrow m) => Maybe ScrollId -> m ([Hit a], Maybe ScrollId)
811+scroll' Nothing = return ([], Nothing)
812+scroll' (Just sid) = do
813+ res <- advanceScroll sid 60
814+ case res of
815+ Right SearchResult {..} -> return (hits searchHits, scrollId)
816+ Left _ -> return ([], Nothing)
817+
818+-- | Use the given scroll to fetch the next page of documents. If there are no
819+-- further pages, 'SearchResult.searchHits.hits' will be '[]'.
820+advanceScroll
821+ :: ( FromJSON a
822+ , MonadBH m
823+ , MonadThrow m
824+ )
825+ => ScrollId
826+ -> NominalDiffTime
827+ -- ^ How long should the snapshot of data be kept around? This timeout is updated every time 'advanceScroll' is used, so don't feel the need to set it to the entire duration of your search processing. Note that durations < 1s will be rounded up. Also note that 'NominalDiffTime' is an instance of Num so literals like 60 will be interpreted as seconds. 60s is a reasonable default.
828+ -> m (Either EsError (SearchResult a))
829+advanceScroll (ScrollId sid) scroll = do
830+ url <- joinPath ["_search/scroll?scroll=" <> scrollTime]
831+ parseEsResponse =<< post url (Just . L.fromStrict $ T.encodeUtf8 sid)
832+ where scrollTime = showText secs <> "s"
833+ secs :: Integer
834+ secs = round scroll
835+
836+simpleAccumulator :: (FromJSON a, MonadBH m, MonadThrow m) => [Hit a] -> ([Hit a], Maybe ScrollId) -> m ([Hit a], Maybe ScrollId)
837+simpleAccumulator oldHits (newHits, Nothing) = return (oldHits ++ newHits, Nothing)
838+simpleAccumulator oldHits ([], _) = return (oldHits, Nothing)
839+simpleAccumulator oldHits (newHits, msid) = do
840+ (newHits', msid') <- scroll' msid
841+ simpleAccumulator (oldHits ++ newHits) (newHits', msid')
842+
843+-- | 'scanSearch' uses the 'scan&scroll' API of elastic,
844+-- for a given 'IndexName' and 'MappingName'. Note that this will
845+-- consume the entire search result set and will be doing O(n) list
846+-- appends so this may not be suitable for large result sets. In that
847+-- case, 'getInitialScroll' and 'advanceScroll' are good low level
848+-- tools. You should be able to hook them up trivially to conduit,
849+-- pipes, or your favorite streaming IO abstraction of choice. Note
850+-- that ordering on the search would destroy performance and thus is
851+-- ignored.
852+scanSearch :: (FromJSON a, MonadBH m, MonadThrow m) => IndexName -> MappingName -> Search -> m [Hit a]
853+scanSearch indexName mappingName search = do
854+ msid <- getInitialScroll indexName mappingName search
855+ (hits, msid') <- scroll' msid
856+ (totalHits, _) <- simpleAccumulator [] (hits, msid')
857+ return totalHits
858+
859+-- | 'mkSearch' is a helper function for defaulting additional fields of a 'Search'
860+-- to Nothing in case you only care about your 'Query' and 'Filter'. Use record update
861+-- syntax if you want to add things like aggregations or highlights while still using
862+-- this helper function.
863+mkSearch :: Maybe Query -> Maybe Filter -> Search
864+mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing
865+
866+-- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for
867+-- the 'Query' and the 'Aggregation'.
868+mkAggregateSearch :: Maybe Query -> Aggregations -> Search
869+mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0) SearchTypeQueryThenFetch Nothing Nothing
870+
871+-- | 'mkHighlightSearch' is a helper function that defaults everything in a 'Search' except for
872+-- the 'Query' and the 'Aggregation'.
873+mkHighlightSearch :: Maybe Query -> Highlights -> Search
874+mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing
875+
876+-- | 'pageSearch' is a helper function that takes a search and assigns the from
877+-- and size fields for the search. The from parameter defines the offset
878+-- from the first result you want to fetch. The size parameter allows you to
879+-- configure the maximum amount of hits to be returned.
880+pageSearch :: From -- ^ The result offset
881+ -> Size -- ^ The number of results to return
882+ -> Search -- ^ The current seach
883+ -> Search -- ^ The paged search
884+pageSearch resultOffset pageSize search = search { from = resultOffset, size = pageSize }
885+
886+parseUrl' :: MonadThrow m => Text -> m Request
887+parseUrl' t = parseRequest (URI.escapeURIString URI.isAllowedInURI (T.unpack t))
888+
889+-- | Was there an optimistic concurrency control conflict when
890+-- indexing a document?
891+isVersionConflict :: Reply -> Bool
892+isVersionConflict = statusCheck (== 409)
893+
894+isSuccess :: Reply -> Bool
895+isSuccess = statusCheck (inRange (200, 299))
896+
897+isCreated :: Reply -> Bool
898+isCreated = statusCheck (== 201)
899+
900+statusCheck :: (Int -> Bool) -> Reply -> Bool
901+statusCheck prd = prd . NHTS.statusCode . responseStatus
902+
903+-- | This is a hook that can be set via the 'bhRequestHook' function
904+-- that will authenticate all requests using an HTTP Basic
905+-- Authentication header. Note that it is *strongly* recommended that
906+-- this option only be used over an SSL connection.
907+--
908+-- >> (mkBHEnv myServer myManager) { bhRequestHook = basicAuthHook (EsUsername "myuser") (EsPassword "mypass") }
909+basicAuthHook :: Monad m => EsUsername -> EsPassword -> Request -> m Request
910+basicAuthHook (EsUsername u) (EsPassword p) = return . applyBasicAuth u' p'
911+ where u' = T.encodeUtf8 u
912+ p' = T.encodeUtf8 p
913+
914+
915+boolQP :: Bool -> Text
916+boolQP True = "true"
917+boolQP False = "false"
src/Database/V1/Bloodhound/Types.hsnew file+5319
@@ -0,0 +1,5319 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+{-# LANGUAGE MultiParamTypeClasses #-}
7+-- {-# LANGUAGE NoMonomorphismRestriction #-}
8+{-# LANGUAGE NamedFieldPuns #-}
9+{-# LANGUAGE OverloadedStrings #-}
10+{-# LANGUAGE RecordWildCards #-}
11+{-# LANGUAGE UndecidableInstances #-}
12+
13+-------------------------------------------------------------------------------
14+-- |
15+-- Module : Database.Bloodhound.Types
16+-- Copyright : (C) 2014, 2015, 2016 Chris Allen
17+-- License : BSD-style (see the file LICENSE)
18+-- Maintainer : Chris Allen <cma@bitemyapp.com
19+-- Stability : provisional
20+-- Portability : DeriveGeneric, RecordWildCards
21+--
22+-- Data types for describing actions and data structures performed to interact
23+-- with Elasticsearch. The two main buckets your queries against Elasticsearch
24+-- will fall into are 'Query's and 'Filter's. 'Filter's are more like
25+-- traditional database constraints and often have preferable performance
26+-- properties. 'Query's support human-written textual queries, such as fuzzy
27+-- queries.
28+-------------------------------------------------------------------------------
29+
30+
31+
32+module Database.V1.Bloodhound.Types
33+ ( defaultCache
34+ , defaultIndexSettings
35+ , defaultIndexDocumentSettings
36+ , mkSort
37+ , showText
38+ , unpackId
39+ , mkMatchQuery
40+ , mkMultiMatchQuery
41+ , mkBoolQuery
42+ , mkRangeQuery
43+ , mkQueryStringQuery
44+ , mkAggregations
45+ , mkTermsAggregation
46+ , mkTermsScriptAggregation
47+ , mkDateHistogram
48+ , mkCardinalityAggregation
49+ , mkDocVersion
50+ , docVersionNumber
51+ , toMissing
52+ , toTerms
53+ , toDateHistogram
54+ , toTopHits
55+ , omitNulls
56+ , BH(..)
57+ , runBH
58+ , BHEnv
59+ , bhServer
60+ , bhManager
61+ , bhRequestHook
62+ , mkBHEnv
63+ , MonadBH(..)
64+ , Version(..)
65+ , VersionNumber(..)
66+ , MaybeNA(..)
67+ , BuildHash(..)
68+ , Status(..)
69+ , Existence(..)
70+ , NullValue(..)
71+ , IndexSettings(..)
72+ , UpdatableIndexSetting(..)
73+ , IndexSettingsSummary(..)
74+ , AllocationPolicy(..)
75+ , ReplicaBounds(..)
76+ , Bytes(..)
77+ , gigabytes
78+ , megabytes
79+ , kilobytes
80+ , FSType(..)
81+ , InitialShardCount(..)
82+ , NodeAttrFilter(..)
83+ , NodeAttrName(..)
84+ , CompoundFormat(..)
85+ , IndexTemplate(..)
86+ , Server(..)
87+ , Reply
88+ , EsResult(..)
89+ , EsResultFound(..)
90+ , EsError(..)
91+ , EsProtocolException(..)
92+ , IndexAlias(..)
93+ , IndexAliasName(..)
94+ , IndexAliasAction(..)
95+ , IndexAliasCreate(..)
96+ , IndexAliasSummary(..)
97+ , IndexAliasesSummary(..)
98+ , AliasRouting(..)
99+ , SearchAliasRouting(..)
100+ , IndexAliasRouting(..)
101+ , RoutingValue(..)
102+ , DocVersion
103+ , ExternalDocVersion(..)
104+ , VersionControl(..)
105+ , DocumentParent(..)
106+ , IndexDocumentSettings(..)
107+ , Query(..)
108+ , Search(..)
109+ , SearchType(..)
110+ , SearchResult(..)
111+ , ScrollId(..)
112+ , SearchHits(..)
113+ , TrackSortScores
114+ , From(..)
115+ , Size(..)
116+ , Source(..)
117+ , PatternOrPatterns(..)
118+ , Include(..)
119+ , Exclude(..)
120+ , Pattern(..)
121+ , ShardResult(..)
122+ , Hit(..)
123+ , Filter(..)
124+ , Seminearring(..)
125+ , BoolMatch(..)
126+ , Term(..)
127+ , GeoPoint(..)
128+ , GeoBoundingBoxConstraint(..)
129+ , GeoBoundingBox(..)
130+ , GeoFilterType(..)
131+ , Distance(..)
132+ , DistanceUnit(..)
133+ , DistanceType(..)
134+ , DistanceRange(..)
135+ , OptimizeBbox(..)
136+ , LatLon(..)
137+ , RangeValue(..)
138+ , RangeExecution(..)
139+ , LessThan(..)
140+ , LessThanEq(..)
141+ , GreaterThan(..)
142+ , GreaterThanEq(..)
143+ , LessThanD(..)
144+ , LessThanEqD(..)
145+ , GreaterThanD(..)
146+ , GreaterThanEqD(..)
147+ , Regexp(..)
148+ , RegexpFlags(..)
149+ , RegexpFlag(..)
150+ , FieldName(..)
151+ , Script(..)
152+ , IndexName(..)
153+ , IndexSelection(..)
154+ , NodeSelection(..)
155+ , NodeSelector(..)
156+ , IndexOptimizationSettings(..)
157+ , defaultIndexOptimizationSettings
158+ , TemplateName(..)
159+ , TemplatePattern(..)
160+ , MappingName(..)
161+ , DocId(..)
162+ , CacheName(..)
163+ , CacheKey(..)
164+ , BulkOperation(..)
165+ , ReplicaCount(..)
166+ , ShardCount(..)
167+ , Sort
168+ , SortMode(..)
169+ , SortOrder(..)
170+ , SortSpec(..)
171+ , DefaultSort(..)
172+ , Missing(..)
173+ , OpenCloseIndex(..)
174+ , Method
175+ , Boost(..)
176+ , MatchQuery(..)
177+ , MultiMatchQuery(..)
178+ , BoolQuery(..)
179+ , BoostingQuery(..)
180+ , CommonTermsQuery(..)
181+ , DisMaxQuery(..)
182+ , FilteredQuery(..)
183+ , FuzzyLikeThisQuery(..)
184+ , FuzzyLikeFieldQuery(..)
185+ , FuzzyQuery(..)
186+ , HasChildQuery(..)
187+ , HasParentQuery(..)
188+ , IndicesQuery(..)
189+ , MoreLikeThisQuery(..)
190+ , MoreLikeThisFieldQuery(..)
191+ , NestedQuery(..)
192+ , PrefixQuery(..)
193+ , QueryStringQuery(..)
194+ , SimpleQueryStringQuery(..)
195+ , RangeQuery(..)
196+ , RegexpQuery(..)
197+ , QueryString(..)
198+ , TemplateQueryInline(..)
199+ , TemplateQueryKeyValuePairs(..)
200+ , BooleanOperator(..)
201+ , ZeroTermsQuery(..)
202+ , CutoffFrequency(..)
203+ , Analyzer(..)
204+ , MaxExpansions(..)
205+ , Lenient(..)
206+ , MatchQueryType(..)
207+ , MultiMatchQueryType(..)
208+ , Tiebreaker(..)
209+ , MinimumMatch(..)
210+ , DisableCoord(..)
211+ , CommonMinimumMatch(..)
212+ , MinimumMatchHighLow(..)
213+ , PrefixLength(..)
214+ , Fuzziness(..)
215+ , IgnoreTermFrequency(..)
216+ , MaxQueryTerms(..)
217+ , ScoreType(..)
218+ , Score
219+ , Cache
220+ , TypeName(..)
221+ , BoostTerms(..)
222+ , MaxWordLength(..)
223+ , MinWordLength(..)
224+ , MaxDocFrequency(..)
225+ , MinDocFrequency(..)
226+ , PhraseSlop(..)
227+ , StopWord(..)
228+ , QueryPath(..)
229+ , MinimumTermFrequency(..)
230+ , PercentMatch(..)
231+ , FieldDefinition(..)
232+ , MappingField(..)
233+ , Mapping(..)
234+ , AllowLeadingWildcard(..)
235+ , LowercaseExpanded(..)
236+ , GeneratePhraseQueries(..)
237+ , Locale(..)
238+ , AnalyzeWildcard(..)
239+ , EnablePositionIncrements(..)
240+ , SimpleQueryFlag(..)
241+ , FieldOrFields(..)
242+ , Monoid(..)
243+ , ToJSON(..)
244+ , Interval(..)
245+ , TimeInterval(..)
246+ , ExecutionHint(..)
247+ , CollectionMode(..)
248+ , TermOrder(..)
249+ , TermInclusion(..)
250+ , SnapshotRepoSelection(..)
251+ , GenericSnapshotRepo(..)
252+ , SnapshotRepo(..)
253+ , SnapshotRepoConversionError(..)
254+ , SnapshotRepoType(..)
255+ , GenericSnapshotRepoSettings(..)
256+ , SnapshotRepoUpdateSettings(..)
257+ , defaultSnapshotRepoUpdateSettings
258+ , SnapshotRepoName(..)
259+ , SnapshotRepoPattern(..)
260+ , SnapshotVerification(..)
261+ , SnapshotNodeVerification(..)
262+ , FullNodeId(..)
263+ , NodeName(..)
264+ , ClusterName(..)
265+ , NodesInfo(..)
266+ , NodesStats(..)
267+ , NodeStats(..)
268+ , NodeBreakersStats(..)
269+ , NodeBreakerStats(..)
270+ , NodeHTTPStats(..)
271+ , NodeTransportStats(..)
272+ , NodeFSStats(..)
273+ , NodeDataPathStats(..)
274+ , NodeFSTotalStats(..)
275+ , NodeNetworkStats(..)
276+ , NodeThreadPoolsStats(..)
277+ , NodeThreadPoolStats(..)
278+ , NodeJVMStats(..)
279+ , JVMBufferPoolStats(..)
280+ , JVMGCStats(..)
281+ , JVMPoolStats(..)
282+ , NodeProcessStats(..)
283+ , NodeOSStats(..)
284+ , LoadAvgs(..)
285+ , NodeIndicesStats(..)
286+ , EsAddress(..)
287+ , PluginName(..)
288+ , NodeInfo(..)
289+ , NodePluginInfo(..)
290+ , NodeHTTPInfo(..)
291+ , NodeTransportInfo(..)
292+ , BoundTransportAddress(..)
293+ , NodeNetworkInfo(..)
294+ , MacAddress(..)
295+ , NetworkInterfaceName(..)
296+ , NodeNetworkInterface(..)
297+ , NodeThreadPoolsInfo(..)
298+ , NodeThreadPoolInfo(..)
299+ , ThreadPoolSize(..)
300+ , ThreadPoolType(..)
301+ , NodeJVMInfo(..)
302+ , JVMMemoryPool(..)
303+ , JVMGCCollector(..)
304+ , JVMMemoryInfo(..)
305+ , PID(..)
306+ , NodeOSInfo(..)
307+ , CPUInfo(..)
308+ , NodeProcessInfo(..)
309+ , FsSnapshotRepo(..)
310+ , SnapshotCreateSettings(..)
311+ , defaultSnapshotCreateSettings
312+ , SnapshotSelection(..)
313+ , SnapshotPattern(..)
314+ , SnapshotInfo(..)
315+ , SnapshotShardFailure(..)
316+ , ShardId(..)
317+ , SnapshotName(..)
318+ , SnapshotState(..)
319+ , SnapshotRestoreSettings(..)
320+ , defaultSnapshotRestoreSettings
321+ , RestoreRenamePattern(..)
322+ , RestoreRenameToken(..)
323+ , RRGroupRefNum
324+ , rrGroupRefNum
325+ , mkRRGroupRefNum
326+ , RestoreIndexSettings(..)
327+
328+ , Aggregation(..)
329+ , Aggregations
330+ , AggregationResults
331+ , BucketValue(..)
332+ , Bucket(..)
333+ , BucketAggregation(..)
334+ , TermsAggregation(..)
335+ , MissingAggregation(..)
336+ , ValueCountAggregation(..)
337+ , FilterAggregation(..)
338+ , CardinalityAggregation(..)
339+ , DateHistogramAggregation(..)
340+ , DateRangeAggregation(..)
341+ , DateRangeAggRange(..)
342+ , DateMathExpr(..)
343+ , DateMathAnchor(..)
344+ , DateMathModifier(..)
345+ , DateMathUnit(..)
346+ , TopHitsAggregation(..)
347+
348+ , Highlights(..)
349+ , FieldHighlight(..)
350+ , HighlightSettings(..)
351+ , PlainHighlight(..)
352+ , PostingsHighlight(..)
353+ , FastVectorHighlight(..)
354+ , CommonHighlight(..)
355+ , NonPostings(..)
356+ , HighlightEncoder(..)
357+ , HighlightTag(..)
358+ , HitHighlight
359+
360+ , MissingResult(..)
361+ , TermsResult(..)
362+ , DateHistogramResult(..)
363+ , DateRangeResult(..)
364+ , TopHitResult(..)
365+
366+ , EsUsername(..)
367+ , EsPassword(..)
368+ ) where
369+
370+import Control.Applicative as A
371+import Control.Arrow (first)
372+import Control.Monad.Catch
373+import Control.Monad.Except
374+import Control.Monad.Reader (MonadReader (..),
375+ ReaderT (..))
376+import Control.Monad.State (MonadState)
377+import Control.Monad.Writer (MonadWriter)
378+import Data.Aeson
379+import Data.Aeson.Types (Pair, Parser,
380+ emptyObject,
381+ parseEither, parseMaybe)
382+import qualified Data.ByteString.Lazy.Char8 as L
383+import Data.Char
384+import Data.Hashable (Hashable)
385+import qualified Data.HashMap.Strict as HM
386+import Data.List (foldl', intercalate,
387+ nub)
388+import Data.List.NonEmpty (NonEmpty (..), toList)
389+import qualified Data.Map.Strict as M
390+import Data.Maybe
391+import Data.Scientific (Scientific)
392+import Data.Semigroup
393+import Data.Text (Text)
394+import qualified Data.Text as T
395+import Data.Time.Calendar
396+import Data.Time.Clock (NominalDiffTime,
397+ UTCTime)
398+import Data.Time.Clock.POSIX
399+import qualified Data.Traversable as DT
400+import Data.Typeable (Typeable)
401+import qualified Data.Vector as V
402+import qualified Data.Version as Vers
403+import GHC.Enum
404+import GHC.Generics (Generic)
405+import Network.HTTP.Client
406+import qualified Network.HTTP.Types.Method as NHTM
407+import qualified Text.ParserCombinators.ReadP as RP
408+import qualified Text.Read as TR
409+
410+import Database.V1.Bloodhound.Types.Class
411+import Database.V1.Bloodhound.Types.Internal
412+
413+-- $setup
414+-- >>> :set -XOverloadedStrings
415+-- >>> import Data.Aeson
416+-- >>> import Database.V1.Bloodhound
417+-- >>> let testServer = (Server "http://localhost:9200")
418+-- >>> let testIndex = IndexName "twitter"
419+-- >>> let testMapping = MappingName "tweet"
420+-- >>> let defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
421+
422+-- defaultIndexSettings is exported by Database.Bloodhound as well
423+-- no trailing slashes in servers, library handles building the path.
424+
425+-- | Create a 'BHEnv' with all optional fields defaulted. HTTP hook
426+-- will be a noop. You can use the exported fields to customize it further, e.g.:
427+--
428+-- >> (mkBHEnv myServer myManager) { bhRequestHook = customHook }
429+mkBHEnv :: Server -> Manager -> BHEnv
430+mkBHEnv s m = BHEnv s m return
431+
432+newtype BH m a = BH {
433+ unBH :: ReaderT BHEnv m a
434+ } deriving ( Functor
435+ , A.Applicative
436+ , Monad
437+ , MonadIO
438+ , MonadState s
439+ , MonadWriter w
440+ , MonadError e
441+ , Alternative
442+ , MonadPlus
443+ , MonadFix
444+ , MonadThrow
445+ , MonadCatch
446+ , MonadMask)
447+
448+instance MonadTrans BH where
449+ lift = BH . lift
450+
451+instance (MonadReader r m) => MonadReader r (BH m) where
452+ ask = lift ask
453+ local f (BH (ReaderT m)) = BH $ ReaderT $ \r ->
454+ local f (m r)
455+
456+instance (Functor m, Applicative m, MonadIO m) => MonadBH (BH m) where
457+ getBHEnv = BH getBHEnv
458+
459+runBH :: BHEnv -> BH m a -> m a
460+runBH e f = runReaderT (unBH f) e
461+
462+{-| 'Version' is embedded in 'Status' -}
463+data Version = Version { number :: VersionNumber
464+ , build_hash :: BuildHash
465+ , build_timestamp :: UTCTime
466+ , build_snapshot :: Bool
467+ , lucene_version :: VersionNumber } deriving (Eq, Read, Show, Generic, Typeable)
468+
469+-- | Traditional software versioning number
470+newtype VersionNumber = VersionNumber { versionNumber :: Vers.Version
471+ } deriving (Eq, Read, Show, Generic, Typeable, Ord)
472+
473+{-| 'Status' is a data type for describing the JSON body returned by
474+ Elasticsearch when you query its status. This was deprecated in 1.2.0.
475+
476+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html#indices-status>
477+-}
478+
479+data Status = Status { ok :: Maybe Bool
480+ , status :: Int
481+ , name :: Text
482+ , version :: Version
483+ , tagline :: Text } deriving (Eq, Read, Show, Generic)
484+
485+{-| 'IndexSettings' is used to configure the shards and replicas when you create
486+ an Elasticsearch Index.
487+
488+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html>
489+-}
490+
491+data IndexSettings =
492+ IndexSettings { indexShards :: ShardCount
493+ , indexReplicas :: ReplicaCount } deriving (Eq, Read, Show, Generic, Typeable)
494+
495+{-| 'defaultIndexSettings' is an 'IndexSettings' with 3 shards and 2 replicas. -}
496+defaultIndexSettings :: IndexSettings
497+defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
498+
499+
500+{-| 'IndexOptimizationSettings' is used to configure index optimization. See
501+ <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-optimize.html>
502+ for more info.
503+-}
504+data IndexOptimizationSettings =
505+ IndexOptimizationSettings { maxNumSegments :: Maybe Int
506+ -- ^ Number of segments to optimize to. 1 will fully optimize the index. If omitted, the default behavior is to only optimize if the server deems it necessary.
507+ , onlyExpungeDeletes :: Bool
508+ -- ^ Should the optimize process only expunge segments with deletes in them? If the purpose of the optimization is to free disk space, this should be set to True.
509+ , flushAfterOptimize :: Bool
510+ -- ^ Should a flush be performed after the optimize.
511+ } deriving (Eq, Show, Generic, Typeable)
512+
513+
514+{-| 'defaultIndexOptimizationSettings' implements the default settings that
515+ ElasticSearch uses for index optimization. 'maxNumSegments' is Nothing,
516+ 'onlyExpungeDeletes' is False, and flushAfterOptimize is True.
517+-}
518+defaultIndexOptimizationSettings :: IndexOptimizationSettings
519+defaultIndexOptimizationSettings = IndexOptimizationSettings Nothing False True
520+
521+{-| 'UpdatableIndexSetting' are settings which may be updated after an index is created.
522+
523+ <https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html>
524+-}
525+data UpdatableIndexSetting = NumberOfReplicas ReplicaCount
526+ -- ^ The number of replicas each shard has.
527+ | AutoExpandReplicas ReplicaBounds
528+ | BlocksReadOnly Bool
529+ -- ^ Set to True to have the index read only. False to allow writes and metadata changes.
530+ | BlocksRead Bool
531+ -- ^ Set to True to disable read operations against the index.
532+ | BlocksWrite Bool
533+ -- ^ Set to True to disable write operations against the index.
534+ | BlocksMetaData Bool
535+ -- ^ Set to True to disable metadata operations against the index.
536+ | RefreshInterval NominalDiffTime
537+ -- ^ The async refresh interval of a shard
538+ | IndexConcurrency Int
539+ | FailOnMergeFailure Bool
540+ | TranslogFlushThresholdOps Int
541+ -- ^ When to flush on operations.
542+ | TranslogFlushThresholdSize Bytes
543+ -- ^ When to flush based on translog (bytes) size.
544+ | TranslogFlushThresholdPeriod NominalDiffTime
545+ -- ^ When to flush based on a period of not flushing.
546+ | TranslogDisableFlush Bool
547+ -- ^ Disables flushing. Note, should be set for a short interval and then enabled.
548+ | CacheFilterMaxSize (Maybe Bytes)
549+ -- ^ The maximum size of filter cache (per segment in shard).
550+ | CacheFilterExpire (Maybe NominalDiffTime)
551+ -- ^ The expire after access time for filter cache.
552+ | GatewaySnapshotInterval NominalDiffTime
553+ -- ^ The gateway snapshot interval (only applies to shared gateways).
554+ | RoutingAllocationInclude (NonEmpty NodeAttrFilter)
555+ -- ^ A node matching any rule will be allowed to host shards from the index.
556+ | RoutingAllocationExclude (NonEmpty NodeAttrFilter)
557+ -- ^ A node matching any rule will NOT be allowed to host shards from the index.
558+ | RoutingAllocationRequire (NonEmpty NodeAttrFilter)
559+ -- ^ Only nodes matching all rules will be allowed to host shards from the index.
560+ | RoutingAllocationEnable AllocationPolicy
561+ -- ^ Enables shard allocation for a specific index.
562+ | RoutingAllocationShardsPerNode ShardCount
563+ -- ^ Controls the total number of shards (replicas and primaries) allowed to be allocated on a single node.
564+ | RecoveryInitialShards InitialShardCount
565+ -- ^ When using local gateway a particular shard is recovered only if there can be allocated quorum shards in the cluster.
566+ | GCDeletes NominalDiffTime
567+ | TTLDisablePurge Bool
568+ -- ^ Disables temporarily the purge of expired docs.
569+ | TranslogFSType FSType
570+ | IndexCompoundFormat CompoundFormat
571+ | IndexCompoundOnFlush Bool
572+ | WarmerEnabled Bool
573+ deriving (Eq, Show, Generic, Typeable)
574+
575+data AllocationPolicy = AllocAll
576+ -- ^ Allows shard allocation for all shards.
577+ | AllocPrimaries
578+ -- ^ Allows shard allocation only for primary shards.
579+ | AllocNewPrimaries
580+ -- ^ Allows shard allocation only for primary shards for new indices.
581+ | AllocNone
582+ -- ^ No shard allocation is allowed
583+ deriving (Eq, Read, Show, Generic, Typeable)
584+
585+data ReplicaBounds = ReplicasBounded Int Int
586+ | ReplicasLowerBounded Int
587+ | ReplicasUnbounded
588+ deriving (Eq, Read, Show, Generic, Typeable)
589+
590+-- | A measure of bytes used for various configurations. You may want
591+-- to use smart constructors like 'gigabytes' for larger values.
592+--
593+-- >>> gigabytes 9
594+-- Bytes 9000000000
595+--
596+-- >>> megabytes 9
597+-- Bytes 9000000
598+--
599+-- >>> kilobytes 9
600+-- Bytes 9000
601+newtype Bytes = Bytes Int deriving (Eq, Read, Show, Generic, Typeable, Ord, ToJSON, FromJSON)
602+
603+gigabytes :: Int -> Bytes
604+gigabytes n = megabytes (1000 * n)
605+
606+
607+megabytes :: Int -> Bytes
608+megabytes n = kilobytes (1000 * n)
609+
610+
611+kilobytes :: Int -> Bytes
612+kilobytes n = Bytes (1000 * n)
613+
614+
615+data FSType = FSSimple
616+ | FSBuffered deriving (Eq, Read, Show, Generic, Typeable, Ord)
617+
618+data InitialShardCount = QuorumShards
619+ | QuorumMinus1Shards
620+ | FullShards
621+ | FullMinus1Shards
622+ | ExplicitShards Int
623+ deriving (Eq, Read, Show, Generic, Typeable)
624+
625+data NodeAttrFilter = NodeAttrFilter { nodeAttrFilterName :: NodeAttrName
626+ , nodeAttrFilterValues :: NonEmpty Text}
627+ deriving (Eq, Read, Show, Generic, Ord, Typeable)
628+
629+newtype NodeAttrName = NodeAttrName Text deriving (Eq, Read, Show, Ord, Generic, Typeable)
630+
631+data CompoundFormat = CompoundFileFormat Bool
632+ | MergeSegmentVsTotalIndex Double
633+ -- ^ percentage between 0 and 1 where 0 is false, 1 is true
634+ deriving (Eq, Read, Show, Generic, Typeable)
635+
636+newtype NominalDiffTimeJSON = NominalDiffTimeJSON { ndtJSON :: NominalDiffTime }
637+
638+data IndexSettingsSummary = IndexSettingsSummary { sSummaryIndexName :: IndexName
639+ , sSummaryFixedSettings :: IndexSettings
640+ , sSummaryUpdateable :: [UpdatableIndexSetting]}
641+ deriving (Eq, Show, Generic, Typeable)
642+
643+{-| 'Reply' and 'Method' are type synonyms from 'Network.HTTP.Types.Method.Method' -}
644+type Reply = Network.HTTP.Client.Response L.ByteString
645+type Method = NHTM.Method
646+
647+{-| 'OpenCloseIndex' is a sum type for opening and closing indices.
648+
649+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html>
650+-}
651+data OpenCloseIndex = OpenIndex | CloseIndex deriving (Eq, Read, Show, Generic, Typeable)
652+
653+data FieldType = GeoPointType
654+ | GeoShapeType
655+ | FloatType
656+ | IntegerType
657+ | LongType
658+ | ShortType
659+ | ByteType deriving (Eq, Read, Show, Generic, Typeable)
660+
661+data FieldDefinition =
662+ FieldDefinition { fieldType :: FieldType } deriving (Eq, Read, Show, Generic, Typeable)
663+
664+{-| An 'IndexTemplate' defines a template that will automatically be
665+ applied to new indices created. The templates include both
666+ 'IndexSettings' and mappings, and a simple 'TemplatePattern' that
667+ controls if the template will be applied to the index created.
668+ Specify mappings as follows: @[toJSON TweetMapping, ...]@
669+
670+ https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html
671+-}
672+data IndexTemplate =
673+ IndexTemplate { templatePattern :: TemplatePattern
674+ , templateSettings :: Maybe IndexSettings
675+ , templateMappings :: [Value]
676+ }
677+
678+data MappingField =
679+ MappingField { mappingFieldName :: FieldName
680+ , fieldDefinition :: FieldDefinition } deriving (Eq, Read, Show, Generic, Typeable)
681+
682+{-| Support for type reification of 'Mapping's is currently incomplete, for
683+ now the mapping API verbiage expects a 'ToJSON'able blob.
684+
685+ Indexes have mappings, mappings are schemas for the documents contained in the
686+ index. I'd recommend having only one mapping per index, always having a mapping,
687+ and keeping different kinds of documents separated if possible.
688+-}
689+data Mapping = Mapping { typeName :: TypeName
690+ , mappingFields :: [MappingField] } deriving (Eq, Read, Show, Generic, Typeable)
691+
692+{-| 'BulkOperation' is a sum type for expressing the four kinds of bulk
693+ operation index, create, delete, and update. 'BulkIndex' behaves like an
694+ "upsert", 'BulkCreate' will fail if a document already exists at the DocId.
695+
696+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk>
697+-}
698+data BulkOperation =
699+ BulkIndex IndexName MappingName DocId Value
700+ | BulkCreate IndexName MappingName DocId Value
701+ | BulkDelete IndexName MappingName DocId
702+ | BulkUpdate IndexName MappingName DocId Value deriving (Eq, Read, Show, Generic, Typeable)
703+
704+{-| 'EsResult' describes the standard wrapper JSON document that you see in
705+ successful Elasticsearch lookups or lookups that couldn't find the document.
706+-}
707+data EsResult a = EsResult { _index :: Text
708+ , _type :: Text
709+ , _id :: Text
710+ , foundResult :: Maybe (EsResultFound a)} deriving (Eq, Read, Show, Generic, Typeable)
711+
712+{-| 'EsResultFound' contains the document and its metadata inside of an
713+ 'EsResult' when the document was successfully found.
714+-}
715+data EsResultFound a = EsResultFound { _version :: DocVersion
716+ , _source :: a } deriving (Eq, Read, Show, Generic, Typeable)
717+
718+{-| 'EsError' is the generic type that will be returned when there was a
719+ problem. If you can't parse the expected response, its a good idea to
720+ try parsing this.
721+-}
722+data EsError = EsError { errorStatus :: Int
723+ , errorMessage :: Text } deriving (Eq, Read, Show, Generic, Typeable)
724+
725+{-| 'EsProtocolException' will be thrown if Bloodhound cannot parse a response
726+returned by the ElasticSearch server. If you encounter this error, please
727+verify that your domain data types and FromJSON instances are working properly
728+(for example, the 'a' of '[Hit a]' in 'SearchResult.searchHits.hits'). If you're
729+sure that your mappings are correct, then this error may be an indication of an
730+incompatibility between Bloodhound and ElasticSearch. Please open a bug report
731+and be sure to include the exception body.
732+-}
733+data EsProtocolException = EsProtocolException { esProtoExBody :: L.ByteString }
734+ deriving (Eq, Read, Show, Generic, Typeable)
735+
736+instance Exception EsProtocolException
737+
738+data IndexAlias = IndexAlias { srcIndex :: IndexName
739+ , indexAlias :: IndexAliasName } deriving (Eq, Read, Show, Generic, Typeable)
740+
741+newtype IndexAliasName = IndexAliasName { indexAliasName :: IndexName } deriving (Eq, Read, Show, Generic, ToJSON)
742+
743+data IndexAliasAction = AddAlias IndexAlias IndexAliasCreate
744+ | RemoveAlias IndexAlias deriving (Read, Show, Eq, Generic, Typeable)
745+
746+data IndexAliasCreate = IndexAliasCreate { aliasCreateRouting :: Maybe AliasRouting
747+ , aliasCreateFilter :: Maybe Filter}
748+ deriving (Read, Show, Eq, Generic, Typeable)
749+
750+data AliasRouting = AllAliasRouting RoutingValue
751+ | GranularAliasRouting (Maybe SearchAliasRouting) (Maybe IndexAliasRouting)
752+ deriving (Read, Show, Eq, Generic, Typeable)
753+
754+newtype SearchAliasRouting = SearchAliasRouting (NonEmpty RoutingValue) deriving (Read, Show, Eq, Generic, Typeable)
755+
756+newtype IndexAliasRouting = IndexAliasRouting RoutingValue deriving (Read, Show, Eq, Generic, ToJSON, FromJSON, Typeable)
757+
758+newtype RoutingValue = RoutingValue { routingValue :: Text } deriving (Read, Show, Eq, Generic, ToJSON, FromJSON, Typeable)
759+
760+newtype IndexAliasesSummary = IndexAliasesSummary { indexAliasesSummary :: [IndexAliasSummary] } deriving (Read, Show, Eq, Generic, Typeable)
761+
762+{-| 'IndexAliasSummary' is a summary of an index alias configured for a server. -}
763+data IndexAliasSummary = IndexAliasSummary { indexAliasSummaryAlias :: IndexAlias
764+ , indexAliasSummaryCreate :: IndexAliasCreate} deriving (Read, Show, Eq, Generic, Typeable)
765+
766+{-| 'DocVersion' is an integer version number for a document between 1
767+and 9.2e+18 used for <<https://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html optimistic concurrency control>>.
768+-}
769+newtype DocVersion = DocVersion {
770+ docVersionNumber :: Int
771+ } deriving (Eq, Read, Show, Generic, Ord, ToJSON)
772+
773+-- | Smart constructor for in-range doc version
774+mkDocVersion :: Int -> Maybe DocVersion
775+mkDocVersion i
776+ | i >= (docVersionNumber minBound) && i <= (docVersionNumber maxBound) =
777+ Just $ DocVersion i
778+ | otherwise = Nothing
779+
780+
781+{-| 'ExternalDocVersion' is a convenience wrapper if your code uses its
782+own version numbers instead of ones from ES.
783+-}
784+newtype ExternalDocVersion = ExternalDocVersion DocVersion
785+ deriving (Eq, Read, Show, Generic, Ord, Bounded, Enum, ToJSON)
786+
787+{-| 'VersionControl' is specified when indexing documents as a
788+optimistic concurrency control.
789+-}
790+data VersionControl = NoVersionControl
791+ -- ^ Don't send a version. This is a pure overwrite.
792+ | InternalVersion DocVersion
793+ -- ^ Use the default ES versioning scheme. Only
794+ -- index the document if the version is the same
795+ -- as the one specified. Only applicable to
796+ -- updates, as you should be getting Version from
797+ -- a search result.
798+ | ExternalGT ExternalDocVersion
799+ -- ^ Use your own version numbering. Only index
800+ -- the document if the version is strictly higher
801+ -- OR the document doesn't exist. The given
802+ -- version will be used as the new version number
803+ -- for the stored document. N.B. All updates must
804+ -- increment this number, meaning there is some
805+ -- global, external ordering of updates.
806+ | ExternalGTE ExternalDocVersion
807+ -- ^ Use your own version numbering. Only index
808+ -- the document if the version is equal or higher
809+ -- than the stored version. Will succeed if there
810+ -- is no existing document. The given version will
811+ -- be used as the new version number for the
812+ -- stored document. Use with care, as this could
813+ -- result in data loss.
814+ | ForceVersion ExternalDocVersion
815+ -- ^ The document will always be indexed and the
816+ -- given version will be the new version. This is
817+ -- typically used for correcting errors. Use with
818+ -- care, as this could result in data loss.
819+ deriving (Read, Show, Eq, Generic, Ord)
820+
821+{-| 'DocumentParent' is used to specify a parent document.
822+-}
823+newtype DocumentParent = DocumentParent DocId
824+ deriving (Eq, Read, Show, Generic, Typeable)
825+
826+{-| 'IndexDocumentSettings' are special settings supplied when indexing
827+a document. For the best backwards compatiblity when new fields are
828+added, you should probably prefer to start with 'defaultIndexDocumentSettings'
829+-}
830+data IndexDocumentSettings =
831+ IndexDocumentSettings { idsVersionControl :: VersionControl
832+ , idsParent :: Maybe DocumentParent
833+ } deriving (Eq, Read, Show, Generic, Typeable)
834+
835+{-| Reasonable default settings. Chooses no version control and no parent.
836+-}
837+defaultIndexDocumentSettings :: IndexDocumentSettings
838+defaultIndexDocumentSettings = IndexDocumentSettings NoVersionControl Nothing
839+
840+{-| 'Sort' is a synonym for a list of 'SortSpec's. Sort behavior is order
841+ dependent with later sorts acting as tie-breakers for earlier sorts.
842+-}
843+type Sort = [SortSpec]
844+
845+{-| The two main kinds of 'SortSpec' are 'DefaultSortSpec' and
846+ 'GeoDistanceSortSpec'. The latter takes a 'SortOrder', 'GeoPoint', and
847+ 'DistanceUnit' to express "nearness" to a single geographical point as a
848+ sort specification.
849+
850+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
851+-}
852+data SortSpec = DefaultSortSpec DefaultSort
853+ | GeoDistanceSortSpec SortOrder GeoPoint DistanceUnit deriving (Eq, Read, Show, Generic, Typeable)
854+
855+{-| 'DefaultSort' is usually the kind of 'SortSpec' you'll want. There's a
856+ 'mkSort' convenience function for when you want to specify only the most
857+ common parameters.
858+
859+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
860+-}
861+data DefaultSort =
862+ DefaultSort { sortFieldName :: FieldName
863+ , sortOrder :: SortOrder
864+ -- default False
865+ , ignoreUnmapped :: Bool
866+ , sortMode :: Maybe SortMode
867+ , missingSort :: Maybe Missing
868+ , nestedFilter :: Maybe Filter } deriving (Eq, Read, Show, Generic, Typeable)
869+
870+{-| 'SortOrder' is 'Ascending' or 'Descending', as you might expect. These get
871+ encoded into "asc" or "desc" when turned into JSON.
872+
873+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
874+-}
875+data SortOrder = Ascending
876+ | Descending deriving (Eq, Read, Show, Generic, Typeable)
877+
878+{-| 'Missing' prescribes how to handle missing fields. A missing field can be
879+ sorted last, first, or using a custom value as a substitute.
880+
881+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_missing_values>
882+-}
883+data Missing = LastMissing
884+ | FirstMissing
885+ | CustomMissing Text deriving (Eq, Read, Show, Generic, Typeable)
886+
887+{-| 'SortMode' prescribes how to handle sorting array/multi-valued fields.
888+
889+http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_sort_mode_option
890+-}
891+data SortMode = SortMin
892+ | SortMax
893+ | SortSum
894+ | SortAvg deriving (Eq, Read, Show, Generic, Typeable)
895+
896+{-| 'mkSort' defaults everything but the 'FieldName' and the 'SortOrder' so
897+ that you can concisely describe the usual kind of 'SortSpec's you want.
898+-}
899+mkSort :: FieldName -> SortOrder -> DefaultSort
900+mkSort fieldName sOrder = DefaultSort fieldName sOrder False Nothing Nothing Nothing
901+
902+{-| 'Cache' is for telling ES whether it should cache a 'Filter' not.
903+ 'Query's cannot be cached.
904+-}
905+type Cache = Bool -- caching on/off
906+defaultCache :: Cache
907+defaultCache = False
908+
909+{-| 'PrefixValue' is used in 'PrefixQuery' as the main query component.
910+-}
911+type PrefixValue = Text
912+
913+{-| 'BooleanOperator' is the usual And/Or operators with an ES compatible
914+ JSON encoding baked in. Used all over the place.
915+-}
916+data BooleanOperator = And | Or deriving (Eq, Read, Show, Generic, Typeable)
917+
918+{-| 'ShardCount' is part of 'IndexSettings'
919+-}
920+newtype ShardCount = ShardCount Int deriving (Eq, Read, Show, Generic, ToJSON, Typeable)
921+
922+{-| 'ReplicaCount' is part of 'IndexSettings'
923+-}
924+newtype ReplicaCount = ReplicaCount Int deriving (Eq, Read, Show, Generic, ToJSON, Typeable)
925+
926+{-| 'IndexName' is used to describe which index to query/create/delete
927+-}
928+newtype IndexName = IndexName Text deriving (Eq, Generic, Read, Show, ToJSON, FromJSON, Typeable)
929+
930+{-| 'IndexSelection' is used for APIs which take a single index, a list of
931+ indexes, or the special @_all@ index.
932+-}
933+--TODO: this does not fully support <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/multi-index.html multi-index syntax>. It wouldn't be too hard to implement but you'd have to add the optional parameters (ignore_unavailable, allow_no_indices, expand_wildcards) to any APIs using it. Also would be a breaking API.
934+data IndexSelection = IndexList (NonEmpty IndexName)
935+ | AllIndexes deriving (Eq, Generic, Show, Typeable)
936+
937+{-| 'NodeSelection' is used for most cluster APIs. See <https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html#cluster-nodes here> for more details.
938+-}
939+data NodeSelection = LocalNode
940+ -- ^ Whatever node receives this request
941+ | NodeList (NonEmpty NodeSelector)
942+ | AllNodes deriving (Eq, Generic, Show, Typeable)
943+
944+
945+-- | An exact match or pattern to identify a node. Note that All of
946+-- these options support wildcarding, so your node name, server, attr
947+-- name can all contain * characters to be a fuzzy match.
948+data NodeSelector = NodeByName NodeName
949+ | NodeByFullNodeId FullNodeId
950+ | NodeByHost Server
951+ -- ^ e.g. 10.0.0.1 or even 10.0.0.*
952+ | NodeByAttribute NodeAttrName Text
953+ -- ^ NodeAttrName can be a pattern, e.g. rack*. The value can too.
954+ deriving (Eq, Generic, Show, Typeable)
955+
956+{-| 'TemplateName' is used to describe which template to query/create/delete
957+-}
958+newtype TemplateName = TemplateName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
959+
960+{-| 'TemplatePattern' represents a pattern which is matched against index names
961+-}
962+newtype TemplatePattern = TemplatePattern Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
963+
964+{-| 'MappingName' is part of mappings which are how ES describes and schematizes
965+ the data in the indices.
966+-}
967+newtype MappingName = MappingName Text deriving (Eq, Generic, Read, Show, ToJSON, FromJSON, Typeable)
968+
969+{-| 'DocId' is a generic wrapper value for expressing unique Document IDs.
970+ Can be set by the user or created by ES itself. Often used in client
971+ functions for poking at specific documents.
972+-}
973+newtype DocId = DocId Text deriving (Eq, Generic, Read, Show, ToJSON, FromJSON, Typeable)
974+
975+{-| 'QueryString' is used to wrap query text bodies, be they human written or not.
976+-}
977+newtype QueryString = QueryString Text deriving (Eq, Generic, Read, Show, ToJSON, FromJSON, Typeable)
978+
979+{-| 'FieldName' is used all over the place wherever a specific field within
980+ a document needs to be specified, usually in 'Query's or 'Filter's.
981+-}
982+newtype FieldName = FieldName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
983+
984+
985+{-| 'Script' is often used in place of 'FieldName' to specify more
986+complex ways of extracting a value from a document.
987+-}
988+newtype Script = Script { scriptText :: Text } deriving (Eq, Read, Show, Generic, Typeable)
989+
990+{-| 'CacheName' is used in 'RegexpFilter' for describing the
991+ 'CacheKey' keyed caching behavior.
992+-}
993+newtype CacheName = CacheName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
994+
995+{-| 'CacheKey' is used in 'RegexpFilter' to key regex caching.
996+-}
997+newtype CacheKey =
998+ CacheKey Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
999+newtype Existence =
1000+ Existence Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1001+newtype NullValue =
1002+ NullValue Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1003+newtype CutoffFrequency =
1004+ CutoffFrequency Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1005+newtype Analyzer =
1006+ Analyzer Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1007+newtype MaxExpansions =
1008+ MaxExpansions Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1009+
1010+{-| 'Lenient', if set to true, will cause format based failures to be
1011+ ignored. I don't know what the bloody default is, Elasticsearch
1012+ documentation didn't say what it was. Let me know if you figure it out.
1013+-}
1014+newtype Lenient =
1015+ Lenient Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1016+newtype Tiebreaker =
1017+ Tiebreaker Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1018+newtype Boost =
1019+ Boost Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1020+newtype BoostTerms =
1021+ BoostTerms Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1022+
1023+{-| 'MinimumMatch' controls how many should clauses in the bool query should
1024+ match. Can be an absolute value (2) or a percentage (30%) or a
1025+ combination of both.
1026+-}
1027+newtype MinimumMatch =
1028+ MinimumMatch Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1029+newtype DisableCoord =
1030+ DisableCoord Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1031+newtype IgnoreTermFrequency =
1032+ IgnoreTermFrequency Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1033+newtype MinimumTermFrequency =
1034+ MinimumTermFrequency Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1035+newtype MaxQueryTerms =
1036+ MaxQueryTerms Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1037+newtype Fuzziness =
1038+ Fuzziness Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1039+
1040+{-| 'PrefixLength' is the prefix length used in queries, defaults to 0. -}
1041+newtype PrefixLength =
1042+ PrefixLength Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1043+newtype TypeName =
1044+ TypeName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1045+newtype PercentMatch =
1046+ PercentMatch Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1047+newtype StopWord =
1048+ StopWord Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1049+newtype QueryPath =
1050+ QueryPath Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1051+
1052+{-| Allowing a wildcard at the beginning of a word (eg "*ing") is particularly
1053+ heavy, because all terms in the index need to be examined, just in case
1054+ they match. Leading wildcards can be disabled by setting
1055+ 'AllowLeadingWildcard' to false. -}
1056+newtype AllowLeadingWildcard =
1057+ AllowLeadingWildcard Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1058+newtype LowercaseExpanded =
1059+ LowercaseExpanded Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1060+newtype EnablePositionIncrements =
1061+ EnablePositionIncrements Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1062+
1063+{-| By default, wildcard terms in a query are not analyzed.
1064+ Setting 'AnalyzeWildcard' to true enables best-effort analysis.
1065+-}
1066+newtype AnalyzeWildcard = AnalyzeWildcard Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1067+
1068+{-| 'GeneratePhraseQueries' defaults to false.
1069+-}
1070+newtype GeneratePhraseQueries =
1071+ GeneratePhraseQueries Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1072+
1073+{-| 'Locale' is used for string conversions - defaults to ROOT.
1074+-}
1075+newtype Locale = Locale Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1076+newtype MaxWordLength = MaxWordLength Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1077+newtype MinWordLength = MinWordLength Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1078+
1079+{-| 'PhraseSlop' sets the default slop for phrases, 0 means exact
1080+ phrase matches. Default is 0.
1081+-}
1082+newtype PhraseSlop = PhraseSlop Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1083+newtype MinDocFrequency = MinDocFrequency Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1084+newtype MaxDocFrequency = MaxDocFrequency Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1085+
1086+-- | Newtype wrapper to parse ES's concerning tendency to in some APIs return a floating point number of milliseconds since epoch ಠ_ಠ
1087+newtype POSIXMS = POSIXMS { posixMS :: UTCTime }
1088+
1089+{-| 'unpackId' is a silly convenience function that gets used once.
1090+-}
1091+unpackId :: DocId -> Text
1092+unpackId (DocId docId) = docId
1093+
1094+type TrackSortScores = Bool
1095+newtype From = From Int deriving (Eq, Read, Show, Generic, ToJSON)
1096+newtype Size = Size Int deriving (Eq, Read, Show, Generic, ToJSON)
1097+
1098+data Search = Search { queryBody :: Maybe Query
1099+ , filterBody :: Maybe Filter
1100+ , sortBody :: Maybe Sort
1101+ , aggBody :: Maybe Aggregations
1102+ , highlight :: Maybe Highlights
1103+ -- default False
1104+ , trackSortScores :: TrackSortScores
1105+ , from :: From
1106+ , size :: Size
1107+ , searchType :: SearchType
1108+ , fields :: Maybe [FieldName]
1109+ , source :: Maybe Source } deriving (Eq, Read, Show, Generic, Typeable)
1110+
1111+data SearchType = SearchTypeQueryThenFetch
1112+ | SearchTypeDfsQueryThenFetch
1113+ | SearchTypeCount
1114+ | SearchTypeScan
1115+ | SearchTypeQueryAndFetch
1116+ | SearchTypeDfsQueryAndFetch
1117+ deriving (Eq, Read, Show, Generic, Typeable)
1118+
1119+data Source =
1120+ NoSource
1121+ | SourcePatterns PatternOrPatterns
1122+ | SourceIncludeExclude Include Exclude
1123+ deriving (Read, Show, Eq, Generic, Typeable)
1124+
1125+data PatternOrPatterns = PopPattern Pattern
1126+ | PopPatterns [Pattern] deriving (Eq, Read, Show, Generic, Typeable)
1127+
1128+data Include = Include [Pattern] deriving (Eq, Read, Show, Generic, Typeable)
1129+data Exclude = Exclude [Pattern] deriving (Eq, Read, Show, Generic, Typeable)
1130+
1131+newtype Pattern = Pattern Text deriving (Eq, Read, Show, Generic, Typeable)
1132+
1133+data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
1134+ , highlightFields :: [FieldHighlight]
1135+ } deriving (Read, Show, Eq, Generic, Typeable)
1136+
1137+data FieldHighlight = FieldHighlight FieldName (Maybe HighlightSettings)
1138+ deriving (Read, Show, Eq, Generic, Typeable)
1139+
1140+
1141+data HighlightSettings = Plain PlainHighlight
1142+ | Postings PostingsHighlight
1143+ | FastVector FastVectorHighlight
1144+ deriving (Read, Show, Eq, Generic, Typeable)
1145+data PlainHighlight =
1146+ PlainHighlight { plainCommon :: Maybe CommonHighlight
1147+ , plainNonPost :: Maybe NonPostings } deriving (Read, Show, Eq, Generic, Typeable)
1148+
1149+ -- This requires that index_options are set to 'offset' in the mapping.
1150+data PostingsHighlight = PostingsHighlight (Maybe CommonHighlight) deriving (Read, Show, Eq, Generic, Typeable)
1151+
1152+-- This requires that term_vector is set to 'with_positions_offsets' in the mapping.
1153+data FastVectorHighlight =
1154+ FastVectorHighlight { fvCommon :: Maybe CommonHighlight
1155+ , fvNonPostSettings :: Maybe NonPostings
1156+ , boundaryChars :: Maybe Text
1157+ , boundaryMaxScan :: Maybe Int
1158+ , fragmentOffset :: Maybe Int
1159+ , matchedFields :: [Text]
1160+ , phraseLimit :: Maybe Int
1161+ } deriving (Read, Show, Eq, Generic, Typeable)
1162+
1163+data CommonHighlight =
1164+ CommonHighlight { order :: Maybe Text
1165+ , forceSource :: Maybe Bool
1166+ , tag :: Maybe HighlightTag
1167+ , encoder :: Maybe HighlightEncoder
1168+ , noMatchSize :: Maybe Int
1169+ , highlightQuery :: Maybe Query
1170+ , requireFieldMatch :: Maybe Bool
1171+ } deriving (Read, Show, Eq, Generic, Typeable)
1172+
1173+-- Settings that are only applicable to FastVector and Plain highlighters.
1174+data NonPostings =
1175+ NonPostings { fragmentSize :: Maybe Int
1176+ , numberOfFragments :: Maybe Int} deriving (Read, Show, Eq, Generic, Typeable)
1177+
1178+data HighlightEncoder = DefaultEncoder
1179+ | HTMLEncoder
1180+ deriving (Read, Show, Eq, Generic, Typeable)
1181+
1182+-- NOTE: Should the tags use some kind of HTML type, rather than Text?
1183+data HighlightTag = TagSchema Text
1184+ | CustomTags ([Text], [Text]) -- Only uses more than the first value in the lists if fvh
1185+ deriving (Read, Show, Eq, Generic, Typeable)
1186+
1187+
1188+data Query =
1189+ TermQuery Term (Maybe Boost)
1190+ | TermsQuery Text (NonEmpty Text)
1191+ | QueryMatchQuery MatchQuery
1192+ | QueryMultiMatchQuery MultiMatchQuery
1193+ | QueryBoolQuery BoolQuery
1194+ | QueryBoostingQuery BoostingQuery
1195+ | QueryCommonTermsQuery CommonTermsQuery
1196+ | ConstantScoreFilter Filter Boost
1197+ | ConstantScoreQuery Query Boost
1198+ | QueryDisMaxQuery DisMaxQuery
1199+ | QueryFilteredQuery FilteredQuery
1200+ | QueryFuzzyLikeThisQuery FuzzyLikeThisQuery
1201+ | QueryFuzzyLikeFieldQuery FuzzyLikeFieldQuery
1202+ | QueryFuzzyQuery FuzzyQuery
1203+ | QueryHasChildQuery HasChildQuery
1204+ | QueryHasParentQuery HasParentQuery
1205+ | IdsQuery MappingName [DocId]
1206+ | QueryIndicesQuery IndicesQuery
1207+ | MatchAllQuery (Maybe Boost)
1208+ | QueryMoreLikeThisQuery MoreLikeThisQuery
1209+ | QueryMoreLikeThisFieldQuery MoreLikeThisFieldQuery
1210+ | QueryNestedQuery NestedQuery
1211+ | QueryPrefixQuery PrefixQuery
1212+ | QueryQueryStringQuery QueryStringQuery
1213+ | QuerySimpleQueryStringQuery SimpleQueryStringQuery
1214+ | QueryRangeQuery RangeQuery
1215+ | QueryRegexpQuery RegexpQuery
1216+ | QueryTemplateQueryInline TemplateQueryInline
1217+ deriving (Eq, Read, Show, Generic, Typeable)
1218+
1219+data RegexpQuery =
1220+ RegexpQuery { regexpQueryField :: FieldName
1221+ , regexpQuery :: Regexp
1222+ , regexpQueryFlags :: RegexpFlags
1223+ , regexpQueryBoost :: Maybe Boost
1224+ } deriving (Eq, Read, Show, Generic, Typeable)
1225+
1226+data RangeQuery =
1227+ RangeQuery { rangeQueryField :: FieldName
1228+ , rangeQueryRange :: RangeValue
1229+ , rangeQueryBoost :: Boost } deriving (Eq, Read, Show, Generic, Typeable)
1230+
1231+mkRangeQuery :: FieldName -> RangeValue -> RangeQuery
1232+mkRangeQuery f r = RangeQuery f r (Boost 1.0)
1233+
1234+data SimpleQueryStringQuery =
1235+ SimpleQueryStringQuery
1236+ { simpleQueryStringQuery :: QueryString
1237+ , simpleQueryStringField :: Maybe FieldOrFields
1238+ , simpleQueryStringOperator :: Maybe BooleanOperator
1239+ , simpleQueryStringAnalyzer :: Maybe Analyzer
1240+ , simpleQueryStringFlags :: Maybe (NonEmpty SimpleQueryFlag)
1241+ , simpleQueryStringLowercaseExpanded :: Maybe LowercaseExpanded
1242+ , simpleQueryStringLocale :: Maybe Locale
1243+ } deriving (Eq, Read, Show, Generic, Typeable)
1244+
1245+data SimpleQueryFlag =
1246+ SimpleQueryAll
1247+ | SimpleQueryNone
1248+ | SimpleQueryAnd
1249+ | SimpleQueryOr
1250+ | SimpleQueryPrefix
1251+ | SimpleQueryPhrase
1252+ | SimpleQueryPrecedence
1253+ | SimpleQueryEscape
1254+ | SimpleQueryWhitespace
1255+ | SimpleQueryFuzzy
1256+ | SimpleQueryNear
1257+ | SimpleQuerySlop deriving (Eq, Read, Show, Generic, Typeable)
1258+
1259+-- use_dis_max and tie_breaker when fields are plural?
1260+data QueryStringQuery =
1261+ QueryStringQuery
1262+ { queryStringQuery :: QueryString
1263+ , queryStringDefaultField :: Maybe FieldName
1264+ , queryStringOperator :: Maybe BooleanOperator
1265+ , queryStringAnalyzer :: Maybe Analyzer
1266+ , queryStringAllowLeadingWildcard :: Maybe AllowLeadingWildcard
1267+ , queryStringLowercaseExpanded :: Maybe LowercaseExpanded
1268+ , queryStringEnablePositionIncrements :: Maybe EnablePositionIncrements
1269+ , queryStringFuzzyMaxExpansions :: Maybe MaxExpansions
1270+ , queryStringFuzziness :: Maybe Fuzziness
1271+ , queryStringFuzzyPrefixLength :: Maybe PrefixLength
1272+ , queryStringPhraseSlop :: Maybe PhraseSlop
1273+ , queryStringBoost :: Maybe Boost
1274+ , queryStringAnalyzeWildcard :: Maybe AnalyzeWildcard
1275+ , queryStringGeneratePhraseQueries :: Maybe GeneratePhraseQueries
1276+ , queryStringMinimumShouldMatch :: Maybe MinimumMatch
1277+ , queryStringLenient :: Maybe Lenient
1278+ , queryStringLocale :: Maybe Locale
1279+ } deriving (Eq, Read, Show, Generic, Typeable)
1280+
1281+mkQueryStringQuery :: QueryString -> QueryStringQuery
1282+mkQueryStringQuery qs =
1283+ QueryStringQuery qs Nothing Nothing
1284+ Nothing Nothing Nothing Nothing
1285+ Nothing Nothing Nothing Nothing
1286+ Nothing Nothing Nothing Nothing
1287+ Nothing Nothing
1288+
1289+data FieldOrFields = FofField FieldName
1290+ | FofFields (NonEmpty FieldName) deriving (Eq, Read, Show, Generic, Typeable)
1291+
1292+data PrefixQuery =
1293+ PrefixQuery
1294+ { prefixQueryField :: FieldName
1295+ , prefixQueryPrefixValue :: Text
1296+ , prefixQueryBoost :: Maybe Boost } deriving (Eq, Read, Show, Generic, Typeable)
1297+
1298+data NestedQuery =
1299+ NestedQuery
1300+ { nestedQueryPath :: QueryPath
1301+ , nestedQueryScoreType :: ScoreType
1302+ , nestedQuery :: Query } deriving (Eq, Read, Show, Generic, Typeable)
1303+
1304+data MoreLikeThisFieldQuery =
1305+ MoreLikeThisFieldQuery
1306+ { moreLikeThisFieldText :: Text
1307+ , moreLikeThisFieldFields :: FieldName
1308+ -- default 0.3 (30%)
1309+ , moreLikeThisFieldPercentMatch :: Maybe PercentMatch
1310+ , moreLikeThisFieldMinimumTermFreq :: Maybe MinimumTermFrequency
1311+ , moreLikeThisFieldMaxQueryTerms :: Maybe MaxQueryTerms
1312+ , moreLikeThisFieldStopWords :: Maybe (NonEmpty StopWord)
1313+ , moreLikeThisFieldMinDocFrequency :: Maybe MinDocFrequency
1314+ , moreLikeThisFieldMaxDocFrequency :: Maybe MaxDocFrequency
1315+ , moreLikeThisFieldMinWordLength :: Maybe MinWordLength
1316+ , moreLikeThisFieldMaxWordLength :: Maybe MaxWordLength
1317+ , moreLikeThisFieldBoostTerms :: Maybe BoostTerms
1318+ , moreLikeThisFieldBoost :: Maybe Boost
1319+ , moreLikeThisFieldAnalyzer :: Maybe Analyzer
1320+ } deriving (Eq, Read, Show, Generic, Typeable)
1321+
1322+data MoreLikeThisQuery =
1323+ MoreLikeThisQuery
1324+ { moreLikeThisText :: Text
1325+ , moreLikeThisFields :: Maybe (NonEmpty FieldName)
1326+ -- default 0.3 (30%)
1327+ , moreLikeThisPercentMatch :: Maybe PercentMatch
1328+ , moreLikeThisMinimumTermFreq :: Maybe MinimumTermFrequency
1329+ , moreLikeThisMaxQueryTerms :: Maybe MaxQueryTerms
1330+ , moreLikeThisStopWords :: Maybe (NonEmpty StopWord)
1331+ , moreLikeThisMinDocFrequency :: Maybe MinDocFrequency
1332+ , moreLikeThisMaxDocFrequency :: Maybe MaxDocFrequency
1333+ , moreLikeThisMinWordLength :: Maybe MinWordLength
1334+ , moreLikeThisMaxWordLength :: Maybe MaxWordLength
1335+ , moreLikeThisBoostTerms :: Maybe BoostTerms
1336+ , moreLikeThisBoost :: Maybe Boost
1337+ , moreLikeThisAnalyzer :: Maybe Analyzer
1338+ } deriving (Eq, Read, Show, Generic, Typeable)
1339+
1340+data IndicesQuery =
1341+ IndicesQuery
1342+ { indicesQueryIndices :: [IndexName]
1343+ , indicesQuery :: Query
1344+ -- default "all"
1345+ , indicesQueryNoMatch :: Maybe Query } deriving (Eq, Read, Show, Generic, Typeable)
1346+
1347+data HasParentQuery =
1348+ HasParentQuery
1349+ { hasParentQueryType :: TypeName
1350+ , hasParentQuery :: Query
1351+ , hasParentQueryScoreType :: Maybe ScoreType } deriving (Eq, Read, Show, Generic, Typeable)
1352+
1353+data HasChildQuery =
1354+ HasChildQuery
1355+ { hasChildQueryType :: TypeName
1356+ , hasChildQuery :: Query
1357+ , hasChildQueryScoreType :: Maybe ScoreType } deriving (Eq, Read, Show, Generic, Typeable)
1358+
1359+data ScoreType =
1360+ ScoreTypeMax
1361+ | ScoreTypeSum
1362+ | ScoreTypeAvg
1363+ | ScoreTypeNone deriving (Eq, Read, Show, Generic, Typeable)
1364+
1365+data FuzzyQuery =
1366+ FuzzyQuery { fuzzyQueryField :: FieldName
1367+ , fuzzyQueryValue :: Text
1368+ , fuzzyQueryPrefixLength :: PrefixLength
1369+ , fuzzyQueryMaxExpansions :: MaxExpansions
1370+ , fuzzyQueryFuzziness :: Fuzziness
1371+ , fuzzyQueryBoost :: Maybe Boost
1372+ } deriving (Eq, Read, Show, Generic, Typeable)
1373+
1374+data FuzzyLikeFieldQuery =
1375+ FuzzyLikeFieldQuery
1376+ { fuzzyLikeField :: FieldName
1377+ -- anaphora is good for the soul.
1378+ , fuzzyLikeFieldText :: Text
1379+ , fuzzyLikeFieldMaxQueryTerms :: MaxQueryTerms
1380+ , fuzzyLikeFieldIgnoreTermFrequency :: IgnoreTermFrequency
1381+ , fuzzyLikeFieldFuzziness :: Fuzziness
1382+ , fuzzyLikeFieldPrefixLength :: PrefixLength
1383+ , fuzzyLikeFieldBoost :: Boost
1384+ , fuzzyLikeFieldAnalyzer :: Maybe Analyzer
1385+ } deriving (Eq, Read, Show, Generic, Typeable)
1386+
1387+data FuzzyLikeThisQuery =
1388+ FuzzyLikeThisQuery
1389+ { fuzzyLikeFields :: [FieldName]
1390+ , fuzzyLikeText :: Text
1391+ , fuzzyLikeMaxQueryTerms :: MaxQueryTerms
1392+ , fuzzyLikeIgnoreTermFrequency :: IgnoreTermFrequency
1393+ , fuzzyLikeFuzziness :: Fuzziness
1394+ , fuzzyLikePrefixLength :: PrefixLength
1395+ , fuzzyLikeBoost :: Boost
1396+ , fuzzyLikeAnalyzer :: Maybe Analyzer
1397+ } deriving (Eq, Read, Show, Generic, Typeable)
1398+
1399+data FilteredQuery =
1400+ FilteredQuery
1401+ { filteredQuery :: Query
1402+ , filteredFilter :: Filter } deriving (Eq, Read, Show, Generic, Typeable)
1403+
1404+data DisMaxQuery =
1405+ DisMaxQuery { disMaxQueries :: [Query]
1406+ -- default 0.0
1407+ , disMaxTiebreaker :: Tiebreaker
1408+ , disMaxBoost :: Maybe Boost
1409+ } deriving (Eq, Read, Show, Generic, Typeable)
1410+
1411+data MatchQuery =
1412+ MatchQuery { matchQueryField :: FieldName
1413+ , matchQueryQueryString :: QueryString
1414+ , matchQueryOperator :: BooleanOperator
1415+ , matchQueryZeroTerms :: ZeroTermsQuery
1416+ , matchQueryCutoffFrequency :: Maybe CutoffFrequency
1417+ , matchQueryMatchType :: Maybe MatchQueryType
1418+ , matchQueryAnalyzer :: Maybe Analyzer
1419+ , matchQueryMaxExpansions :: Maybe MaxExpansions
1420+ , matchQueryLenient :: Maybe Lenient
1421+ , matchQueryBoost :: Maybe Boost } deriving (Eq, Read, Show, Generic, Typeable)
1422+
1423+{-| 'mkMatchQuery' is a convenience function that defaults the less common parameters,
1424+ enabling you to provide only the 'FieldName' and 'QueryString' to make a 'MatchQuery'
1425+-}
1426+mkMatchQuery :: FieldName -> QueryString -> MatchQuery
1427+mkMatchQuery field query = MatchQuery field query Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1428+
1429+data MatchQueryType =
1430+ MatchPhrase
1431+ | MatchPhrasePrefix deriving (Eq, Read, Show, Generic, Typeable)
1432+
1433+data MultiMatchQuery =
1434+ MultiMatchQuery { multiMatchQueryFields :: [FieldName]
1435+ , multiMatchQueryString :: QueryString
1436+ , multiMatchQueryOperator :: BooleanOperator
1437+ , multiMatchQueryZeroTerms :: ZeroTermsQuery
1438+ , multiMatchQueryTiebreaker :: Maybe Tiebreaker
1439+ , multiMatchQueryType :: Maybe MultiMatchQueryType
1440+ , multiMatchQueryCutoffFrequency :: Maybe CutoffFrequency
1441+ , multiMatchQueryAnalyzer :: Maybe Analyzer
1442+ , multiMatchQueryMaxExpansions :: Maybe MaxExpansions
1443+ , multiMatchQueryLenient :: Maybe Lenient } deriving (Eq, Read, Show, Generic, Typeable)
1444+
1445+{-| 'mkMultiMatchQuery' is a convenience function that defaults the less common parameters,
1446+ enabling you to provide only the list of 'FieldName's and 'QueryString' to
1447+ make a 'MultiMatchQuery'.
1448+-}
1449+
1450+mkMultiMatchQuery :: [FieldName] -> QueryString -> MultiMatchQuery
1451+mkMultiMatchQuery matchFields query =
1452+ MultiMatchQuery matchFields query
1453+ Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1454+
1455+data MultiMatchQueryType =
1456+ MultiMatchBestFields
1457+ | MultiMatchMostFields
1458+ | MultiMatchCrossFields
1459+ | MultiMatchPhrase
1460+ | MultiMatchPhrasePrefix deriving (Eq, Read, Show, Generic, Typeable)
1461+
1462+data BoolQuery =
1463+ BoolQuery { boolQueryMustMatch :: [Query]
1464+ , boolQueryMustNotMatch :: [Query]
1465+ , boolQueryShouldMatch :: [Query]
1466+ , boolQueryMinimumShouldMatch :: Maybe MinimumMatch
1467+ , boolQueryBoost :: Maybe Boost
1468+ , boolQueryDisableCoord :: Maybe DisableCoord
1469+ } deriving (Eq, Read, Show, Generic, Typeable)
1470+
1471+mkBoolQuery :: [Query] -> [Query] -> [Query] -> BoolQuery
1472+mkBoolQuery must mustNot should =
1473+ BoolQuery must mustNot should Nothing Nothing Nothing
1474+
1475+data BoostingQuery =
1476+ BoostingQuery { positiveQuery :: Query
1477+ , negativeQuery :: Query
1478+ , negativeBoost :: Boost } deriving (Eq, Read, Show, Generic, Typeable)
1479+
1480+data CommonTermsQuery =
1481+ CommonTermsQuery { commonField :: FieldName
1482+ , commonQuery :: QueryString
1483+ , commonCutoffFrequency :: CutoffFrequency
1484+ , commonLowFreqOperator :: BooleanOperator
1485+ , commonHighFreqOperator :: BooleanOperator
1486+ , commonMinimumShouldMatch :: Maybe CommonMinimumMatch
1487+ , commonBoost :: Maybe Boost
1488+ , commonAnalyzer :: Maybe Analyzer
1489+ , commonDisableCoord :: Maybe DisableCoord
1490+ } deriving (Eq, Read, Show, Generic, Typeable)
1491+
1492+data CommonMinimumMatch =
1493+ CommonMinimumMatchHighLow MinimumMatchHighLow
1494+ | CommonMinimumMatch MinimumMatch
1495+ deriving (Eq, Read, Show, Generic, Typeable)
1496+
1497+data MinimumMatchHighLow =
1498+ MinimumMatchHighLow { lowFreq :: MinimumMatch
1499+ , highFreq :: MinimumMatch } deriving (Eq, Read, Show, Generic, Typeable)
1500+
1501+data Filter = AndFilter [Filter] Cache
1502+ | OrFilter [Filter] Cache
1503+ | NotFilter Filter Cache
1504+ | IdentityFilter
1505+ | BoolFilter BoolMatch
1506+ | ExistsFilter FieldName -- always cached
1507+ | GeoBoundingBoxFilter GeoBoundingBoxConstraint
1508+ | GeoDistanceFilter GeoPoint Distance DistanceType OptimizeBbox Cache
1509+ | GeoDistanceRangeFilter GeoPoint DistanceRange
1510+ | GeoPolygonFilter FieldName [LatLon]
1511+ | IdsFilter MappingName [DocId]
1512+ | LimitFilter Int
1513+ | MissingFilter FieldName Existence NullValue
1514+ | PrefixFilter FieldName PrefixValue Cache
1515+ | QueryFilter Query Cache
1516+ | RangeFilter FieldName RangeValue RangeExecution Cache
1517+ | RegexpFilter FieldName Regexp RegexpFlags CacheName Cache CacheKey
1518+ | TermFilter Term Cache
1519+ deriving (Eq, Read, Show, Generic, Typeable)
1520+
1521+data ZeroTermsQuery = ZeroTermsNone
1522+ | ZeroTermsAll deriving (Eq, Read, Show, Generic, Typeable)
1523+
1524+data RangeExecution = RangeExecutionIndex
1525+ | RangeExecutionFielddata deriving (Eq, Read, Show, Generic, Typeable)
1526+
1527+newtype Regexp = Regexp Text deriving (Eq, Read, Show, Generic, Typeable, FromJSON)
1528+
1529+data RegexpFlags = AllRegexpFlags
1530+ | NoRegexpFlags
1531+ | SomeRegexpFlags (NonEmpty RegexpFlag) deriving (Eq, Read, Show, Generic, Typeable)
1532+
1533+data RegexpFlag = AnyString
1534+ | Automaton
1535+ | Complement
1536+ | Empty
1537+ | Intersection
1538+ | Interval deriving (Eq, Read, Show, Generic, Typeable)
1539+
1540+newtype LessThan = LessThan Double deriving (Eq, Read, Show, Generic, Typeable)
1541+newtype LessThanEq = LessThanEq Double deriving (Eq, Read, Show, Generic, Typeable)
1542+newtype GreaterThan = GreaterThan Double deriving (Eq, Read, Show, Generic, Typeable)
1543+newtype GreaterThanEq = GreaterThanEq Double deriving (Eq, Read, Show, Generic, Typeable)
1544+
1545+newtype LessThanD = LessThanD UTCTime deriving (Eq, Read, Show, Generic, Typeable)
1546+newtype LessThanEqD = LessThanEqD UTCTime deriving (Eq, Read, Show, Generic, Typeable)
1547+newtype GreaterThanD = GreaterThanD UTCTime deriving (Eq, Read, Show, Generic, Typeable)
1548+newtype GreaterThanEqD = GreaterThanEqD UTCTime deriving (Eq, Read, Show, Generic, Typeable)
1549+
1550+data RangeValue = RangeDateLte LessThanEqD
1551+ | RangeDateLt LessThanD
1552+ | RangeDateGte GreaterThanEqD
1553+ | RangeDateGt GreaterThanD
1554+ | RangeDateGtLt GreaterThanD LessThanD
1555+ | RangeDateGteLte GreaterThanEqD LessThanEqD
1556+ | RangeDateGteLt GreaterThanEqD LessThanD
1557+ | RangeDateGtLte GreaterThanD LessThanEqD
1558+ | RangeDoubleLte LessThanEq
1559+ | RangeDoubleLt LessThan
1560+ | RangeDoubleGte GreaterThanEq
1561+ | RangeDoubleGt GreaterThan
1562+ | RangeDoubleGtLt GreaterThan LessThan
1563+ | RangeDoubleGteLte GreaterThanEq LessThanEq
1564+ | RangeDoubleGteLt GreaterThanEq LessThan
1565+ | RangeDoubleGtLte GreaterThan LessThanEq
1566+ deriving (Eq, Read, Show, Generic, Typeable)
1567+
1568+rangeValueToPair :: RangeValue -> [Pair]
1569+rangeValueToPair rv = case rv of
1570+ RangeDateLte (LessThanEqD t) -> ["lte" .= t]
1571+ RangeDateGte (GreaterThanEqD t) -> ["gte" .= t]
1572+ RangeDateLt (LessThanD t) -> ["lt" .= t]
1573+ RangeDateGt (GreaterThanD t) -> ["gt" .= t]
1574+ RangeDateGteLte (GreaterThanEqD l) (LessThanEqD g) -> ["gte" .= l, "lte" .= g]
1575+ RangeDateGtLte (GreaterThanD l) (LessThanEqD g) -> ["gt" .= l, "lte" .= g]
1576+ RangeDateGteLt (GreaterThanEqD l) (LessThanD g) -> ["gte" .= l, "lt" .= g]
1577+ RangeDateGtLt (GreaterThanD l) (LessThanD g) -> ["gt" .= l, "lt" .= g]
1578+ RangeDoubleLte (LessThanEq t) -> ["lte" .= t]
1579+ RangeDoubleGte (GreaterThanEq t) -> ["gte" .= t]
1580+ RangeDoubleLt (LessThan t) -> ["lt" .= t]
1581+ RangeDoubleGt (GreaterThan t) -> ["gt" .= t]
1582+ RangeDoubleGteLte (GreaterThanEq l) (LessThanEq g) -> ["gte" .= l, "lte" .= g]
1583+ RangeDoubleGtLte (GreaterThan l) (LessThanEq g) -> ["gt" .= l, "lte" .= g]
1584+ RangeDoubleGteLt (GreaterThanEq l) (LessThan g) -> ["gte" .= l, "lt" .= g]
1585+ RangeDoubleGtLt (GreaterThan l) (LessThan g) -> ["gt" .= l, "lt" .= g]
1586+
1587+data Term = Term { termField :: Text
1588+ , termValue :: Text } deriving (Eq, Read, Show, Generic, Typeable)
1589+
1590+data BoolMatch = MustMatch Term Cache
1591+ | MustNotMatch Term Cache
1592+ | ShouldMatch [Term] Cache deriving (Eq, Read, Show, Generic, Typeable)
1593+
1594+-- "memory" or "indexed"
1595+data GeoFilterType = GeoFilterMemory
1596+ | GeoFilterIndexed deriving (Eq, Read, Show, Generic, Typeable)
1597+
1598+data LatLon = LatLon { lat :: Double
1599+ , lon :: Double } deriving (Eq, Read, Show, Generic, Typeable)
1600+
1601+data GeoBoundingBox =
1602+ GeoBoundingBox { topLeft :: LatLon
1603+ , bottomRight :: LatLon } deriving (Eq, Read, Show, Generic, Typeable)
1604+
1605+data GeoBoundingBoxConstraint =
1606+ GeoBoundingBoxConstraint { geoBBField :: FieldName
1607+ , constraintBox :: GeoBoundingBox
1608+ , bbConstraintcache :: Cache
1609+ , geoType :: GeoFilterType
1610+ } deriving (Eq, Read, Show, Generic, Typeable)
1611+
1612+data GeoPoint =
1613+ GeoPoint { geoField :: FieldName
1614+ , latLon :: LatLon} deriving (Eq, Read, Show, Generic, Typeable)
1615+
1616+data DistanceUnit = Miles
1617+ | Yards
1618+ | Feet
1619+ | Inches
1620+ | Kilometers
1621+ | Meters
1622+ | Centimeters
1623+ | Millimeters
1624+ | NauticalMiles deriving (Eq, Read, Show, Generic, Typeable)
1625+
1626+data DistanceType = Arc
1627+ | SloppyArc -- doesn't exist <1.0
1628+ | Plane deriving (Eq, Read, Show, Generic, Typeable)
1629+
1630+data OptimizeBbox = OptimizeGeoFilterType GeoFilterType
1631+ | NoOptimizeBbox deriving (Eq, Read, Show, Generic, Typeable)
1632+
1633+data Distance =
1634+ Distance { coefficient :: Double
1635+ , unit :: DistanceUnit } deriving (Eq, Read, Show, Generic, Typeable)
1636+
1637+data DistanceRange =
1638+ DistanceRange { distanceFrom :: Distance
1639+ , distanceTo :: Distance } deriving (Eq, Read, Show, Generic, Typeable)
1640+
1641+type TemplateQueryKey = Text
1642+type TemplateQueryValue = Text
1643+
1644+newtype TemplateQueryKeyValuePairs = TemplateQueryKeyValuePairs (HM.HashMap TemplateQueryKey TemplateQueryValue)
1645+ deriving (Eq, Read, Show, Generic)
1646+
1647+instance ToJSON TemplateQueryKeyValuePairs where
1648+ toJSON (TemplateQueryKeyValuePairs x) = Object $ HM.map toJSON x
1649+
1650+instance FromJSON TemplateQueryKeyValuePairs where
1651+ parseJSON (Object o) = pure . TemplateQueryKeyValuePairs $ HM.mapMaybe getValue o
1652+ where getValue (String x) = Just x
1653+ getValue _ = Nothing
1654+ parseJSON _ = fail "error parsing TemplateQueryKeyValuePairs"
1655+
1656+data TemplateQueryInline =
1657+ TemplateQueryInline { inline :: Query
1658+ , params :: TemplateQueryKeyValuePairs
1659+ }
1660+ deriving (Eq, Read, Show, Generic, Typeable)
1661+
1662+instance ToJSON TemplateQueryInline where
1663+ toJSON TemplateQueryInline{..} = object [ "query" .= inline
1664+ , "params" .= params
1665+ ]
1666+
1667+instance FromJSON TemplateQueryInline where
1668+ parseJSON = withObject "TemplateQueryInline" parse
1669+ where parse o = TemplateQueryInline
1670+ <$> o .: "query"
1671+ <*> o .: "params"
1672+
1673+data SearchResult a =
1674+ SearchResult { took :: Int
1675+ , timedOut :: Bool
1676+ , shards :: ShardResult
1677+ , searchHits :: SearchHits a
1678+ , aggregations :: Maybe AggregationResults
1679+ , scrollId :: Maybe ScrollId } deriving (Eq, Read, Show, Generic, Typeable)
1680+
1681+newtype ScrollId = ScrollId Text deriving (Eq, Read, Show, Generic, Ord, ToJSON, FromJSON)
1682+
1683+type Score = Maybe Double
1684+
1685+data SearchHits a =
1686+ SearchHits { hitsTotal :: Int
1687+ , maxScore :: Score
1688+ , hits :: [Hit a] } deriving (Eq, Read, Show, Generic, Typeable)
1689+
1690+instance Semigroup (SearchHits a) where
1691+ (SearchHits ta ma ha) <> (SearchHits tb mb hb) = SearchHits (ta + tb) (max ma mb) (ha <> hb)
1692+
1693+instance Monoid (SearchHits a) where
1694+ mempty = SearchHits 0 Nothing mempty
1695+ mappend = (<>)
1696+
1697+data Hit a =
1698+ Hit { hitIndex :: IndexName
1699+ , hitType :: MappingName
1700+ , hitDocId :: DocId
1701+ , hitScore :: Score
1702+ , hitSource :: Maybe a
1703+ , hitHighlight :: Maybe HitHighlight } deriving (Eq, Read, Show, Generic, Typeable)
1704+
1705+data ShardResult =
1706+ ShardResult { shardTotal :: Int
1707+ , shardsSuccessful :: Int
1708+ , shardsFailed :: Int } deriving (Eq, Read, Show, Generic, Typeable)
1709+
1710+type HitHighlight = M.Map Text [Text]
1711+
1712+showText :: Show a => a -> Text
1713+showText = T.pack . show
1714+
1715+readMay :: Read a => String -> Maybe a
1716+readMay s = case reads s of
1717+ (a, ""):_ -> Just a
1718+ _ -> Nothing
1719+
1720+parseReadText :: Read a => Text -> Parser a
1721+parseReadText = maybe mzero return . readMay . T.unpack
1722+
1723+type Aggregations = M.Map Text Aggregation
1724+
1725+emptyAggregations :: Aggregations
1726+emptyAggregations = M.empty
1727+
1728+mkAggregations :: Text -> Aggregation -> Aggregations
1729+mkAggregations name aggregation = M.insert name aggregation emptyAggregations
1730+
1731+data TermOrder = TermOrder{ termSortField :: Text
1732+ , termSortOrder :: SortOrder } deriving (Eq, Read, Show, Generic, Typeable)
1733+
1734+data TermInclusion = TermInclusion Text
1735+ | TermPattern Text Text deriving (Eq, Read, Show, Generic, Typeable)
1736+
1737+data CollectionMode = BreadthFirst
1738+ | DepthFirst deriving (Eq, Read, Show, Generic, Typeable)
1739+
1740+data ExecutionHint = Ordinals
1741+ | GlobalOrdinals
1742+ | GlobalOrdinalsHash
1743+ | GlobalOrdinalsLowCardinality
1744+ | Map deriving (Eq, Read, Show, Generic, Typeable)
1745+
1746+data TimeInterval = Weeks
1747+ | Days
1748+ | Hours
1749+ | Minutes
1750+ | Seconds deriving Eq
1751+
1752+data Interval = Year
1753+ | Quarter
1754+ | Month
1755+ | Week
1756+ | Day
1757+ | Hour
1758+ | Minute
1759+ | Second
1760+ | FractionalInterval Float TimeInterval deriving (Eq, Read, Show, Generic, Typeable)
1761+
1762+data Aggregation = TermsAgg TermsAggregation
1763+ | CardinalityAgg CardinalityAggregation
1764+ | DateHistogramAgg DateHistogramAggregation
1765+ | ValueCountAgg ValueCountAggregation
1766+ | FilterAgg FilterAggregation
1767+ | DateRangeAgg DateRangeAggregation
1768+ | MissingAgg MissingAggregation
1769+ | TopHitsAgg TopHitsAggregation
1770+ deriving (Eq, Read, Show, Generic, Typeable)
1771+
1772+data TopHitsAggregation = TopHitsAggregation
1773+ { taFrom :: Maybe From
1774+ , taSize :: Maybe Size
1775+ , taSort :: Maybe Sort
1776+ } deriving (Eq, Read, Show)
1777+
1778+data MissingAggregation = MissingAggregation
1779+ { maField :: Text
1780+ } deriving (Eq, Read, Show, Generic, Typeable)
1781+
1782+data TermsAggregation = TermsAggregation { term :: Either Text Text
1783+ , termInclude :: Maybe TermInclusion
1784+ , termExclude :: Maybe TermInclusion
1785+ , termOrder :: Maybe TermOrder
1786+ , termMinDocCount :: Maybe Int
1787+ , termSize :: Maybe Int
1788+ , termShardSize :: Maybe Int
1789+ , termCollectMode :: Maybe CollectionMode
1790+ , termExecutionHint :: Maybe ExecutionHint
1791+ , termAggs :: Maybe Aggregations
1792+ } deriving (Eq, Read, Show, Generic, Typeable)
1793+
1794+data CardinalityAggregation = CardinalityAggregation { cardinalityField :: FieldName,
1795+ precisionThreshold :: Maybe Int
1796+ } deriving (Eq, Read, Show, Generic, Typeable)
1797+
1798+data DateHistogramAggregation = DateHistogramAggregation { dateField :: FieldName
1799+ , dateInterval :: Interval
1800+ , dateFormat :: Maybe Text
1801+ -- pre and post deprecated in 1.5
1802+ , datePreZone :: Maybe Text
1803+ , datePostZone :: Maybe Text
1804+ , datePreOffset :: Maybe Text
1805+ , datePostOffset :: Maybe Text
1806+ , dateAggs :: Maybe Aggregations
1807+ } deriving (Eq, Read, Show, Generic, Typeable)
1808+
1809+
1810+data DateRangeAggregation = DateRangeAggregation { draField :: FieldName
1811+ , draFormat :: Maybe Text
1812+ , draRanges :: NonEmpty DateRangeAggRange
1813+ } deriving (Eq, Read, Show, Generic, Typeable)
1814+
1815+data DateRangeAggRange = DateRangeFrom DateMathExpr
1816+ | DateRangeTo DateMathExpr
1817+ | DateRangeFromAndTo DateMathExpr DateMathExpr deriving (Eq, Read, Show, Generic, Typeable)
1818+
1819+-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math> for more information.
1820+data DateMathExpr = DateMathExpr DateMathAnchor [DateMathModifier] deriving (Eq, Read, Show, Generic, Typeable)
1821+
1822+
1823+-- | Starting point for a date range. This along with the 'DateMathModifiers' gets you the date ES will start from.
1824+data DateMathAnchor = DMNow
1825+ | DMDate Day deriving (Eq, Read, Show, Generic, Typeable)
1826+
1827+data DateMathModifier = AddTime Int DateMathUnit
1828+ | SubtractTime Int DateMathUnit
1829+ | RoundDownTo DateMathUnit deriving (Eq, Read, Show, Generic, Typeable)
1830+
1831+data DateMathUnit = DMYear
1832+ | DMMonth
1833+ | DMWeek
1834+ | DMDay
1835+ | DMHour
1836+ | DMMinute
1837+ | DMSecond deriving (Eq, Read, Show, Generic, Typeable)
1838+
1839+-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html> for more information.
1840+data ValueCountAggregation = FieldValueCount FieldName
1841+ | ScriptValueCount Script deriving (Eq, Read, Show, Generic, Typeable)
1842+
1843+-- | Single-bucket filter aggregations. See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html#search-aggregations-bucket-filter-aggregation> for more information.
1844+data FilterAggregation = FilterAggregation { faFilter :: Filter
1845+ , faAggs :: Maybe Aggregations} deriving (Eq, Read, Show, Generic, Typeable)
1846+
1847+mkTermsAggregation :: Text -> TermsAggregation
1848+mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1849+
1850+mkTermsScriptAggregation :: Text -> TermsAggregation
1851+mkTermsScriptAggregation t = TermsAggregation (Right t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1852+
1853+mkDateHistogram :: FieldName -> Interval -> DateHistogramAggregation
1854+mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothing Nothing Nothing
1855+
1856+mkCardinalityAggregation :: FieldName -> CardinalityAggregation
1857+mkCardinalityAggregation t = CardinalityAggregation t Nothing
1858+
1859+instance ToJSON Version where
1860+ toJSON Version {..} = object ["number" .= number
1861+ ,"build_hash" .= build_hash
1862+ ,"build_timestamp" .= build_timestamp
1863+ ,"build_snapshot" .= build_snapshot
1864+ ,"lucene_version" .= lucene_version]
1865+
1866+instance FromJSON Version where
1867+ parseJSON = withObject "Version" parse
1868+ where parse o = Version
1869+ <$> o .: "number"
1870+ <*> o .: "build_hash"
1871+ <*> o .: "build_timestamp"
1872+ <*> o .: "build_snapshot"
1873+ <*> o .: "lucene_version"
1874+
1875+instance ToJSON VersionNumber where
1876+ toJSON = toJSON . Vers.showVersion . versionNumber
1877+
1878+instance FromJSON VersionNumber where
1879+ parseJSON = withText "VersionNumber" (parse . T.unpack)
1880+ where
1881+ parse s = case filter (null . snd)(RP.readP_to_S Vers.parseVersion s) of
1882+ [(v, _)] -> pure (VersionNumber v)
1883+ [] -> fail ("Invalid version string " ++ s)
1884+ xs -> fail ("Ambiguous version string " ++ s ++ " (" ++ intercalate ", " (Vers.showVersion . fst <$> xs) ++ ")")
1885+
1886+instance ToJSON TermOrder where
1887+ toJSON (TermOrder termSortField termSortOrder) = object [termSortField .= termSortOrder]
1888+
1889+instance ToJSON TermInclusion where
1890+ toJSON (TermInclusion x) = toJSON x
1891+ toJSON (TermPattern pattern flags) = omitNulls [ "pattern" .= pattern,
1892+ "flags" .= flags]
1893+
1894+instance ToJSON CollectionMode where
1895+ toJSON BreadthFirst = "breadth_first"
1896+ toJSON DepthFirst = "depth_first"
1897+
1898+instance ToJSON ExecutionHint where
1899+ toJSON Ordinals = "ordinals"
1900+ toJSON GlobalOrdinals = "global_ordinals"
1901+ toJSON GlobalOrdinalsHash = "global_ordinals_hash"
1902+ toJSON GlobalOrdinalsLowCardinality = "global_ordinals_low_cardinality"
1903+ toJSON Map = "map"
1904+
1905+instance ToJSON Interval where
1906+ toJSON Year = "year"
1907+ toJSON Quarter = "quarter"
1908+ toJSON Month = "month"
1909+ toJSON Week = "week"
1910+ toJSON Day = "day"
1911+ toJSON Hour = "hour"
1912+ toJSON Minute = "minute"
1913+ toJSON Second = "second"
1914+ toJSON (FractionalInterval fraction interval) = toJSON $ show fraction ++ show interval
1915+
1916+instance Show TimeInterval where
1917+ show Weeks = "w"
1918+ show Days = "d"
1919+ show Hours = "h"
1920+ show Minutes = "m"
1921+ show Seconds = "s"
1922+
1923+instance Read TimeInterval where
1924+ readPrec = f =<< TR.get
1925+ where
1926+ f 'w' = return Weeks
1927+ f 'd' = return Days
1928+ f 'h' = return Hours
1929+ f 'm' = return Minutes
1930+ f 's' = return Seconds
1931+ f _ = fail "TimeInterval expected one of w, d, h, m, s"
1932+
1933+instance ToJSON Aggregation where
1934+ toJSON (TermsAgg (TermsAggregation term include exclude order minDocCount size shardSize collectMode executionHint termAggs)) =
1935+ omitNulls ["terms" .= omitNulls [ toJSON' term,
1936+ "include" .= include,
1937+ "exclude" .= exclude,
1938+ "order" .= order,
1939+ "min_doc_count" .= minDocCount,
1940+ "size" .= size,
1941+ "shard_size" .= shardSize,
1942+ "collect_mode" .= collectMode,
1943+ "execution_hint" .= executionHint
1944+ ],
1945+ "aggs" .= termAggs ]
1946+ where
1947+ toJSON' x = case x of { Left y -> "field" .= y; Right y -> "script" .= y }
1948+
1949+ toJSON (CardinalityAgg (CardinalityAggregation field precisionThreshold)) =
1950+ object ["cardinality" .= omitNulls [ "field" .= field,
1951+ "precisionThreshold" .= precisionThreshold
1952+ ]
1953+ ]
1954+
1955+ toJSON (DateHistogramAgg (DateHistogramAggregation field interval format preZone postZone preOffset postOffset dateHistoAggs)) =
1956+ omitNulls ["date_histogram" .= omitNulls [ "field" .= field,
1957+ "interval" .= interval,
1958+ "format" .= format,
1959+ "pre_zone" .= preZone,
1960+ "post_zone" .= postZone,
1961+ "pre_offset" .= preOffset,
1962+ "post_offset" .= postOffset
1963+ ],
1964+ "aggs" .= dateHistoAggs ]
1965+ toJSON (ValueCountAgg a) = object ["value_count" .= v]
1966+ where v = case a of
1967+ (FieldValueCount (FieldName n)) -> object ["field" .= n]
1968+ (ScriptValueCount (Script s)) -> object ["script" .= s]
1969+ toJSON (FilterAgg (FilterAggregation filt ags)) =
1970+ omitNulls [ "filter" .= filt
1971+ , "aggs" .= ags]
1972+ toJSON (DateRangeAgg a) = object [ "date_range" .= a
1973+ ]
1974+ toJSON (MissingAgg (MissingAggregation{..})) =
1975+ object ["missing" .= object ["field" .= maField]]
1976+
1977+ toJSON (TopHitsAgg (TopHitsAggregation mfrom msize msort)) =
1978+ omitNulls ["top_hits" .= omitNulls [ "size" .= msize
1979+ , "from" .= mfrom
1980+ , "sort" .= msort
1981+ ]
1982+ ]
1983+
1984+instance ToJSON DateRangeAggregation where
1985+ toJSON DateRangeAggregation {..} =
1986+ omitNulls [ "field" .= draField
1987+ , "format" .= draFormat
1988+ , "ranges" .= toList draRanges
1989+ ]
1990+
1991+instance ToJSON DateRangeAggRange where
1992+ toJSON (DateRangeFrom e) = object [ "from" .= e ]
1993+ toJSON (DateRangeTo e) = object [ "to" .= e ]
1994+ toJSON (DateRangeFromAndTo f t) = object [ "from" .= f, "to" .= t ]
1995+
1996+instance ToJSON DateMathExpr where
1997+ toJSON (DateMathExpr a mods) = String (fmtA a <> mconcat (fmtMod <$> mods))
1998+ where fmtA DMNow = "now"
1999+ fmtA (DMDate date) = (T.pack $ showGregorian date) <> "||"
2000+ fmtMod (AddTime n u) = "+" <> showText n <> fmtU u
2001+ fmtMod (SubtractTime n u) = "-" <> showText n <> fmtU u
2002+ fmtMod (RoundDownTo u) = "/" <> fmtU u
2003+ fmtU DMYear = "y"
2004+ fmtU DMMonth = "M"
2005+ fmtU DMWeek = "w"
2006+ fmtU DMDay = "d"
2007+ fmtU DMHour = "h"
2008+ fmtU DMMinute = "m"
2009+ fmtU DMSecond = "s"
2010+
2011+
2012+type AggregationResults = M.Map Text Value
2013+
2014+class BucketAggregation a where
2015+ key :: a -> BucketValue
2016+ docCount :: a -> Int
2017+ aggs :: a -> Maybe AggregationResults
2018+
2019+
2020+data Bucket a = Bucket { buckets :: [a]} deriving (Read, Show)
2021+
2022+data BucketValue = TextValue Text
2023+ | ScientificValue Scientific
2024+ | BoolValue Bool deriving (Read, Show)
2025+
2026+data MissingResult = MissingResult { missingDocCount :: Int } deriving (Show)
2027+
2028+data TopHitResult a = TopHitResult { tarHits :: (SearchHits a)
2029+ } deriving Show
2030+
2031+data TermsResult = TermsResult { termKey :: BucketValue
2032+ , termsDocCount :: Int
2033+ , termsAggs :: Maybe AggregationResults } deriving (Read, Show)
2034+
2035+data DateHistogramResult = DateHistogramResult { dateKey :: Int
2036+ , dateKeyStr :: Maybe Text
2037+ , dateDocCount :: Int
2038+ , dateHistogramAggs :: Maybe AggregationResults } deriving (Read, Show)
2039+
2040+data DateRangeResult = DateRangeResult { dateRangeKey :: Text
2041+ , dateRangeFrom :: Maybe UTCTime
2042+ , dateRangeFromAsString :: Maybe Text
2043+ , dateRangeTo :: Maybe UTCTime
2044+ , dateRangeToAsString :: Maybe Text
2045+ , dateRangeDocCount :: Int
2046+ , dateRangeAggs :: Maybe AggregationResults } deriving (Read, Show, Eq, Generic, Typeable)
2047+
2048+toTerms :: Text -> AggregationResults -> Maybe (Bucket TermsResult)
2049+toTerms = toAggResult
2050+
2051+toDateHistogram :: Text -> AggregationResults -> Maybe (Bucket DateHistogramResult)
2052+toDateHistogram = toAggResult
2053+
2054+toMissing :: Text -> AggregationResults -> Maybe MissingResult
2055+toMissing = toAggResult
2056+
2057+toTopHits :: (FromJSON a) => Text -> AggregationResults -> Maybe (TopHitResult a)
2058+toTopHits = toAggResult
2059+
2060+toAggResult :: (FromJSON a) => Text -> AggregationResults -> Maybe a
2061+toAggResult t a = M.lookup t a >>= deserialize
2062+ where deserialize = parseMaybe parseJSON
2063+
2064+instance BucketAggregation TermsResult where
2065+ key = termKey
2066+ docCount = termsDocCount
2067+ aggs = termsAggs
2068+
2069+instance BucketAggregation DateHistogramResult where
2070+ key = TextValue . showText . dateKey
2071+ docCount = dateDocCount
2072+ aggs = dateHistogramAggs
2073+
2074+instance BucketAggregation DateRangeResult where
2075+ key = TextValue . dateRangeKey
2076+ docCount = dateRangeDocCount
2077+ aggs = dateRangeAggs
2078+
2079+instance (FromJSON a) => FromJSON (Bucket a) where
2080+ parseJSON (Object v) = Bucket <$>
2081+ v .: "buckets"
2082+ parseJSON _ = mempty
2083+
2084+instance FromJSON BucketValue where
2085+ parseJSON (String t) = return $ TextValue t
2086+ parseJSON (Number s) = return $ ScientificValue s
2087+ parseJSON (Bool b) = return $ BoolValue b
2088+ parseJSON _ = mempty
2089+
2090+instance FromJSON MissingResult where
2091+ parseJSON = withObject "MissingResult" parse
2092+ where parse v = MissingResult <$> v .: "doc_count"
2093+
2094+instance FromJSON TermsResult where
2095+ parseJSON (Object v) = TermsResult <$>
2096+ v .: "key" <*>
2097+ v .: "doc_count" <*>
2098+ (pure $ getNamedSubAgg v ["key", "doc_count"])
2099+ parseJSON _ = mempty
2100+
2101+instance FromJSON DateHistogramResult where
2102+ parseJSON (Object v) = DateHistogramResult <$>
2103+ v .: "key" <*>
2104+ v .:? "key_as_string" <*>
2105+ v .: "doc_count" <*>
2106+ (pure $ getNamedSubAgg v [ "key"
2107+ , "doc_count"
2108+ , "key_as_string"
2109+ ]
2110+ )
2111+ parseJSON _ = mempty
2112+
2113+instance FromJSON DateRangeResult where
2114+ parseJSON = withObject "DateRangeResult" parse
2115+ where parse v = DateRangeResult <$>
2116+ v .: "key" <*>
2117+ (fmap posixMS <$> v .:? "from") <*>
2118+ v .:? "from_as_string" <*>
2119+ (fmap posixMS <$> v .:? "to") <*>
2120+ v .:? "to_as_string" <*>
2121+ v .: "doc_count" <*>
2122+ (pure $ getNamedSubAgg v [ "key"
2123+ , "from"
2124+ , "from_as_string"
2125+ , "to"
2126+ , "to_as_string"
2127+ , "doc_count"
2128+ ]
2129+ )
2130+
2131+instance FromJSON POSIXMS where
2132+ parseJSON = withScientific "POSIXMS" (return . parse)
2133+ where parse n = let n' = truncate n :: Integer
2134+ in POSIXMS (posixSecondsToUTCTime (fromInteger (n' `div` 1000)))
2135+
2136+instance (FromJSON a) => FromJSON (TopHitResult a) where
2137+ parseJSON (Object v) = TopHitResult <$>
2138+ v .: "hits"
2139+ parseJSON _ = fail "Failure in FromJSON (TopHitResult a)"
2140+
2141+instance Semigroup Filter where
2142+ a <> b = AndFilter [a, b] defaultCache
2143+
2144+instance Monoid Filter where
2145+ mempty = IdentityFilter
2146+ mappend = (<>)
2147+
2148+instance Seminearring Filter where
2149+ a <||> b = OrFilter [a, b] defaultCache
2150+
2151+instance ToJSON Filter where
2152+ toJSON (AndFilter filters cache) =
2153+ object ["and" .=
2154+ object [ "filters" .= fmap toJSON filters
2155+ , "_cache" .= cache]]
2156+
2157+ toJSON (OrFilter filters cache) =
2158+ object ["or" .=
2159+ object [ "filters" .= fmap toJSON filters
2160+ , "_cache" .= cache]]
2161+
2162+ toJSON (NotFilter notFilter cache) =
2163+ object ["not" .=
2164+ object ["filter" .= notFilter
2165+ , "_cache" .= cache]]
2166+
2167+ toJSON (IdentityFilter) =
2168+ object ["match_all" .= object []]
2169+
2170+ toJSON (TermFilter (Term termFilterField termFilterValue) cache) =
2171+ object ["term" .= object base]
2172+ where base = [termFilterField .= termFilterValue,
2173+ "_cache" .= cache]
2174+
2175+ toJSON (ExistsFilter (FieldName fieldName)) =
2176+ object ["exists" .= object
2177+ ["field" .= fieldName]]
2178+
2179+ toJSON (BoolFilter boolMatch) =
2180+ object ["bool" .= boolMatch]
2181+
2182+ toJSON (GeoBoundingBoxFilter bbConstraint) =
2183+ object ["geo_bounding_box" .= bbConstraint]
2184+
2185+ toJSON (GeoDistanceFilter (GeoPoint (FieldName distanceGeoField) geoDistLatLon)
2186+ distance distanceType optimizeBbox cache) =
2187+ object ["geo_distance" .=
2188+ object ["distance" .= distance
2189+ , "distance_type" .= distanceType
2190+ , "optimize_bbox" .= optimizeBbox
2191+ , distanceGeoField .= geoDistLatLon
2192+ , "_cache" .= cache]]
2193+
2194+ toJSON (GeoDistanceRangeFilter (GeoPoint (FieldName gddrField) drLatLon)
2195+ (DistanceRange geoDistRangeDistFrom drDistanceTo)) =
2196+ object ["geo_distance_range" .=
2197+ object ["from" .= geoDistRangeDistFrom
2198+ , "to" .= drDistanceTo
2199+ , gddrField .= drLatLon]]
2200+
2201+ toJSON (GeoPolygonFilter (FieldName geoPolygonFilterField) latLons) =
2202+ object ["geo_polygon" .=
2203+ object [geoPolygonFilterField .=
2204+ object ["points" .= fmap toJSON latLons]]]
2205+
2206+ toJSON (IdsFilter (MappingName mappingName) values) =
2207+ object ["ids" .=
2208+ object ["type" .= mappingName
2209+ , "values" .= fmap unpackId values]]
2210+
2211+ toJSON (LimitFilter limit) =
2212+ object ["limit" .= object ["value" .= limit]]
2213+
2214+ toJSON (MissingFilter (FieldName fieldName) (Existence existence) (NullValue nullValue)) =
2215+ object ["missing" .=
2216+ object ["field" .= fieldName
2217+ , "existence" .= existence
2218+ , "null_value" .= nullValue]]
2219+
2220+ toJSON (PrefixFilter (FieldName fieldName) fieldValue cache) =
2221+ object ["prefix" .=
2222+ object [fieldName .= fieldValue
2223+ , "_cache" .= cache]]
2224+
2225+ toJSON (QueryFilter query False) =
2226+ object ["query" .= toJSON query ]
2227+ toJSON (QueryFilter query True) =
2228+ object ["fquery" .=
2229+ object [ "query" .= toJSON query
2230+ , "_cache" .= True ]]
2231+
2232+ toJSON (RangeFilter (FieldName fieldName) rangeValue rangeExecution cache) =
2233+ object ["range" .=
2234+ object [ fieldName .= object (rangeValueToPair rangeValue)
2235+ , "execution" .= rangeExecution
2236+ , "_cache" .= cache]]
2237+
2238+ toJSON (RegexpFilter (FieldName fieldName)
2239+ (Regexp regexText) flags (CacheName cacheName) cache (CacheKey cacheKey)) =
2240+ object ["regexp" .=
2241+ object [fieldName .=
2242+ object ["value" .= regexText
2243+ , "flags" .= flags]
2244+ , "_name" .= cacheName
2245+ , "_cache" .= cache
2246+ , "_cache_key" .= cacheKey]]
2247+
2248+instance FromJSON Filter where
2249+ parseJSON = withObject "Filter" parse
2250+ where parse o = andFilter `taggedWith` "and"
2251+ <|> orFilter `taggedWith` "or"
2252+ <|> notFilter `taggedWith` "not"
2253+ <|> identityFilter `taggedWith` "match_all"
2254+ <|> boolFilter `taggedWith` "bool"
2255+ <|> existsFilter `taggedWith` "exists"
2256+ <|> geoBoundingBoxFilter `taggedWith` "geo_bounding_box"
2257+ <|> geoDistanceFilter `taggedWith` "geo_distance"
2258+ <|> geoDistanceRangeFilter `taggedWith` "geo_distance_range"
2259+ <|> geoPolygonFilter `taggedWith` "geo_polygon"
2260+ <|> idsFilter `taggedWith` "ids"
2261+ <|> limitFilter `taggedWith` "limit"
2262+ <|> missingFilter `taggedWith` "missing"
2263+ <|> prefixFilter `taggedWith` "prefix"
2264+ <|> queryFilter `taggedWith` "query"
2265+ <|> fqueryFilter `taggedWith` "fquery"
2266+ <|> rangeFilter `taggedWith` "range"
2267+ <|> regexpFilter `taggedWith` "regexp"
2268+ <|> termFilter `taggedWith` "term"
2269+ where taggedWith parser k = parser =<< o .: k
2270+ andFilter o = AndFilter <$> o .: "filters"
2271+ <*> o .:? "_cache" .!= defaultCache
2272+ orFilter o = OrFilter <$> o .: "filters"
2273+ <*> o .:? "_cache" .!= defaultCache
2274+ notFilter o = NotFilter <$> o .: "filter"
2275+ <*> o .: "_cache" .!= defaultCache
2276+ identityFilter :: Object -> Parser Filter
2277+ identityFilter m
2278+ | HM.null m = pure IdentityFilter
2279+ | otherwise = fail ("Identityfilter expected empty object but got " <> show m)
2280+ boolFilter = pure . BoolFilter
2281+ existsFilter o = ExistsFilter <$> o .: "field"
2282+ geoBoundingBoxFilter = pure . GeoBoundingBoxFilter
2283+ geoDistanceFilter o = do
2284+ case HM.toList (deleteSeveral ["distance", "distance_type", "optimize_bbox", "_cache"] o) of
2285+ [(fn, v)] -> do
2286+ gp <- GeoPoint (FieldName fn) <$> parseJSON v
2287+ GeoDistanceFilter gp <$> o .: "distance"
2288+ <*> o .: "distance_type"
2289+ <*> o .: "optimize_bbox"
2290+ <*> o .:? "_cache" .!= defaultCache
2291+ _ -> fail "Could not find GeoDistanceFilter field name"
2292+ geoDistanceRangeFilter o = do
2293+ case HM.toList (deleteSeveral ["from", "to"] o) of
2294+ [(fn, v)] -> do
2295+ gp <- GeoPoint (FieldName fn) <$> parseJSON v
2296+ rng <- DistanceRange <$> o .: "from" <*> o .: "to"
2297+ return (GeoDistanceRangeFilter gp rng)
2298+ _ -> fail "Could not find GeoDistanceRangeFilter field name"
2299+ geoPolygonFilter = fieldTagged $ \fn o -> GeoPolygonFilter fn <$> o .: "points"
2300+ idsFilter o = IdsFilter <$> o .: "type"
2301+ <*> o .: "values"
2302+ limitFilter o = LimitFilter <$> o .: "value"
2303+ missingFilter o = MissingFilter <$> o .: "field"
2304+ <*> o .: "existence"
2305+ <*> o .: "null_value"
2306+ prefixFilter o = case HM.toList (HM.delete "_cache" o) of
2307+ [(fn, String v)] -> PrefixFilter (FieldName fn) v <$> o .:? "_cache" .!= defaultCache
2308+ _ -> fail "Could not parse PrefixFilter"
2309+
2310+ queryFilter q = pure (QueryFilter q False)
2311+ fqueryFilter o = QueryFilter <$> o .: "query" <*> pure True
2312+ rangeFilter o = case HM.toList (deleteSeveral ["execution", "_cache"] o) of
2313+ [(fn, v)] -> RangeFilter (FieldName fn)
2314+ <$> parseJSON v
2315+ <*> o .: "execution"
2316+ <*> o .:? "_cache" .!= defaultCache
2317+ _ -> fail "Could not find field name for RangeFilter"
2318+ regexpFilter o = case HM.toList (deleteSeveral ["_name", "_cache", "_cache_key"] o) of
2319+ [(fn, Object o')] -> RegexpFilter (FieldName fn)
2320+ <$> o' .: "value"
2321+ <*> o' .: "flags"
2322+ <*> o .: "_name"
2323+ <*> o .:? "_cache" .!= defaultCache
2324+ <*> o .: "_cache_key"
2325+ _ -> fail "Could not find field name for RegexpFilter"
2326+ termFilter o = case HM.toList (HM.delete "_cache" o) of
2327+ [(termField, String termVal)] -> TermFilter (Term termField termVal)
2328+ <$> o .:? "_cache" .!= defaultCache
2329+ _ -> fail "Could not find term field for TermFilter"
2330+
2331+fieldTagged :: Monad m => (FieldName -> Object -> m a) -> Object -> m a
2332+fieldTagged f o = case HM.toList o of
2333+ [(k, Object o')] -> f (FieldName k) o'
2334+ _ -> fail "Expected object with 1 field-named key"
2335+
2336+-- Try to get an AggregationResults when we don't know the
2337+-- field name. We filter out the known keys to try to minimize the noise.
2338+getNamedSubAgg :: Object -> [Text] -> Maybe AggregationResults
2339+getNamedSubAgg o knownKeys = maggRes
2340+ where unknownKeys = HM.filterWithKey (\k _ -> k `notElem` knownKeys) o
2341+ maggRes
2342+ | HM.null unknownKeys = Nothing
2343+ | otherwise = Just . M.fromList $ HM.toList unknownKeys
2344+
2345+instance ToJSON GeoPoint where
2346+ toJSON (GeoPoint (FieldName geoPointField) geoPointLatLon) =
2347+ object [ geoPointField .= geoPointLatLon ]
2348+
2349+
2350+instance ToJSON Query where
2351+ toJSON (TermQuery (Term termQueryField termQueryValue) boost) =
2352+ object [ "term" .=
2353+ object [termQueryField .= object merged]]
2354+ where
2355+ base = [ "value" .= termQueryValue ]
2356+ boosted = maybe [] (return . ("boost" .=)) boost
2357+ merged = mappend base boosted
2358+
2359+ toJSON (TermsQuery fieldName terms) =
2360+ object [ "terms" .= object conjoined ]
2361+ where conjoined = [fieldName .= terms]
2362+
2363+ toJSON (IdsQuery idsQueryMappingName docIds) =
2364+ object [ "ids" .= object conjoined ]
2365+ where conjoined = [ "type" .= idsQueryMappingName
2366+ , "values" .= fmap toJSON docIds ]
2367+
2368+ toJSON (QueryQueryStringQuery qQueryStringQuery) =
2369+ object [ "query_string" .= qQueryStringQuery ]
2370+
2371+ toJSON (QueryMatchQuery matchQuery) =
2372+ object [ "match" .= matchQuery ]
2373+
2374+ toJSON (QueryMultiMatchQuery multiMatchQuery) =
2375+ toJSON multiMatchQuery
2376+
2377+ toJSON (QueryBoolQuery boolQuery) =
2378+ object [ "bool" .= boolQuery ]
2379+
2380+ toJSON (QueryBoostingQuery boostingQuery) =
2381+ object [ "boosting" .= boostingQuery ]
2382+
2383+ toJSON (QueryCommonTermsQuery commonTermsQuery) =
2384+ object [ "common" .= commonTermsQuery ]
2385+
2386+ toJSON (ConstantScoreFilter csFilter boost) =
2387+ object ["constant_score" .= object ["filter" .= csFilter
2388+ , "boost" .= boost]]
2389+
2390+ toJSON (ConstantScoreQuery query boost) =
2391+ object ["constant_score" .= object ["query" .= query
2392+ , "boost" .= boost]]
2393+
2394+ toJSON (QueryDisMaxQuery disMaxQuery) =
2395+ object [ "dis_max" .= disMaxQuery ]
2396+
2397+ toJSON (QueryFilteredQuery qFilteredQuery) =
2398+ object [ "filtered" .= qFilteredQuery ]
2399+
2400+ toJSON (QueryFuzzyLikeThisQuery fuzzyQuery) =
2401+ object [ "fuzzy_like_this" .= fuzzyQuery ]
2402+
2403+ toJSON (QueryFuzzyLikeFieldQuery fuzzyFieldQuery) =
2404+ object [ "fuzzy_like_this_field" .= fuzzyFieldQuery ]
2405+
2406+ toJSON (QueryFuzzyQuery fuzzyQuery) =
2407+ object [ "fuzzy" .= fuzzyQuery ]
2408+
2409+ toJSON (QueryHasChildQuery childQuery) =
2410+ object [ "has_child" .= childQuery ]
2411+
2412+ toJSON (QueryHasParentQuery parentQuery) =
2413+ object [ "has_parent" .= parentQuery ]
2414+
2415+ toJSON (QueryIndicesQuery qIndicesQuery) =
2416+ object [ "indices" .= qIndicesQuery ]
2417+
2418+ toJSON (MatchAllQuery boost) =
2419+ object [ "match_all" .= omitNulls [ "boost" .= boost ] ]
2420+
2421+ toJSON (QueryMoreLikeThisQuery query) =
2422+ object [ "more_like_this" .= query ]
2423+
2424+ toJSON (QueryMoreLikeThisFieldQuery query) =
2425+ object [ "more_like_this_field" .= query ]
2426+
2427+ toJSON (QueryNestedQuery query) =
2428+ object [ "nested" .= query ]
2429+
2430+ toJSON (QueryPrefixQuery query) =
2431+ object [ "prefix" .= query ]
2432+
2433+ toJSON (QueryRangeQuery query) =
2434+ object [ "range" .= query ]
2435+
2436+ toJSON (QueryRegexpQuery query) =
2437+ object [ "regexp" .= query ]
2438+
2439+ toJSON (QuerySimpleQueryStringQuery query) =
2440+ object [ "simple_query_string" .= query ]
2441+
2442+ toJSON (QueryTemplateQueryInline templateQuery) =
2443+ object [ "template" .= templateQuery ]
2444+
2445+instance FromJSON Query where
2446+ parseJSON v = withObject "Query" parse v
2447+ where parse o = termQuery `taggedWith` "term"
2448+ <|> termsQuery `taggedWith` "terms"
2449+ <|> idsQuery `taggedWith` "ids"
2450+ <|> queryQueryStringQuery `taggedWith` "query_string"
2451+ <|> queryMatchQuery `taggedWith` "match"
2452+ <|> queryMultiMatchQuery
2453+ <|> queryBoolQuery `taggedWith` "bool"
2454+ <|> queryBoostingQuery `taggedWith` "boosting"
2455+ <|> queryCommonTermsQuery `taggedWith` "common"
2456+ <|> constantScoreFilter `taggedWith` "constant_score"
2457+ <|> constantScoreQuery `taggedWith` "constant_score"
2458+ <|> queryDisMaxQuery `taggedWith` "dis_max"
2459+ <|> queryFilteredQuery `taggedWith` "filtered"
2460+ <|> queryFuzzyLikeThisQuery `taggedWith` "fuzzy_like_this"
2461+ <|> queryFuzzyLikeFieldQuery `taggedWith` "fuzzy_like_this_field"
2462+ <|> queryFuzzyQuery `taggedWith` "fuzzy"
2463+ <|> queryHasChildQuery `taggedWith` "has_child"
2464+ <|> queryHasParentQuery `taggedWith` "has_parent"
2465+ <|> queryIndicesQuery `taggedWith` "indices"
2466+ <|> matchAllQuery `taggedWith` "match_all"
2467+ <|> queryMoreLikeThisQuery `taggedWith` "more_like_this"
2468+ <|> queryMoreLikeThisFieldQuery `taggedWith` "more_like_this_field"
2469+ <|> queryNestedQuery `taggedWith` "nested"
2470+ <|> queryPrefixQuery `taggedWith` "prefix"
2471+ <|> queryRangeQuery `taggedWith` "range"
2472+ <|> queryRegexpQuery `taggedWith` "regexp"
2473+ <|> querySimpleQueryStringQuery `taggedWith` "simple_query_string"
2474+ <|> queryTemplateQueryInline `taggedWith` "template"
2475+ where taggedWith parser k = parser =<< o .: k
2476+ termQuery = fieldTagged $ \(FieldName fn) o ->
2477+ TermQuery <$> (Term fn <$> o .: "value") <*> o .:? "boost"
2478+ termsQuery o = case HM.toList o of
2479+ [(fn, vs)] -> do vals <- parseJSON vs
2480+ case vals of
2481+ x:xs -> return (TermsQuery fn (x :| xs))
2482+ _ -> fail "Expected non empty list of values"
2483+ _ -> fail "Expected object with 1 field-named key"
2484+ idsQuery o = IdsQuery <$> o .: "type"
2485+ <*> o .: "values"
2486+ queryQueryStringQuery = pure . QueryQueryStringQuery
2487+ queryMatchQuery = pure . QueryMatchQuery
2488+ queryMultiMatchQuery = QueryMultiMatchQuery <$> parseJSON v
2489+ queryBoolQuery = pure . QueryBoolQuery
2490+ queryBoostingQuery = pure . QueryBoostingQuery
2491+ queryCommonTermsQuery = pure . QueryCommonTermsQuery
2492+ constantScoreFilter o = case HM.lookup "filter" o of
2493+ Just x -> ConstantScoreFilter <$> parseJSON x
2494+ <*> o .: "boost"
2495+ _ -> fail "Does not appear to be a ConstantScoreFilter"
2496+ constantScoreQuery o = case HM.lookup "query" o of
2497+ Just x -> ConstantScoreQuery <$> parseJSON x
2498+ <*> o .: "boost"
2499+ _ -> fail "Does not appear to be a ConstantScoreQuery"
2500+ queryDisMaxQuery = pure . QueryDisMaxQuery
2501+ queryFilteredQuery = pure . QueryFilteredQuery
2502+ queryFuzzyLikeThisQuery = pure . QueryFuzzyLikeThisQuery
2503+ queryFuzzyLikeFieldQuery = pure . QueryFuzzyLikeFieldQuery
2504+ queryFuzzyQuery = pure . QueryFuzzyQuery
2505+ queryHasChildQuery = pure . QueryHasChildQuery
2506+ queryHasParentQuery = pure . QueryHasParentQuery
2507+ queryIndicesQuery = pure . QueryIndicesQuery
2508+ matchAllQuery o = MatchAllQuery <$> o .:? "boost"
2509+ queryMoreLikeThisQuery = pure . QueryMoreLikeThisQuery
2510+ queryMoreLikeThisFieldQuery = pure . QueryMoreLikeThisFieldQuery
2511+ queryNestedQuery = pure . QueryNestedQuery
2512+ queryPrefixQuery = pure . QueryPrefixQuery
2513+ queryRangeQuery = pure . QueryRangeQuery
2514+ queryRegexpQuery = pure . QueryRegexpQuery
2515+ querySimpleQueryStringQuery = pure . QuerySimpleQueryStringQuery
2516+ queryTemplateQueryInline = pure . QueryTemplateQueryInline
2517+
2518+
2519+omitNulls :: [(Text, Value)] -> Value
2520+omitNulls = object . filter notNull where
2521+ notNull (_, Null) = False
2522+ notNull (_, Array a) = (not . V.null) a
2523+ notNull _ = True
2524+
2525+
2526+instance ToJSON SimpleQueryStringQuery where
2527+ toJSON SimpleQueryStringQuery {..} =
2528+ omitNulls (base ++ maybeAdd)
2529+ where base = [ "query" .= simpleQueryStringQuery ]
2530+ maybeAdd = [ "fields" .= simpleQueryStringField
2531+ , "default_operator" .= simpleQueryStringOperator
2532+ , "analyzer" .= simpleQueryStringAnalyzer
2533+ , "flags" .= simpleQueryStringFlags
2534+ , "lowercase_expanded_terms" .= simpleQueryStringLowercaseExpanded
2535+ , "locale" .= simpleQueryStringLocale ]
2536+
2537+instance FromJSON SimpleQueryStringQuery where
2538+ parseJSON = withObject "SimpleQueryStringQuery" parse
2539+ where parse o = SimpleQueryStringQuery <$> o .: "query"
2540+ <*> o .:? "fields"
2541+ <*> o .:? "default_operator"
2542+ <*> o .:? "analyzer"
2543+ <*> (parseFlags <$> o .:? "flags")
2544+ <*> o .:? "lowercase_expanded_terms"
2545+ <*> o .:? "locale"
2546+ parseFlags (Just (x:xs)) = Just (x :| xs)
2547+ parseFlags _ = Nothing
2548+
2549+instance ToJSON FieldOrFields where
2550+ toJSON (FofField fieldName) =
2551+ toJSON fieldName
2552+ toJSON (FofFields fieldNames) =
2553+ toJSON fieldNames
2554+
2555+instance FromJSON FieldOrFields where
2556+ parseJSON v = FofField <$> parseJSON v
2557+ <|> FofFields <$> (parseNEJSON =<< parseJSON v)
2558+
2559+instance ToJSON SimpleQueryFlag where
2560+ toJSON SimpleQueryAll = "ALL"
2561+ toJSON SimpleQueryNone = "NONE"
2562+ toJSON SimpleQueryAnd = "AND"
2563+ toJSON SimpleQueryOr = "OR"
2564+ toJSON SimpleQueryPrefix = "PREFIX"
2565+ toJSON SimpleQueryPhrase = "PHRASE"
2566+ toJSON SimpleQueryPrecedence = "PRECEDENCE"
2567+ toJSON SimpleQueryEscape = "ESCAPE"
2568+ toJSON SimpleQueryWhitespace = "WHITESPACE"
2569+ toJSON SimpleQueryFuzzy = "FUZZY"
2570+ toJSON SimpleQueryNear = "NEAR"
2571+ toJSON SimpleQuerySlop = "SLOP"
2572+
2573+instance FromJSON SimpleQueryFlag where
2574+ parseJSON = withText "SimpleQueryFlag" parse
2575+ where parse "ALL" = pure SimpleQueryAll
2576+ parse "NONE" = pure SimpleQueryNone
2577+ parse "AND" = pure SimpleQueryAnd
2578+ parse "OR" = pure SimpleQueryOr
2579+ parse "PREFIX" = pure SimpleQueryPrefix
2580+ parse "PHRASE" = pure SimpleQueryPhrase
2581+ parse "PRECEDENCE" = pure SimpleQueryPrecedence
2582+ parse "ESCAPE" = pure SimpleQueryEscape
2583+ parse "WHITESPACE" = pure SimpleQueryWhitespace
2584+ parse "FUZZY" = pure SimpleQueryFuzzy
2585+ parse "NEAR" = pure SimpleQueryNear
2586+ parse "SLOP" = pure SimpleQuerySlop
2587+ parse f = fail ("Unexpected SimpleQueryFlag: " <> show f)
2588+
2589+instance ToJSON RegexpQuery where
2590+ toJSON (RegexpQuery (FieldName rqQueryField)
2591+ (Regexp regexpQueryQuery) rqQueryFlags
2592+ rqQueryBoost) =
2593+ object [ rqQueryField .= omitNulls base ]
2594+ where base = [ "value" .= regexpQueryQuery
2595+ , "flags" .= rqQueryFlags
2596+ , "boost" .= rqQueryBoost ]
2597+
2598+instance FromJSON RegexpQuery where
2599+ parseJSON = withObject "RegexpQuery" parse
2600+ where parse = fieldTagged $ \fn o ->
2601+ RegexpQuery fn
2602+ <$> o .: "value"
2603+ <*> o .: "flags"
2604+ <*> o .:? "boost"
2605+
2606+instance ToJSON QueryStringQuery where
2607+ toJSON (QueryStringQuery qsQueryString
2608+ qsDefaultField qsOperator
2609+ qsAnalyzer qsAllowWildcard
2610+ qsLowercaseExpanded qsEnablePositionIncrements
2611+ qsFuzzyMaxExpansions qsFuzziness
2612+ qsFuzzyPrefixLength qsPhraseSlop
2613+ qsBoost qsAnalyzeWildcard
2614+ qsGeneratePhraseQueries qsMinimumShouldMatch
2615+ qsLenient qsLocale) =
2616+ omitNulls base
2617+ where
2618+ base = [ "query" .= qsQueryString
2619+ , "default_field" .= qsDefaultField
2620+ , "default_operator" .= qsOperator
2621+ , "analyzer" .= qsAnalyzer
2622+ , "allow_leading_wildcard" .= qsAllowWildcard
2623+ , "lowercase_expanded_terms" .= qsLowercaseExpanded
2624+ , "enable_position_increments" .= qsEnablePositionIncrements
2625+ , "fuzzy_max_expansions" .= qsFuzzyMaxExpansions
2626+ , "fuzziness" .= qsFuzziness
2627+ , "fuzzy_prefix_length" .= qsFuzzyPrefixLength
2628+ , "phrase_slop" .= qsPhraseSlop
2629+ , "boost" .= qsBoost
2630+ , "analyze_wildcard" .= qsAnalyzeWildcard
2631+ , "auto_generate_phrase_queries" .= qsGeneratePhraseQueries
2632+ , "minimum_should_match" .= qsMinimumShouldMatch
2633+ , "lenient" .= qsLenient
2634+ , "locale" .= qsLocale ]
2635+
2636+instance FromJSON QueryStringQuery where
2637+ parseJSON = withObject "QueryStringQuery" parse
2638+ where parse o = QueryStringQuery
2639+ <$> o .: "query"
2640+ <*> o .:? "default_field"
2641+ <*> o .:? "default_operator"
2642+ <*> o .:? "analyzer"
2643+ <*> o .:? "allow_leading_wildcard"
2644+ <*> o .:? "lowercase_expanded_terms"
2645+ <*> o .:? "enable_position_increments"
2646+ <*> o .:? "fuzzy_max_expansions"
2647+ <*> o .:? "fuzziness"
2648+ <*> o .:? "fuzzy_prefix_length"
2649+ <*> o .:? "phrase_slop"
2650+ <*> o .:? "boost"
2651+ <*> o .:? "analyze_wildcard"
2652+ <*> o .:? "auto_generate_phrase_queries"
2653+ <*> o .:? "minimum_should_match"
2654+ <*> o .:? "lenient"
2655+ <*> o .:? "locale"
2656+
2657+instance ToJSON RangeQuery where
2658+ toJSON (RangeQuery (FieldName fieldName) range boost) =
2659+ object [ fieldName .= object conjoined ]
2660+ where conjoined = [ "boost" .= boost ] ++ (rangeValueToPair range)
2661+
2662+instance FromJSON RangeQuery where
2663+ parseJSON = withObject "RangeQuery" parse
2664+ where parse = fieldTagged $ \fn o ->
2665+ RangeQuery fn
2666+ <$> parseJSON (Object o)
2667+ <*> o .: "boost"
2668+
2669+instance FromJSON RangeValue where
2670+ parseJSON = withObject "RangeValue" parse
2671+ where parse o = parseDate o
2672+ <|> parseDouble o
2673+ parseDate o = do lt <- o .:? "lt"
2674+ lte <- o .:? "lte"
2675+ gt <- o .:? "gt"
2676+ gte <- o .:? "gte"
2677+ case (lt, lte, gt, gte) of
2678+ (Just a, _, Just b, _) -> return (RangeDateGtLt (GreaterThanD b) (LessThanD a))
2679+ (Just a, _, _, Just b)-> return (RangeDateGteLt (GreaterThanEqD b) (LessThanD a))
2680+ (_, Just a, Just b, _)-> return (RangeDateGtLte (GreaterThanD b) (LessThanEqD a))
2681+ (_, Just a, _, Just b)-> return (RangeDateGteLte (GreaterThanEqD b) (LessThanEqD a))
2682+ (_, _, Just a, _)-> return (RangeDateGt (GreaterThanD a))
2683+ (Just a, _, _, _)-> return (RangeDateLt (LessThanD a))
2684+ (_, _, _, Just a)-> return (RangeDateGte (GreaterThanEqD a))
2685+ (_, Just a, _, _)-> return (RangeDateLte (LessThanEqD a))
2686+ (Nothing, Nothing, Nothing, Nothing) -> mzero
2687+ parseDouble o = do lt <- o .:? "lt"
2688+ lte <- o .:? "lte"
2689+ gt <- o .:? "gt"
2690+ gte <- o .:? "gte"
2691+ case (lt, lte, gt, gte) of
2692+ (Just a, _, Just b, _) -> return (RangeDoubleGtLt (GreaterThan b) (LessThan a))
2693+ (Just a, _, _, Just b)-> return (RangeDoubleGteLt (GreaterThanEq b) (LessThan a))
2694+ (_, Just a, Just b, _)-> return (RangeDoubleGtLte (GreaterThan b) (LessThanEq a))
2695+ (_, Just a, _, Just b)-> return (RangeDoubleGteLte (GreaterThanEq b) (LessThanEq a))
2696+ (_, _, Just a, _)-> return (RangeDoubleGt (GreaterThan a))
2697+ (Just a, _, _, _)-> return (RangeDoubleLt (LessThan a))
2698+ (_, _, _, Just a)-> return (RangeDoubleGte (GreaterThanEq a))
2699+ (_, Just a, _, _)-> return (RangeDoubleLte (LessThanEq a))
2700+ (Nothing, Nothing, Nothing, Nothing) -> mzero
2701+
2702+instance ToJSON PrefixQuery where
2703+ toJSON (PrefixQuery (FieldName fieldName) queryValue boost) =
2704+ object [ fieldName .= omitNulls base ]
2705+ where base = [ "value" .= queryValue
2706+ , "boost" .= boost ]
2707+
2708+instance FromJSON PrefixQuery where
2709+ parseJSON = withObject "PrefixQuery" parse
2710+ where parse = fieldTagged $ \fn o ->
2711+ PrefixQuery fn
2712+ <$> o .: "value"
2713+ <*> o .:? "boost"
2714+
2715+instance ToJSON NestedQuery where
2716+ toJSON (NestedQuery nqPath nqScoreType nqQuery) =
2717+ object [ "path" .= nqPath
2718+ , "score_mode" .= nqScoreType
2719+ , "query" .= nqQuery ]
2720+
2721+instance FromJSON NestedQuery where
2722+ parseJSON = withObject "NestedQuery" parse
2723+ where parse o = NestedQuery
2724+ <$> o .: "path"
2725+ <*> o .: "score_mode"
2726+ <*> o .: "query"
2727+
2728+instance ToJSON MoreLikeThisFieldQuery where
2729+ toJSON (MoreLikeThisFieldQuery text (FieldName fieldName)
2730+ percent mtf mqt stopwords mindf maxdf
2731+ minwl maxwl boostTerms boost analyzer) =
2732+ object [ fieldName .= omitNulls base ]
2733+ where base = [ "like_text" .= text
2734+ , "percent_terms_to_match" .= percent
2735+ , "min_term_freq" .= mtf
2736+ , "max_query_terms" .= mqt
2737+ , "stop_words" .= stopwords
2738+ , "min_doc_freq" .= mindf
2739+ , "max_doc_freq" .= maxdf
2740+ , "min_word_length" .= minwl
2741+ , "max_word_length" .= maxwl
2742+ , "boost_terms" .= boostTerms
2743+ , "boost" .= boost
2744+ , "analyzer" .= analyzer ]
2745+
2746+instance FromJSON MoreLikeThisFieldQuery where
2747+ parseJSON = withObject "MoreLikeThisFieldQuery" parse
2748+ where parse = fieldTagged $ \fn o ->
2749+ MoreLikeThisFieldQuery
2750+ <$> o .: "like_text"
2751+ <*> pure fn
2752+ <*> o .:? "percent_terms_to_match"
2753+ <*> o .:? "min_term_freq"
2754+ <*> o .:? "max_query_terms"
2755+ -- <*> (optionalNE =<< o .:? "stop_words")
2756+ <*> o .:? "stop_words"
2757+ <*> o .:? "min_doc_freq"
2758+ <*> o .:? "max_doc_freq"
2759+ <*> o .:? "min_word_length"
2760+ <*> o .:? "max_word_length"
2761+ <*> o .:? "boost_terms"
2762+ <*> o .:? "boost"
2763+ <*> o .:? "analyzer"
2764+ -- optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
2765+
2766+instance ToJSON MoreLikeThisQuery where
2767+ toJSON (MoreLikeThisQuery text fields percent
2768+ mtf mqt stopwords mindf maxdf
2769+ minwl maxwl boostTerms boost analyzer) =
2770+ omitNulls base
2771+ where base = [ "like_text" .= text
2772+ , "fields" .= fields
2773+ , "percent_terms_to_match" .= percent
2774+ , "min_term_freq" .= mtf
2775+ , "max_query_terms" .= mqt
2776+ , "stop_words" .= stopwords
2777+ , "min_doc_freq" .= mindf
2778+ , "max_doc_freq" .= maxdf
2779+ , "min_word_length" .= minwl
2780+ , "max_word_length" .= maxwl
2781+ , "boost_terms" .= boostTerms
2782+ , "boost" .= boost
2783+ , "analyzer" .= analyzer ]
2784+
2785+instance FromJSON MoreLikeThisQuery where
2786+ parseJSON = withObject "MoreLikeThisQuery" parse
2787+ where parse o = MoreLikeThisQuery
2788+ <$> o .: "like_text"
2789+ -- <*> (optionalNE =<< o .:? "fields")
2790+ <*> o .:? "fields"
2791+ <*> o .:? "percent_terms_to_match"
2792+ <*> o .:? "min_term_freq"
2793+ <*> o .:? "max_query_terms"
2794+ -- <*> (optionalNE =<< o .:? "stop_words")
2795+ <*> o .:? "stop_words"
2796+ <*> o .:? "min_doc_freq"
2797+ <*> o .:? "max_doc_freq"
2798+ <*> o .:? "min_word_length"
2799+ <*> o .:? "max_word_length"
2800+ <*> o .:? "boost_terms"
2801+ <*> o .:? "boost"
2802+ <*> o .:? "analyzer"
2803+ -- optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
2804+
2805+instance ToJSON IndicesQuery where
2806+ toJSON (IndicesQuery indices query noMatch) =
2807+ omitNulls [ "indices" .= indices
2808+ , "no_match_query" .= noMatch
2809+ , "query" .= query ]
2810+
2811+instance FromJSON IndicesQuery where
2812+ parseJSON = withObject "IndicesQuery" parse
2813+ where parse o = IndicesQuery
2814+ <$> o .:? "indices" .!= []
2815+ <*> o .: "query"
2816+ <*> o .:? "no_match_query"
2817+
2818+instance ToJSON HasParentQuery where
2819+ toJSON (HasParentQuery queryType query scoreType) =
2820+ omitNulls [ "parent_type" .= queryType
2821+ , "score_type" .= scoreType
2822+ , "query" .= query ]
2823+
2824+instance FromJSON HasParentQuery where
2825+ parseJSON = withObject "HasParentQuery" parse
2826+ where parse o = HasParentQuery
2827+ <$> o .: "parent_type"
2828+ <*> o .: "query"
2829+ <*> o .:? "score_type"
2830+
2831+instance ToJSON HasChildQuery where
2832+ toJSON (HasChildQuery queryType query scoreType) =
2833+ omitNulls [ "query" .= query
2834+ , "score_type" .= scoreType
2835+ , "type" .= queryType ]
2836+
2837+instance FromJSON HasChildQuery where
2838+ parseJSON = withObject "HasChildQuery" parse
2839+ where parse o = HasChildQuery
2840+ <$> o .: "type"
2841+ <*> o .: "query"
2842+ <*> o .:? "score_type"
2843+
2844+instance ToJSON FuzzyQuery where
2845+ toJSON (FuzzyQuery (FieldName fieldName) queryText
2846+ prefixLength maxEx fuzziness boost) =
2847+ object [ fieldName .= omitNulls base ]
2848+ where base = [ "value" .= queryText
2849+ , "fuzziness" .= fuzziness
2850+ , "prefix_length" .= prefixLength
2851+ , "boost" .= boost
2852+ , "max_expansions" .= maxEx ]
2853+
2854+instance FromJSON FuzzyQuery where
2855+ parseJSON = withObject "FuzzyQuery" parse
2856+ where parse = fieldTagged $ \fn o ->
2857+ FuzzyQuery fn
2858+ <$> o .: "value"
2859+ <*> o .: "prefix_length"
2860+ <*> o .: "max_expansions"
2861+ <*> o .: "fuzziness"
2862+ <*> o .:? "boost"
2863+
2864+instance ToJSON FuzzyLikeFieldQuery where
2865+ toJSON (FuzzyLikeFieldQuery (FieldName fieldName)
2866+ fieldText maxTerms ignoreFreq fuzziness prefixLength
2867+ boost analyzer) =
2868+ object [ fieldName .=
2869+ omitNulls [ "like_text" .= fieldText
2870+ , "max_query_terms" .= maxTerms
2871+ , "ignore_tf" .= ignoreFreq
2872+ , "fuzziness" .= fuzziness
2873+ , "prefix_length" .= prefixLength
2874+ , "analyzer" .= analyzer
2875+ , "boost" .= boost ]]
2876+
2877+instance FromJSON FuzzyLikeFieldQuery where
2878+ parseJSON = withObject "FuzzyLikeFieldQuery" parse
2879+ where parse = fieldTagged $ \fn o ->
2880+ FuzzyLikeFieldQuery fn
2881+ <$> o .: "like_text"
2882+ <*> o .: "max_query_terms"
2883+ <*> o .: "ignore_tf"
2884+ <*> o .: "fuzziness"
2885+ <*> o .: "prefix_length"
2886+ <*> o .: "boost"
2887+ <*> o .:? "analyzer"
2888+
2889+instance ToJSON FuzzyLikeThisQuery where
2890+ toJSON (FuzzyLikeThisQuery fields text maxTerms
2891+ ignoreFreq fuzziness prefixLength boost analyzer) =
2892+ omitNulls base
2893+ where base = [ "fields" .= fields
2894+ , "like_text" .= text
2895+ , "max_query_terms" .= maxTerms
2896+ , "ignore_tf" .= ignoreFreq
2897+ , "fuzziness" .= fuzziness
2898+ , "prefix_length" .= prefixLength
2899+ , "analyzer" .= analyzer
2900+ , "boost" .= boost ]
2901+
2902+instance FromJSON FuzzyLikeThisQuery where
2903+ parseJSON = withObject "FuzzyLikeThisQuery" parse
2904+ where parse o = FuzzyLikeThisQuery
2905+ <$> o .:? "fields" .!= []
2906+ <*> o .: "like_text"
2907+ <*> o .: "max_query_terms"
2908+ <*> o .: "ignore_tf"
2909+ <*> o .: "fuzziness"
2910+ <*> o .: "prefix_length"
2911+ <*> o .: "boost"
2912+ <*> o .:? "analyzer"
2913+
2914+instance ToJSON FilteredQuery where
2915+ toJSON (FilteredQuery query fFilter) =
2916+ object [ "query" .= query
2917+ , "filter" .= fFilter ]
2918+
2919+instance FromJSON FilteredQuery where
2920+ parseJSON = withObject "FilteredQuery" parse
2921+ where parse o = FilteredQuery
2922+ <$> o .: "query"
2923+ <*> o .: "filter"
2924+
2925+instance ToJSON DisMaxQuery where
2926+ toJSON (DisMaxQuery queries tiebreaker boost) =
2927+ omitNulls base
2928+ where base = [ "queries" .= queries
2929+ , "boost" .= boost
2930+ , "tie_breaker" .= tiebreaker ]
2931+
2932+instance FromJSON DisMaxQuery where
2933+ parseJSON = withObject "DisMaxQuery" parse
2934+ where parse o = DisMaxQuery
2935+ <$> o .:? "queries" .!= []
2936+ <*> o .: "tie_breaker"
2937+ <*> o .:? "boost"
2938+
2939+instance ToJSON CommonTermsQuery where
2940+ toJSON (CommonTermsQuery (FieldName fieldName)
2941+ (QueryString query) cf lfo hfo msm
2942+ boost analyzer disableCoord) =
2943+ object [fieldName .= omitNulls base ]
2944+ where base = [ "query" .= query
2945+ , "cutoff_frequency" .= cf
2946+ , "low_freq_operator" .= lfo
2947+ , "minimum_should_match" .= msm
2948+ , "boost" .= boost
2949+ , "analyzer" .= analyzer
2950+ , "disable_coord" .= disableCoord
2951+ , "high_freq_operator" .= hfo ]
2952+
2953+instance FromJSON CommonTermsQuery where
2954+ parseJSON = withObject "CommonTermsQuery" parse
2955+ where parse = fieldTagged $ \fn o ->
2956+ CommonTermsQuery fn
2957+ <$> o .: "query"
2958+ <*> o .: "cutoff_frequency"
2959+ <*> o .: "low_freq_operator"
2960+ <*> o .: "high_freq_operator"
2961+ <*> o .:? "minimum_should_match"
2962+ <*> o .:? "boost"
2963+ <*> o .:? "analyzer"
2964+ <*> o .:? "disable_coord"
2965+
2966+instance ToJSON CommonMinimumMatch where
2967+ toJSON (CommonMinimumMatch mm) = toJSON mm
2968+ toJSON (CommonMinimumMatchHighLow (MinimumMatchHighLow lowF highF)) =
2969+ object [ "low_freq" .= lowF
2970+ , "high_freq" .= highF ]
2971+
2972+instance FromJSON CommonMinimumMatch where
2973+ parseJSON v = parseMinimum v
2974+ <|> parseMinimumHighLow v
2975+ where parseMinimum = fmap CommonMinimumMatch . parseJSON
2976+ parseMinimumHighLow = fmap CommonMinimumMatchHighLow . withObject "CommonMinimumMatchHighLow" (\o ->
2977+ MinimumMatchHighLow
2978+ <$> o .: "low_freq"
2979+ <*> o .: "high_freq")
2980+
2981+
2982+instance ToJSON BoostingQuery where
2983+ toJSON (BoostingQuery bqPositiveQuery bqNegativeQuery bqNegativeBoost) =
2984+ object [ "positive" .= bqPositiveQuery
2985+ , "negative" .= bqNegativeQuery
2986+ , "negative_boost" .= bqNegativeBoost ]
2987+
2988+instance FromJSON BoostingQuery where
2989+ parseJSON = withObject "BoostingQuery" parse
2990+ where parse o = BoostingQuery
2991+ <$> o .: "positive"
2992+ <*> o .: "negative"
2993+ <*> o .: "negative_boost"
2994+
2995+instance ToJSON BoolQuery where
2996+ toJSON (BoolQuery mustM notM shouldM bqMin boost disableCoord) =
2997+ omitNulls base
2998+ where base = [ "must" .= mustM
2999+ , "must_not" .= notM
3000+ , "should" .= shouldM
3001+ , "minimum_should_match" .= bqMin
3002+ , "boost" .= boost
3003+ , "disable_coord" .= disableCoord ]
3004+
3005+instance FromJSON BoolQuery where
3006+ parseJSON = withObject "BoolQuery" parse
3007+ where parse o = BoolQuery
3008+ <$> o .:? "must" .!= []
3009+ <*> o .:? "must_not" .!= []
3010+ <*> o .:? "should" .!= []
3011+ <*> o .:? "minimum_should_match"
3012+ <*> o .:? "boost"
3013+ <*> o .:? "disable_coord"
3014+
3015+instance ToJSON MatchQuery where
3016+ toJSON (MatchQuery (FieldName fieldName)
3017+ (QueryString mqQueryString) booleanOperator
3018+ zeroTermsQuery cutoffFrequency matchQueryType
3019+ analyzer maxExpansions lenient boost) =
3020+ object [ fieldName .= omitNulls base ]
3021+ where base = [ "query" .= mqQueryString
3022+ , "operator" .= booleanOperator
3023+ , "zero_terms_query" .= zeroTermsQuery
3024+ , "cutoff_frequency" .= cutoffFrequency
3025+ , "type" .= matchQueryType
3026+ , "analyzer" .= analyzer
3027+ , "max_expansions" .= maxExpansions
3028+ , "lenient" .= lenient
3029+ , "boost" .= boost ]
3030+
3031+instance FromJSON MatchQuery where
3032+ parseJSON = withObject "MatchQuery" parse
3033+ where parse = fieldTagged $ \fn o ->
3034+ MatchQuery fn
3035+ <$> o .: "query"
3036+ <*> o .: "operator"
3037+ <*> o .: "zero_terms_query"
3038+ <*> o .:? "cutoff_frequency"
3039+ <*> o .:? "type"
3040+ <*> o .:? "analyzer"
3041+ <*> o .:? "max_expansions"
3042+ <*> o .:? "lenient"
3043+ <*> o .:? "boost"
3044+
3045+instance ToJSON MultiMatchQuery where
3046+ toJSON (MultiMatchQuery fields (QueryString query) boolOp
3047+ ztQ tb mmqt cf analyzer maxEx lenient) =
3048+ object ["multi_match" .= omitNulls base]
3049+ where base = [ "fields" .= fmap toJSON fields
3050+ , "query" .= query
3051+ , "operator" .= boolOp
3052+ , "zero_terms_query" .= ztQ
3053+ , "tie_breaker" .= tb
3054+ , "type" .= mmqt
3055+ , "cutoff_frequency" .= cf
3056+ , "analyzer" .= analyzer
3057+ , "max_expansions" .= maxEx
3058+ , "lenient" .= lenient ]
3059+
3060+instance FromJSON MultiMatchQuery where
3061+ parseJSON = withObject "MultiMatchQuery" parse
3062+ where parse raw = do o <- raw .: "multi_match"
3063+ MultiMatchQuery
3064+ <$> o .:? "fields" .!= []
3065+ <*> o .: "query"
3066+ <*> o .: "operator"
3067+ <*> o .: "zero_terms_query"
3068+ <*> o .:? "tie_breaker"
3069+ <*> o .:? "type"
3070+ <*> o .:? "cutoff_frequency"
3071+ <*> o .:? "analyzer"
3072+ <*> o .:? "max_expansions"
3073+ <*> o .:? "lenient"
3074+
3075+instance ToJSON MultiMatchQueryType where
3076+ toJSON MultiMatchBestFields = "best_fields"
3077+ toJSON MultiMatchMostFields = "most_fields"
3078+ toJSON MultiMatchCrossFields = "cross_fields"
3079+ toJSON MultiMatchPhrase = "phrase"
3080+ toJSON MultiMatchPhrasePrefix = "phrase_prefix"
3081+
3082+instance FromJSON MultiMatchQueryType where
3083+ parseJSON = withText "MultiMatchPhrasePrefix" parse
3084+ where parse "best_fields" = pure MultiMatchBestFields
3085+ parse "most_fields" = pure MultiMatchMostFields
3086+ parse "cross_fields" = pure MultiMatchCrossFields
3087+ parse "phrase" = pure MultiMatchPhrase
3088+ parse "phrase_prefix" = pure MultiMatchPhrasePrefix
3089+ parse t = fail ("Unexpected MultiMatchPhrasePrefix: " <> show t)
3090+
3091+instance ToJSON BooleanOperator where
3092+ toJSON And = String "and"
3093+ toJSON Or = String "or"
3094+
3095+instance FromJSON BooleanOperator where
3096+ parseJSON = withText "BooleanOperator" parse
3097+ where parse "and" = pure And
3098+ parse "or" = pure Or
3099+ parse o = fail ("Unexpected BooleanOperator: " <> show o)
3100+
3101+instance ToJSON ZeroTermsQuery where
3102+ toJSON ZeroTermsNone = String "none"
3103+ toJSON ZeroTermsAll = String "all"
3104+
3105+instance FromJSON ZeroTermsQuery where
3106+ parseJSON = withText "ZeroTermsQuery" parse
3107+ where parse "none" = pure ZeroTermsNone
3108+ parse "all" = pure ZeroTermsAll
3109+ parse q = fail ("Unexpected ZeroTermsQuery: " <> show q)
3110+
3111+instance ToJSON MatchQueryType where
3112+ toJSON MatchPhrase = "phrase"
3113+ toJSON MatchPhrasePrefix = "phrase_prefix"
3114+
3115+instance FromJSON MatchQueryType where
3116+ parseJSON = withText "MatchQueryType" parse
3117+ where parse "phrase" = pure MatchPhrase
3118+ parse "phrase_prefix" = pure MatchPhrasePrefix
3119+ parse t = fail ("Unexpected MatchQueryType: " <> show t)
3120+
3121+instance FromJSON Status where
3122+ parseJSON (Object v) = Status <$>
3123+ v .:? "ok" <*>
3124+ (v .:? "status" .!= 200) <*>
3125+ v .: "name" <*>
3126+ v .: "version" <*>
3127+ v .: "tagline"
3128+ parseJSON _ = empty
3129+
3130+
3131+instance ToJSON IndexSettings where
3132+ toJSON (IndexSettings s r) = object ["settings" .=
3133+ object ["index" .=
3134+ object ["number_of_shards" .= s, "number_of_replicas" .= r]
3135+ ]
3136+ ]
3137+
3138+instance FromJSON IndexSettings where
3139+ parseJSON = withObject "IndexSettings" parse
3140+ where parse o = do s <- o .: "settings"
3141+ i <- s .: "index"
3142+ IndexSettings <$> i .: "number_of_shards"
3143+ <*> i .: "number_of_replicas"
3144+
3145+instance ToJSON UpdatableIndexSetting where
3146+ toJSON (NumberOfReplicas x) = oPath ("index" :| ["number_of_replicas"]) x
3147+ toJSON (AutoExpandReplicas x) = oPath ("index" :| ["auto_expand_replicas"]) x
3148+ toJSON (RefreshInterval x) = oPath ("index" :| ["refresh_interval"]) (NominalDiffTimeJSON x)
3149+ toJSON (IndexConcurrency x) = oPath ("index" :| ["concurrency"]) x
3150+ toJSON (FailOnMergeFailure x) = oPath ("index" :| ["fail_on_merge_failure"]) x
3151+ toJSON (TranslogFlushThresholdOps x) = oPath ("index" :| ["translog", "flush_threshold_ops"]) x
3152+ toJSON (TranslogFlushThresholdSize x) = oPath ("index" :| ["translog", "flush_threshold_size"]) x
3153+ toJSON (TranslogFlushThresholdPeriod x) = oPath ("index" :| ["translog", "flush_threshold_period"]) (NominalDiffTimeJSON x)
3154+ toJSON (TranslogDisableFlush x) = oPath ("index" :| ["translog", "disable_flush"]) x
3155+ toJSON (CacheFilterMaxSize x) = oPath ("index" :| ["cache", "filter", "max_size"]) x
3156+ toJSON (CacheFilterExpire x) = oPath ("index" :| ["cache", "filter", "expire"]) (NominalDiffTimeJSON <$> x)
3157+ toJSON (GatewaySnapshotInterval x) = oPath ("index" :| ["gateway", "snapshot_interval"]) (NominalDiffTimeJSON x)
3158+ toJSON (RoutingAllocationInclude fs) = oPath ("index" :| ["routing", "allocation", "include"]) (attrFilterJSON fs)
3159+ toJSON (RoutingAllocationExclude fs) = oPath ("index" :| ["routing", "allocation", "exclude"]) (attrFilterJSON fs)
3160+ toJSON (RoutingAllocationRequire fs) = oPath ("index" :| ["routing", "allocation", "require"]) (attrFilterJSON fs)
3161+ toJSON (RoutingAllocationEnable x) = oPath ("index" :| ["routing", "allocation", "enable"]) x
3162+ toJSON (RoutingAllocationShardsPerNode x) = oPath ("index" :| ["routing", "allocation", "total_shards_per_node"]) x
3163+ toJSON (RecoveryInitialShards x) = oPath ("index" :| ["recovery", "initial_shards"]) x
3164+ toJSON (GCDeletes x) = oPath ("index" :| ["gc_deletes"]) (NominalDiffTimeJSON x)
3165+ toJSON (TTLDisablePurge x) = oPath ("index" :| ["ttl", "disable_purge"]) x
3166+ toJSON (TranslogFSType x) = oPath ("index" :| ["translog", "fs", "type"]) x
3167+ toJSON (IndexCompoundFormat x) = oPath ("index" :| ["compound_format"]) x
3168+ toJSON (IndexCompoundOnFlush x) = oPath ("index" :| ["compound_on_flush"]) x
3169+ toJSON (WarmerEnabled x) = oPath ("index" :| ["warmer", "enabled"]) x
3170+ toJSON (BlocksReadOnly x) = oPath ("blocks" :| ["read_only"]) x
3171+ toJSON (BlocksRead x) = oPath ("blocks" :| ["read"]) x
3172+ toJSON (BlocksWrite x) = oPath ("blocks" :| ["write"]) x
3173+ toJSON (BlocksMetaData x) = oPath ("blocks" :| ["metadata"]) x
3174+
3175+instance FromJSON UpdatableIndexSetting where
3176+ parseJSON = withObject "UpdatableIndexSetting" parse
3177+ where parse o = numberOfReplicas `taggedAt` ["index", "number_of_replicas"]
3178+ <|> autoExpandReplicas `taggedAt` ["index", "auto_expand_replicas"]
3179+ <|> refreshInterval `taggedAt` ["index", "refresh_interval"]
3180+ <|> indexConcurrency `taggedAt` ["index", "concurrency"]
3181+ <|> failOnMergeFailure `taggedAt` ["index", "fail_on_merge_failure"]
3182+ <|> translogFlushThresholdOps `taggedAt` ["index", "translog", "flush_threshold_ops"]
3183+ <|> translogFlushThresholdSize `taggedAt` ["index", "translog", "flush_threshold_size"]
3184+ <|> translogFlushThresholdPeriod `taggedAt` ["index", "translog", "flush_threshold_period"]
3185+ <|> translogDisableFlush `taggedAt` ["index", "translog", "disable_flush"]
3186+ <|> cacheFilterMaxSize `taggedAt` ["index", "cache", "filter", "max_size"]
3187+ <|> cacheFilterExpire `taggedAt` ["index", "cache", "filter", "expire"]
3188+ <|> gatewaySnapshotInterval `taggedAt` ["index", "gateway", "snapshot_interval"]
3189+ <|> routingAllocationInclude `taggedAt` ["index", "routing", "allocation", "include"]
3190+ <|> routingAllocationExclude `taggedAt` ["index", "routing", "allocation", "exclude"]
3191+ <|> routingAllocationRequire `taggedAt` ["index", "routing", "allocation", "require"]
3192+ <|> routingAllocationEnable `taggedAt` ["index", "routing", "allocation", "enable"]
3193+ <|> routingAllocationShardsPerNode `taggedAt` ["index", "routing", "allocation", "total_shards_per_node"]
3194+ <|> recoveryInitialShards `taggedAt` ["index", "recovery", "initial_shards"]
3195+ <|> gcDeletes `taggedAt` ["index", "gc_deletes"]
3196+ <|> ttlDisablePurge `taggedAt` ["index", "ttl", "disable_purge"]
3197+ <|> translogFSType `taggedAt` ["index", "translog", "fs", "type"]
3198+ <|> compoundFormat `taggedAt` ["index", "compound_format"]
3199+ <|> compoundOnFlush `taggedAt` ["index", "compound_on_flush"]
3200+ <|> warmerEnabled `taggedAt` ["index", "warmer", "enabled"]
3201+ <|> blocksReadOnly `taggedAt` ["blocks", "read_only"]
3202+ <|> blocksRead `taggedAt` ["blocks", "read"]
3203+ <|> blocksWrite `taggedAt` ["blocks", "write"]
3204+ <|> blocksMetaData `taggedAt` ["blocks", "metadata"]
3205+ where taggedAt f ks = taggedAt' f (Object o) ks
3206+ taggedAt' f v [] = f =<< (parseJSON v <|> (parseJSON (unStringlyTypeJSON v)))
3207+ taggedAt' f v (k:ks) = withObject "Object" (\o -> do v' <- o .: k
3208+ taggedAt' f v' ks) v
3209+ numberOfReplicas = pure . NumberOfReplicas
3210+ autoExpandReplicas = pure . AutoExpandReplicas
3211+ refreshInterval = pure . RefreshInterval . ndtJSON
3212+ indexConcurrency = pure . IndexConcurrency
3213+ failOnMergeFailure = pure . FailOnMergeFailure
3214+ translogFlushThresholdOps = pure . TranslogFlushThresholdOps
3215+ translogFlushThresholdSize = pure . TranslogFlushThresholdSize
3216+ translogFlushThresholdPeriod = pure . TranslogFlushThresholdPeriod . ndtJSON
3217+ translogDisableFlush = pure . TranslogDisableFlush
3218+ cacheFilterMaxSize = pure . CacheFilterMaxSize
3219+ cacheFilterExpire = pure . CacheFilterExpire . fmap ndtJSON
3220+ gatewaySnapshotInterval = pure . GatewaySnapshotInterval . ndtJSON
3221+ routingAllocationInclude = fmap RoutingAllocationInclude . parseAttrFilter
3222+ routingAllocationExclude = fmap RoutingAllocationExclude . parseAttrFilter
3223+ routingAllocationRequire = fmap RoutingAllocationRequire . parseAttrFilter
3224+ routingAllocationEnable = pure . RoutingAllocationEnable
3225+ routingAllocationShardsPerNode = pure . RoutingAllocationShardsPerNode
3226+ recoveryInitialShards = pure . RecoveryInitialShards
3227+ gcDeletes = pure . GCDeletes . ndtJSON
3228+ ttlDisablePurge = pure . TTLDisablePurge
3229+ translogFSType = pure . TranslogFSType
3230+ compoundFormat = pure . IndexCompoundFormat
3231+ compoundOnFlush = pure . IndexCompoundOnFlush
3232+ warmerEnabled = pure . WarmerEnabled
3233+ blocksReadOnly = pure . BlocksReadOnly
3234+ blocksRead = pure . BlocksRead
3235+ blocksWrite = pure . BlocksWrite
3236+ blocksMetaData = pure . BlocksMetaData
3237+
3238+instance FromJSON IndexSettingsSummary where
3239+ parseJSON = withObject "IndexSettingsSummary" parse
3240+ where parse o = case HM.toList o of
3241+ [(ixn, v@(Object o'))] -> IndexSettingsSummary (IndexName ixn)
3242+ <$> parseJSON v
3243+ <*> (fmap (filter (not . redundant)) . parseSettings =<< o' .: "settings")
3244+ _ -> fail "Expected single-key object with index name"
3245+ redundant (NumberOfReplicas _) = True
3246+ redundant _ = False
3247+
3248+-- | For some reason in several settings APIs, all leaf values get returned
3249+-- as strings. This function attepmts to recover from this for all
3250+-- non-recursive JSON types. If nothing can be done, the value is left alone.
3251+unStringlyTypeJSON :: Value -> Value
3252+unStringlyTypeJSON (String "true") = Bool True
3253+unStringlyTypeJSON (String "false") = Bool False
3254+unStringlyTypeJSON (String "null") = Null
3255+unStringlyTypeJSON v@(String t) = case readMay (T.unpack t) of
3256+ Just n -> Number n
3257+ Nothing -> v
3258+unStringlyTypeJSON v = v
3259+
3260+
3261+parseSettings :: Object -> Parser [UpdatableIndexSetting]
3262+parseSettings o = do
3263+ o' <- o .: "index"
3264+ -- slice the index object into singleton hashmaps and try to parse each
3265+ parses <- forM (HM.toList o') $ \(k, v) -> do
3266+ -- blocks are now nested into the "index" key, which is not how they're serialized
3267+ let atRoot = Object (HM.singleton k v)
3268+ let atIndex = Object (HM.singleton "index" atRoot)
3269+ optional (parseJSON atRoot <|> parseJSON atIndex)
3270+ return (catMaybes parses)
3271+
3272+oPath :: ToJSON a => NonEmpty Text -> a -> Value
3273+oPath (k :| []) v = object [k .= v]
3274+oPath (k:| (h:t)) v = object [k .= oPath (h :| t) v]
3275+
3276+attrFilterJSON :: NonEmpty NodeAttrFilter -> Value
3277+attrFilterJSON fs = object [ n .= T.intercalate "," (toList vs)
3278+ | NodeAttrFilter (NodeAttrName n) vs <- toList fs]
3279+
3280+parseAttrFilter :: Value -> Parser (NonEmpty NodeAttrFilter)
3281+parseAttrFilter = withObject "NonEmpty NodeAttrFilter" parse
3282+ where parse o = case HM.toList o of
3283+ [] -> fail "Expected non-empty list of NodeAttrFilters"
3284+ x:xs -> DT.mapM (uncurry parse') (x :| xs)
3285+ parse' n = withText "Text" $ \t ->
3286+ case T.splitOn "," t of
3287+ fv:fvs -> return (NodeAttrFilter (NodeAttrName n) (fv :| fvs))
3288+ [] -> fail "Expected non-empty list of filter values"
3289+
3290+instance ToJSON ReplicaBounds where
3291+ toJSON (ReplicasBounded a b) = String (showText a <> "-" <> showText b)
3292+ toJSON (ReplicasLowerBounded a) = String (showText a <> "-all")
3293+ toJSON ReplicasUnbounded = Bool False
3294+
3295+instance FromJSON ReplicaBounds where
3296+ parseJSON v = withText "ReplicaBounds" parseText v
3297+ <|> withBool "ReplicaBounds" parseBool v
3298+ where parseText t = case T.splitOn "-" t of
3299+ [a, "all"] -> ReplicasLowerBounded <$> parseReadText a
3300+ [a, b] -> ReplicasBounded <$> parseReadText a
3301+ <*> parseReadText b
3302+ _ -> fail ("Could not parse ReplicaBounds: " <> show t)
3303+ parseBool False = pure ReplicasUnbounded
3304+ parseBool _ = fail "ReplicasUnbounded cannot be represented with True"
3305+
3306+instance ToJSON AllocationPolicy where
3307+ toJSON AllocAll = String "all"
3308+ toJSON AllocPrimaries = String "primaries"
3309+ toJSON AllocNewPrimaries = String "new_primaries"
3310+ toJSON AllocNone = String "none"
3311+
3312+instance FromJSON AllocationPolicy where
3313+ parseJSON = withText "AllocationPolicy" parse
3314+ where parse "all" = pure AllocAll
3315+ parse "primaries" = pure AllocPrimaries
3316+ parse "new_primaries" = pure AllocNewPrimaries
3317+ parse "none" = pure AllocNone
3318+ parse t = fail ("Invlaid AllocationPolicy: " <> show t)
3319+
3320+instance ToJSON InitialShardCount where
3321+ toJSON QuorumShards = String "quorum"
3322+ toJSON QuorumMinus1Shards = String "quorum-1"
3323+ toJSON FullShards = String "full"
3324+ toJSON FullMinus1Shards = String "full-1"
3325+ toJSON (ExplicitShards x) = toJSON x
3326+
3327+instance FromJSON InitialShardCount where
3328+ parseJSON v = withText "InitialShardCount" parseText v
3329+ <|> ExplicitShards <$> parseJSON v
3330+ where parseText "quorum" = pure QuorumShards
3331+ parseText "quorum-1" = pure QuorumMinus1Shards
3332+ parseText "full" = pure FullShards
3333+ parseText "full-1" = pure FullMinus1Shards
3334+ parseText _ = mzero
3335+
3336+instance ToJSON FSType where
3337+ toJSON FSSimple = "simple"
3338+ toJSON FSBuffered = "buffered"
3339+
3340+instance FromJSON FSType where
3341+ parseJSON = withText "FSType" parse
3342+ where parse "simple" = pure FSSimple
3343+ parse "buffered" = pure FSBuffered
3344+ parse t = fail ("Invalid FSType: " <> show t)
3345+
3346+instance ToJSON CompoundFormat where
3347+ toJSON (CompoundFileFormat x) = Bool x
3348+ toJSON (MergeSegmentVsTotalIndex x) = toJSON x
3349+
3350+instance FromJSON CompoundFormat where
3351+ parseJSON v = CompoundFileFormat <$> parseJSON v
3352+ <|> MergeSegmentVsTotalIndex <$> parseJSON v
3353+
3354+instance ToJSON NominalDiffTimeJSON where
3355+ toJSON (NominalDiffTimeJSON t) = String (showText (round t :: Integer) <> "s")
3356+
3357+instance FromJSON NominalDiffTimeJSON where
3358+ parseJSON = withText "NominalDiffTime" parse
3359+ where parse t = case T.takeEnd 1 t of
3360+ "s" -> NominalDiffTimeJSON . fromInteger <$> parseReadText (T.dropEnd 1 t)
3361+ _ -> fail "Invalid or missing NominalDiffTime unit (expected s)"
3362+
3363+instance ToJSON IndexTemplate where
3364+ toJSON (IndexTemplate p s m) = merge
3365+ (object [ "template" .= p
3366+ , "mappings" .= foldl' merge (object []) m
3367+ ])
3368+ (toJSON s)
3369+ where
3370+ merge (Object o1) (Object o2) = toJSON $ HM.union o1 o2
3371+ merge o Null = o
3372+ merge _ _ = undefined
3373+
3374+instance (FromJSON a) => FromJSON (EsResult a) where
3375+ parseJSON jsonVal@(Object v) = do
3376+ found <- v .:? "found" .!= False
3377+ fr <- if found
3378+ then parseJSON jsonVal
3379+ else return Nothing
3380+ EsResult <$> v .: "_index" <*>
3381+ v .: "_type" <*>
3382+ v .: "_id" <*>
3383+ pure fr
3384+ parseJSON _ = empty
3385+
3386+instance (FromJSON a) => FromJSON (EsResultFound a) where
3387+ parseJSON (Object v) = EsResultFound <$>
3388+ v .: "_version" <*>
3389+ v .: "_source"
3390+ parseJSON _ = empty
3391+
3392+instance FromJSON EsError where
3393+ parseJSON (Object v) = EsError <$>
3394+ v .: "status" <*>
3395+ (v .: "error" <|> (v .: "error" >>= (.: "reason")))
3396+ parseJSON _ = empty
3397+
3398+instance FromJSON IndexAliasesSummary where
3399+ parseJSON = withObject "IndexAliasesSummary" parse
3400+ where parse o = IndexAliasesSummary . mconcat <$> mapM (uncurry go) (HM.toList o)
3401+ go ixn = withObject "index aliases" $ \ia -> do
3402+ aliases <- ia .:? "aliases" .!= mempty
3403+ forM (HM.toList aliases) $ \(aName, v) -> do
3404+ let indexAlias = IndexAlias (IndexName ixn) (IndexAliasName (IndexName aName))
3405+ IndexAliasSummary indexAlias <$> parseJSON v
3406+
3407+
3408+instance ToJSON IndexAliasAction where
3409+ toJSON (AddAlias ia opts) = object ["add" .= (iaObj <> optsObj)]
3410+ where Object iaObj = toJSON ia
3411+ Object optsObj = toJSON opts
3412+ toJSON (RemoveAlias ia) = object ["remove" .= iaObj]
3413+ where Object iaObj = toJSON ia
3414+
3415+instance ToJSON IndexAlias where
3416+ toJSON IndexAlias {..} = object ["index" .= srcIndex
3417+ , "alias" .= indexAlias
3418+ ]
3419+
3420+instance ToJSON IndexAliasCreate where
3421+ toJSON IndexAliasCreate {..} = Object (filterObj <> routingObj)
3422+ where filterObj = maybe mempty (HM.singleton "filter" . toJSON) aliasCreateFilter
3423+ Object routingObj = maybe (Object mempty) toJSON aliasCreateRouting
3424+
3425+instance ToJSON AliasRouting where
3426+ toJSON (AllAliasRouting v) = object ["routing" .= v]
3427+ toJSON (GranularAliasRouting srch idx) = object (catMaybes prs)
3428+ where prs = [("search_routing" .=) <$> srch
3429+ ,("index_routing" .=) <$> idx]
3430+
3431+instance FromJSON AliasRouting where
3432+ parseJSON = withObject "AliasRouting" parse
3433+ where parse o = parseAll o <|> parseGranular o
3434+ parseAll o = AllAliasRouting <$> o .: "routing"
3435+ parseGranular o = do
3436+ sr <- o .:? "search_routing"
3437+ ir <- o .:? "index_routing"
3438+ if isNothing sr && isNothing ir
3439+ then fail "Both search_routing and index_routing can't be blank"
3440+ else return (GranularAliasRouting sr ir)
3441+
3442+instance FromJSON IndexAliasCreate where
3443+ parseJSON v = withObject "IndexAliasCreate" parse v
3444+ where parse o = IndexAliasCreate <$> optional (parseJSON v)
3445+ <*> o .:? "filter"
3446+
3447+instance ToJSON SearchAliasRouting where
3448+ toJSON (SearchAliasRouting rvs) = toJSON (T.intercalate "," (routingValue <$> toList rvs))
3449+
3450+instance FromJSON SearchAliasRouting where
3451+ parseJSON = withText "SearchAliasRouting" parse
3452+ where parse t = SearchAliasRouting <$> parseNEJSON (String <$> T.splitOn "," t)
3453+
3454+instance ToJSON Search where
3455+ toJSON (Search query sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _ sFields sSource) =
3456+ omitNulls [ "query" .= query
3457+ , "filter" .= sFilter
3458+ , "sort" .= sort
3459+ , "aggregations" .= searchAggs
3460+ , "highlight" .= highlight
3461+ , "from" .= sFrom
3462+ , "size" .= sSize
3463+ , "track_scores" .= sTrackSortScores
3464+ , "fields" .= sFields
3465+ , "_source" .= sSource]
3466+
3467+
3468+instance ToJSON Source where
3469+ toJSON NoSource = toJSON False
3470+ toJSON (SourcePatterns patterns) = toJSON patterns
3471+ toJSON (SourceIncludeExclude incl excl) = object [ "include" .= incl, "exclude" .= excl ]
3472+
3473+instance ToJSON PatternOrPatterns where
3474+ toJSON (PopPattern pattern) = toJSON pattern
3475+ toJSON (PopPatterns patterns) = toJSON patterns
3476+
3477+instance ToJSON Include where
3478+ toJSON (Include patterns) = toJSON patterns
3479+
3480+instance ToJSON Exclude where
3481+ toJSON (Exclude patterns) = toJSON patterns
3482+
3483+instance ToJSON Pattern where
3484+ toJSON (Pattern pattern) = toJSON pattern
3485+
3486+
3487+instance ToJSON FieldHighlight where
3488+ toJSON (FieldHighlight (FieldName fName) (Just fSettings)) =
3489+ object [ fName .= fSettings ]
3490+ toJSON (FieldHighlight (FieldName fName) Nothing) =
3491+ object [ fName .= emptyObject ]
3492+
3493+instance ToJSON Highlights where
3494+ toJSON (Highlights global fields) =
3495+ omitNulls (("fields" .= fields)
3496+ : highlightSettingsPairs global)
3497+
3498+instance ToJSON HighlightSettings where
3499+ toJSON hs = omitNulls (highlightSettingsPairs (Just hs))
3500+
3501+highlightSettingsPairs :: Maybe HighlightSettings -> [Pair]
3502+highlightSettingsPairs Nothing = []
3503+highlightSettingsPairs (Just (Plain plh)) = plainHighPairs (Just plh)
3504+highlightSettingsPairs (Just (Postings ph)) = postHighPairs (Just ph)
3505+highlightSettingsPairs (Just (FastVector fvh)) = fastVectorHighPairs (Just fvh)
3506+
3507+
3508+plainHighPairs :: Maybe PlainHighlight -> [Pair]
3509+plainHighPairs Nothing = []
3510+plainHighPairs (Just (PlainHighlight plCom plNonPost)) =
3511+ [ "type" .= String "plain"]
3512+ ++ commonHighlightPairs plCom
3513+ ++ nonPostingsToPairs plNonPost
3514+
3515+postHighPairs :: Maybe PostingsHighlight -> [Pair]
3516+postHighPairs Nothing = []
3517+postHighPairs (Just (PostingsHighlight pCom)) =
3518+ ("type" .= String "postings")
3519+ : commonHighlightPairs pCom
3520+
3521+fastVectorHighPairs :: Maybe FastVectorHighlight -> [Pair]
3522+fastVectorHighPairs Nothing = []
3523+fastVectorHighPairs (Just
3524+ (FastVectorHighlight fvCom fvNonPostSettings fvBoundChars
3525+ fvBoundMaxScan fvFragOff fvMatchedFields
3526+ fvPhraseLim)) =
3527+ [ "type" .= String "fvh"
3528+ , "boundary_chars" .= fvBoundChars
3529+ , "boundary_max_scan" .= fvBoundMaxScan
3530+ , "fragment_offset" .= fvFragOff
3531+ , "matched_fields" .= fvMatchedFields
3532+ , "phraseLimit" .= fvPhraseLim]
3533+ ++ commonHighlightPairs fvCom
3534+ ++ nonPostingsToPairs fvNonPostSettings
3535+
3536+deleteSeveral :: (Eq k, Hashable k) => [k] -> HM.HashMap k v -> HM.HashMap k v
3537+deleteSeveral ks hm = foldr HM.delete hm ks
3538+
3539+commonHighlightPairs :: Maybe CommonHighlight -> [Pair]
3540+commonHighlightPairs Nothing = []
3541+commonHighlightPairs (Just (CommonHighlight chScore chForceSource chTag chEncoder
3542+ chNoMatchSize chHighlightQuery
3543+ chRequireFieldMatch)) =
3544+ [ "order" .= chScore
3545+ , "force_source" .= chForceSource
3546+ , "encoder" .= chEncoder
3547+ , "no_match_size" .= chNoMatchSize
3548+ , "highlight_query" .= chHighlightQuery
3549+ , "require_fieldMatch" .= chRequireFieldMatch]
3550+ ++ highlightTagToPairs chTag
3551+
3552+
3553+nonPostingsToPairs :: Maybe NonPostings -> [Pair]
3554+nonPostingsToPairs Nothing = []
3555+nonPostingsToPairs (Just (NonPostings npFragSize npNumOfFrags)) =
3556+ [ "fragment_size" .= npFragSize
3557+ , "number_of_fragments" .= npNumOfFrags]
3558+
3559+parseNEJSON :: (FromJSON a) => [Value] -> Parser (NonEmpty a)
3560+parseNEJSON [] = fail "Expected non-empty list"
3561+parseNEJSON (x:xs) = DT.mapM parseJSON (x :| xs)
3562+
3563+
3564+instance ToJSON HighlightEncoder where
3565+ toJSON DefaultEncoder = String "default"
3566+ toJSON HTMLEncoder = String "html"
3567+
3568+highlightTagToPairs :: Maybe HighlightTag -> [Pair]
3569+highlightTagToPairs (Just (TagSchema _)) = [ "scheme" .= String "default"]
3570+highlightTagToPairs (Just (CustomTags (pre, post))) = [ "pre_tags" .= pre
3571+ , "post_tags" .= post]
3572+highlightTagToPairs Nothing = []
3573+
3574+instance ToJSON SortSpec where
3575+ toJSON (DefaultSortSpec
3576+ (DefaultSort (FieldName dsSortFieldName) dsSortOrder dsIgnoreUnmapped
3577+ dsSortMode dsMissingSort dsNestedFilter)) =
3578+ object [dsSortFieldName .= omitNulls base] where
3579+ base = [ "order" .= dsSortOrder
3580+ , "ignore_unmapped" .= dsIgnoreUnmapped
3581+ , "mode" .= dsSortMode
3582+ , "missing" .= dsMissingSort
3583+ , "nested_filter" .= dsNestedFilter ]
3584+
3585+ toJSON (GeoDistanceSortSpec gdsSortOrder (GeoPoint (FieldName field) gdsLatLon) units) =
3586+ object [ "unit" .= units
3587+ , field .= gdsLatLon
3588+ , "order" .= gdsSortOrder ]
3589+
3590+
3591+instance ToJSON SortOrder where
3592+ toJSON Ascending = String "asc"
3593+ toJSON Descending = String "desc"
3594+
3595+
3596+instance ToJSON SortMode where
3597+ toJSON SortMin = String "min"
3598+ toJSON SortMax = String "max"
3599+ toJSON SortSum = String "sum"
3600+ toJSON SortAvg = String "avg"
3601+
3602+
3603+instance ToJSON Missing where
3604+ toJSON LastMissing = String "_last"
3605+ toJSON FirstMissing = String "_first"
3606+ toJSON (CustomMissing txt) = String txt
3607+
3608+
3609+instance ToJSON ScoreType where
3610+ toJSON ScoreTypeMax = "max"
3611+ toJSON ScoreTypeAvg = "avg"
3612+ toJSON ScoreTypeSum = "sum"
3613+ toJSON ScoreTypeNone = "none"
3614+
3615+instance FromJSON ScoreType where
3616+ parseJSON = withText "ScoreType" parse
3617+ where parse "max" = pure ScoreTypeMax
3618+ parse "avg" = pure ScoreTypeAvg
3619+ parse "sum" = pure ScoreTypeSum
3620+ parse "none" = pure ScoreTypeNone
3621+ parse t = fail ("Unexpected ScoreType: " <> show t)
3622+
3623+instance ToJSON Distance where
3624+ toJSON (Distance dCoefficient dUnit) =
3625+ String boltedTogether where
3626+ coefText = showText dCoefficient
3627+ (String unitText) = toJSON dUnit
3628+ boltedTogether = mappend coefText unitText
3629+
3630+instance FromJSON Distance where
3631+ parseJSON = withText "Distance" parse
3632+ where parse t = Distance <$> parseCoeff nT
3633+ <*> parseJSON (String unitT)
3634+ where (nT, unitT) = T.span validForNumber t
3635+ -- may be a better way to do this
3636+ validForNumber '-' = True
3637+ validForNumber '.' = True
3638+ validForNumber 'e' = True
3639+ validForNumber c = isNumber c
3640+ parseCoeff "" = fail "Empty string cannot be parsed as number"
3641+ parseCoeff s = return (read (T.unpack s))
3642+
3643+instance ToJSON DistanceUnit where
3644+ toJSON Miles = String "mi"
3645+ toJSON Yards = String "yd"
3646+ toJSON Feet = String "ft"
3647+ toJSON Inches = String "in"
3648+ toJSON Kilometers = String "km"
3649+ toJSON Meters = String "m"
3650+ toJSON Centimeters = String "cm"
3651+ toJSON Millimeters = String "mm"
3652+ toJSON NauticalMiles = String "nmi"
3653+
3654+
3655+instance FromJSON DistanceUnit where
3656+ parseJSON = withText "DistanceUnit" parse
3657+ where parse "mi" = pure Miles
3658+ parse "yd" = pure Yards
3659+ parse "ft" = pure Feet
3660+ parse "in" = pure Inches
3661+ parse "km" = pure Kilometers
3662+ parse "m" = pure Meters
3663+ parse "cm" = pure Centimeters
3664+ parse "mm" = pure Millimeters
3665+ parse "nmi" = pure NauticalMiles
3666+ parse u = fail ("Unrecognized DistanceUnit: " <> show u)
3667+
3668+instance ToJSON DistanceType where
3669+ toJSON Arc = String "arc"
3670+ toJSON SloppyArc = String "sloppy_arc"
3671+ toJSON Plane = String "plane"
3672+
3673+instance FromJSON DistanceType where
3674+ parseJSON = withText "DistanceType" parse
3675+ where parse "arc" = pure Arc
3676+ parse "sloppy_arc" = pure SloppyArc
3677+ parse "plane" = pure Plane
3678+ parse t = fail ("Unrecognized DistanceType: " <> show t)
3679+
3680+
3681+instance ToJSON OptimizeBbox where
3682+ toJSON NoOptimizeBbox = String "none"
3683+ toJSON (OptimizeGeoFilterType gft) = toJSON gft
3684+
3685+instance FromJSON OptimizeBbox where
3686+ parseJSON v = withText "NoOptimizeBbox" parseNoOptimize v
3687+ <|> parseOptimize v
3688+ where parseNoOptimize "none" = pure NoOptimizeBbox
3689+ parseNoOptimize _ = mzero
3690+ parseOptimize = fmap OptimizeGeoFilterType . parseJSON
3691+
3692+instance ToJSON GeoBoundingBoxConstraint where
3693+ toJSON (GeoBoundingBoxConstraint
3694+ (FieldName gbbcGeoBBField) gbbcConstraintBox cache type') =
3695+ object [gbbcGeoBBField .= gbbcConstraintBox
3696+ , "_cache" .= cache
3697+ , "type" .= type']
3698+
3699+instance FromJSON GeoBoundingBoxConstraint where
3700+ parseJSON = withObject "GeoBoundingBoxConstraint" parse
3701+ where parse o = case HM.toList (deleteSeveral ["type", "_cache"] o) of
3702+ [(fn, v)] -> GeoBoundingBoxConstraint (FieldName fn)
3703+ <$> parseJSON v
3704+ <*> o .:? "_cache" .!= defaultCache
3705+ <*> o .: "type"
3706+ _ -> fail "Could not find field name for GeoBoundingBoxConstraint"
3707+
3708+instance ToJSON GeoFilterType where
3709+ toJSON GeoFilterMemory = String "memory"
3710+ toJSON GeoFilterIndexed = String "indexed"
3711+
3712+instance FromJSON GeoFilterType where
3713+ parseJSON = withText "GeoFilterType" parse
3714+ where parse "memory" = pure GeoFilterMemory
3715+ parse "indexed" = pure GeoFilterIndexed
3716+ parse t = fail ("Unrecognized GeoFilterType: " <> show t)
3717+
3718+instance ToJSON GeoBoundingBox where
3719+ toJSON (GeoBoundingBox gbbTopLeft gbbBottomRight) =
3720+ object ["top_left" .= gbbTopLeft
3721+ , "bottom_right" .= gbbBottomRight]
3722+
3723+instance FromJSON GeoBoundingBox where
3724+ parseJSON = withObject "GeoBoundingBox" parse
3725+ where parse o = GeoBoundingBox
3726+ <$> o .: "top_left"
3727+ <*> o .: "bottom_right"
3728+
3729+instance ToJSON LatLon where
3730+ toJSON (LatLon lLat lLon) =
3731+ object ["lat" .= lLat
3732+ , "lon" .= lLon]
3733+
3734+instance FromJSON LatLon where
3735+ parseJSON = withObject "LatLon" parse
3736+ where parse o = LatLon <$> o .: "lat"
3737+ <*> o .: "lon"
3738+
3739+-- index for smaller ranges, fielddata for longer ranges
3740+instance ToJSON RangeExecution where
3741+ toJSON RangeExecutionIndex = "index"
3742+ toJSON RangeExecutionFielddata = "fielddata"
3743+
3744+
3745+instance FromJSON RangeExecution where
3746+ parseJSON = withText "RangeExecution" parse
3747+ where parse "index" = pure RangeExecutionIndex
3748+ parse "fielddata" = pure RangeExecutionFielddata
3749+ parse t = error ("Unrecognized RangeExecution " <> show t)
3750+
3751+instance ToJSON RegexpFlags where
3752+ toJSON AllRegexpFlags = String "ALL"
3753+ toJSON NoRegexpFlags = String "NONE"
3754+ toJSON (SomeRegexpFlags (h :| fs)) = String $ T.intercalate "|" flagStrs
3755+ where flagStrs = map flagStr . nub $ h:fs
3756+ flagStr AnyString = "ANYSTRING"
3757+ flagStr Automaton = "AUTOMATON"
3758+ flagStr Complement = "COMPLEMENT"
3759+ flagStr Empty = "EMPTY"
3760+ flagStr Intersection = "INTERSECTION"
3761+ flagStr Interval = "INTERVAL"
3762+
3763+instance FromJSON RegexpFlags where
3764+ parseJSON = withText "RegexpFlags" parse
3765+ where parse "ALL" = pure AllRegexpFlags
3766+ parse "NONE" = pure NoRegexpFlags
3767+ parse t = SomeRegexpFlags <$> parseNEJSON (String <$> T.splitOn "|" t)
3768+
3769+instance FromJSON RegexpFlag where
3770+ parseJSON = withText "RegexpFlag" parse
3771+ where parse "ANYSTRING" = pure AnyString
3772+ parse "AUTOMATON" = pure Automaton
3773+ parse "COMPLEMENT" = pure Complement
3774+ parse "EMPTY" = pure Empty
3775+ parse "INTERSECTION" = pure Intersection
3776+ parse "INTERVAL" = pure Interval
3777+ parse f = fail ("Unknown RegexpFlag: " <> show f)
3778+
3779+instance ToJSON Term where
3780+ toJSON (Term field value) = object ["term" .= object
3781+ [field .= value]]
3782+
3783+instance FromJSON Term where
3784+ parseJSON = withObject "Term" parse
3785+ where parse o = do termObj <- o .: "term"
3786+ case HM.toList termObj of
3787+ [(fn, v)] -> Term fn <$> parseJSON v
3788+ _ -> fail "Expected object with 1 field-named key"
3789+
3790+instance ToJSON BoolMatch where
3791+ toJSON (MustMatch term cache) = object ["must" .= term,
3792+ "_cache" .= cache]
3793+ toJSON (MustNotMatch term cache) = object ["must_not" .= term,
3794+ "_cache" .= cache]
3795+ toJSON (ShouldMatch terms cache) = object ["should" .= fmap toJSON terms,
3796+ "_cache" .= cache]
3797+
3798+instance FromJSON BoolMatch where
3799+ parseJSON = withObject "BoolMatch" parse
3800+ where parse o = mustMatch `taggedWith` "must"
3801+ <|> mustNotMatch `taggedWith` "must_not"
3802+ <|> shouldMatch `taggedWith` "should"
3803+ where taggedWith parser k = parser =<< o .: k
3804+ mustMatch t = MustMatch t <$> o .:? "_cache" .!= defaultCache
3805+ mustNotMatch t = MustNotMatch t <$> o .:? "_cache" .!= defaultCache
3806+ shouldMatch t = ShouldMatch t <$> o .:? "_cache" .!= defaultCache
3807+
3808+instance (FromJSON a) => FromJSON (SearchResult a) where
3809+ parseJSON (Object v) = SearchResult <$>
3810+ v .: "took" <*>
3811+ v .: "timed_out" <*>
3812+ v .: "_shards" <*>
3813+ v .: "hits" <*>
3814+ v .:? "aggregations" <*>
3815+ v .:? "_scroll_id"
3816+ parseJSON _ = empty
3817+
3818+instance (FromJSON a) => FromJSON (SearchHits a) where
3819+ parseJSON (Object v) = SearchHits <$>
3820+ v .: "total" <*>
3821+ v .: "max_score" <*>
3822+ v .: "hits"
3823+ parseJSON _ = empty
3824+
3825+instance (FromJSON a) => FromJSON (Hit a) where
3826+ parseJSON (Object v) = Hit <$>
3827+ v .: "_index" <*>
3828+ v .: "_type" <*>
3829+ v .: "_id" <*>
3830+ v .: "_score" <*>
3831+ v .:? "_source" <*>
3832+ v .:? "highlight"
3833+ parseJSON _ = empty
3834+
3835+instance FromJSON ShardResult where
3836+ parseJSON (Object v) = ShardResult <$>
3837+ v .: "total" <*>
3838+ v .: "successful" <*>
3839+ v .: "failed"
3840+ parseJSON _ = empty
3841+
3842+
3843+instance FromJSON DocVersion where
3844+ parseJSON v = do
3845+ i <- parseJSON v
3846+ maybe (fail "DocVersion out of range") return $ mkDocVersion i
3847+
3848+-- This insanity is because ES *sometimes* returns Replica/Shard counts as strings
3849+instance FromJSON ReplicaCount where
3850+ parseJSON v = parseAsInt v
3851+ <|> parseAsString v
3852+ where parseAsInt = fmap ReplicaCount . parseJSON
3853+ parseAsString = withText "ReplicaCount" (fmap ReplicaCount . parseReadText)
3854+
3855+instance FromJSON ShardCount where
3856+ parseJSON v = parseAsInt v
3857+ <|> parseAsString v
3858+ where parseAsInt = fmap ShardCount . parseJSON
3859+ parseAsString = withText "ShardCount" (fmap ShardCount . parseReadText)
3860+
3861+instance Bounded DocVersion where
3862+ minBound = DocVersion 1
3863+ maxBound = DocVersion 9200000000000000000 -- 9.2e+18
3864+
3865+instance Enum DocVersion where
3866+ succ x
3867+ | x /= maxBound = DocVersion (succ $ docVersionNumber x)
3868+ | otherwise = succError "DocVersion"
3869+ pred x
3870+ | x /= minBound = DocVersion (pred $ docVersionNumber x)
3871+ | otherwise = predError "DocVersion"
3872+ toEnum i =
3873+ fromMaybe (error $ show i ++ " out of DocVersion range") $ mkDocVersion i
3874+ fromEnum = docVersionNumber
3875+ enumFrom = boundedEnumFrom
3876+ enumFromThen = boundedEnumFromThen
3877+
3878+-- | Username type used for HTTP Basic authentication. See 'basicAuthHook'.
3879+newtype EsUsername = EsUsername { esUsername :: Text } deriving (Read, Show, Eq)
3880+
3881+-- | Password type used for HTTP Basic authentication. See 'basicAuthHook'.
3882+newtype EsPassword = EsPassword { esPassword :: Text } deriving (Read, Show, Eq)
3883+
3884+
3885+data SnapshotRepoSelection = SnapshotRepoList (NonEmpty SnapshotRepoPattern)
3886+ | AllSnapshotRepos deriving (Eq, Generic, Show, Typeable)
3887+
3888+
3889+-- | Either specifies an exact repo name or one with globs in it,
3890+-- e.g. @RepoPattern "foo*"@ __NOTE__: Patterns are not supported on ES < 1.7
3891+data SnapshotRepoPattern = ExactRepo SnapshotRepoName
3892+ | RepoPattern Text
3893+ deriving (Eq, Generic, Show, Typeable)
3894+
3895+-- | The unique name of a snapshot repository.
3896+newtype SnapshotRepoName = SnapshotRepoName { snapshotRepoName :: Text }
3897+ deriving (Eq, Ord, Generic, Show, Typeable, ToJSON, FromJSON)
3898+
3899+
3900+-- | A generic representation of a snapshot repo. This is what gets
3901+-- sent to and parsed from the server. For repo types enabled by
3902+-- plugins that aren't exported by this library, consider making a
3903+-- custom type which implements 'SnapshotRepo'. If it is a common repo
3904+-- type, consider submitting a pull request to have it included in the
3905+-- library proper
3906+data GenericSnapshotRepo = GenericSnapshotRepo {
3907+ gSnapshotRepoName :: SnapshotRepoName
3908+ , gSnapshotRepoType :: SnapshotRepoType
3909+ , gSnapshotRepoSettings :: GenericSnapshotRepoSettings
3910+ } deriving (Eq, Generic, Show, Typeable)
3911+
3912+
3913+instance SnapshotRepo GenericSnapshotRepo where
3914+ toGSnapshotRepo = id
3915+ fromGSnapshotRepo = Right
3916+
3917+
3918+newtype SnapshotRepoType = SnapshotRepoType { snapshotRepoType :: Text }
3919+ deriving (Eq, Ord, Generic, Show, Typeable, ToJSON, FromJSON)
3920+
3921+
3922+-- | Opaque representation of snapshot repo settings. Instances of
3923+-- 'SnapshotRepo' will produce this.
3924+newtype GenericSnapshotRepoSettings = GenericSnapshotRepoSettings { gSnapshotRepoSettingsObject :: Object }
3925+ deriving (Eq, Generic, Show, Typeable, ToJSON)
3926+
3927+
3928+ -- Regardless of whether you send strongly typed json, my version of
3929+ -- ES sends back stringly typed json in the settings, e.g. booleans
3930+ -- as strings, so we'll try to convert them.
3931+instance FromJSON GenericSnapshotRepoSettings where
3932+ parseJSON = fmap (GenericSnapshotRepoSettings . fmap unStringlyTypeJSON). parseJSON
3933+
3934+-- | The result of running 'verifySnapshotRepo'.
3935+newtype SnapshotVerification = SnapshotVerification { snapshotNodeVerifications :: [SnapshotNodeVerification] }
3936+ deriving (Eq, Generic, Show, Typeable)
3937+
3938+
3939+instance FromJSON SnapshotVerification where
3940+ parseJSON = withObject "SnapshotVerification" parse
3941+ where
3942+ parse o = do
3943+ o2 <- o .: "nodes"
3944+ SnapshotVerification <$> mapM (uncurry parse') (HM.toList o2)
3945+ parse' rawFullId = withObject "SnapshotNodeVerification" $ \o ->
3946+ SnapshotNodeVerification (FullNodeId rawFullId) <$> o .: "name"
3947+
3948+
3949+-- | A node that has verified a snapshot
3950+data SnapshotNodeVerification = SnapshotNodeVerification {
3951+ snvFullId :: FullNodeId
3952+ , snvNodeName :: NodeName
3953+ } deriving (Eq, Generic, Show, Typeable)
3954+
3955+
3956+-- | Unique, automatically-generated name assigned to nodes that are
3957+-- usually returned in node-oriented APIs.
3958+newtype FullNodeId = FullNodeId { fullNodeId :: Text }
3959+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
3960+
3961+
3962+-- | A human-readable node name that is supplied by the user in the
3963+-- node config or automatically generated by ElasticSearch.
3964+newtype NodeName = NodeName { nodeName :: Text }
3965+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
3966+
3967+newtype ClusterName = ClusterName { clusterName :: Text }
3968+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
3969+
3970+data NodesInfo = NodesInfo {
3971+ nodesInfo :: [NodeInfo]
3972+ , nodesClusterName :: ClusterName
3973+ } deriving (Eq, Show, Generic, Typeable)
3974+
3975+data NodesStats = NodesStats {
3976+ nodesStats :: [NodeStats]
3977+ , nodesStatsClusterName :: ClusterName
3978+ } deriving (Eq, Show, Generic, Typeable)
3979+
3980+data NodeStats = NodeStats {
3981+ nodeStatsName :: NodeName
3982+ , nodeStatsFullId :: FullNodeId
3983+ , nodeStatsBreakersStats :: Maybe NodeBreakersStats
3984+ , nodeStatsHTTP :: NodeHTTPStats
3985+ , nodeStatsTransport :: NodeTransportStats
3986+ , nodeStatsFS :: NodeFSStats
3987+ , nodeStatsNetwork :: NodeNetworkStats
3988+ , nodeStatsThreadPool :: NodeThreadPoolsStats
3989+ , nodeStatsJVM :: NodeJVMStats
3990+ , nodeStatsProcess :: NodeProcessStats
3991+ , nodeStatsOS :: NodeOSStats
3992+ , nodeStatsIndices :: NodeIndicesStats
3993+ } deriving (Eq, Show, Generic, Typeable)
3994+
3995+data NodeBreakersStats = NodeBreakersStats {
3996+ nodeStatsParentBreaker :: NodeBreakerStats
3997+ , nodeStatsRequestBreaker :: NodeBreakerStats
3998+ , nodeStatsFieldDataBreaker :: NodeBreakerStats
3999+ } deriving (Eq, Show, Generic, Typeable)
4000+
4001+data NodeBreakerStats = NodeBreakerStats {
4002+ nodeBreakersTripped :: Int
4003+ , nodeBreakersOverhead :: Double
4004+ , nodeBreakersEstSize :: Bytes
4005+ , nodeBreakersLimitSize :: Bytes
4006+ } deriving (Eq, Show, Generic, Typeable)
4007+
4008+data NodeHTTPStats = NodeHTTPStats {
4009+ nodeHTTPTotalOpened :: Int
4010+ , nodeHTTPCurrentOpen :: Int
4011+ } deriving (Eq, Show, Generic, Typeable)
4012+
4013+data NodeTransportStats = NodeTransportStats {
4014+ nodeTransportTXSize :: Bytes
4015+ , nodeTransportCount :: Int
4016+ , nodeTransportRXSize :: Bytes
4017+ , nodeTransportRXCount :: Int
4018+ , nodeTransportServerOpen :: Int
4019+ } deriving (Eq, Show, Generic, Typeable)
4020+
4021+data NodeFSStats = NodeFSStats {
4022+ nodeFSDataPaths :: [NodeDataPathStats]
4023+ , nodeFSTotal :: NodeFSTotalStats
4024+ , nodeFSTimestamp :: UTCTime
4025+ } deriving (Eq, Show, Generic, Typeable)
4026+
4027+data NodeDataPathStats = NodeDataPathStats {
4028+ nodeDataPathDiskServiceTime :: Maybe Double
4029+ , nodeDataPathDiskQueue :: Maybe Double
4030+ , nodeDataPathIOSize :: Maybe Bytes
4031+ , nodeDataPathWriteSize :: Maybe Bytes
4032+ , nodeDataPathReadSize :: Maybe Bytes
4033+ , nodeDataPathIOOps :: Maybe Int
4034+ , nodeDataPathWrites :: Maybe Int
4035+ , nodeDataPathReads :: Maybe Int
4036+ , nodeDataPathAvailable :: Bytes
4037+ , nodeDataPathFree :: Bytes
4038+ , nodeDataPathTotal :: Bytes
4039+ , nodeDataPathType :: Maybe Text
4040+ , nodeDataPathDevice :: Text
4041+ , nodeDataPathMount :: Text
4042+ , nodeDataPathPath :: Text
4043+ } deriving (Eq, Show, Generic, Typeable)
4044+
4045+data NodeFSTotalStats = NodeFSTotalStats {
4046+ nodeFSTotalDiskServiceTime :: Maybe Double
4047+ , nodeFSTotalDiskQueue :: Maybe Double
4048+ , nodeFSTotalIOSize :: Maybe Bytes
4049+ , nodeFSTotalWriteSize :: Maybe Bytes
4050+ , nodeFSTotalReadSize :: Maybe Bytes
4051+ , nodeFSTotalIOOps :: Maybe Int
4052+ , nodeFSTotalWrites :: Maybe Int
4053+ , nodeFSTotalReads :: Maybe Int
4054+ , nodeFSTotalAvailable :: Bytes
4055+ , nodeFSTotalFree :: Bytes
4056+ , nodeFSTotalTotal :: Bytes
4057+ } deriving (Eq, Show, Generic, Typeable)
4058+
4059+data NodeNetworkStats = NodeNetworkStats {
4060+ nodeNetTCPOutRSTs :: Int
4061+ , nodeNetTCPInErrs :: Int
4062+ , nodeNetTCPAttemptFails :: Int
4063+ , nodeNetTCPEstabResets :: Int
4064+ , nodeNetTCPRetransSegs :: Int
4065+ , nodeNetTCPOutSegs :: Int
4066+ , nodeNetTCPInSegs :: Int
4067+ , nodeNetTCPCurrEstab :: Int
4068+ , nodeNetTCPPassiveOpens :: Int
4069+ , nodeNetTCPActiveOpens :: Int
4070+ } deriving (Eq, Show, Generic, Typeable)
4071+
4072+data NodeThreadPoolsStats = NodeThreadPoolsStats {
4073+ nodeThreadPoolsStatsSnapshot :: NodeThreadPoolStats
4074+ , nodeThreadPoolsStatsBulk :: NodeThreadPoolStats
4075+ , nodeThreadPoolsStatsMerge :: NodeThreadPoolStats
4076+ , nodeThreadPoolsStatsGet :: NodeThreadPoolStats
4077+ , nodeThreadPoolsStatsManagement :: NodeThreadPoolStats
4078+ , nodeThreadPoolsStatsFetchShardStore :: Maybe NodeThreadPoolStats
4079+ , nodeThreadPoolsStatsOptimize :: NodeThreadPoolStats
4080+ , nodeThreadPoolsStatsFlush :: NodeThreadPoolStats
4081+ , nodeThreadPoolsStatsSearch :: NodeThreadPoolStats
4082+ , nodeThreadPoolsStatsWarmer :: NodeThreadPoolStats
4083+ , nodeThreadPoolsStatsGeneric :: NodeThreadPoolStats
4084+ , nodeThreadPoolsStatsSuggest :: NodeThreadPoolStats
4085+ , nodeThreadPoolsStatsRefresh :: NodeThreadPoolStats
4086+ , nodeThreadPoolsStatsIndex :: NodeThreadPoolStats
4087+ , nodeThreadPoolsStatsListener :: Maybe NodeThreadPoolStats
4088+ , nodeThreadPoolsStatsFetchShardStarted :: Maybe NodeThreadPoolStats
4089+ , nodeThreadPoolsStatsPercolate :: NodeThreadPoolStats
4090+ } deriving (Eq, Show, Generic, Typeable)
4091+
4092+data NodeThreadPoolStats = NodeThreadPoolStats {
4093+ nodeThreadPoolCompleted :: Int
4094+ , nodeThreadPoolLargest :: Int
4095+ , nodeThreadPoolRejected :: Int
4096+ , nodeThreadPoolActive :: Int
4097+ , nodeThreadPoolQueue :: Int
4098+ , nodeThreadPoolThreads :: Int
4099+ } deriving (Eq, Show, Generic, Typeable)
4100+
4101+data NodeJVMStats = NodeJVMStats {
4102+ nodeJVMStatsMappedBufferPool :: JVMBufferPoolStats
4103+ , nodeJVMStatsDirectBufferPool :: JVMBufferPoolStats
4104+ , nodeJVMStatsGCOldCollector :: JVMGCStats
4105+ , nodeJVMStatsGCYoungCollector :: JVMGCStats
4106+ , nodeJVMStatsPeakThreadsCount :: Int
4107+ , nodeJVMStatsThreadsCount :: Int
4108+ , nodeJVMStatsOldPool :: JVMPoolStats
4109+ , nodeJVMStatsSurvivorPool :: JVMPoolStats
4110+ , nodeJVMStatsYoungPool :: JVMPoolStats
4111+ , nodeJVMStatsNonHeapCommitted :: Bytes
4112+ , nodeJVMStatsNonHeapUsed :: Bytes
4113+ , nodeJVMStatsHeapMax :: Bytes
4114+ , nodeJVMStatsHeapCommitted :: Bytes
4115+ , nodeJVMStatsHeapUsedPercent :: Int
4116+ , nodeJVMStatsHeapUsed :: Bytes
4117+ , nodeJVMStatsUptime :: NominalDiffTime
4118+ , nodeJVMStatsTimestamp :: UTCTime
4119+ } deriving (Eq, Show, Generic, Typeable)
4120+
4121+data JVMBufferPoolStats = JVMBufferPoolStats {
4122+ jvmBufferPoolStatsTotalCapacity :: Bytes
4123+ , jvmBufferPoolStatsUsed :: Bytes
4124+ , jvmBufferPoolStatsCount :: Int
4125+ } deriving (Eq, Show, Generic, Typeable)
4126+
4127+data JVMGCStats = JVMGCStats {
4128+ jvmGCStatsCollectionTime :: NominalDiffTime
4129+ , jvmGCStatsCollectionCount :: Int
4130+ } deriving (Eq, Show, Generic, Typeable)
4131+
4132+data JVMPoolStats = JVMPoolStats {
4133+ jvmPoolStatsPeakMax :: Bytes
4134+ , jvmPoolStatsPeakUsed :: Bytes
4135+ , jvmPoolStatsMax :: Bytes
4136+ , jvmPoolStatsUsed :: Bytes
4137+ } deriving (Eq, Show, Generic, Typeable)
4138+
4139+data NodeProcessStats = NodeProcessStats {
4140+ nodeProcessMemTotalVirtual :: Bytes
4141+ , nodeProcessMemShare :: Bytes
4142+ , nodeProcessMemResident :: Bytes
4143+ , nodeProcessCPUTotal :: NominalDiffTime
4144+ , nodeProcessCPUUser :: NominalDiffTime
4145+ , nodeProcessCPUSys :: NominalDiffTime
4146+ , nodeProcessCPUPercent :: Int
4147+ , nodeProcessOpenFDs :: Int
4148+ , nodeProcessTimestamp :: UTCTime
4149+ } deriving (Eq, Show, Generic, Typeable)
4150+
4151+data NodeOSStats = NodeOSStats {
4152+ nodeOSSwapFree :: Bytes
4153+ , nodeOSSwapUsed :: Bytes
4154+ , nodeOSMemActualUsed :: Bytes
4155+ , nodeOSMemActualFree :: Bytes
4156+ , nodeOSMemUsedPercent :: Int
4157+ , nodeOSMemFreePercent :: Int
4158+ , nodeOSMemUsed :: Bytes
4159+ , nodeOSMemFree :: Bytes
4160+ , nodeOSCPUStolen :: Int
4161+ , nodeOSCPUUsage :: Int
4162+ , nodeOSCPUIdle :: Int
4163+ , nodeOSCPUUser :: Int
4164+ , nodeOSCPUSys :: Int
4165+ , nodeOSLoad :: Maybe LoadAvgs
4166+ , nodeOSUptime :: NominalDiffTime
4167+ , nodeOSTimestamp :: UTCTime
4168+ } deriving (Eq, Show, Generic, Typeable)
4169+
4170+data LoadAvgs = LoadAvgs {
4171+ loadAvg1Min :: Double
4172+ , loadAvg5Min :: Double
4173+ , loadAvg15Min :: Double
4174+ } deriving (Eq, Show, Generic, Typeable)
4175+
4176+data NodeIndicesStats = NodeIndicesStats {
4177+ nodeIndicesStatsRecoveryThrottleTime :: Maybe NominalDiffTime
4178+ , nodeIndicesStatsRecoveryCurrentAsTarget :: Maybe Int
4179+ , nodeIndicesStatsRecoveryCurrentAsSource :: Maybe Int
4180+ , nodeIndicesStatsQueryCacheMisses :: Maybe Int
4181+ , nodeIndicesStatsQueryCacheHits :: Maybe Int
4182+ , nodeIndicesStatsQueryCacheEvictions :: Maybe Int
4183+ , nodeIndicesStatsQueryCacheSize :: Maybe Bytes
4184+ , nodeIndicesStatsSuggestCurrent :: Int
4185+ , nodeIndicesStatsSuggestTime :: NominalDiffTime
4186+ , nodeIndicesStatsSuggestTotal :: Int
4187+ , nodeIndicesStatsTranslogSize :: Bytes
4188+ , nodeIndicesStatsTranslogOps :: Int
4189+ , nodeIndicesStatsSegFixedBitSetMemory :: Maybe Bytes
4190+ , nodeIndicesStatsSegVersionMapMemory :: Bytes
4191+ , nodeIndicesStatsSegIndexWriterMaxMemory :: Maybe Bytes
4192+ , nodeIndicesStatsSegIndexWriterMemory :: Bytes
4193+ , nodeIndicesStatsSegMemory :: Bytes
4194+ , nodeIndicesStatsSegCount :: Int
4195+ , nodeIndicesStatsCompletionSize :: Bytes
4196+ , nodeIndicesStatsPercolateQueries :: Int
4197+ , nodeIndicesStatsPercolateMemory :: Bytes
4198+ , nodeIndicesStatsPercolateCurrent :: Int
4199+ , nodeIndicesStatsPercolateTime :: NominalDiffTime
4200+ , nodeIndicesStatsPercolateTotal :: Int
4201+ , nodeIndicesStatsFieldDataEvictions :: Int
4202+ , nodeIndicesStatsFieldDataMemory :: Bytes
4203+ , nodeIndicesStatsIDCacheMemory :: Bytes
4204+ , nodeIndicesStatsFilterCacheEvictions :: Int
4205+ , nodeIndicesStatsFilterCacheMemory :: Bytes
4206+ , nodeIndicesStatsWarmerTotalTime :: NominalDiffTime
4207+ , nodeIndicesStatsWarmerTotal :: Int
4208+ , nodeIndicesStatsWarmerCurrent :: Int
4209+ , nodeIndicesStatsFlushTotalTime :: NominalDiffTime
4210+ , nodeIndicesStatsFlushTotal :: Int
4211+ , nodeIndicesStatsRefreshTotalTime :: NominalDiffTime
4212+ , nodeIndicesStatsRefreshTotal :: Int
4213+ , nodeIndicesStatsMergesTotalSize :: Bytes
4214+ , nodeIndicesStatsMergesTotalDocs :: Int
4215+ , nodeIndicesStatsMergesTotalTime :: NominalDiffTime
4216+ , nodeIndicesStatsMergesTotal :: Int
4217+ , nodeIndicesStatsMergesCurrentSize :: Bytes
4218+ , nodeIndicesStatsMergesCurrentDocs :: Int
4219+ , nodeIndicesStatsMergesCurrent :: Int
4220+ , nodeIndicesStatsSearchFetchCurrent :: Int
4221+ , nodeIndicesStatsSearchFetchTime :: NominalDiffTime
4222+ , nodeIndicesStatsSearchFetchTotal :: Int
4223+ , nodeIndicesStatsSearchQueryCurrent :: Int
4224+ , nodeIndicesStatsSearchQueryTime :: NominalDiffTime
4225+ , nodeIndicesStatsSearchQueryTotal :: Int
4226+ , nodeIndicesStatsSearchOpenContexts :: Int
4227+ , nodeIndicesStatsGetCurrent :: Int
4228+ , nodeIndicesStatsGetMissingTime :: NominalDiffTime
4229+ , nodeIndicesStatsGetMissingTotal :: Int
4230+ , nodeIndicesStatsGetExistsTime :: NominalDiffTime
4231+ , nodeIndicesStatsGetExistsTotal :: Int
4232+ , nodeIndicesStatsGetTime :: NominalDiffTime
4233+ , nodeIndicesStatsGetTotal :: Int
4234+ , nodeIndicesStatsIndexingThrottleTime :: Maybe NominalDiffTime
4235+ , nodeIndicesStatsIndexingIsThrottled :: Maybe Bool
4236+ , nodeIndicesStatsIndexingNoopUpdateTotal :: Maybe Int
4237+ , nodeIndicesStatsIndexingDeleteCurrent :: Int
4238+ , nodeIndicesStatsIndexingDeleteTime :: NominalDiffTime
4239+ , nodeIndicesStatsIndexingDeleteTotal :: Int
4240+ , nodeIndicesStatsIndexingIndexCurrent :: Int
4241+ , nodeIndicesStatsIndexingIndexTime :: NominalDiffTime
4242+ , nodeIndicesStatsIndexingTotal :: Int
4243+ , nodeIndicesStatsStoreThrottleTime :: NominalDiffTime
4244+ , nodeIndicesStatsStoreSize :: Bytes
4245+ , nodeIndicesStatsDocsDeleted :: Int
4246+ , nodeIndicesStatsDocsCount :: Int
4247+ } deriving (Eq, Show, Generic, Typeable)
4248+
4249+-- | A quirky address format used throughout ElasticSearch. An example
4250+-- would be inet[/1.1.1.1:9200]. inet may be a placeholder for a
4251+-- <https://en.wikipedia.org/wiki/Fully_qualified_domain_name FQDN>.
4252+newtype EsAddress = EsAddress { esAddress :: Text }
4253+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
4254+
4255+-- | Typically a 7 character hex string.
4256+newtype BuildHash = BuildHash { buildHash :: Text }
4257+ deriving (Eq, Ord, Generic, Read, Show, Typeable, FromJSON, ToJSON)
4258+
4259+newtype PluginName = PluginName { pluginName :: Text }
4260+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
4261+
4262+data NodeInfo = NodeInfo {
4263+ nodeInfoHTTPAddress :: EsAddress
4264+ , nodeInfoBuild :: BuildHash
4265+ , nodeInfoESVersion :: VersionNumber
4266+ , nodeInfoIP :: Server
4267+ , nodeInfoHost :: Server
4268+ , nodeInfoTransportAddress :: EsAddress
4269+ , nodeInfoName :: NodeName
4270+ , nodeInfoFullId :: FullNodeId
4271+ , nodeInfoPlugins :: [NodePluginInfo]
4272+ , nodeInfoHTTP :: NodeHTTPInfo
4273+ , nodeInfoTransport :: NodeTransportInfo
4274+ , nodeInfoNetwork :: NodeNetworkInfo
4275+ , nodeInfoThreadPool :: NodeThreadPoolsInfo
4276+ , nodeInfoJVM :: NodeJVMInfo
4277+ , nodeInfoProcess :: NodeProcessInfo
4278+ , nodeInfoOS :: NodeOSInfo
4279+ , nodeInfoSettings :: Object
4280+ -- ^ The members of the settings objects are not consistent,
4281+ -- dependent on plugins, etc.
4282+ } deriving (Eq, Show, Generic, Typeable)
4283+
4284+data NodePluginInfo = NodePluginInfo {
4285+ nodePluginSite :: Bool
4286+ -- ^ Is this a site plugin?
4287+ , nodePluginJVM :: Bool
4288+ -- ^ Is this plugin running on the JVM
4289+ , nodePluginDescription :: Text
4290+ , nodePluginVersion :: MaybeNA VersionNumber
4291+ , nodePluginName :: PluginName
4292+ } deriving (Eq, Show, Generic, Typeable)
4293+
4294+data NodeHTTPInfo = NodeHTTPInfo {
4295+ nodeHTTPMaxContentLength :: Bytes
4296+ , nodeHTTPTransportAddress :: BoundTransportAddress
4297+ } deriving (Eq, Show, Generic, Typeable)
4298+
4299+data NodeTransportInfo = NodeTransportInfo {
4300+ nodeTransportProfiles :: [BoundTransportAddress]
4301+ , nodeTransportAddress :: BoundTransportAddress
4302+ } deriving (Eq, Show, Generic, Typeable)
4303+
4304+data BoundTransportAddress = BoundTransportAddress {
4305+ publishAddress :: EsAddress
4306+ , boundAddress :: EsAddress
4307+ } deriving (Eq, Show, Generic, Typeable)
4308+
4309+data NodeNetworkInfo = NodeNetworkInfo {
4310+ nodeNetworkPrimaryInterface :: NodeNetworkInterface
4311+ , nodeNetworkRefreshInterval :: NominalDiffTime
4312+ } deriving (Eq, Show, Generic, Typeable)
4313+
4314+newtype MacAddress = MacAddress { macAddress :: Text }
4315+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
4316+
4317+newtype NetworkInterfaceName = NetworkInterfaceName { networkInterfaceName :: Text }
4318+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
4319+
4320+data NodeNetworkInterface = NodeNetworkInterface {
4321+ nodeNetIfaceMacAddress :: MacAddress
4322+ , nodeNetIfaceName :: NetworkInterfaceName
4323+ , nodeNetIfaceAddress :: Server
4324+ } deriving (Eq, Show, Generic, Typeable)
4325+
4326+data NodeThreadPoolsInfo = NodeThreadPoolsInfo {
4327+ nodeThreadPoolsRefresh :: NodeThreadPoolInfo
4328+ , nodeThreadPoolsManagement :: NodeThreadPoolInfo
4329+ , nodeThreadPoolsPercolate :: NodeThreadPoolInfo
4330+ , nodeThreadPoolsListener :: Maybe NodeThreadPoolInfo
4331+ , nodeThreadPoolsFetchShardStarted :: Maybe NodeThreadPoolInfo
4332+ , nodeThreadPoolsSearch :: NodeThreadPoolInfo
4333+ , nodeThreadPoolsFlush :: NodeThreadPoolInfo
4334+ , nodeThreadPoolsWarmer :: NodeThreadPoolInfo
4335+ , nodeThreadPoolsOptimize :: NodeThreadPoolInfo
4336+ , nodeThreadPoolsBulk :: NodeThreadPoolInfo
4337+ , nodeThreadPoolsSuggest :: NodeThreadPoolInfo
4338+ , nodeThreadPoolsMerge :: NodeThreadPoolInfo
4339+ , nodeThreadPoolsSnapshot :: NodeThreadPoolInfo
4340+ , nodeThreadPoolsGet :: NodeThreadPoolInfo
4341+ , nodeThreadPoolsFetchShardStore :: Maybe NodeThreadPoolInfo
4342+ , nodeThreadPoolsIndex :: NodeThreadPoolInfo
4343+ , nodeThreadPoolsGeneric :: NodeThreadPoolInfo
4344+ } deriving (Eq, Show, Generic, Typeable)
4345+
4346+data NodeThreadPoolInfo = NodeThreadPoolInfo {
4347+ nodeThreadPoolQueueSize :: ThreadPoolSize
4348+ , nodeThreadPoolKeepalive :: Maybe NominalDiffTime
4349+ , nodeThreadPoolMin :: Maybe Int
4350+ , nodeThreadPoolMax :: Maybe Int
4351+ , nodeThreadPoolType :: ThreadPoolType
4352+ } deriving (Eq, Show, Generic, Typeable)
4353+
4354+data ThreadPoolSize = ThreadPoolBounded Int
4355+ | ThreadPoolUnbounded
4356+ deriving (Eq, Show, Generic, Typeable)
4357+
4358+data ThreadPoolType = ThreadPoolScaling
4359+ | ThreadPoolFixed
4360+ | ThreadPoolCached
4361+ deriving (Eq, Show, Generic, Typeable)
4362+
4363+data NodeJVMInfo = NodeJVMInfo {
4364+ nodeJVMInfoMemoryPools :: [JVMMemoryPool]
4365+ , nodeJVMInfoMemoryPoolsGCCollectors :: [JVMGCCollector]
4366+ , nodeJVMInfoMemoryInfo :: JVMMemoryInfo
4367+ , nodeJVMInfoStartTime :: UTCTime
4368+ , nodeJVMInfoVMVendor :: Text
4369+ , nodeJVMVMVersion :: VersionNumber
4370+ -- ^ JVM doesn't seme to follow normal version conventions
4371+ , nodeJVMVMName :: Text
4372+ , nodeJVMVersion :: VersionNumber
4373+ , nodeJVMPID :: PID
4374+ } deriving (Eq, Show, Generic, Typeable)
4375+
4376+-- | Handles quirks in the way JVM versions are rendered (1.7.0_101 -> 1.7.0.101)
4377+newtype JVMVersion = JVMVersion { unJVMVersion :: VersionNumber }
4378+
4379+data JVMMemoryInfo = JVMMemoryInfo {
4380+ jvmMemoryInfoDirectMax :: Bytes
4381+ , jvmMemoryInfoNonHeapMax :: Bytes
4382+ , jvmMemoryInfoNonHeapInit :: Bytes
4383+ , jvmMemoryInfoHeapMax :: Bytes
4384+ , jvmMemoryInfoHeapInit :: Bytes
4385+ } deriving (Eq, Show, Generic, Typeable)
4386+
4387+newtype JVMMemoryPool = JVMMemoryPool {
4388+ jvmMemoryPool :: Text
4389+ } deriving (Eq, Show, Generic, Typeable, FromJSON)
4390+
4391+newtype JVMGCCollector = JVMGCCollector {
4392+ jvmGCCollector :: Text
4393+ } deriving (Eq, Show, Generic, Typeable, FromJSON)
4394+
4395+newtype PID = PID {
4396+ pid :: Int
4397+ } deriving (Eq, Show, Generic, Typeable, FromJSON)
4398+
4399+data NodeOSInfo = NodeOSInfo {
4400+ nodeOSSwap :: Bytes
4401+ , nodeOSMem :: Bytes
4402+ , nodeOSCPUInfo :: CPUInfo
4403+ , nodeOSAvailableProcessors :: Int
4404+ , nodeOSRefreshInterval :: NominalDiffTime
4405+ } deriving (Eq, Show, Generic, Typeable)
4406+
4407+data CPUInfo = CPUInfo {
4408+ cpuCacheSize :: Bytes
4409+ , cpuCoresPerSocket :: Int
4410+ , cpuTotalSockets :: Int
4411+ , cpuTotalCores :: Int
4412+ , cpuMHZ :: Int
4413+ , cpuModel :: Text
4414+ , cpuVendor :: Text
4415+ } deriving (Eq, Show, Generic, Typeable)
4416+
4417+data NodeProcessInfo = NodeProcessInfo {
4418+ nodeProcessMLockAll :: Bool
4419+ -- ^ See <https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
4420+ , nodeProcessMaxFileDescriptors :: Int
4421+ , nodeProcessId :: PID
4422+ , nodeProcessRefreshInterval :: NominalDiffTime
4423+ } deriving (Eq, Show, Generic, Typeable)
4424+
4425+data SnapshotRepoUpdateSettings = SnapshotRepoUpdateSettings {
4426+ repoUpdateVerify :: Bool
4427+ -- ^ After creation/update, synchronously check that nodes can
4428+ -- write to this repo. Defaults to True. You may use False if you
4429+ -- need a faster response and plan on verifying manually later
4430+ -- with 'verifySnapshotRepo'.
4431+ } deriving (Eq, Show, Generic, Typeable)
4432+
4433+
4434+-- | Reasonable defaults for repo creation/update
4435+--
4436+-- * repoUpdateVerify True
4437+defaultSnapshotRepoUpdateSettings :: SnapshotRepoUpdateSettings
4438+defaultSnapshotRepoUpdateSettings = SnapshotRepoUpdateSettings True
4439+
4440+
4441+-- | A filesystem-based snapshot repo that ships with
4442+-- ElasticSearch. This is an instance of 'SnapshotRepo' so it can be
4443+-- used with 'updateSnapshotRepo'
4444+data FsSnapshotRepo = FsSnapshotRepo {
4445+ fsrName :: SnapshotRepoName
4446+ , fsrLocation :: FilePath
4447+ , fsrCompressMetadata :: Bool
4448+ , fsrChunkSize :: Maybe Bytes
4449+ -- ^ Size by which to split large files during snapshotting.
4450+ , fsrMaxRestoreBytesPerSec :: Maybe Bytes
4451+ -- ^ Throttle node restore rate. If not supplied, defaults to 40mb/sec
4452+ , fsrMaxSnapshotBytesPerSec :: Maybe Bytes
4453+ -- ^ Throttle node snapshot rate. If not supplied, defaults to 40mb/sec
4454+ } deriving (Eq, Generic, Show, Typeable)
4455+
4456+
4457+instance SnapshotRepo FsSnapshotRepo where
4458+ toGSnapshotRepo FsSnapshotRepo {..} =
4459+ GenericSnapshotRepo fsrName fsRepoType (GenericSnapshotRepoSettings settings)
4460+ where
4461+ Object settings = object $ [ "location" .= fsrLocation
4462+ , "compress" .= fsrCompressMetadata
4463+ ] ++ optionalPairs
4464+ optionalPairs = catMaybes [ ("chunk_size" .=) <$> fsrChunkSize
4465+ , ("max_restore_bytes_per_sec" .=) <$> fsrMaxRestoreBytesPerSec
4466+ , ("max_snapshot_bytes_per_sec" .=) <$> fsrMaxSnapshotBytesPerSec
4467+ ]
4468+ fromGSnapshotRepo GenericSnapshotRepo {..}
4469+ | gSnapshotRepoType == fsRepoType = do
4470+ let o = gSnapshotRepoSettingsObject gSnapshotRepoSettings
4471+ parseRepo $ do
4472+ FsSnapshotRepo gSnapshotRepoName <$> o .: "location"
4473+ <*> o .:? "compress" .!= False
4474+ <*> o .:? "chunk_size"
4475+ <*> o .:? "max_restore_bytes_per_sec"
4476+ <*> o .:? "max_snapshot_bytes_per_sec"
4477+ | otherwise = Left (RepoTypeMismatch fsRepoType gSnapshotRepoType)
4478+
4479+
4480+parseRepo :: Parser a -> Either SnapshotRepoConversionError a
4481+parseRepo parser = case parseEither (const parser) () of
4482+ Left e -> Left (OtherRepoConversionError (T.pack e))
4483+ Right a -> Right a
4484+
4485+
4486+fsRepoType :: SnapshotRepoType
4487+fsRepoType = SnapshotRepoType "fs"
4488+
4489+-- | Law: fromGSnapshotRepo (toGSnapshotRepo r) == Right r
4490+class SnapshotRepo r where
4491+ toGSnapshotRepo :: r -> GenericSnapshotRepo
4492+ fromGSnapshotRepo :: GenericSnapshotRepo -> Either SnapshotRepoConversionError r
4493+
4494+
4495+data SnapshotRepoConversionError = RepoTypeMismatch SnapshotRepoType SnapshotRepoType
4496+ -- ^ Expected type and actual type
4497+ | OtherRepoConversionError Text
4498+ deriving (Show, Eq, Generic, Typeable)
4499+
4500+
4501+instance Exception SnapshotRepoConversionError
4502+
4503+
4504+data SnapshotCreateSettings = SnapshotCreateSettings {
4505+ snapWaitForCompletion :: Bool
4506+ -- ^ Should the API call return immediately after initializing
4507+ -- the snapshot or wait until completed? Note that if this is
4508+ -- enabled it could wait a long time, so you should adjust your
4509+ -- 'ManagerSettings' accordingly to set long timeouts or
4510+ -- explicitly handle timeouts.
4511+ , snapIndices :: Maybe IndexSelection
4512+ -- ^ Nothing will snapshot all indices. Just [] is permissable and
4513+ -- will essentially be a no-op snapshot.
4514+ , snapIgnoreUnavailable :: Bool
4515+ -- ^ If set to True, any matched indices that don't exist will be
4516+ -- ignored. Otherwise it will be an error and fail.
4517+ , snapIncludeGlobalState :: Bool
4518+ , snapPartial :: Bool
4519+ -- ^ If some indices failed to snapshot (e.g. if not all primary
4520+ -- shards are available), should the process proceed?
4521+ } deriving (Eq, Generic, Show, Typeable)
4522+
4523+
4524+-- | Reasonable defaults for snapshot creation
4525+--
4526+-- * snapWaitForCompletion False
4527+-- * snapIndices Nothing
4528+-- * snapIgnoreUnavailable False
4529+-- * snapIncludeGlobalState True
4530+-- * snapPartial False
4531+defaultSnapshotCreateSettings :: SnapshotCreateSettings
4532+defaultSnapshotCreateSettings = SnapshotCreateSettings {
4533+ snapWaitForCompletion = False
4534+ , snapIndices = Nothing
4535+ , snapIgnoreUnavailable = False
4536+ , snapIncludeGlobalState = True
4537+ , snapPartial = False
4538+ }
4539+
4540+
4541+data SnapshotSelection = SnapshotList (NonEmpty SnapshotPattern)
4542+ | AllSnapshots deriving (Eq, Generic, Show, Typeable)
4543+
4544+
4545+-- | Either specifies an exact snapshot name or one with globs in it,
4546+-- e.g. @SnapPattern "foo*"@ __NOTE__: Patterns are not supported on
4547+-- ES < 1.7
4548+data SnapshotPattern = ExactSnap SnapshotName
4549+ | SnapPattern Text
4550+ deriving (Eq, Generic, Show, Typeable)
4551+
4552+
4553+-- | General information about the state of a snapshot. Has some
4554+-- redundancies with 'SnapshotStatus'
4555+data SnapshotInfo = SnapshotInfo {
4556+ snapInfoShards :: ShardResult
4557+ , snapInfoFailures :: [SnapshotShardFailure]
4558+ , snapInfoDuration :: NominalDiffTime
4559+ , snapInfoEndTime :: UTCTime
4560+ , snapInfoStartTime :: UTCTime
4561+ , snapInfoState :: SnapshotState
4562+ , snapInfoIndices :: [IndexName]
4563+ , snapInfoName :: SnapshotName
4564+ } deriving (Eq, Generic, Show, Typeable)
4565+
4566+
4567+instance FromJSON SnapshotInfo where
4568+ parseJSON = withObject "SnapshotInfo" parse
4569+ where
4570+ parse o = SnapshotInfo <$> o .: "shards"
4571+ <*> o .: "failures"
4572+ <*> (unMS <$> o .: "duration_in_millis")
4573+ <*> (posixMS <$> o .: "end_time_in_millis")
4574+ <*> (posixMS <$> o .: "start_time_in_millis")
4575+ <*> o .: "state"
4576+ <*> o .: "indices"
4577+ <*> o .: "snapshot"
4578+
4579+data SnapshotShardFailure = SnapshotShardFailure {
4580+ snapShardFailureIndex :: IndexName
4581+ , snapShardFailureNodeId :: Maybe NodeName -- I'm not 100% sure this isn't actually 'FullNodeId'
4582+ , snapShardFailureReason :: Text
4583+ , snapShardFailureShardId :: ShardId
4584+ } deriving (Eq, Show, Generic, Typeable)
4585+
4586+
4587+instance FromJSON SnapshotShardFailure where
4588+ parseJSON = withObject "SnapshotShardFailure" parse
4589+ where
4590+ parse o = SnapshotShardFailure <$> o .: "index"
4591+ <*> o .:? "node_id"
4592+ <*> o .: "reason"
4593+ <*> o .: "shard_id"
4594+
4595+
4596+newtype ShardId = ShardId { shardId :: Int }
4597+ deriving (Eq, Show, Generic, Typeable, FromJSON)
4598+
4599+-- | Milliseconds
4600+newtype MS = MS NominalDiffTime
4601+
4602+
4603+-- keeps the unexported constructor warnings at bay
4604+unMS :: MS -> NominalDiffTime
4605+unMS (MS t) = t
4606+
4607+
4608+instance FromJSON MS where
4609+ parseJSON = withScientific "MS" (return . MS . parse)
4610+ where
4611+ parse n = fromInteger ((truncate n) * 1000)
4612+
4613+
4614+data SnapshotState = SnapshotInit
4615+ | SnapshotStarted
4616+ | SnapshotSuccess
4617+ | SnapshotFailed
4618+ | SnapshotAborted
4619+ | SnapshotMissing
4620+ | SnapshotWaiting
4621+ deriving (Show, Eq, Generic, Typeable)
4622+
4623+instance FromJSON SnapshotState where
4624+ parseJSON = withText "SnapshotState" parse
4625+ where
4626+ parse "INIT" = return SnapshotInit
4627+ parse "STARTED" = return SnapshotStarted
4628+ parse "SUCCESS" = return SnapshotSuccess
4629+ parse "FAILED" = return SnapshotFailed
4630+ parse "ABORTED" = return SnapshotAborted
4631+ parse "MISSING" = return SnapshotMissing
4632+ parse "WAITING" = return SnapshotWaiting
4633+ parse t = fail ("Invalid snapshot state " <> T.unpack t)
4634+
4635+
4636+newtype SnapshotName = SnapshotName { snapshotName :: Text }
4637+ deriving (Show, Eq, Ord, Generic, Typeable, ToJSON, FromJSON)
4638+
4639+
4640+data SnapshotRestoreSettings = SnapshotRestoreSettings {
4641+ snapRestoreWaitForCompletion :: Bool
4642+ -- ^ Should the API call return immediately after initializing
4643+ -- the restore or wait until completed? Note that if this is
4644+ -- enabled, it could wait a long time, so you should adjust your
4645+ -- 'ManagerSettings' accordingly to set long timeouts or
4646+ -- explicitly handle timeouts.
4647+ , snapRestoreIndices :: Maybe IndexSelection
4648+ -- ^ Nothing will restore all indices in the snapshot. Just [] is
4649+ -- permissable and will essentially be a no-op restore.
4650+ , snapRestoreIgnoreUnavailable :: Bool
4651+ -- ^ If set to True, any indices that do not exist will be ignored
4652+ -- during snapshot rather than failing the restore.
4653+ , snapRestoreIncludeGlobalState :: Bool
4654+ -- ^ If set to false, will ignore any global state in the snapshot
4655+ -- and will not restore it.
4656+ , snapRestoreRenamePattern :: Maybe RestoreRenamePattern
4657+ -- ^ A regex pattern for matching indices. Used with
4658+ -- 'snapRestoreRenameReplacement', the restore can reference the
4659+ -- matched index and create a new index name upon restore.
4660+ , snapRestoreRenameReplacement :: Maybe (NonEmpty RestoreRenameToken)
4661+ -- ^ Expression of how index renames should be constructed.
4662+ , snapRestorePartial :: Bool
4663+ -- ^ If some indices fail to restore, should the process proceed?
4664+ , snapRestoreIncludeAliases :: Bool
4665+ -- ^ Should the restore also restore the aliases captured in the
4666+ -- snapshot.
4667+ , snapRestoreIndexSettingsOverrides :: Maybe RestoreIndexSettings
4668+ -- ^ Settings to apply during the restore process. __NOTE:__ This
4669+ -- option is not supported in ES < 1.5 and should be set to
4670+ -- Nothing in that case.
4671+ , snapRestoreIgnoreIndexSettings :: Maybe (NonEmpty Text)
4672+ -- ^ This type could be more rich but it isn't clear which
4673+ -- settings are allowed to be ignored during restore, so we're
4674+ -- going with including this feature in a basic form rather than
4675+ -- omitting it. One example here would be
4676+ -- "index.refresh_interval". Any setting specified here will
4677+ -- revert back to the server default during the restore process.
4678+ } deriving (Eq, Generic, Show, Typeable)
4679+
4680+-- | Regex-stype pattern, e.g. "index_(.+)" to match index names
4681+newtype RestoreRenamePattern = RestoreRenamePattern { rrPattern :: Text }
4682+ deriving (Show, Eq, Generic, Typeable, Ord, ToJSON)
4683+
4684+
4685+-- | A single token in a index renaming scheme for a restore. These
4686+-- are concatenated into a string before being sent to
4687+-- ElasticSearch. Check out these Java
4688+-- <https://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html docs> to find out more if you're into that sort of thing.
4689+data RestoreRenameToken = RRTLit Text
4690+ -- ^ Just a literal string of characters
4691+ | RRSubWholeMatch
4692+ -- ^ Equivalent to $0. The entire matched pattern, not any subgroup
4693+ | RRSubGroup RRGroupRefNum
4694+ -- ^ A specific reference to a group number
4695+ deriving (Show, Eq, Generic, Typeable)
4696+
4697+
4698+-- | A group number for regex matching. Only values from 1-9 are
4699+-- supported. Construct with 'mkRRGroupRefNum'
4700+newtype RRGroupRefNum = RRGroupRefNum { rrGroupRefNum :: Int }
4701+ deriving (Show, Eq, Generic, Typeable, Ord)
4702+
4703+instance Bounded RRGroupRefNum where
4704+ minBound = RRGroupRefNum 1
4705+ maxBound = RRGroupRefNum 9
4706+
4707+
4708+-- | Only allows valid group number references (1-9).
4709+mkRRGroupRefNum :: Int -> Maybe RRGroupRefNum
4710+mkRRGroupRefNum i
4711+ | i >= (rrGroupRefNum minBound) && i <= (rrGroupRefNum maxBound) =
4712+ Just $ RRGroupRefNum i
4713+ | otherwise = Nothing
4714+
4715+
4716+-- | Reasonable defaults for snapshot restores
4717+--
4718+-- * snapRestoreWaitForCompletion False
4719+-- * snapRestoreIndices Nothing
4720+-- * snapRestoreIgnoreUnavailable False
4721+-- * snapRestoreIncludeGlobalState True
4722+-- * snapRestoreRenamePattern Nothing
4723+-- * snapRestoreRenameReplacement Nothing
4724+-- * snapRestorePartial False
4725+-- * snapRestoreIncludeAliases True
4726+-- * snapRestoreIndexSettingsOverrides Nothing
4727+-- * snapRestoreIgnoreIndexSettings Nothing
4728+defaultSnapshotRestoreSettings :: SnapshotRestoreSettings
4729+defaultSnapshotRestoreSettings = SnapshotRestoreSettings {
4730+ snapRestoreWaitForCompletion = False
4731+ , snapRestoreIndices = Nothing
4732+ , snapRestoreIgnoreUnavailable = False
4733+ , snapRestoreIncludeGlobalState = True
4734+ , snapRestoreRenamePattern = Nothing
4735+ , snapRestoreRenameReplacement = Nothing
4736+ , snapRestorePartial = False
4737+ , snapRestoreIncludeAliases = True
4738+ , snapRestoreIndexSettingsOverrides = Nothing
4739+ , snapRestoreIgnoreIndexSettings = Nothing
4740+ }
4741+
4742+
4743+-- | Index settings that can be overridden. The docs only mention you
4744+-- can update number of replicas, but there may be more. You
4745+-- definitely cannot override shard count.
4746+data RestoreIndexSettings = RestoreIndexSettings {
4747+ restoreOverrideReplicas :: Maybe ReplicaCount
4748+ } deriving (Show, Eq, Generic, Typeable)
4749+
4750+
4751+instance ToJSON RestoreIndexSettings where
4752+ toJSON RestoreIndexSettings {..} = object prs
4753+ where
4754+ prs = catMaybes [("index.number_of_replicas" .=) <$> restoreOverrideReplicas]
4755+
4756+
4757+instance FromJSON NodesInfo where
4758+ parseJSON = withObject "NodesInfo" parse
4759+ where
4760+ parse o = do
4761+ nodes <- o .: "nodes"
4762+ infos <- forM (HM.toList nodes) $ \(fullNID, v) -> do
4763+ node <- parseJSON v
4764+ parseNodeInfo (FullNodeId fullNID) node
4765+ cn <- o .: "cluster_name"
4766+ return (NodesInfo infos cn)
4767+
4768+instance FromJSON NodesStats where
4769+ parseJSON = withObject "NodesStats" parse
4770+ where
4771+ parse o = do
4772+ nodes <- o .: "nodes"
4773+ stats <- forM (HM.toList nodes) $ \(fullNID, v) -> do
4774+ node <- parseJSON v
4775+ parseNodeStats (FullNodeId fullNID) node
4776+ cn <- o .: "cluster_name"
4777+ return (NodesStats stats cn)
4778+
4779+instance FromJSON NodeBreakerStats where
4780+ parseJSON = withObject "NodeBreakerStats" parse
4781+ where
4782+ parse o = NodeBreakerStats <$> o .: "tripped"
4783+ <*> o .: "overhead"
4784+ <*> o .: "estimated_size_in_bytes"
4785+ <*> o .: "limit_size_in_bytes"
4786+
4787+instance FromJSON NodeHTTPStats where
4788+ parseJSON = withObject "NodeHTTPStats" parse
4789+ where
4790+ parse o = NodeHTTPStats <$> o .: "total_opened"
4791+ <*> o .: "current_open"
4792+
4793+instance FromJSON NodeTransportStats where
4794+ parseJSON = withObject "NodeTransportStats" parse
4795+ where
4796+ parse o = NodeTransportStats <$> o .: "tx_size_in_bytes"
4797+ <*> o .: "tx_count"
4798+ <*> o .: "rx_size_in_bytes"
4799+ <*> o .: "rx_count"
4800+ <*> o .: "server_open"
4801+
4802+instance FromJSON NodeFSStats where
4803+ parseJSON = withObject "NodeFSStats" parse
4804+ where
4805+ parse o = NodeFSStats <$> o .: "data"
4806+ <*> o .: "total"
4807+ <*> (posixMS <$> o .: "timestamp")
4808+
4809+instance FromJSON NodeDataPathStats where
4810+ parseJSON = withObject "NodeDataPathStats" parse
4811+ where
4812+ parse o =
4813+ NodeDataPathStats <$> (fmap unStringlyTypedDouble <$> o .:? "disk_service_time")
4814+ <*> (fmap unStringlyTypedDouble <$> o .:? "disk_queue")
4815+ <*> o .:? "disk_io_size_in_bytes"
4816+ <*> o .:? "disk_write_size_in_bytes"
4817+ <*> o .:? "disk_read_size_in_bytes"
4818+ <*> o .:? "disk_io_op"
4819+ <*> o .:? "disk_writes"
4820+ <*> o .:? "disk_reads"
4821+ <*> o .: "available_in_bytes"
4822+ <*> o .: "free_in_bytes"
4823+ <*> o .: "total_in_bytes"
4824+ <*> o .:? "type"
4825+ <*> o .: "dev"
4826+ <*> o .: "mount"
4827+ <*> o .: "path"
4828+
4829+newtype StringlyTypedDouble = StringlyTypedDouble { unStringlyTypedDouble :: Double }
4830+
4831+
4832+instance FromJSON StringlyTypedDouble where
4833+ parseJSON = fmap StringlyTypedDouble . parseJSON . unStringlyTypeJSON
4834+
4835+
4836+instance FromJSON NodeFSTotalStats where
4837+ parseJSON = withObject "NodeFSTotalStats" parse
4838+ where
4839+ parse o = NodeFSTotalStats <$> (fmap unStringlyTypedDouble <$> o .:? "disk_service_time")
4840+ <*> (fmap unStringlyTypedDouble <$> o .:? "disk_queue")
4841+ <*> o .:? "disk_io_size_in_bytes"
4842+ <*> o .:? "disk_write_size_in_bytes"
4843+ <*> o .:? "disk_read_size_in_bytes"
4844+ <*> o .:? "disk_io_op"
4845+ <*> o .:? "disk_writes"
4846+ <*> o .:? "disk_reads"
4847+ <*> o .: "available_in_bytes"
4848+ <*> o .: "free_in_bytes"
4849+ <*> o .: "total_in_bytes"
4850+
4851+instance FromJSON NodeNetworkStats where
4852+ parseJSON = withObject "NodeNetworkStats" parse
4853+ where
4854+ parse o = do
4855+ tcp <- o .: "tcp"
4856+ NodeNetworkStats <$> tcp .: "out_rsts"
4857+ <*> tcp .: "in_errs"
4858+ <*> tcp .: "attempt_fails"
4859+ <*> tcp .: "estab_resets"
4860+ <*> tcp .: "retrans_segs"
4861+ <*> tcp .: "out_segs"
4862+ <*> tcp .: "in_segs"
4863+ <*> tcp .: "curr_estab"
4864+ <*> tcp .: "passive_opens"
4865+ <*> tcp .: "active_opens"
4866+
4867+instance FromJSON NodeThreadPoolsStats where
4868+ parseJSON = withObject "NodeThreadPoolsStats" parse
4869+ where
4870+ parse o = NodeThreadPoolsStats <$> o .: "snapshot"
4871+ <*> o .: "bulk"
4872+ <*> o .: "merge"
4873+ <*> o .: "get"
4874+ <*> o .: "management"
4875+ <*> o .:? "fetch_shard_store"
4876+ <*> o .: "optimize"
4877+ <*> o .: "flush"
4878+ <*> o .: "search"
4879+ <*> o .: "warmer"
4880+ <*> o .: "generic"
4881+ <*> o .: "suggest"
4882+ <*> o .: "refresh"
4883+ <*> o .: "index"
4884+ <*> o .:? "listener"
4885+ <*> o .:? "fetch_shard_started"
4886+ <*> o .: "percolate"
4887+instance FromJSON NodeThreadPoolStats where
4888+ parseJSON = withObject "NodeThreadPoolStats" parse
4889+ where
4890+ parse o = NodeThreadPoolStats <$> o .: "completed"
4891+ <*> o .: "largest"
4892+ <*> o .: "rejected"
4893+ <*> o .: "active"
4894+ <*> o .: "queue"
4895+ <*> o .: "threads"
4896+
4897+instance FromJSON NodeJVMStats where
4898+ parseJSON = withObject "NodeJVMStats" parse
4899+ where
4900+ parse o = do
4901+ bufferPools <- o .: "buffer_pools"
4902+ mapped <- bufferPools .: "mapped"
4903+ direct <- bufferPools .: "direct"
4904+ gc <- o .: "gc"
4905+ collectors <- gc .: "collectors"
4906+ oldC <- collectors .: "old"
4907+ youngC <- collectors .: "young"
4908+ threads <- o .: "threads"
4909+ mem <- o .: "mem"
4910+ pools <- mem .: "pools"
4911+ oldM <- pools .: "old"
4912+ survivorM <- pools .: "survivor"
4913+ youngM <- pools .: "young"
4914+ NodeJVMStats <$> pure mapped
4915+ <*> pure direct
4916+ <*> pure oldC
4917+ <*> pure youngC
4918+ <*> threads .: "peak_count"
4919+ <*> threads .: "count"
4920+ <*> pure oldM
4921+ <*> pure survivorM
4922+ <*> pure youngM
4923+ <*> mem .: "non_heap_committed_in_bytes"
4924+ <*> mem .: "non_heap_used_in_bytes"
4925+ <*> mem .: "heap_max_in_bytes"
4926+ <*> mem .: "heap_committed_in_bytes"
4927+ <*> mem .: "heap_used_percent"
4928+ <*> mem .: "heap_used_in_bytes"
4929+ <*> (unMS <$> o .: "uptime_in_millis")
4930+ <*> (posixMS <$> o .: "timestamp")
4931+
4932+instance FromJSON JVMBufferPoolStats where
4933+ parseJSON = withObject "JVMBufferPoolStats" parse
4934+ where
4935+ parse o = JVMBufferPoolStats <$> o .: "total_capacity_in_bytes"
4936+ <*> o .: "used_in_bytes"
4937+ <*> o .: "count"
4938+
4939+instance FromJSON JVMGCStats where
4940+ parseJSON = withObject "JVMGCStats" parse
4941+ where
4942+ parse o = JVMGCStats <$> (unMS <$> o .: "collection_time_in_millis")
4943+ <*> o .: "collection_count"
4944+
4945+instance FromJSON JVMPoolStats where
4946+ parseJSON = withObject "JVMPoolStats" parse
4947+ where
4948+ parse o = JVMPoolStats <$> o .: "peak_max_in_bytes"
4949+ <*> o .: "peak_used_in_bytes"
4950+ <*> o .: "max_in_bytes"
4951+ <*> o .: "used_in_bytes"
4952+
4953+instance FromJSON NodeProcessStats where
4954+ parseJSON = withObject "NodeProcessStats" parse
4955+ where
4956+ parse o = do
4957+ mem <- o .: "mem"
4958+ cpu <- o .: "cpu"
4959+ NodeProcessStats <$> mem .: "total_virtual_in_bytes"
4960+ <*> mem .: "share_in_bytes"
4961+ <*> mem .: "resident_in_bytes"
4962+ <*> (unMS <$> cpu .: "total_in_millis")
4963+ <*> (unMS <$> cpu .: "user_in_millis")
4964+ <*> (unMS <$> cpu .: "sys_in_millis")
4965+ <*> cpu .: "percent"
4966+ <*> o .: "open_file_descriptors"
4967+ <*> (posixMS <$> o .: "timestamp")
4968+
4969+instance FromJSON NodeOSStats where
4970+ parseJSON = withObject "NodeOSStats" parse
4971+ where
4972+ parse o = do
4973+ swap <- o .: "swap"
4974+ mem <- o .: "mem"
4975+ cpu <- o .: "cpu"
4976+ load <- o .:? "load_average"
4977+ NodeOSStats <$> swap .: "free_in_bytes"
4978+ <*> swap .: "used_in_bytes"
4979+ <*> mem .: "actual_used_in_bytes"
4980+ <*> mem .: "actual_free_in_bytes"
4981+ <*> mem .: "used_percent"
4982+ <*> mem .: "free_percent"
4983+ <*> mem .: "used_in_bytes"
4984+ <*> mem .: "free_in_bytes"
4985+ <*> cpu .: "stolen"
4986+ <*> cpu .: "usage"
4987+ <*> cpu .: "idle"
4988+ <*> cpu .: "user"
4989+ <*> cpu .: "sys"
4990+ <*> pure load
4991+ <*> (unMS <$> o .: "uptime_in_millis")
4992+ <*> (posixMS <$> o .: "timestamp")
4993+
4994+instance FromJSON LoadAvgs where
4995+ parseJSON = withArray "LoadAvgs" parse
4996+ where
4997+ parse v = case V.toList v of
4998+ [one, five, fifteen] -> LoadAvgs <$> parseJSON one
4999+ <*> parseJSON five
5000+ <*> parseJSON fifteen
5001+ _ -> fail "Expecting a triple of Doubles"
5002+
5003+instance FromJSON NodeIndicesStats where
5004+ parseJSON = withObject "NodeIndicesStats" parse
5005+ where
5006+ parse o = do
5007+ let (.::) mv k = case mv of
5008+ Just v -> Just <$> v .: k
5009+ Nothing -> pure Nothing
5010+ mRecovery <- o .:? "recovery"
5011+ mQueryCache <- o .:? "query_cache"
5012+ suggest <- o .: "suggest"
5013+ translog <- o .: "translog"
5014+ segments <- o .: "segments"
5015+ completion <- o .: "completion"
5016+ percolate <- o .: "percolate"
5017+ fielddata <- o .: "fielddata"
5018+ idCache <- o .: "id_cache"
5019+ filterCache <- o .: "filter_cache"
5020+ warmer <- o .: "warmer"
5021+ flush <- o .: "flush"
5022+ refresh <- o .: "refresh"
5023+ merges <- o .: "merges"
5024+ search <- o .: "search"
5025+ getStats <- o .: "get"
5026+ indexing <- o .: "indexing"
5027+ store <- o .: "store"
5028+ docs <- o .: "docs"
5029+ NodeIndicesStats <$> (fmap unMS <$> mRecovery .:: "throttle_time_in_millis")
5030+ <*> mRecovery .:: "current_as_target"
5031+ <*> mRecovery .:: "current_as_source"
5032+ <*> mQueryCache .:: "miss_count"
5033+ <*> mQueryCache .:: "hit_count"
5034+ <*> mQueryCache .:: "evictions"
5035+ <*> mQueryCache .:: "memory_size_in_bytes"
5036+ <*> suggest .: "current"
5037+ <*> (unMS <$> suggest .: "time_in_millis")
5038+ <*> suggest .: "total"
5039+ <*> translog .: "size_in_bytes"
5040+ <*> translog .: "operations"
5041+ <*> segments .:? "fixed_bit_set_memory_in_bytes"
5042+ <*> segments .: "version_map_memory_in_bytes"
5043+ <*> segments .:? "index_writer_max_memory_in_bytes"
5044+ <*> segments .: "index_writer_memory_in_bytes"
5045+ <*> segments .: "memory_in_bytes"
5046+ <*> segments .: "count"
5047+ <*> completion .: "size_in_bytes"
5048+ <*> percolate .: "queries"
5049+ <*> percolate .: "memory_size_in_bytes"
5050+ <*> percolate .: "current"
5051+ <*> (unMS <$> percolate .: "time_in_millis")
5052+ <*> percolate .: "total"
5053+ <*> fielddata .: "evictions"
5054+ <*> fielddata .: "memory_size_in_bytes"
5055+ <*> idCache .: "memory_size_in_bytes"
5056+ <*> filterCache .: "evictions"
5057+ <*> filterCache .: "memory_size_in_bytes"
5058+ <*> (unMS <$> warmer .: "total_time_in_millis")
5059+ <*> warmer .: "total"
5060+ <*> warmer .: "current"
5061+ <*> (unMS <$> flush .: "total_time_in_millis")
5062+ <*> flush .: "total"
5063+ <*> (unMS <$> refresh .: "total_time_in_millis")
5064+ <*> refresh .: "total"
5065+ <*> merges .: "total_size_in_bytes"
5066+ <*> merges .: "total_docs"
5067+ <*> (unMS <$> merges .: "total_time_in_millis")
5068+ <*> merges .: "total"
5069+ <*> merges .: "current_size_in_bytes"
5070+ <*> merges .: "current_docs"
5071+ <*> merges .: "current"
5072+ <*> search .: "fetch_current"
5073+ <*> (unMS <$> search .: "fetch_time_in_millis")
5074+ <*> search .: "fetch_total"
5075+ <*> search .: "query_current"
5076+ <*> (unMS <$> search .: "query_time_in_millis")
5077+ <*> search .: "query_total"
5078+ <*> search .: "open_contexts"
5079+ <*> getStats .: "current"
5080+ <*> (unMS <$> getStats .: "missing_time_in_millis")
5081+ <*> getStats .: "missing_total"
5082+ <*> (unMS <$> getStats .: "exists_time_in_millis")
5083+ <*> getStats .: "exists_total"
5084+ <*> (unMS <$> getStats .: "time_in_millis")
5085+ <*> getStats .: "total"
5086+ <*> (fmap unMS <$> indexing .:? "throttle_time_in_millis")
5087+ <*> indexing .:? "is_throttled"
5088+ <*> indexing .:? "noop_update_total"
5089+ <*> indexing .: "delete_current"
5090+ <*> (unMS <$> indexing .: "delete_time_in_millis")
5091+ <*> indexing .: "delete_total"
5092+ <*> indexing .: "index_current"
5093+ <*> (unMS <$> indexing .: "index_time_in_millis")
5094+ <*> indexing .: "index_total"
5095+ <*> (unMS <$> store .: "throttle_time_in_millis")
5096+ <*> store .: "size_in_bytes"
5097+ <*> docs .: "deleted"
5098+ <*> docs .: "count"
5099+
5100+instance FromJSON NodeBreakersStats where
5101+ parseJSON = withObject "NodeBreakersStats" parse
5102+ where
5103+ parse o = NodeBreakersStats <$> o .: "parent"
5104+ <*> o .: "request"
5105+ <*> o .: "fielddata"
5106+
5107+parseNodeStats :: FullNodeId -> Object -> Parser NodeStats
5108+parseNodeStats fnid o = do
5109+ NodeStats <$> o .: "name"
5110+ <*> pure fnid
5111+ <*> o .:? "breakers"
5112+ <*> o .: "http"
5113+ <*> o .: "transport"
5114+ <*> o .: "fs"
5115+ <*> o .: "network"
5116+ <*> o .: "thread_pool"
5117+ <*> o .: "jvm"
5118+ <*> o .: "process"
5119+ <*> o .: "os"
5120+ <*> o .: "indices"
5121+
5122+parseNodeInfo :: FullNodeId -> Object -> Parser NodeInfo
5123+parseNodeInfo nid o =
5124+ NodeInfo <$> o .: "http_address"
5125+ <*> o .: "build"
5126+ <*> o .: "version"
5127+ <*> o .: "ip"
5128+ <*> o .: "host"
5129+ <*> o .: "transport_address"
5130+ <*> o .: "name"
5131+ <*> pure nid
5132+ <*> o .: "plugins"
5133+ <*> o .: "http"
5134+ <*> o .: "transport"
5135+ <*> o .: "network"
5136+ <*> o .: "thread_pool"
5137+ <*> o .: "jvm"
5138+ <*> o .: "process"
5139+ <*> o .: "os"
5140+ <*> o .: "settings"
5141+
5142+instance FromJSON NodePluginInfo where
5143+ parseJSON = withObject "NodePluginInfo" parse
5144+ where
5145+ parse o = NodePluginInfo <$> o .: "site"
5146+ <*> o .: "jvm"
5147+ <*> o .: "description"
5148+ <*> o .: "version"
5149+ <*> o .: "name"
5150+
5151+instance FromJSON NodeHTTPInfo where
5152+ parseJSON = withObject "NodeHTTPInfo" parse
5153+ where
5154+ parse o = NodeHTTPInfo <$> o .: "max_content_length_in_bytes"
5155+ <*> parseJSON (Object o)
5156+
5157+instance FromJSON BoundTransportAddress where
5158+ parseJSON = withObject "BoundTransportAddress" parse
5159+ where
5160+ parse o = BoundTransportAddress <$> o .: "publish_address"
5161+ <*> o .: "bound_address"
5162+
5163+instance FromJSON NodeOSInfo where
5164+ parseJSON = withObject "NodeOSInfo" parse
5165+ where
5166+ parse o = do
5167+ swap <- o .: "swap"
5168+ mem <- o .: "mem"
5169+ NodeOSInfo <$> swap .: "total_in_bytes"
5170+ <*> mem .: "total_in_bytes"
5171+ <*> o .: "cpu"
5172+ <*> o .: "available_processors"
5173+ <*> (unMS <$> o .: "refresh_interval_in_millis")
5174+
5175+
5176+instance FromJSON CPUInfo where
5177+ parseJSON = withObject "CPUInfo" parse
5178+ where
5179+ parse o = CPUInfo <$> o .: "cache_size_in_bytes"
5180+ <*> o .: "cores_per_socket"
5181+ <*> o .: "total_sockets"
5182+ <*> o .: "total_cores"
5183+ <*> o .: "mhz"
5184+ <*> o .: "model"
5185+ <*> o .: "vendor"
5186+
5187+instance FromJSON NodeProcessInfo where
5188+ parseJSON = withObject "NodeProcessInfo" parse
5189+ where
5190+ parse o = NodeProcessInfo <$> o .: "mlockall"
5191+ <*> o .: "max_file_descriptors"
5192+ <*> o .: "id"
5193+ <*> (unMS <$> o .: "refresh_interval_in_millis")
5194+
5195+instance FromJSON NodeJVMInfo where
5196+ parseJSON = withObject "NodeJVMInfo" parse
5197+ where
5198+ parse o = NodeJVMInfo <$> o .: "memory_pools"
5199+ <*> o .: "gc_collectors"
5200+ <*> o .: "mem"
5201+ <*> (posixMS <$> o .: "start_time_in_millis")
5202+ <*> o .: "vm_vendor"
5203+ <*> o .: "vm_version"
5204+ <*> o .: "vm_name"
5205+ <*> (unJVMVersion <$> o .: "version")
5206+ <*> o .: "pid"
5207+
5208+instance FromJSON JVMVersion where
5209+ parseJSON (String t) =
5210+ JVMVersion <$> parseJSON (String (T.replace "_" "." t))
5211+ parseJSON v = JVMVersion <$> parseJSON v
5212+
5213+instance FromJSON JVMMemoryInfo where
5214+ parseJSON = withObject "JVMMemoryInfo" parse
5215+ where
5216+ parse o = JVMMemoryInfo <$> o .: "direct_max_in_bytes"
5217+ <*> o .: "non_heap_max_in_bytes"
5218+ <*> o .: "non_heap_init_in_bytes"
5219+ <*> o .: "heap_max_in_bytes"
5220+ <*> o .: "heap_init_in_bytes"
5221+
5222+instance FromJSON NodeThreadPoolsInfo where
5223+ parseJSON = withObject "NodeThreadPoolsInfo" parse
5224+ where
5225+ parse o = NodeThreadPoolsInfo <$> o .: "refresh"
5226+ <*> o .: "management"
5227+ <*> o .: "percolate"
5228+ <*> o .:? "listener"
5229+ <*> o .:? "fetch_shard_started"
5230+ <*> o .: "search"
5231+ <*> o .: "flush"
5232+ <*> o .: "warmer"
5233+ <*> o .: "optimize"
5234+ <*> o .: "bulk"
5235+ <*> o .: "suggest"
5236+ <*> o .: "merge"
5237+ <*> o .: "snapshot"
5238+ <*> o .: "get"
5239+ <*> o .:? "fetch_shard_store"
5240+ <*> o .: "index"
5241+ <*> o .: "generic"
5242+
5243+instance FromJSON NodeThreadPoolInfo where
5244+ parseJSON = withObject "NodeThreadPoolInfo" parse
5245+ where
5246+ parse o = do
5247+ ka <- maybe (return Nothing) (fmap Just . parseStringInterval) =<< o .:? "keep_alive"
5248+ NodeThreadPoolInfo <$> (parseJSON . unStringlyTypeJSON =<< o .: "queue_size")
5249+ <*> pure ka
5250+ <*> o .:? "min"
5251+ <*> o .:? "max"
5252+ <*> o .: "type"
5253+
5254+parseStringInterval :: (Monad m) => String -> m NominalDiffTime
5255+parseStringInterval s = case span isNumber s of
5256+ ("", _) -> fail "Invalid interval"
5257+ (nS, unitS) -> case (readMay nS, readMay unitS) of
5258+ (Just n, Just unit) -> return (fromInteger (n * unitNDT unit))
5259+ (Nothing, _) -> fail "Invalid interval number"
5260+ (_, Nothing) -> fail "Invalid interval unit"
5261+ where
5262+ unitNDT Seconds = 1
5263+ unitNDT Minutes = 60
5264+ unitNDT Hours = 60 * 60
5265+ unitNDT Days = 24 * 60 * 60
5266+ unitNDT Weeks = 7 * 24 * 60 * 60
5267+
5268+instance FromJSON ThreadPoolSize where
5269+ parseJSON v = parseAsNumber v <|> parseAsString v
5270+ where
5271+ parseAsNumber = parseAsInt <=< parseJSON
5272+ parseAsInt (-1) = return ThreadPoolUnbounded
5273+ parseAsInt n
5274+ | n >= 0 = return (ThreadPoolBounded n)
5275+ | otherwise = fail "Thread pool size must be >= -1."
5276+ parseAsString = withText "ThreadPoolSize" $ \t ->
5277+ case first (readMay . T.unpack) (T.span isNumber t) of
5278+ (Just n, "k") -> return (ThreadPoolBounded (n * 1000))
5279+ (Just n, "") -> return (ThreadPoolBounded n)
5280+ _ -> fail ("Invalid thread pool size " <> T.unpack t)
5281+
5282+instance FromJSON ThreadPoolType where
5283+ parseJSON = withText "ThreadPoolType" parse
5284+ where
5285+ parse "scaling" = return ThreadPoolScaling
5286+ parse "fixed" = return ThreadPoolFixed
5287+ parse "cached" = return ThreadPoolCached
5288+ parse e = fail ("Unexpected thread pool type" <> T.unpack e)
5289+
5290+instance FromJSON NodeTransportInfo where
5291+ parseJSON = withObject "NodeTransportInfo" parse
5292+ where
5293+ parse o = NodeTransportInfo <$> (maybe (return mempty) parseProfiles =<< o .:? "profiles")
5294+ <*> parseJSON (Object o)
5295+ parseProfiles (Object o) | HM.null o = return []
5296+ parseProfiles v@(Array _) = parseJSON v
5297+ parseProfiles Null = return []
5298+ parseProfiles _ = fail "Could not parse profiles"
5299+
5300+instance FromJSON NodeNetworkInfo where
5301+ parseJSON = withObject "NodeNetworkInfo" parse
5302+ where
5303+ parse o = NodeNetworkInfo <$> o .: "primary_interface"
5304+ <*> (unMS <$> o .: "refresh_interval_in_millis")
5305+
5306+
5307+instance FromJSON NodeNetworkInterface where
5308+ parseJSON = withObject "NodeNetworkInterface" parse
5309+ where
5310+ parse o = NodeNetworkInterface <$> o .: "mac_address"
5311+ <*> o .: "name"
5312+ <*> o .: "address"
5313+
5314+newtype MaybeNA a = MaybeNA { unMaybeNA :: Maybe a }
5315+ deriving (Show, Eq)
5316+
5317+instance FromJSON a => FromJSON (MaybeNA a) where
5318+ parseJSON (String "NA") = pure $ MaybeNA Nothing
5319+ parseJSON o = MaybeNA . Just <$> parseJSON o
src/Database/V1/Bloodhound/Types/Class.hsnew file+17
@@ -0,0 +1,17 @@
1+{-# LANGUAGE CPP #-}
2+module Database.V1.Bloodhound.Types.Class
3+ ( Seminearring(..) )
4+ where
5+
6+#if !MIN_VERSION_base(4,8,0)
7+import Data.Monoid
8+#endif
9+
10+class Monoid a => Seminearring a where
11+ -- 0, +, *
12+ (<||>) :: a -> a -> a
13+ (<&&>) :: a -> a -> a
14+ (<&&>) = mappend
15+
16+infixr 5 <||>
17+infixr 5 <&&>
src/Database/V1/Bloodhound/Types/Internal.hsnew file+57
@@ -0,0 +1,57 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+
6+-------------------------------------------------------------------------------
7+-- |
8+-- Module : Database.Bloodhound.Types.Internal
9+-- Copyright : (C) 2014 Chris Allen
10+-- License : BSD-style (see the file LICENSE)
11+-- Maintainer : Chris Allen <cma@bitemyapp.com>
12+-- Stability : provisional
13+-- Portability : DeriveGeneric, RecordWildCards
14+--
15+-- Internal data types for Bloodhound. These types may change without
16+-- notice so import at your own risk.
17+-------------------------------------------------------------------------------
18+module Database.V1.Bloodhound.Types.Internal
19+ ( BHEnv(..)
20+ , Server(..)
21+ , MonadBH(..)
22+ ) where
23+
24+
25+import Control.Applicative as A
26+import Control.Monad.Reader
27+import Data.Aeson
28+import Data.Text (Text)
29+import Data.Typeable (Typeable)
30+import GHC.Generics (Generic)
31+import Network.HTTP.Client
32+
33+{-| Common environment for Elasticsearch calls. Connections will be
34+ pipelined according to the provided HTTP connection manager.
35+-}
36+data BHEnv = BHEnv { bhServer :: Server
37+ , bhManager :: Manager
38+ , bhRequestHook :: Request -> IO Request
39+ -- ^ Low-level hook that is run before every request is sent. Used to implement custom authentication strategies. Defaults to 'return' with 'mkBHEnv'.
40+ }
41+
42+instance (Functor m, Applicative m, MonadIO m) => MonadBH (ReaderT BHEnv m) where
43+ getBHEnv = ask
44+
45+{-| 'Server' is used with the client functions to point at the ES instance
46+-}
47+newtype Server = Server Text deriving (Eq, Show, Generic, Typeable, FromJSON)
48+
49+{-| All API calls to Elasticsearch operate within
50+ MonadBH
51+ . The idea is that it can be easily embedded in your
52+ own monad transformer stack. A default instance for a ReaderT and
53+ alias 'BH' is provided for the simple case.
54+-}
55+class (Functor m, A.Applicative m, MonadIO m) => MonadBH m where
56+ getBHEnv :: m BHEnv
57+
src/Database/V5/Bloodhound.hsnew file+10
@@ -0,0 +1,10 @@
1+module Database.V5.Bloodhound
2+ ( -- module Data.Aeson.Types
3+ -- ,
4+ module Database.V5.Bloodhound.Client
5+ , module Database.V5.Bloodhound.Types
6+ ) where
7+
8+-- import Data.Aeson.Types
9+import Database.V5.Bloodhound.Client
10+import Database.V5.Bloodhound.Types
src/Database/Bloodhound/Client.hs → src/Database/V5/Bloodhound/Client.hsrenamed+56−148
@@ -5,9 +5,9 @@
55-------------------------------------------------------------------------------
66-- |
77-- Module : Database.Bloodhound.Client
8−-- Copyright : (C) 2014, 2018 Chris Allen
8+-- Copyright : (C) 2014 Chris Allen
99-- License : BSD-style (see the file LICENSE)
10−-- Maintainer : Chris Allen <cma@bitemyapp.com>
10+-- Maintainer : Chris Allen <cma@bitemyapp.com
1111-- Stability : provisional
1212-- Portability : OverloadedStrings
1313--
@@ -15,7 +15,7 @@
1515--
1616-------------------------------------------------------------------------------
1717
18−module Database.Bloodhound.Client
18+module Database.V5.Bloodhound.Client
1919 ( -- * Bloodhound client functions
2020 -- | The examples in this module assume the following code has been run.
2121 -- The :{ and :} will only work in GHCi. You'll only need the data types
@@ -25,8 +25,6 @@ module Database.Bloodhound.Client
2525 withBH
2626 -- ** Indices
2727 , createIndex
28− , createIndexWith
29− , flushIndex
3028 , deleteIndex
3129 , updateIndexSettings
3230 , getIndexSettings
@@ -39,7 +37,6 @@ module Database.Bloodhound.Client
3937 -- *** Index Aliases
4038 , updateIndexAliases
4139 , getIndexAliases
42− , deleteIndexAlias
4340 -- *** Index Templates
4441 , putTemplate
4542 , templateExists
@@ -55,7 +52,6 @@ module Database.Bloodhound.Client
5552 -- ** Searching
5653 , searchAll
5754 , searchByIndex
58− , searchByIndices
5955 , searchByType
6056 , scanSearch
6157 , getInitialScroll
@@ -125,13 +121,12 @@ import qualified Network.HTTP.Types.URI as NHTU
125121import qualified Network.URI as URI
126122import Prelude hiding (filter, head)
127123
128−import Database.Bloodhound.Types
124+import Database.V5.Bloodhound.Types
129125
130126-- $setup
131127-- >>> :set -XOverloadedStrings
132128-- >>> :set -XDeriveGeneric
133−-- >>> import Database.Bloodhound
134−-- >>> import Network.HTTP.Client
129+-- >>> import Database.V5.Bloodhound
135130-- >>> let testServer = (Server "http://localhost:9200")
136131-- >>> let runBH' = withBH defaultManagerSettings testServer
137132-- >>> let testIndex = IndexName "twitter"
@@ -212,16 +207,8 @@ dispatch dMethod url body = do
212207 initReq <- liftIO $ parseUrl' url
213208 reqHook <- bhRequestHook A.<$> getBHEnv
214209 let reqBody = RequestBodyLBS $ fromMaybe emptyBody body
215− req <- liftIO
216− $ reqHook
217− $ setRequestIgnoreStatus
218− $ initReq { method = dMethod
219− , requestHeaders =
220− -- "application/x-ndjson" for bulk
221− ("Content-Type", "application/json") : requestHeaders initReq
222− , requestBody = reqBody }
223− -- req <- liftIO $ reqHook $ setRequestIgnoreStatus $ initReq { method = dMethod
224− -- , requestBody = reqBody }
210+ req <- liftIO $ reqHook $ setRequestIgnoreStatus $ initReq { method = dMethod
211+ , requestBody = reqBody }
225212 mgr <- bhManager <$> getBHEnv
226213 liftIO $ httpLbs req mgr
227214
@@ -318,7 +305,7 @@ instance FromJSON GSRs where
318305 parseJSON = withObject "Collection of GenericSnapshotRepo" parse
319306 where
320307 parse = fmap GSRs . mapM (uncurry go) . HM.toList
321− go rawName = withObject "GenericSnapshotRepo" $ \o ->
308+ go rawName = withObject "GenericSnapshotRepo" $ \o -> do
322309 GenericSnapshotRepo (SnapshotRepoName rawName) <$> o .: "type"
323310 <*> o .: "settings"
324311
@@ -347,7 +334,7 @@ updateSnapshotRepo SnapshotRepoUpdateSettings {..} repo =
347334
348335
349336-- | Verify if a snapshot repo is working. __NOTE:__ this API did not
350−-- make it into Elasticsearch until 1.4. If you use an older version,
337+-- make it into ElasticSearch until 1.4. If you use an older version,
351338-- you will get an error here.
352339verifySnapshotRepo
353340 :: ( MonadBH m
@@ -516,32 +503,8 @@ createIndex indexSettings (IndexName indexName) =
516503 where url = joinPath [indexName]
517504 body = Just $ encode indexSettings
518505
519−-- | Create an index, providing it with any number of settings. This
520−-- is more expressive than 'createIndex' but makes is more verbose
521−-- for the common case of configuring only the shard count and
522−-- replica count.
523−createIndexWith :: MonadBH m
524− => [UpdatableIndexSetting]
525− -> Int -- ^ shard count
526− -> IndexName
527− -> m Reply
528−createIndexWith updates shards (IndexName indexName) =
529− bindM2 put url (return (Just body))
530− where url = joinPath [indexName]
531− body = encode $ object
532− ["settings" .= deepMerge
533− ( HM.singleton "index.number_of_shards" (toJSON shards) :
534− [u | Object u <- toJSON <$> updates]
535− )
536− ]
537−
538−-- | 'flushIndex' will flush an index given a 'Server' and an 'IndexName'.
539−flushIndex :: MonadBH m => IndexName -> m Reply
540−flushIndex (IndexName indexName) = do
541− path <- joinPath [indexName, "_flush"]
542− post path Nothing
543−
544−-- | 'deleteIndex' will delete an index given a 'Server' and an 'IndexName'.
506+
507+-- | 'deleteIndex' will delete an index given a 'Server', and an 'IndexName'.
545508--
546509-- >>> _ <- runBH' $ createIndex defaultIndexSettings (IndexName "didimakeanindex")
547510-- >>> response <- runBH' $ deleteIndex (IndexName "didimakeanindex")
@@ -562,21 +525,20 @@ deleteIndex (IndexName indexName) =
562525updateIndexSettings :: MonadBH m => NonEmpty UpdatableIndexSetting -> IndexName -> m Reply
563526updateIndexSettings updates (IndexName indexName) =
564527 bindM2 put url (return body)
565− where
566− url = joinPath [indexName, "_settings"]
567− body = Just (encode jsonBody)
568− jsonBody = Object (deepMerge [u | Object u <- toJSON <$> toList updates])
528+ where url = joinPath [indexName, "_settings"]
529+ body = Just (encode jsonBody)
530+ jsonBody = Object (deepMerge [u | Object u <- toJSON <$> toList updates])
569531
570532
571533getIndexSettings :: (MonadBH m, MonadThrow m) => IndexName
572534 -> m (Either EsError IndexSettingsSummary)
573−getIndexSettings (IndexName indexName) =
535+getIndexSettings (IndexName indexName) = do
574536 parseEsResponse =<< get =<< url
575− where
576− url = joinPath [indexName, "_settings"]
537+ where url = joinPath [indexName, "_settings"]
577538
578−-- | 'forceMergeIndex'
579−--
539+
540+-- | 'forceMergeIndex'
541+--
580542-- The force merge API allows to force merging of one or more indices through
581543-- an API. The merge relates to the number of segments a Lucene index holds
582544-- within each shard. The force merge operation allows to reduce the number of
@@ -638,25 +600,20 @@ existentialQuery url = do
638600-- responses from elasticsearch should fall into these two
639601-- categories. If they don't, a 'EsProtocolException' will be
640602-- thrown. If you encounter this, please report the full body it
641−-- reports along with your Elasticsearch verison.
642−parseEsResponse :: ( MonadThrow m
643− , FromJSON a
644− )
645− => Reply
603+-- reports along with your ElasticSearch verison.
604+parseEsResponse :: (MonadThrow m, FromJSON a) => Reply
646605 -> m (Either EsError a)
647606parseEsResponse reply
648607 | respIsTwoHunna reply = case eitherDecode body of
649608 Right a -> return (Right a)
650− Left err ->
651− tryParseError err
652− | otherwise = tryParseError "Non-200 status code"
609+ Left _ -> tryParseError
610+ | otherwise = tryParseError
653611 where body = responseBody reply
654− tryParseError originalError
655− = case eitherDecode body of
656− Right e -> return (Left e)
657− -- Failed to parse the error message.
658− Left err -> explode ("Original error was: " <> originalError <> " Error parse failure was: " <> err)
659− explode errorMsg = throwM (EsProtocolException (T.pack errorMsg) body)
612+ tryParseError = case eitherDecode body of
613+ Right e -> return (Left e)
614+ -- this case should not be possible
615+ Left _ -> explode
616+ explode = throwM (EsProtocolException body)
660617
661618-- | 'indexExists' enables you to check if an index exists. Returns 'Bool'
662619-- in IO
@@ -715,16 +672,14 @@ listIndices :: (MonadThrow m, MonadBH m) => m [IndexName]
715672listIndices =
716673 parse . responseBody =<< get =<< url
717674 where
718− url = joinPath ["_cat/indices?format=json"]
719− parse body = either (\msg -> (throwM (EsProtocolException (T.pack msg) body))) return $ do
720− vals <- eitherDecode body
721− forM vals $ \val ->
722− case val of
723− Object obj ->
724− case HM.lookup "index" obj of
725− (Just (String txt)) -> Right (IndexName txt)
726− v -> Left $ "indexVal in listIndices failed on non-string, was: " <> show v
727− v -> Left $ "One of the values parsed in listIndices wasn't an object, it was: " <> show v
675+ url = joinPath ["_cat/indices?v"]
676+ -- parses the tabular format the indices api provides
677+ parse body = case T.lines (T.decodeUtf8 (L.toStrict body)) of
678+ (hdr:rows) -> let ks = T.words hdr
679+ keyedRows = [ HM.fromList (zip ks (T.words row)) | row <- rows ]
680+ names = catMaybes (HM.lookup "index" <$> keyedRows)
681+ in return (IndexName <$> names)
682+ [] -> throwM (EsProtocolException body)
728683
729684-- | 'updateIndexAliases' updates the server's index alias
730685-- table. Operations are atomic. Explained in further detail at
@@ -755,12 +710,6 @@ getIndexAliases :: (MonadBH m, MonadThrow m)
755710getIndexAliases = parseEsResponse =<< get =<< url
756711 where url = joinPath ["_aliases"]
757712
758−-- | Delete a single alias, removing it from all indices it
759−-- is currently associated with.
760−deleteIndexAlias :: MonadBH m => IndexAliasName -> m Reply
761−deleteIndexAlias (IndexAliasName (IndexName name)) = delete =<< url
762− where url = joinPath ["_all","_alias",name]
763−
764713-- | 'putTemplate' creates a template given an 'IndexTemplate' and a 'TemplateName'.
765714-- Explained in further detail at
766715-- <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html>
@@ -823,10 +772,7 @@ versionCtlParams cfg =
823772-- | 'indexDocument' is the primary way to save a single document in
824773-- Elasticsearch. The document itself is simply something we can
825774-- convert into a JSON 'Value'. The 'DocId' will function as the
826−-- primary key for the document. You are encouraged to generate
827−-- your own id's and not rely on Elasticsearch's automatic id
828−-- generation. Read more about it here:
829−-- https://github.com/bitemyapp/bloodhound/issues/107
775+-- primary key for the document.
830776--
831777-- >>> resp <- runBH' $ indexDocument testIndex testMapping defaultIndexDocumentSettings exampleTweet (DocId "1")
832778-- >>> print resp
@@ -874,11 +820,9 @@ deleteDocument (IndexName indexName)
874820-- >>> _ <- runBH' $ bulk stream
875821-- >>> _ <- runBH' $ refreshIndex testIndex
876822bulk :: MonadBH m => V.Vector BulkOperation -> m Reply
877−bulk bulkOps =
878− bindM2 post url (return body)
879− where
880− url = joinPath ["_bulk"]
881− body = Just $ encodeBulkOperations bulkOps
823+bulk bulkOps = bindM2 post url (return body)
824+ where url = joinPath ["_bulk"]
825+ body = Just $ encodeBulkOperations bulkOps
882826
883827-- | 'encodeBulkOperations' is a convenience function for dumping a vector of 'BulkOperation'
884828-- into an 'L.ByteString'
@@ -888,15 +832,12 @@ bulk bulkOps =
888832-- "\n{\"index\":{\"_type\":\"tweet\",\"_id\":\"2\",\"_index\":\"twitter\"}}\n{\"name\":\"blah\"}\n"
889833encodeBulkOperations :: V.Vector BulkOperation -> L.ByteString
890834encodeBulkOperations stream = collapsed where
891− blobs =
892− fmap encodeBulkOperation stream
893− mashedTaters =
894− mash (mempty :: Builder) blobs
895− collapsed =
896− toLazyByteString $ mappend mashedTaters (byteString "\n")
835+ blobs = fmap encodeBulkOperation stream
836+ mashedTaters = mash (mempty :: Builder) blobs
837+ collapsed = toLazyByteString $ mappend mashedTaters (byteString "\n")
897838
898839mash :: Builder -> V.Vector L.ByteString -> Builder
899−mash = V.foldl' (\b x -> b <> byteString "\n" <> lazyByteString x)
840+mash = V.foldl' (\b x -> b `mappend` (byteString "\n") `mappend` (lazyByteString x))
900841
901842mkBulkStreamValue :: Text -> Text -> Text -> Text -> Value
902843mkBulkStreamValue operation indexName mappingName docId =
@@ -905,12 +846,6 @@ mkBulkStreamValue operation indexName mappingName docId =
905846 , "_type" .= mappingName
906847 , "_id" .= docId]]
907848
908−mkBulkStreamValueAuto :: Text -> Text -> Text -> Value
909−mkBulkStreamValueAuto operation indexName mappingName =
910− object [operation .=
911− object [ "_index" .= indexName
912− , "_type" .= mappingName]]
913−
914849-- | 'encodeBulkOperation' is a convenience function for dumping a single 'BulkOperation'
915850-- into an 'L.ByteString'
916851--
@@ -924,18 +859,6 @@ encodeBulkOperation (BulkIndex (IndexName indexName)
924859 where metadata = mkBulkStreamValue "index" indexName mappingName docId
925860 blob = encode metadata `mappend` "\n" `mappend` encode value
926861
927−encodeBulkOperation (BulkIndexAuto (IndexName indexName)
928− (MappingName mappingName)
929− value) = blob
930− where metadata = mkBulkStreamValueAuto "index" indexName mappingName
931− blob = encode metadata `mappend` "\n" `mappend` encode value
932−
933−encodeBulkOperation (BulkIndexEncodingAuto (IndexName indexName)
934− (MappingName mappingName)
935− encoding) = toLazyByteString blob
936− where metadata = toEncoding (mkBulkStreamValueAuto "index" indexName mappingName)
937− blob = fromEncoding metadata <> "\n" <> fromEncoding encoding
938−
939862encodeBulkOperation (BulkCreate (IndexName indexName)
940863 (MappingName mappingName)
941864 (DocId docId) value) = blob
@@ -955,12 +878,6 @@ encodeBulkOperation (BulkUpdate (IndexName indexName)
955878 doc = object ["doc" .= value]
956879 blob = encode metadata `mappend` "\n" `mappend` encode doc
957880
958−encodeBulkOperation (BulkCreateEncoding (IndexName indexName)
959− (MappingName mappingName)
960− (DocId docId) encoding) = toLazyByteString blob
961− where metadata = toEncoding (mkBulkStreamValue "create" indexName mappingName docId)
962− blob = fromEncoding metadata <> "\n" <> fromEncoding encoding
963−
964881-- | 'getDocument' is a straight-forward way to fetch a single document from
965882-- Elasticsearch using a 'Server', 'IndexName', 'MappingName', and a 'DocId'.
966883-- The 'DocId' is the primary key for your Elasticsearch document.
@@ -1010,15 +927,6 @@ searchByIndex :: MonadBH m => IndexName -> Search -> m Reply
1010927searchByIndex (IndexName indexName) = bindM2 dispatchSearch url . return
1011928 where url = joinPath [indexName, "_search"]
1012929
1013−-- | 'searchByIndices' is a variant of 'searchByIndex' that executes a
1014−-- 'Search' over many indices. This is much faster than using
1015−-- 'mapM' to 'searchByIndex' over a collection since it only
1016−-- causes a single HTTP request to be emitted.
1017−searchByIndices :: MonadBH m => NonEmpty IndexName -> Search -> m Reply
1018−searchByIndices ixs = bindM2 dispatchSearch url . return
1019− where url = joinPath [renderedIxs, "_search"]
1020− renderedIxs = T.intercalate (T.singleton ',') (map (\(IndexName t) -> t) (toList ixs))
1021−
1022930-- | 'searchByType', given a 'Search', 'IndexName', and 'MappingName', will perform that
1023931-- search against a specific mapping within an index on an Elasticsearch server.
1024932--
@@ -1035,10 +943,10 @@ searchByType (IndexName indexName)
1035943-- search results. Note that the search is put into 'SearchTypeScan'
1036944-- mode and thus results will not be sorted. Combine this with
1037945-- 'advanceScroll' to efficiently stream through the full result set
1038−getInitialScroll ::
1039− (FromJSON a, MonadThrow m, MonadBH m) => IndexName ->
1040− MappingName ->
1041− Search ->
946+getInitialScroll ::
947+ (FromJSON a, MonadThrow m, MonadBH m) => IndexName ->
948+ MappingName ->
949+ Search ->
1042950 m (Either EsError (SearchResult a))
1043951getInitialScroll (IndexName indexName) (MappingName mappingName) search' = do
1044952 let url = addQuery params <$> joinPath [indexName, mappingName, "_search"]
@@ -1063,7 +971,7 @@ getInitialSortedScroll (IndexName indexName) (MappingName mappingName) search =
1063971 resp' <- bindM2 dispatchSearch url (return search)
1064972 parseEsResponse resp'
1065973
1066−scroll' :: (FromJSON a, MonadBH m, MonadThrow m) => Maybe ScrollId ->
974+scroll' :: (FromJSON a, MonadBH m, MonadThrow m) => Maybe ScrollId ->
1067975 m ([Hit a], Maybe ScrollId)
1068976scroll' Nothing = return ([], Nothing)
1069977scroll' (Just sid) = do
@@ -1090,13 +998,13 @@ advanceScroll (ScrollId sid) scroll = do
1090998 where scrollTime = showText secs <> "s"
1091999 secs :: Integer
10921000 secs = round scroll
1093−
1001+
10941002 scrollObject = object [ "scroll" .= scrollTime
10951003 , "scroll_id" .= sid
10961004 ]
10971005
1098−simpleAccumulator ::
1099− (FromJSON a, MonadBH m, MonadThrow m) =>
1006+simpleAccumulator ::
1007+ (FromJSON a, MonadBH m, MonadThrow m) =>
11001008 [Hit a] ->
11011009 ([Hit a], Maybe ScrollId) ->
11021010 m ([Hit a], Maybe ScrollId)
@@ -1134,9 +1042,9 @@ scanSearch indexName mappingName search = do
11341042--
11351043-- >>> let query = TermQuery (Term "user" "bitemyapp") Nothing
11361044-- >>> mkSearch (Just query) Nothing
1137−-- Search {queryBody = Just (TermQuery (Term {termField = "user", termValue = "bitemyapp"}) Nothing), filterBody = Nothing, searchAfterKey = Nothing, sortBody = Nothing, aggBody = Nothing, highlight = Nothing, trackSortScores = False, from = From 0, size = Size 10, searchType = SearchTypeQueryThenFetch, fields = Nothing, source = Nothing}
1045+-- 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, searchType = SearchTypeQueryThenFetch, fields = Nothing, source = Nothing}
11381046mkSearch :: Maybe Query -> Maybe Filter -> Search
1139−mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing
1047+mkSearch query filter = Search query filter Nothing Nothing Nothing False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing
11401048
11411049-- | 'mkAggregateSearch' is a helper function that defaults everything in a 'Search' except for
11421050-- the 'Query' and the 'Aggregation'.
@@ -1146,7 +1054,7 @@ mkSearch query filter = Search query filter Nothing Nothing Nothing False (From
11461054-- TermsAgg (TermsAggregation {term = Left "user", termInclude = Nothing, termExclude = Nothing, termOrder = Nothing, termMinDocCount = Nothing, termSize = Nothing, termShardSize = Nothing, termCollectMode = Just BreadthFirst, termExecutionHint = Nothing, termAggs = Nothing})
11471055-- >>> let myAggregation = mkAggregateSearch Nothing $ mkAggregations "users" terms
11481056mkAggregateSearch :: Maybe Query -> Aggregations -> Search
1149−mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0) SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing
1057+mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSearchAggs) Nothing False (From 0) (Size 0) SearchTypeQueryThenFetch Nothing Nothing
11501058
11511059-- | 'mkHighlightSearch' is a helper function that defaults everything in a 'Search' except for
11521060-- the 'Query' and the 'Aggregation'.
@@ -1155,7 +1063,7 @@ mkAggregateSearch query mkSearchAggs = Search query Nothing Nothing (Just mkSear
11551063-- >>> let testHighlight = Highlights Nothing [FieldHighlight (FieldName "message") Nothing]
11561064-- >>> let search = mkHighlightSearch (Just query) testHighlight
11571065mkHighlightSearch :: Maybe Query -> Highlights -> Search
1158−mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing Nothing Nothing Nothing
1066+mkHighlightSearch query searchHighlights = Search query Nothing Nothing Nothing (Just searchHighlights) False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing
11591067
11601068-- | 'pageSearch' is a helper function that takes a search and assigns the from
11611069-- and size fields for the search. The from parameter defines the offset
src/Database/V5/Bloodhound/Types.hsnew file+5097
@@ -0,0 +1,5097 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+{-# LANGUAGE MultiParamTypeClasses #-}
7+-- {-# LANGUAGE NoMonomorphismRestriction #-}
8+{-# LANGUAGE NamedFieldPuns #-}
9+{-# LANGUAGE OverloadedStrings #-}
10+{-# LANGUAGE RecordWildCards #-}
11+{-# LANGUAGE UndecidableInstances #-}
12+
13+-------------------------------------------------------------------------------
14+-- |
15+-- Module : Database.Bloodhound.Types
16+-- Copyright : (C) 2014, 2015, 2016 Chris Allen
17+-- License : BSD-style (see the file LICENSE)
18+-- Maintainer : Chris Allen <cma@bitemyapp.com
19+-- Stability : provisional
20+-- Portability : DeriveGeneric, RecordWildCards
21+--
22+-- Data types for describing actions and data structures performed to interact
23+-- with Elasticsearch. The two main buckets your queries against Elasticsearch
24+-- will fall into are 'Query's and 'Filter's. 'Filter's are more like
25+-- traditional database constraints and often have preferable performance
26+-- properties. 'Query's support human-written textual queries, such as fuzzy
27+-- queries.
28+-------------------------------------------------------------------------------
29+
30+
31+
32+module Database.V5.Bloodhound.Types
33+ ( defaultCache
34+ , defaultIndexSettings
35+ , defaultIndexDocumentSettings
36+ , mkSort
37+ , showText
38+ , unpackId
39+ , mkMatchQuery
40+ , mkMultiMatchQuery
41+ , mkBoolQuery
42+ , mkRangeQuery
43+ , mkQueryStringQuery
44+ , mkAggregations
45+ , mkTermsAggregation
46+ , mkTermsScriptAggregation
47+ , mkDateHistogram
48+ , mkCardinalityAggregation
49+ , mkDocVersion
50+ , mkStatsAggregation
51+ , mkExtendedStatsAggregation
52+ , docVersionNumber
53+ , toMissing
54+ , toTerms
55+ , toDateHistogram
56+ , toTopHits
57+ , omitNulls
58+ , BH(..)
59+ , runBH
60+ , BHEnv
61+ , bhServer
62+ , bhManager
63+ , bhRequestHook
64+ , mkBHEnv
65+ , MonadBH(..)
66+ , Version(..)
67+ , VersionNumber(..)
68+ , MaybeNA(..)
69+ , BuildHash(..)
70+ , Status(..)
71+ , Existence(..)
72+ , NullValue(..)
73+ , IndexSettings(..)
74+ , UpdatableIndexSetting(..)
75+ , IndexSettingsSummary(..)
76+ , AllocationPolicy(..)
77+ , ReplicaBounds(..)
78+ , Bytes(..)
79+ , gigabytes
80+ , megabytes
81+ , kilobytes
82+ , FSType(..)
83+ , InitialShardCount(..)
84+ , NodeAttrFilter(..)
85+ , NodeAttrName(..)
86+ , CompoundFormat(..)
87+ , IndexTemplate(..)
88+ , Server(..)
89+ , Reply
90+ , EsResult(..)
91+ , EsResultFound(..)
92+ , EsError(..)
93+ , EsProtocolException(..)
94+ , IndexAlias(..)
95+ , IndexAliasName(..)
96+ , IndexAliasAction(..)
97+ , IndexAliasCreate(..)
98+ , IndexAliasSummary(..)
99+ , IndexAliasesSummary(..)
100+ , AliasRouting(..)
101+ , SearchAliasRouting(..)
102+ , IndexAliasRouting(..)
103+ , RoutingValue(..)
104+ , DocVersion
105+ , ExternalDocVersion(..)
106+ , VersionControl(..)
107+ , DocumentParent(..)
108+ , IndexDocumentSettings(..)
109+ , Query(..)
110+ , Search(..)
111+ , SearchType(..)
112+ , SearchResult(..)
113+ , ScrollId(..)
114+ , SearchHits(..)
115+ , TrackSortScores
116+ , From(..)
117+ , Size(..)
118+ , Source(..)
119+ , PatternOrPatterns(..)
120+ , Include(..)
121+ , Exclude(..)
122+ , Pattern(..)
123+ , ShardResult(..)
124+ , Hit(..)
125+ , Filter(..)
126+ , Seminearring(..)
127+ , BoolMatch(..)
128+ , Term(..)
129+ , GeoPoint(..)
130+ , GeoBoundingBoxConstraint(..)
131+ , GeoBoundingBox(..)
132+ , GeoFilterType(..)
133+ , Distance(..)
134+ , DistanceUnit(..)
135+ , DistanceType(..)
136+ , DistanceRange(..)
137+ , OptimizeBbox(..)
138+ , LatLon(..)
139+ , RangeValue(..)
140+ , RangeExecution(..)
141+ , LessThan(..)
142+ , LessThanEq(..)
143+ , GreaterThan(..)
144+ , GreaterThanEq(..)
145+ , LessThanD(..)
146+ , LessThanEqD(..)
147+ , GreaterThanD(..)
148+ , GreaterThanEqD(..)
149+ , Regexp(..)
150+ , RegexpFlags(..)
151+ , RegexpFlag(..)
152+ , FieldName(..)
153+ , Script(..)
154+ , IndexName(..)
155+ , IndexSelection(..)
156+ , NodeSelection(..)
157+ , NodeSelector(..)
158+ , ForceMergeIndexSettings(..)
159+ , defaultForceMergeIndexSettings
160+ , TemplateName(..)
161+ , TemplatePattern(..)
162+ , MappingName(..)
163+ , DocId(..)
164+ , CacheName(..)
165+ , CacheKey(..)
166+ , BulkOperation(..)
167+ , ReplicaCount(..)
168+ , ShardCount(..)
169+ , Sort
170+ , SortMode(..)
171+ , SortOrder(..)
172+ , SortSpec(..)
173+ , DefaultSort(..)
174+ , Missing(..)
175+ , OpenCloseIndex(..)
176+ , Method
177+ , Boost(..)
178+ , MatchQuery(..)
179+ , MultiMatchQuery(..)
180+ , BoolQuery(..)
181+ , BoostingQuery(..)
182+ , CommonTermsQuery(..)
183+ , DisMaxQuery(..)
184+ , FuzzyLikeThisQuery(..)
185+ , FuzzyLikeFieldQuery(..)
186+ , FuzzyQuery(..)
187+ , HasChildQuery(..)
188+ , HasParentQuery(..)
189+ , IndicesQuery(..)
190+ , MoreLikeThisQuery(..)
191+ , MoreLikeThisFieldQuery(..)
192+ , NestedQuery(..)
193+ , PrefixQuery(..)
194+ , QueryStringQuery(..)
195+ , SimpleQueryStringQuery(..)
196+ , RangeQuery(..)
197+ , RegexpQuery(..)
198+ , QueryString(..)
199+ , TemplateQueryInline(..)
200+ , TemplateQueryKeyValuePairs(..)
201+ , BooleanOperator(..)
202+ , ZeroTermsQuery(..)
203+ , CutoffFrequency(..)
204+ , Analyzer(..)
205+ , MaxExpansions(..)
206+ , Lenient(..)
207+ , MatchQueryType(..)
208+ , MultiMatchQueryType(..)
209+ , Tiebreaker(..)
210+ , MinimumMatch(..)
211+ , DisableCoord(..)
212+ , CommonMinimumMatch(..)
213+ , MinimumMatchHighLow(..)
214+ , PrefixLength(..)
215+ , Fuzziness(..)
216+ , IgnoreTermFrequency(..)
217+ , MaxQueryTerms(..)
218+ , ScoreType(..)
219+ , Score
220+ , Cache
221+ , TypeName(..)
222+ , BoostTerms(..)
223+ , MaxWordLength(..)
224+ , MinWordLength(..)
225+ , MaxDocFrequency(..)
226+ , MinDocFrequency(..)
227+ , PhraseSlop(..)
228+ , StopWord(..)
229+ , QueryPath(..)
230+ , MinimumTermFrequency(..)
231+ , PercentMatch(..)
232+ , FieldDefinition(..)
233+ , MappingField(..)
234+ , Mapping(..)
235+ , AllowLeadingWildcard(..)
236+ , LowercaseExpanded(..)
237+ , GeneratePhraseQueries(..)
238+ , Locale(..)
239+ , AnalyzeWildcard(..)
240+ , EnablePositionIncrements(..)
241+ , SimpleQueryFlag(..)
242+ , FieldOrFields(..)
243+ , Monoid(..)
244+ , ToJSON(..)
245+ , Interval(..)
246+ , TimeInterval(..)
247+ , ExecutionHint(..)
248+ , CollectionMode(..)
249+ , TermOrder(..)
250+ , TermInclusion(..)
251+ , SnapshotRepoSelection(..)
252+ , GenericSnapshotRepo(..)
253+ , SnapshotRepo(..)
254+ , SnapshotRepoConversionError(..)
255+ , SnapshotRepoType(..)
256+ , GenericSnapshotRepoSettings(..)
257+ , SnapshotRepoUpdateSettings(..)
258+ , defaultSnapshotRepoUpdateSettings
259+ , SnapshotRepoName(..)
260+ , SnapshotRepoPattern(..)
261+ , SnapshotVerification(..)
262+ , SnapshotNodeVerification(..)
263+ , FullNodeId(..)
264+ , NodeName(..)
265+ , ClusterName(..)
266+ , NodesInfo(..)
267+ , NodesStats(..)
268+ , NodeStats(..)
269+ , NodeBreakersStats(..)
270+ , NodeBreakerStats(..)
271+ , NodeHTTPStats(..)
272+ , NodeTransportStats(..)
273+ , NodeFSStats(..)
274+ , NodeDataPathStats(..)
275+ , NodeFSTotalStats(..)
276+ , NodeNetworkStats(..)
277+ , NodeThreadPoolsStats(..)
278+ , NodeThreadPoolStats(..)
279+ , NodeJVMStats(..)
280+ , JVMBufferPoolStats(..)
281+ , JVMGCStats(..)
282+ , JVMPoolStats(..)
283+ , NodeProcessStats(..)
284+ , NodeOSStats(..)
285+ , LoadAvgs(..)
286+ , NodeIndicesStats(..)
287+ , EsAddress(..)
288+ , PluginName(..)
289+ , NodeInfo(..)
290+ , NodePluginInfo(..)
291+ , NodeHTTPInfo(..)
292+ , NodeTransportInfo(..)
293+ , BoundTransportAddress(..)
294+ , NodeNetworkInfo(..)
295+ , MacAddress(..)
296+ , NetworkInterfaceName(..)
297+ , NodeNetworkInterface(..)
298+ , NodeThreadPoolsInfo(..)
299+ , NodeThreadPoolInfo(..)
300+ , ThreadPoolSize(..)
301+ , ThreadPoolType(..)
302+ , NodeJVMInfo(..)
303+ , JVMMemoryPool(..)
304+ , JVMGCCollector(..)
305+ , JVMMemoryInfo(..)
306+ , PID(..)
307+ , NodeOSInfo(..)
308+ , CPUInfo(..)
309+ , NodeProcessInfo(..)
310+ , FsSnapshotRepo(..)
311+ , SnapshotCreateSettings(..)
312+ , defaultSnapshotCreateSettings
313+ , SnapshotSelection(..)
314+ , SnapshotPattern(..)
315+ , SnapshotInfo(..)
316+ , SnapshotShardFailure(..)
317+ , ShardId(..)
318+ , SnapshotName(..)
319+ , SnapshotState(..)
320+ , SnapshotRestoreSettings(..)
321+ , defaultSnapshotRestoreSettings
322+ , RestoreRenamePattern(..)
323+ , RestoreRenameToken(..)
324+ , RRGroupRefNum
325+ , rrGroupRefNum
326+ , mkRRGroupRefNum
327+ , RestoreIndexSettings(..)
328+
329+ , Aggregation(..)
330+ , Aggregations
331+ , AggregationResults
332+ , BucketValue(..)
333+ , Bucket(..)
334+ , BucketAggregation(..)
335+ , TermsAggregation(..)
336+ , MissingAggregation(..)
337+ , ValueCountAggregation(..)
338+ , FilterAggregation(..)
339+ , CardinalityAggregation(..)
340+ , DateHistogramAggregation(..)
341+ , DateRangeAggregation(..)
342+ , DateRangeAggRange(..)
343+ , DateMathExpr(..)
344+ , DateMathAnchor(..)
345+ , DateMathModifier(..)
346+ , DateMathUnit(..)
347+ , TopHitsAggregation(..)
348+ , StatisticsAggregation(..)
349+
350+ , Highlights(..)
351+ , FieldHighlight(..)
352+ , HighlightSettings(..)
353+ , PlainHighlight(..)
354+ , PostingsHighlight(..)
355+ , FastVectorHighlight(..)
356+ , CommonHighlight(..)
357+ , NonPostings(..)
358+ , HighlightEncoder(..)
359+ , HighlightTag(..)
360+ , HitHighlight
361+
362+ , MissingResult(..)
363+ , TermsResult(..)
364+ , DateHistogramResult(..)
365+ , DateRangeResult(..)
366+ , TopHitResult(..)
367+
368+ , EsUsername(..)
369+ , EsPassword(..)
370+ ) where
371+
372+import Control.Applicative as A
373+import Control.Arrow (first)
374+import Control.Monad.Catch
375+import Control.Monad.Except
376+import Control.Monad.Reader (MonadReader (..),
377+ ReaderT (..))
378+import Control.Monad.State (MonadState)
379+import Control.Monad.Writer (MonadWriter)
380+import Data.Aeson
381+import Data.Aeson.Types (Pair, Parser,
382+ emptyObject,
383+ parseEither, parseMaybe)
384+import qualified Data.ByteString.Lazy.Char8 as L
385+import Data.Char
386+import Data.Hashable (Hashable)
387+import qualified Data.HashMap.Strict as HM
388+import Data.List (foldl', intercalate,
389+ nub)
390+import Data.List.NonEmpty (NonEmpty (..), toList)
391+import qualified Data.Map.Strict as M
392+import Data.Maybe
393+import Data.Scientific (Scientific)
394+import Data.Semigroup
395+import Data.Text (Text)
396+import qualified Data.Text as T
397+import Data.Time.Calendar
398+import Data.Time.Clock (NominalDiffTime,
399+ UTCTime)
400+import Data.Time.Clock.POSIX
401+import qualified Data.Traversable as DT
402+import Data.Typeable (Typeable)
403+import qualified Data.Vector as V
404+import qualified Data.Version as Vers
405+import GHC.Enum
406+import GHC.Generics (Generic)
407+import Network.HTTP.Client
408+import qualified Network.HTTP.Types.Method as NHTM
409+import qualified Text.ParserCombinators.ReadP as RP
410+import qualified Text.Read as TR
411+
412+import Database.V5.Bloodhound.Types.Class
413+import Database.V5.Bloodhound.Types.Internal
414+
415+-- $setup
416+-- >>> :set -XOverloadedStrings
417+-- >>> import Data.Aeson
418+-- >>> import Database.V5.Bloodhound
419+-- >>> let testServer = (Server "http://localhost:9200")
420+-- >>> let testIndex = IndexName "twitter"
421+-- >>> let testMapping = MappingName "tweet"
422+-- >>> let defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
423+
424+-- defaultIndexSettings is exported by Database.Bloodhound as well
425+-- no trailing slashes in servers, library handles building the path.
426+
427+-- | Create a 'BHEnv' with all optional fields defaulted. HTTP hook
428+-- will be a noop. You can use the exported fields to customize it further, e.g.:
429+--
430+-- >> (mkBHEnv myServer myManager) { bhRequestHook = customHook }
431+mkBHEnv :: Server -> Manager -> BHEnv
432+mkBHEnv s m = BHEnv s m return
433+
434+newtype BH m a = BH {
435+ unBH :: ReaderT BHEnv m a
436+ } deriving ( Functor
437+ , A.Applicative
438+ , Monad
439+ , MonadIO
440+ , MonadState s
441+ , MonadWriter w
442+ , MonadError e
443+ , Alternative
444+ , MonadPlus
445+ , MonadFix
446+ , MonadThrow
447+ , MonadCatch
448+ , MonadMask)
449+
450+instance MonadTrans BH where
451+ lift = BH . lift
452+
453+instance (MonadReader r m) => MonadReader r (BH m) where
454+ ask = lift ask
455+ local f (BH (ReaderT m)) = BH $ ReaderT $ \r ->
456+ local f (m r)
457+
458+instance (Functor m, Applicative m, MonadIO m) => MonadBH (BH m) where
459+ getBHEnv = BH getBHEnv
460+
461+runBH :: BHEnv -> BH m a -> m a
462+runBH e f = runReaderT (unBH f) e
463+
464+{-| 'Version' is embedded in 'Status' -}
465+data Version = Version { number :: VersionNumber
466+ , build_hash :: BuildHash
467+ , build_date :: UTCTime
468+ , build_snapshot :: Bool
469+ , lucene_version :: VersionNumber } deriving (Eq, Read, Show, Generic, Typeable)
470+
471+-- | Traditional software versioning number
472+newtype VersionNumber = VersionNumber { versionNumber :: Vers.Version
473+ } deriving (Eq, Read, Show, Generic, Typeable, Ord)
474+
475+{-| 'Status' is a data type for describing the JSON body returned by
476+ Elasticsearch when you query its status. This was deprecated in 1.2.0.
477+
478+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html#indices-status>
479+-}
480+
481+data Status = Status { name :: Text
482+ , cluster_name :: Text
483+ , cluster_uuid :: Text
484+ , version :: Version
485+ , tagline :: Text } deriving (Eq, Read, Show, Generic)
486+
487+{-| 'IndexSettings' is used to configure the shards and replicas when you create
488+ an Elasticsearch Index.
489+
490+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html>
491+-}
492+
493+data IndexSettings =
494+ IndexSettings { indexShards :: ShardCount
495+ , indexReplicas :: ReplicaCount } deriving (Eq, Read, Show, Generic, Typeable)
496+
497+{-| 'defaultIndexSettings' is an 'IndexSettings' with 3 shards and 2 replicas. -}
498+defaultIndexSettings :: IndexSettings
499+defaultIndexSettings = IndexSettings (ShardCount 3) (ReplicaCount 2)
500+
501+
502+{-| 'ForceMergeIndexSettings' is used to configure index optimization. See
503+ <https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html>
504+ for more info.
505+-}
506+data ForceMergeIndexSettings =
507+ ForceMergeIndexSettings { maxNumSegments :: Maybe Int
508+ -- ^ Number of segments to optimize to. 1 will fully optimize the index. If omitted, the default behavior is to only optimize if the server deems it necessary.
509+ , onlyExpungeDeletes :: Bool
510+ -- ^ Should the optimize process only expunge segments with deletes in them? If the purpose of the optimization is to free disk space, this should be set to True.
511+ , flushAfterOptimize :: Bool
512+ -- ^ Should a flush be performed after the optimize.
513+ } deriving (Eq, Show, Generic, Typeable)
514+
515+
516+{-| 'defaultForceMergeIndexSettings' implements the default settings that
517+ ElasticSearch uses for index optimization. 'maxNumSegments' is Nothing,
518+ 'onlyExpungeDeletes' is False, and flushAfterOptimize is True.
519+-}
520+defaultForceMergeIndexSettings :: ForceMergeIndexSettings
521+defaultForceMergeIndexSettings = ForceMergeIndexSettings Nothing False True
522+
523+{-| 'UpdatableIndexSetting' are settings which may be updated after an index is created.
524+
525+ <https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html>
526+-}
527+data UpdatableIndexSetting = NumberOfReplicas ReplicaCount
528+ -- ^ The number of replicas each shard has.
529+ | AutoExpandReplicas ReplicaBounds
530+ | BlocksReadOnly Bool
531+ -- ^ Set to True to have the index read only. False to allow writes and metadata changes.
532+ | BlocksRead Bool
533+ -- ^ Set to True to disable read operations against the index.
534+ | BlocksWrite Bool
535+ -- ^ Set to True to disable write operations against the index.
536+ | BlocksMetaData Bool
537+ -- ^ Set to True to disable metadata operations against the index.
538+ | RefreshInterval NominalDiffTime
539+ -- ^ The async refresh interval of a shard
540+ | IndexConcurrency Int
541+ | FailOnMergeFailure Bool
542+ | TranslogFlushThresholdOps Int
543+ -- ^ When to flush on operations.
544+ | TranslogFlushThresholdSize Bytes
545+ -- ^ When to flush based on translog (bytes) size.
546+ | TranslogFlushThresholdPeriod NominalDiffTime
547+ -- ^ When to flush based on a period of not flushing.
548+ | TranslogDisableFlush Bool
549+ -- ^ Disables flushing. Note, should be set for a short interval and then enabled.
550+ | CacheFilterMaxSize (Maybe Bytes)
551+ -- ^ The maximum size of filter cache (per segment in shard).
552+ | CacheFilterExpire (Maybe NominalDiffTime)
553+ -- ^ The expire after access time for filter cache.
554+ | GatewaySnapshotInterval NominalDiffTime
555+ -- ^ The gateway snapshot interval (only applies to shared gateways).
556+ | RoutingAllocationInclude (NonEmpty NodeAttrFilter)
557+ -- ^ A node matching any rule will be allowed to host shards from the index.
558+ | RoutingAllocationExclude (NonEmpty NodeAttrFilter)
559+ -- ^ A node matching any rule will NOT be allowed to host shards from the index.
560+ | RoutingAllocationRequire (NonEmpty NodeAttrFilter)
561+ -- ^ Only nodes matching all rules will be allowed to host shards from the index.
562+ | RoutingAllocationEnable AllocationPolicy
563+ -- ^ Enables shard allocation for a specific index.
564+ | RoutingAllocationShardsPerNode ShardCount
565+ -- ^ Controls the total number of shards (replicas and primaries) allowed to be allocated on a single node.
566+ | RecoveryInitialShards InitialShardCount
567+ -- ^ When using local gateway a particular shard is recovered only if there can be allocated quorum shards in the cluster.
568+ | GCDeletes NominalDiffTime
569+ | TTLDisablePurge Bool
570+ -- ^ Disables temporarily the purge of expired docs.
571+ | TranslogFSType FSType
572+ | IndexCompoundFormat CompoundFormat
573+ | IndexCompoundOnFlush Bool
574+ | WarmerEnabled Bool
575+ deriving (Eq, Show, Generic, Typeable)
576+
577+data AllocationPolicy = AllocAll
578+ -- ^ Allows shard allocation for all shards.
579+ | AllocPrimaries
580+ -- ^ Allows shard allocation only for primary shards.
581+ | AllocNewPrimaries
582+ -- ^ Allows shard allocation only for primary shards for new indices.
583+ | AllocNone
584+ -- ^ No shard allocation is allowed
585+ deriving (Eq, Read, Show, Generic, Typeable)
586+
587+data ReplicaBounds = ReplicasBounded Int Int
588+ | ReplicasLowerBounded Int
589+ | ReplicasUnbounded
590+ deriving (Eq, Read, Show, Generic, Typeable)
591+
592+-- | A measure of bytes used for various configurations. You may want
593+-- to use smart constructors like 'gigabytes' for larger values.
594+--
595+-- >>> gigabytes 9
596+-- Bytes 9000000000
597+--
598+-- >>> megabytes 9
599+-- Bytes 9000000
600+--
601+-- >>> kilobytes 9
602+-- Bytes 9000
603+newtype Bytes = Bytes Int deriving (Eq, Read, Show, Generic, Typeable, Ord, ToJSON, FromJSON)
604+
605+gigabytes :: Int -> Bytes
606+gigabytes n = megabytes (1000 * n)
607+
608+
609+megabytes :: Int -> Bytes
610+megabytes n = kilobytes (1000 * n)
611+
612+
613+kilobytes :: Int -> Bytes
614+kilobytes n = Bytes (1000 * n)
615+
616+
617+data FSType = FSSimple
618+ | FSBuffered deriving (Eq, Read, Show, Generic, Typeable, Ord)
619+
620+data InitialShardCount = QuorumShards
621+ | QuorumMinus1Shards
622+ | FullShards
623+ | FullMinus1Shards
624+ | ExplicitShards Int
625+ deriving (Eq, Read, Show, Generic, Typeable)
626+
627+data NodeAttrFilter = NodeAttrFilter { nodeAttrFilterName :: NodeAttrName
628+ , nodeAttrFilterValues :: NonEmpty Text}
629+ deriving (Eq, Read, Show, Generic, Ord, Typeable)
630+
631+newtype NodeAttrName = NodeAttrName Text deriving (Eq, Read, Show, Ord, Generic, Typeable)
632+
633+data CompoundFormat = CompoundFileFormat Bool
634+ | MergeSegmentVsTotalIndex Double
635+ -- ^ percentage between 0 and 1 where 0 is false, 1 is true
636+ deriving (Eq, Read, Show, Generic, Typeable)
637+
638+newtype NominalDiffTimeJSON = NominalDiffTimeJSON { ndtJSON :: NominalDiffTime }
639+
640+data IndexSettingsSummary = IndexSettingsSummary { sSummaryIndexName :: IndexName
641+ , sSummaryFixedSettings :: IndexSettings
642+ , sSummaryUpdateable :: [UpdatableIndexSetting]}
643+ deriving (Eq, Show, Generic, Typeable)
644+
645+{-| 'Reply' and 'Method' are type synonyms from 'Network.HTTP.Types.Method.Method' -}
646+type Reply = Network.HTTP.Client.Response L.ByteString
647+type Method = NHTM.Method
648+
649+{-| 'OpenCloseIndex' is a sum type for opening and closing indices.
650+
651+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html>
652+-}
653+data OpenCloseIndex = OpenIndex | CloseIndex deriving (Eq, Read, Show, Generic, Typeable)
654+
655+data FieldType = GeoPointType
656+ | GeoShapeType
657+ | FloatType
658+ | IntegerType
659+ | LongType
660+ | ShortType
661+ | ByteType deriving (Eq, Read, Show, Generic, Typeable)
662+
663+data FieldDefinition =
664+ FieldDefinition { fieldType :: FieldType } deriving (Eq, Read, Show, Generic, Typeable)
665+
666+{-| An 'IndexTemplate' defines a template that will automatically be
667+ applied to new indices created. The templates include both
668+ 'IndexSettings' and mappings, and a simple 'TemplatePattern' that
669+ controls if the template will be applied to the index created.
670+ Specify mappings as follows: @[toJSON TweetMapping, ...]@
671+
672+ https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-templates.html
673+-}
674+data IndexTemplate =
675+ IndexTemplate { templatePattern :: TemplatePattern
676+ , templateSettings :: Maybe IndexSettings
677+ , templateMappings :: [Value]
678+ }
679+
680+data MappingField =
681+ MappingField { mappingFieldName :: FieldName
682+ , fieldDefinition :: FieldDefinition } deriving (Eq, Read, Show, Generic, Typeable)
683+
684+{-| Support for type reification of 'Mapping's is currently incomplete, for
685+ now the mapping API verbiage expects a 'ToJSON'able blob.
686+
687+ Indexes have mappings, mappings are schemas for the documents contained in the
688+ index. I'd recommend having only one mapping per index, always having a mapping,
689+ and keeping different kinds of documents separated if possible.
690+-}
691+data Mapping = Mapping { typeName :: TypeName
692+ , mappingFields :: [MappingField] } deriving (Eq, Read, Show, Generic, Typeable)
693+
694+{-| 'BulkOperation' is a sum type for expressing the four kinds of bulk
695+ operation index, create, delete, and update. 'BulkIndex' behaves like an
696+ "upsert", 'BulkCreate' will fail if a document already exists at the DocId.
697+
698+ <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk>
699+-}
700+data BulkOperation =
701+ BulkIndex IndexName MappingName DocId Value
702+ | BulkCreate IndexName MappingName DocId Value
703+ | BulkDelete IndexName MappingName DocId
704+ | BulkUpdate IndexName MappingName DocId Value deriving (Eq, Read, Show, Generic, Typeable)
705+
706+{-| 'EsResult' describes the standard wrapper JSON document that you see in
707+ successful Elasticsearch lookups or lookups that couldn't find the document.
708+-}
709+data EsResult a = EsResult { _index :: Text
710+ , _type :: Text
711+ , _id :: Text
712+ , foundResult :: Maybe (EsResultFound a)} deriving (Eq, Read, Show, Generic, Typeable)
713+
714+{-| 'EsResultFound' contains the document and its metadata inside of an
715+ 'EsResult' when the document was successfully found.
716+-}
717+data EsResultFound a = EsResultFound { _version :: DocVersion
718+ , _source :: a } deriving (Eq, Read, Show, Generic, Typeable)
719+
720+{-| 'EsError' is the generic type that will be returned when there was a
721+ problem. If you can't parse the expected response, its a good idea to
722+ try parsing this.
723+-}
724+data EsError = EsError { errorStatus :: Int
725+ , errorMessage :: Text } deriving (Eq, Read, Show, Generic, Typeable)
726+
727+{-| 'EsProtocolException' will be thrown if Bloodhound cannot parse a response
728+returned by the ElasticSearch server. If you encounter this error, please
729+verify that your domain data types and FromJSON instances are working properly
730+(for example, the 'a' of '[Hit a]' in 'SearchResult.searchHits.hits'). If you're
731+sure that your mappings are correct, then this error may be an indication of an
732+incompatibility between Bloodhound and ElasticSearch. Please open a bug report
733+and be sure to include the exception body.
734+-}
735+data EsProtocolException = EsProtocolException { esProtoExBody :: L.ByteString }
736+ deriving (Eq, Read, Show, Generic, Typeable)
737+
738+instance Exception EsProtocolException
739+
740+data IndexAlias = IndexAlias { srcIndex :: IndexName
741+ , indexAlias :: IndexAliasName } deriving (Eq, Read, Show, Generic, Typeable)
742+
743+newtype IndexAliasName = IndexAliasName { indexAliasName :: IndexName } deriving (Eq, Read, Show, Generic, ToJSON)
744+
745+data IndexAliasAction = AddAlias IndexAlias IndexAliasCreate
746+ | RemoveAlias IndexAlias deriving (Read, Show, Eq, Generic, Typeable)
747+
748+data IndexAliasCreate = IndexAliasCreate { aliasCreateRouting :: Maybe AliasRouting
749+ , aliasCreateFilter :: Maybe Filter}
750+ deriving (Read, Show, Eq, Generic, Typeable)
751+
752+data AliasRouting = AllAliasRouting RoutingValue
753+ | GranularAliasRouting (Maybe SearchAliasRouting) (Maybe IndexAliasRouting)
754+ deriving (Read, Show, Eq, Generic, Typeable)
755+
756+newtype SearchAliasRouting = SearchAliasRouting (NonEmpty RoutingValue) deriving (Read, Show, Eq, Generic, Typeable)
757+
758+newtype IndexAliasRouting = IndexAliasRouting RoutingValue deriving (Read, Show, Eq, Generic, ToJSON, FromJSON, Typeable)
759+
760+newtype RoutingValue = RoutingValue { routingValue :: Text } deriving (Read, Show, Eq, Generic, ToJSON, FromJSON, Typeable)
761+
762+newtype IndexAliasesSummary = IndexAliasesSummary { indexAliasesSummary :: [IndexAliasSummary] } deriving (Read, Show, Eq, Generic, Typeable)
763+
764+{-| 'IndexAliasSummary' is a summary of an index alias configured for a server. -}
765+data IndexAliasSummary = IndexAliasSummary { indexAliasSummaryAlias :: IndexAlias
766+ , indexAliasSummaryCreate :: IndexAliasCreate} deriving (Read, Show, Eq, Generic, Typeable)
767+
768+{-| 'DocVersion' is an integer version number for a document between 1
769+and 9.2e+18 used for <<https://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html optimistic concurrency control>>.
770+-}
771+newtype DocVersion = DocVersion {
772+ docVersionNumber :: Int
773+ } deriving (Eq, Read, Show, Generic, Ord, ToJSON)
774+
775+-- | Smart constructor for in-range doc version
776+mkDocVersion :: Int -> Maybe DocVersion
777+mkDocVersion i
778+ | i >= (docVersionNumber minBound) && i <= (docVersionNumber maxBound) =
779+ Just $ DocVersion i
780+ | otherwise = Nothing
781+
782+
783+{-| 'ExternalDocVersion' is a convenience wrapper if your code uses its
784+own version numbers instead of ones from ES.
785+-}
786+newtype ExternalDocVersion = ExternalDocVersion DocVersion
787+ deriving (Eq, Read, Show, Generic, Ord, Bounded, Enum, ToJSON)
788+
789+{-| 'VersionControl' is specified when indexing documents as a
790+optimistic concurrency control.
791+-}
792+data VersionControl = NoVersionControl
793+ -- ^ Don't send a version. This is a pure overwrite.
794+ | InternalVersion DocVersion
795+ -- ^ Use the default ES versioning scheme. Only
796+ -- index the document if the version is the same
797+ -- as the one specified. Only applicable to
798+ -- updates, as you should be getting Version from
799+ -- a search result.
800+ | ExternalGT ExternalDocVersion
801+ -- ^ Use your own version numbering. Only index
802+ -- the document if the version is strictly higher
803+ -- OR the document doesn't exist. The given
804+ -- version will be used as the new version number
805+ -- for the stored document. N.B. All updates must
806+ -- increment this number, meaning there is some
807+ -- global, external ordering of updates.
808+ | ExternalGTE ExternalDocVersion
809+ -- ^ Use your own version numbering. Only index
810+ -- the document if the version is equal or higher
811+ -- than the stored version. Will succeed if there
812+ -- is no existing document. The given version will
813+ -- be used as the new version number for the
814+ -- stored document. Use with care, as this could
815+ -- result in data loss.
816+ | ForceVersion ExternalDocVersion
817+ -- ^ The document will always be indexed and the
818+ -- given version will be the new version. This is
819+ -- typically used for correcting errors. Use with
820+ -- care, as this could result in data loss.
821+ deriving (Read, Show, Eq, Generic, Ord)
822+
823+{-| 'DocumentParent' is used to specify a parent document.
824+-}
825+newtype DocumentParent = DocumentParent DocId
826+ deriving (Eq, Read, Show, Generic, Typeable)
827+
828+{-| 'IndexDocumentSettings' are special settings supplied when indexing
829+a document. For the best backwards compatiblity when new fields are
830+added, you should probably prefer to start with 'defaultIndexDocumentSettings'
831+-}
832+data IndexDocumentSettings =
833+ IndexDocumentSettings { idsVersionControl :: VersionControl
834+ , idsParent :: Maybe DocumentParent
835+ } deriving (Eq, Read, Show, Generic, Typeable)
836+
837+{-| Reasonable default settings. Chooses no version control and no parent.
838+-}
839+defaultIndexDocumentSettings :: IndexDocumentSettings
840+defaultIndexDocumentSettings = IndexDocumentSettings NoVersionControl Nothing
841+
842+{-| 'Sort' is a synonym for a list of 'SortSpec's. Sort behavior is order
843+ dependent with later sorts acting as tie-breakers for earlier sorts.
844+-}
845+type Sort = [SortSpec]
846+
847+{-| The two main kinds of 'SortSpec' are 'DefaultSortSpec' and
848+ 'GeoDistanceSortSpec'. The latter takes a 'SortOrder', 'GeoPoint', and
849+ 'DistanceUnit' to express "nearness" to a single geographical point as a
850+ sort specification.
851+
852+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
853+-}
854+data SortSpec = DefaultSortSpec DefaultSort
855+ | GeoDistanceSortSpec SortOrder GeoPoint DistanceUnit deriving (Eq, Read, Show, Generic, Typeable)
856+
857+{-| 'DefaultSort' is usually the kind of 'SortSpec' you'll want. There's a
858+ 'mkSort' convenience function for when you want to specify only the most
859+ common parameters.
860+
861+ The `ignoreUnmapped`, when `Just` field is used to set the elastic 'unmapped_type'
862+
863+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
864+-}
865+data DefaultSort =
866+ DefaultSort { sortFieldName :: FieldName
867+ , sortOrder :: SortOrder
868+ -- default False
869+ , ignoreUnmapped :: Maybe Text
870+ , sortMode :: Maybe SortMode
871+ , missingSort :: Maybe Missing
872+ , nestedFilter :: Maybe Filter } deriving (Eq, Read, Show, Generic, Typeable)
873+
874+{-| 'SortOrder' is 'Ascending' or 'Descending', as you might expect. These get
875+ encoded into "asc" or "desc" when turned into JSON.
876+
877+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#search-request-sort>
878+-}
879+data SortOrder = Ascending
880+ | Descending deriving (Eq, Read, Show, Generic, Typeable)
881+
882+{-| 'Missing' prescribes how to handle missing fields. A missing field can be
883+ sorted last, first, or using a custom value as a substitute.
884+
885+<http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_missing_values>
886+-}
887+data Missing = LastMissing
888+ | FirstMissing
889+ | CustomMissing Text deriving (Eq, Read, Show, Generic, Typeable)
890+
891+{-| 'SortMode' prescribes how to handle sorting array/multi-valued fields.
892+
893+http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_sort_mode_option
894+-}
895+data SortMode = SortMin
896+ | SortMax
897+ | SortSum
898+ | SortAvg deriving (Eq, Read, Show, Generic, Typeable)
899+
900+{-| 'mkSort' defaults everything but the 'FieldName' and the 'SortOrder' so
901+ that you can concisely describe the usual kind of 'SortSpec's you want.
902+-}
903+mkSort :: FieldName -> SortOrder -> DefaultSort
904+mkSort fieldName sOrder = DefaultSort fieldName sOrder Nothing Nothing Nothing Nothing
905+
906+{-| 'Cache' is for telling ES whether it should cache a 'Filter' not.
907+ 'Query's cannot be cached.
908+-}
909+type Cache = Bool -- caching on/off
910+defaultCache :: Cache
911+defaultCache = False
912+
913+{-| 'PrefixValue' is used in 'PrefixQuery' as the main query component.
914+-}
915+type PrefixValue = Text
916+
917+{-| 'BooleanOperator' is the usual And/Or operators with an ES compatible
918+ JSON encoding baked in. Used all over the place.
919+-}
920+data BooleanOperator = And | Or deriving (Eq, Read, Show, Generic, Typeable)
921+
922+{-| 'ShardCount' is part of 'IndexSettings'
923+-}
924+newtype ShardCount = ShardCount Int deriving (Eq, Read, Show, Generic, ToJSON, Typeable)
925+
926+{-| 'ReplicaCount' is part of 'IndexSettings'
927+-}
928+newtype ReplicaCount = ReplicaCount Int deriving (Eq, Read, Show, Generic, ToJSON, Typeable)
929+
930+{-| 'IndexName' is used to describe which index to query/create/delete
931+-}
932+newtype IndexName = IndexName Text deriving (Eq, Generic, Read, Show, ToJSON, FromJSON, Typeable)
933+
934+{-| 'IndexSelection' is used for APIs which take a single index, a list of
935+ indexes, or the special @_all@ index.
936+-}
937+--TODO: this does not fully support <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/multi-index.html multi-index syntax>. It wouldn't be too hard to implement but you'd have to add the optional parameters (ignore_unavailable, allow_no_indices, expand_wildcards) to any APIs using it. Also would be a breaking API.
938+data IndexSelection = IndexList (NonEmpty IndexName)
939+ | AllIndexes deriving (Eq, Generic, Show, Typeable)
940+
941+{-| 'NodeSelection' is used for most cluster APIs. See <https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html#cluster-nodes here> for more details.
942+-}
943+data NodeSelection = LocalNode
944+ -- ^ Whatever node receives this request
945+ | NodeList (NonEmpty NodeSelector)
946+ | AllNodes deriving (Eq, Generic, Show, Typeable)
947+
948+
949+-- | An exact match or pattern to identify a node. Note that All of
950+-- these options support wildcarding, so your node name, server, attr
951+-- name can all contain * characters to be a fuzzy match.
952+data NodeSelector = NodeByName NodeName
953+ | NodeByFullNodeId FullNodeId
954+ | NodeByHost Server
955+ -- ^ e.g. 10.0.0.1 or even 10.0.0.*
956+ | NodeByAttribute NodeAttrName Text
957+ -- ^ NodeAttrName can be a pattern, e.g. rack*. The value can too.
958+ deriving (Eq, Generic, Show, Typeable)
959+
960+{-| 'TemplateName' is used to describe which template to query/create/delete
961+-}
962+newtype TemplateName = TemplateName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
963+
964+{-| 'TemplatePattern' represents a pattern which is matched against index names
965+-}
966+newtype TemplatePattern = TemplatePattern Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
967+
968+{-| 'MappingName' is part of mappings which are how ES describes and schematizes
969+ the data in the indices.
970+-}
971+newtype MappingName = MappingName Text deriving (Eq, Generic, Read, Show, ToJSON, FromJSON, Typeable)
972+
973+{-| 'DocId' is a generic wrapper value for expressing unique Document IDs.
974+ Can be set by the user or created by ES itself. Often used in client
975+ functions for poking at specific documents.
976+-}
977+newtype DocId = DocId Text deriving (Eq, Generic, Read, Show, ToJSON, FromJSON, Typeable)
978+
979+{-| 'QueryString' is used to wrap query text bodies, be they human written or not.
980+-}
981+newtype QueryString = QueryString Text deriving (Eq, Generic, Read, Show, ToJSON, FromJSON, Typeable)
982+
983+{-| 'FieldName' is used all over the place wherever a specific field within
984+ a document needs to be specified, usually in 'Query's or 'Filter's.
985+-}
986+newtype FieldName = FieldName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
987+
988+
989+{-| 'Script' is often used in place of 'FieldName' to specify more
990+complex ways of extracting a value from a document.
991+-}
992+newtype Script = Script { scriptText :: Text } deriving (Eq, Read, Show, Generic, Typeable)
993+
994+{-| 'CacheName' is used in 'RegexpFilter' for describing the
995+ 'CacheKey' keyed caching behavior.
996+-}
997+newtype CacheName = CacheName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
998+
999+{-| 'CacheKey' is used in 'RegexpFilter' to key regex caching.
1000+-}
1001+newtype CacheKey =
1002+ CacheKey Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1003+newtype Existence =
1004+ Existence Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1005+newtype NullValue =
1006+ NullValue Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1007+newtype CutoffFrequency =
1008+ CutoffFrequency Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1009+newtype Analyzer =
1010+ Analyzer Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1011+newtype MaxExpansions =
1012+ MaxExpansions Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1013+
1014+{-| 'Lenient', if set to true, will cause format based failures to be
1015+ ignored. I don't know what the bloody default is, Elasticsearch
1016+ documentation didn't say what it was. Let me know if you figure it out.
1017+-}
1018+newtype Lenient =
1019+ Lenient Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1020+newtype Tiebreaker =
1021+ Tiebreaker Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1022+newtype Boost =
1023+ Boost Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1024+newtype BoostTerms =
1025+ BoostTerms Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1026+
1027+{-| 'MinimumMatch' controls how many should clauses in the bool query should
1028+ match. Can be an absolute value (2) or a percentage (30%) or a
1029+ combination of both.
1030+-}
1031+newtype MinimumMatch =
1032+ MinimumMatch Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1033+newtype DisableCoord =
1034+ DisableCoord Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1035+newtype IgnoreTermFrequency =
1036+ IgnoreTermFrequency Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1037+newtype MinimumTermFrequency =
1038+ MinimumTermFrequency Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1039+newtype MaxQueryTerms =
1040+ MaxQueryTerms Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1041+newtype Fuzziness =
1042+ Fuzziness Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1043+
1044+{-| 'PrefixLength' is the prefix length used in queries, defaults to 0. -}
1045+newtype PrefixLength =
1046+ PrefixLength Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1047+newtype TypeName =
1048+ TypeName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1049+newtype PercentMatch =
1050+ PercentMatch Double deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1051+newtype StopWord =
1052+ StopWord Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1053+newtype QueryPath =
1054+ QueryPath Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1055+
1056+{-| Allowing a wildcard at the beginning of a word (eg "*ing") is particularly
1057+ heavy, because all terms in the index need to be examined, just in case
1058+ they match. Leading wildcards can be disabled by setting
1059+ 'AllowLeadingWildcard' to false. -}
1060+newtype AllowLeadingWildcard =
1061+ AllowLeadingWildcard Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1062+newtype LowercaseExpanded =
1063+ LowercaseExpanded Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1064+newtype EnablePositionIncrements =
1065+ EnablePositionIncrements Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1066+
1067+{-| By default, wildcard terms in a query are not analyzed.
1068+ Setting 'AnalyzeWildcard' to true enables best-effort analysis.
1069+-}
1070+newtype AnalyzeWildcard = AnalyzeWildcard Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1071+
1072+{-| 'GeneratePhraseQueries' defaults to false.
1073+-}
1074+newtype GeneratePhraseQueries =
1075+ GeneratePhraseQueries Bool deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1076+
1077+{-| 'Locale' is used for string conversions - defaults to ROOT.
1078+-}
1079+newtype Locale = Locale Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1080+newtype MaxWordLength = MaxWordLength Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1081+newtype MinWordLength = MinWordLength Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1082+
1083+{-| 'PhraseSlop' sets the default slop for phrases, 0 means exact
1084+ phrase matches. Default is 0.
1085+-}
1086+newtype PhraseSlop = PhraseSlop Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1087+newtype MinDocFrequency = MinDocFrequency Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1088+newtype MaxDocFrequency = MaxDocFrequency Int deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1089+
1090+-- | Newtype wrapper to parse ES's concerning tendency to in some APIs return a floating point number of milliseconds since epoch ಠ_ಠ
1091+newtype POSIXMS = POSIXMS { posixMS :: UTCTime }
1092+
1093+{-| 'unpackId' is a silly convenience function that gets used once.
1094+-}
1095+unpackId :: DocId -> Text
1096+unpackId (DocId docId) = docId
1097+
1098+type TrackSortScores = Bool
1099+newtype From = From Int deriving (Eq, Read, Show, Generic, ToJSON)
1100+newtype Size = Size Int deriving (Eq, Read, Show, Generic, ToJSON)
1101+
1102+data Search = Search { queryBody :: Maybe Query
1103+ , filterBody :: Maybe Filter
1104+ , sortBody :: Maybe Sort
1105+ , aggBody :: Maybe Aggregations
1106+ , highlight :: Maybe Highlights
1107+ -- default False
1108+ , trackSortScores :: TrackSortScores
1109+ , from :: From
1110+ , size :: Size
1111+ , searchType :: SearchType
1112+ , fields :: Maybe [FieldName]
1113+ , source :: Maybe Source } deriving (Eq, Read, Show, Generic, Typeable)
1114+
1115+data SearchType = SearchTypeQueryThenFetch
1116+ | SearchTypeDfsQueryThenFetch
1117+ deriving (Eq, Read, Show, Generic, Typeable)
1118+
1119+data Source =
1120+ NoSource
1121+ | SourcePatterns PatternOrPatterns
1122+ | SourceIncludeExclude Include Exclude
1123+ deriving (Read, Show, Eq, Generic, Typeable)
1124+
1125+data PatternOrPatterns = PopPattern Pattern
1126+ | PopPatterns [Pattern] deriving (Eq, Read, Show, Generic, Typeable)
1127+
1128+data Include = Include [Pattern] deriving (Eq, Read, Show, Generic, Typeable)
1129+data Exclude = Exclude [Pattern] deriving (Eq, Read, Show, Generic, Typeable)
1130+
1131+newtype Pattern = Pattern Text deriving (Eq, Read, Show, Generic, Typeable)
1132+
1133+data Highlights = Highlights { globalsettings :: Maybe HighlightSettings
1134+ , highlightFields :: [FieldHighlight]
1135+ } deriving (Read, Show, Eq, Generic, Typeable)
1136+
1137+data FieldHighlight = FieldHighlight FieldName (Maybe HighlightSettings)
1138+ deriving (Read, Show, Eq, Generic, Typeable)
1139+
1140+
1141+data HighlightSettings = Plain PlainHighlight
1142+ | Postings PostingsHighlight
1143+ | FastVector FastVectorHighlight
1144+ deriving (Read, Show, Eq, Generic, Typeable)
1145+data PlainHighlight =
1146+ PlainHighlight { plainCommon :: Maybe CommonHighlight
1147+ , plainNonPost :: Maybe NonPostings } deriving (Read, Show, Eq, Generic, Typeable)
1148+
1149+ -- This requires that index_options are set to 'offset' in the mapping.
1150+data PostingsHighlight = PostingsHighlight (Maybe CommonHighlight) deriving (Read, Show, Eq, Generic, Typeable)
1151+
1152+-- This requires that term_vector is set to 'with_positions_offsets' in the mapping.
1153+data FastVectorHighlight =
1154+ FastVectorHighlight { fvCommon :: Maybe CommonHighlight
1155+ , fvNonPostSettings :: Maybe NonPostings
1156+ , boundaryChars :: Maybe Text
1157+ , boundaryMaxScan :: Maybe Int
1158+ , fragmentOffset :: Maybe Int
1159+ , matchedFields :: [Text]
1160+ , phraseLimit :: Maybe Int
1161+ } deriving (Read, Show, Eq, Generic, Typeable)
1162+
1163+data CommonHighlight =
1164+ CommonHighlight { order :: Maybe Text
1165+ , forceSource :: Maybe Bool
1166+ , tag :: Maybe HighlightTag
1167+ , encoder :: Maybe HighlightEncoder
1168+ , noMatchSize :: Maybe Int
1169+ , highlightQuery :: Maybe Query
1170+ , requireFieldMatch :: Maybe Bool
1171+ } deriving (Read, Show, Eq, Generic, Typeable)
1172+
1173+-- Settings that are only applicable to FastVector and Plain highlighters.
1174+data NonPostings =
1175+ NonPostings { fragmentSize :: Maybe Int
1176+ , numberOfFragments :: Maybe Int} deriving (Read, Show, Eq, Generic, Typeable)
1177+
1178+data HighlightEncoder = DefaultEncoder
1179+ | HTMLEncoder
1180+ deriving (Read, Show, Eq, Generic, Typeable)
1181+
1182+-- NOTE: Should the tags use some kind of HTML type, rather than Text?
1183+data HighlightTag = TagSchema Text
1184+ | CustomTags ([Text], [Text]) -- Only uses more than the first value in the lists if fvh
1185+ deriving (Read, Show, Eq, Generic, Typeable)
1186+
1187+
1188+data Query =
1189+ TermQuery Term (Maybe Boost)
1190+ | TermsQuery Text (NonEmpty Text)
1191+ | QueryMatchQuery MatchQuery
1192+ | QueryMultiMatchQuery MultiMatchQuery
1193+ | QueryBoolQuery BoolQuery
1194+ | QueryBoostingQuery BoostingQuery
1195+ | QueryCommonTermsQuery CommonTermsQuery
1196+ | ConstantScoreQuery Query Boost
1197+ | QueryDisMaxQuery DisMaxQuery
1198+ | QueryFuzzyLikeThisQuery FuzzyLikeThisQuery
1199+ | QueryFuzzyLikeFieldQuery FuzzyLikeFieldQuery
1200+ | QueryFuzzyQuery FuzzyQuery
1201+ | QueryHasChildQuery HasChildQuery
1202+ | QueryHasParentQuery HasParentQuery
1203+ | IdsQuery MappingName [DocId]
1204+ | QueryIndicesQuery IndicesQuery
1205+ | MatchAllQuery (Maybe Boost)
1206+ | QueryMoreLikeThisQuery MoreLikeThisQuery
1207+ | QueryMoreLikeThisFieldQuery MoreLikeThisFieldQuery
1208+ | QueryNestedQuery NestedQuery
1209+ | QueryPrefixQuery PrefixQuery
1210+ | QueryQueryStringQuery QueryStringQuery
1211+ | QuerySimpleQueryStringQuery SimpleQueryStringQuery
1212+ | QueryRangeQuery RangeQuery
1213+ | QueryRegexpQuery RegexpQuery
1214+ | QueryExistsQuery FieldName
1215+ | QueryMatchNoneQuery
1216+ | QueryTemplateQueryInline TemplateQueryInline
1217+ deriving (Eq, Read, Show, Generic, Typeable)
1218+
1219+-- | As of Elastic 2.0, 'Filters' are just 'Queries' housed in a Bool Query, and
1220+-- flagged in a different context.
1221+newtype Filter = Filter { unFilter :: Query }
1222+ deriving (Eq, Read, Show, Generic, Typeable)
1223+
1224+instance ToJSON Filter where
1225+ toJSON = toJSON . unFilter
1226+
1227+instance FromJSON Filter where
1228+ parseJSON v = Filter <$> parseJSON v
1229+
1230+data RegexpQuery =
1231+ RegexpQuery { regexpQueryField :: FieldName
1232+ , regexpQuery :: Regexp
1233+ , regexpQueryFlags :: RegexpFlags
1234+ , regexpQueryBoost :: Maybe Boost
1235+ } deriving (Eq, Read, Show, Generic, Typeable)
1236+
1237+data RangeQuery =
1238+ RangeQuery { rangeQueryField :: FieldName
1239+ , rangeQueryRange :: RangeValue
1240+ , rangeQueryBoost :: Boost } deriving (Eq, Read, Show, Generic, Typeable)
1241+
1242+mkRangeQuery :: FieldName -> RangeValue -> RangeQuery
1243+mkRangeQuery f r = RangeQuery f r (Boost 1.0)
1244+
1245+data SimpleQueryStringQuery =
1246+ SimpleQueryStringQuery
1247+ { simpleQueryStringQuery :: QueryString
1248+ , simpleQueryStringField :: Maybe FieldOrFields
1249+ , simpleQueryStringOperator :: Maybe BooleanOperator
1250+ , simpleQueryStringAnalyzer :: Maybe Analyzer
1251+ , simpleQueryStringFlags :: Maybe (NonEmpty SimpleQueryFlag)
1252+ , simpleQueryStringLowercaseExpanded :: Maybe LowercaseExpanded
1253+ , simpleQueryStringLocale :: Maybe Locale
1254+ } deriving (Eq, Read, Show, Generic, Typeable)
1255+
1256+data SimpleQueryFlag =
1257+ SimpleQueryAll
1258+ | SimpleQueryNone
1259+ | SimpleQueryAnd
1260+ | SimpleQueryOr
1261+ | SimpleQueryPrefix
1262+ | SimpleQueryPhrase
1263+ | SimpleQueryPrecedence
1264+ | SimpleQueryEscape
1265+ | SimpleQueryWhitespace
1266+ | SimpleQueryFuzzy
1267+ | SimpleQueryNear
1268+ | SimpleQuerySlop deriving (Eq, Read, Show, Generic, Typeable)
1269+
1270+-- use_dis_max and tie_breaker when fields are plural?
1271+data QueryStringQuery =
1272+ QueryStringQuery
1273+ { queryStringQuery :: QueryString
1274+ , queryStringDefaultField :: Maybe FieldName
1275+ , queryStringOperator :: Maybe BooleanOperator
1276+ , queryStringAnalyzer :: Maybe Analyzer
1277+ , queryStringAllowLeadingWildcard :: Maybe AllowLeadingWildcard
1278+ , queryStringLowercaseExpanded :: Maybe LowercaseExpanded
1279+ , queryStringEnablePositionIncrements :: Maybe EnablePositionIncrements
1280+ , queryStringFuzzyMaxExpansions :: Maybe MaxExpansions
1281+ , queryStringFuzziness :: Maybe Fuzziness
1282+ , queryStringFuzzyPrefixLength :: Maybe PrefixLength
1283+ , queryStringPhraseSlop :: Maybe PhraseSlop
1284+ , queryStringBoost :: Maybe Boost
1285+ , queryStringAnalyzeWildcard :: Maybe AnalyzeWildcard
1286+ , queryStringGeneratePhraseQueries :: Maybe GeneratePhraseQueries
1287+ , queryStringMinimumShouldMatch :: Maybe MinimumMatch
1288+ , queryStringLenient :: Maybe Lenient
1289+ , queryStringLocale :: Maybe Locale
1290+ } deriving (Eq, Read, Show, Generic, Typeable)
1291+
1292+mkQueryStringQuery :: QueryString -> QueryStringQuery
1293+mkQueryStringQuery qs =
1294+ QueryStringQuery qs Nothing Nothing
1295+ Nothing Nothing Nothing Nothing
1296+ Nothing Nothing Nothing Nothing
1297+ Nothing Nothing Nothing Nothing
1298+ Nothing Nothing
1299+
1300+data FieldOrFields = FofField FieldName
1301+ | FofFields (NonEmpty FieldName) deriving (Eq, Read, Show, Generic, Typeable)
1302+
1303+data PrefixQuery =
1304+ PrefixQuery
1305+ { prefixQueryField :: FieldName
1306+ , prefixQueryPrefixValue :: Text
1307+ , prefixQueryBoost :: Maybe Boost } deriving (Eq, Read, Show, Generic, Typeable)
1308+
1309+data NestedQuery =
1310+ NestedQuery
1311+ { nestedQueryPath :: QueryPath
1312+ , nestedQueryScoreType :: ScoreType
1313+ , nestedQuery :: Query } deriving (Eq, Read, Show, Generic, Typeable)
1314+
1315+data MoreLikeThisFieldQuery =
1316+ MoreLikeThisFieldQuery
1317+ { moreLikeThisFieldText :: Text
1318+ , moreLikeThisFieldFields :: FieldName
1319+ -- default 0.3 (30%)
1320+ , moreLikeThisFieldPercentMatch :: Maybe PercentMatch
1321+ , moreLikeThisFieldMinimumTermFreq :: Maybe MinimumTermFrequency
1322+ , moreLikeThisFieldMaxQueryTerms :: Maybe MaxQueryTerms
1323+ , moreLikeThisFieldStopWords :: Maybe (NonEmpty StopWord)
1324+ , moreLikeThisFieldMinDocFrequency :: Maybe MinDocFrequency
1325+ , moreLikeThisFieldMaxDocFrequency :: Maybe MaxDocFrequency
1326+ , moreLikeThisFieldMinWordLength :: Maybe MinWordLength
1327+ , moreLikeThisFieldMaxWordLength :: Maybe MaxWordLength
1328+ , moreLikeThisFieldBoostTerms :: Maybe BoostTerms
1329+ , moreLikeThisFieldBoost :: Maybe Boost
1330+ , moreLikeThisFieldAnalyzer :: Maybe Analyzer
1331+ } deriving (Eq, Read, Show, Generic, Typeable)
1332+
1333+data MoreLikeThisQuery =
1334+ MoreLikeThisQuery
1335+ { moreLikeThisText :: Text
1336+ , moreLikeThisFields :: Maybe (NonEmpty FieldName)
1337+ -- default 0.3 (30%)
1338+ , moreLikeThisPercentMatch :: Maybe PercentMatch
1339+ , moreLikeThisMinimumTermFreq :: Maybe MinimumTermFrequency
1340+ , moreLikeThisMaxQueryTerms :: Maybe MaxQueryTerms
1341+ , moreLikeThisStopWords :: Maybe (NonEmpty StopWord)
1342+ , moreLikeThisMinDocFrequency :: Maybe MinDocFrequency
1343+ , moreLikeThisMaxDocFrequency :: Maybe MaxDocFrequency
1344+ , moreLikeThisMinWordLength :: Maybe MinWordLength
1345+ , moreLikeThisMaxWordLength :: Maybe MaxWordLength
1346+ , moreLikeThisBoostTerms :: Maybe BoostTerms
1347+ , moreLikeThisBoost :: Maybe Boost
1348+ , moreLikeThisAnalyzer :: Maybe Analyzer
1349+ } deriving (Eq, Read, Show, Generic, Typeable)
1350+
1351+data IndicesQuery =
1352+ IndicesQuery
1353+ { indicesQueryIndices :: [IndexName]
1354+ , indicesQuery :: Query
1355+ -- default "all"
1356+ , indicesQueryNoMatch :: Maybe Query } deriving (Eq, Read, Show, Generic, Typeable)
1357+
1358+data HasParentQuery =
1359+ HasParentQuery
1360+ { hasParentQueryType :: TypeName
1361+ , hasParentQuery :: Query
1362+ , hasParentQueryScoreType :: Maybe ScoreType } deriving (Eq, Read, Show, Generic, Typeable)
1363+
1364+data HasChildQuery =
1365+ HasChildQuery
1366+ { hasChildQueryType :: TypeName
1367+ , hasChildQuery :: Query
1368+ , hasChildQueryScoreType :: Maybe ScoreType } deriving (Eq, Read, Show, Generic, Typeable)
1369+
1370+data ScoreType =
1371+ ScoreTypeMax
1372+ | ScoreTypeSum
1373+ | ScoreTypeAvg
1374+ | ScoreTypeNone deriving (Eq, Read, Show, Generic, Typeable)
1375+
1376+data FuzzyQuery =
1377+ FuzzyQuery { fuzzyQueryField :: FieldName
1378+ , fuzzyQueryValue :: Text
1379+ , fuzzyQueryPrefixLength :: PrefixLength
1380+ , fuzzyQueryMaxExpansions :: MaxExpansions
1381+ , fuzzyQueryFuzziness :: Fuzziness
1382+ , fuzzyQueryBoost :: Maybe Boost
1383+ } deriving (Eq, Read, Show, Generic, Typeable)
1384+
1385+data FuzzyLikeFieldQuery =
1386+ FuzzyLikeFieldQuery
1387+ { fuzzyLikeField :: FieldName
1388+ -- anaphora is good for the soul.
1389+ , fuzzyLikeFieldText :: Text
1390+ , fuzzyLikeFieldMaxQueryTerms :: MaxQueryTerms
1391+ , fuzzyLikeFieldIgnoreTermFrequency :: IgnoreTermFrequency
1392+ , fuzzyLikeFieldFuzziness :: Fuzziness
1393+ , fuzzyLikeFieldPrefixLength :: PrefixLength
1394+ , fuzzyLikeFieldBoost :: Boost
1395+ , fuzzyLikeFieldAnalyzer :: Maybe Analyzer
1396+ } deriving (Eq, Read, Show, Generic, Typeable)
1397+
1398+data FuzzyLikeThisQuery =
1399+ FuzzyLikeThisQuery
1400+ { fuzzyLikeFields :: [FieldName]
1401+ , fuzzyLikeText :: Text
1402+ , fuzzyLikeMaxQueryTerms :: MaxQueryTerms
1403+ , fuzzyLikeIgnoreTermFrequency :: IgnoreTermFrequency
1404+ , fuzzyLikeFuzziness :: Fuzziness
1405+ , fuzzyLikePrefixLength :: PrefixLength
1406+ , fuzzyLikeBoost :: Boost
1407+ , fuzzyLikeAnalyzer :: Maybe Analyzer
1408+ } deriving (Eq, Read, Show, Generic, Typeable)
1409+
1410+data DisMaxQuery =
1411+ DisMaxQuery { disMaxQueries :: [Query]
1412+ -- default 0.0
1413+ , disMaxTiebreaker :: Tiebreaker
1414+ , disMaxBoost :: Maybe Boost
1415+ } deriving (Eq, Read, Show, Generic, Typeable)
1416+
1417+data MatchQuery =
1418+ MatchQuery { matchQueryField :: FieldName
1419+ , matchQueryQueryString :: QueryString
1420+ , matchQueryOperator :: BooleanOperator
1421+ , matchQueryZeroTerms :: ZeroTermsQuery
1422+ , matchQueryCutoffFrequency :: Maybe CutoffFrequency
1423+ , matchQueryMatchType :: Maybe MatchQueryType
1424+ , matchQueryAnalyzer :: Maybe Analyzer
1425+ , matchQueryMaxExpansions :: Maybe MaxExpansions
1426+ , matchQueryLenient :: Maybe Lenient
1427+ , matchQueryBoost :: Maybe Boost } deriving (Eq, Read, Show, Generic, Typeable)
1428+
1429+{-| 'mkMatchQuery' is a convenience function that defaults the less common parameters,
1430+ enabling you to provide only the 'FieldName' and 'QueryString' to make a 'MatchQuery'
1431+-}
1432+mkMatchQuery :: FieldName -> QueryString -> MatchQuery
1433+mkMatchQuery field query = MatchQuery field query Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1434+
1435+data MatchQueryType =
1436+ MatchPhrase
1437+ | MatchPhrasePrefix deriving (Eq, Read, Show, Generic, Typeable)
1438+
1439+data MultiMatchQuery =
1440+ MultiMatchQuery { multiMatchQueryFields :: [FieldName]
1441+ , multiMatchQueryString :: QueryString
1442+ , multiMatchQueryOperator :: BooleanOperator
1443+ , multiMatchQueryZeroTerms :: ZeroTermsQuery
1444+ , multiMatchQueryTiebreaker :: Maybe Tiebreaker
1445+ , multiMatchQueryType :: Maybe MultiMatchQueryType
1446+ , multiMatchQueryCutoffFrequency :: Maybe CutoffFrequency
1447+ , multiMatchQueryAnalyzer :: Maybe Analyzer
1448+ , multiMatchQueryMaxExpansions :: Maybe MaxExpansions
1449+ , multiMatchQueryLenient :: Maybe Lenient } deriving (Eq, Read, Show, Generic, Typeable)
1450+
1451+{-| 'mkMultiMatchQuery' is a convenience function that defaults the less common parameters,
1452+ enabling you to provide only the list of 'FieldName's and 'QueryString' to
1453+ make a 'MultiMatchQuery'.
1454+-}
1455+
1456+mkMultiMatchQuery :: [FieldName] -> QueryString -> MultiMatchQuery
1457+mkMultiMatchQuery matchFields query =
1458+ MultiMatchQuery matchFields query
1459+ Or ZeroTermsNone Nothing Nothing Nothing Nothing Nothing Nothing
1460+
1461+data MultiMatchQueryType =
1462+ MultiMatchBestFields
1463+ | MultiMatchMostFields
1464+ | MultiMatchCrossFields
1465+ | MultiMatchPhrase
1466+ | MultiMatchPhrasePrefix deriving (Eq, Read, Show, Generic, Typeable)
1467+
1468+data BoolQuery =
1469+ BoolQuery { boolQueryMustMatch :: [Query]
1470+ , boolQueryFilter :: [Filter]
1471+ , boolQueryMustNotMatch :: [Query]
1472+ , boolQueryShouldMatch :: [Query]
1473+ , boolQueryMinimumShouldMatch :: Maybe MinimumMatch
1474+ , boolQueryBoost :: Maybe Boost
1475+ , boolQueryDisableCoord :: Maybe DisableCoord
1476+ } deriving (Eq, Read, Show, Generic, Typeable)
1477+
1478+mkBoolQuery :: [Query] -> [Filter] -> [Query] -> [Query] -> BoolQuery
1479+mkBoolQuery must filt mustNot should =
1480+ BoolQuery must filt mustNot should Nothing Nothing Nothing
1481+
1482+data BoostingQuery =
1483+ BoostingQuery { positiveQuery :: Query
1484+ , negativeQuery :: Query
1485+ , negativeBoost :: Boost } deriving (Eq, Read, Show, Generic, Typeable)
1486+
1487+data CommonTermsQuery =
1488+ CommonTermsQuery { commonField :: FieldName
1489+ , commonQuery :: QueryString
1490+ , commonCutoffFrequency :: CutoffFrequency
1491+ , commonLowFreqOperator :: BooleanOperator
1492+ , commonHighFreqOperator :: BooleanOperator
1493+ , commonMinimumShouldMatch :: Maybe CommonMinimumMatch
1494+ , commonBoost :: Maybe Boost
1495+ , commonAnalyzer :: Maybe Analyzer
1496+ , commonDisableCoord :: Maybe DisableCoord
1497+ } deriving (Eq, Read, Show, Generic, Typeable)
1498+
1499+data CommonMinimumMatch =
1500+ CommonMinimumMatchHighLow MinimumMatchHighLow
1501+ | CommonMinimumMatch MinimumMatch
1502+ deriving (Eq, Read, Show, Generic, Typeable)
1503+
1504+data MinimumMatchHighLow =
1505+ MinimumMatchHighLow { lowFreq :: MinimumMatch
1506+ , highFreq :: MinimumMatch } deriving (Eq, Read, Show, Generic, Typeable)
1507+
1508+data ZeroTermsQuery = ZeroTermsNone
1509+ | ZeroTermsAll deriving (Eq, Read, Show, Generic, Typeable)
1510+
1511+data RangeExecution = RangeExecutionIndex
1512+ | RangeExecutionFielddata deriving (Eq, Read, Show, Generic, Typeable)
1513+
1514+newtype Regexp = Regexp Text deriving (Eq, Read, Show, Generic, Typeable, FromJSON)
1515+
1516+data RegexpFlags = AllRegexpFlags
1517+ | NoRegexpFlags
1518+ | SomeRegexpFlags (NonEmpty RegexpFlag) deriving (Eq, Read, Show, Generic, Typeable)
1519+
1520+data RegexpFlag = AnyString
1521+ | Automaton
1522+ | Complement
1523+ | Empty
1524+ | Intersection
1525+ | Interval deriving (Eq, Read, Show, Generic, Typeable)
1526+
1527+newtype LessThan = LessThan Double deriving (Eq, Read, Show, Generic, Typeable)
1528+newtype LessThanEq = LessThanEq Double deriving (Eq, Read, Show, Generic, Typeable)
1529+newtype GreaterThan = GreaterThan Double deriving (Eq, Read, Show, Generic, Typeable)
1530+newtype GreaterThanEq = GreaterThanEq Double deriving (Eq, Read, Show, Generic, Typeable)
1531+
1532+newtype LessThanD = LessThanD UTCTime deriving (Eq, Read, Show, Generic, Typeable)
1533+newtype LessThanEqD = LessThanEqD UTCTime deriving (Eq, Read, Show, Generic, Typeable)
1534+newtype GreaterThanD = GreaterThanD UTCTime deriving (Eq, Read, Show, Generic, Typeable)
1535+newtype GreaterThanEqD = GreaterThanEqD UTCTime deriving (Eq, Read, Show, Generic, Typeable)
1536+
1537+data RangeValue = RangeDateLte LessThanEqD
1538+ | RangeDateLt LessThanD
1539+ | RangeDateGte GreaterThanEqD
1540+ | RangeDateGt GreaterThanD
1541+ | RangeDateGtLt GreaterThanD LessThanD
1542+ | RangeDateGteLte GreaterThanEqD LessThanEqD
1543+ | RangeDateGteLt GreaterThanEqD LessThanD
1544+ | RangeDateGtLte GreaterThanD LessThanEqD
1545+ | RangeDoubleLte LessThanEq
1546+ | RangeDoubleLt LessThan
1547+ | RangeDoubleGte GreaterThanEq
1548+ | RangeDoubleGt GreaterThan
1549+ | RangeDoubleGtLt GreaterThan LessThan
1550+ | RangeDoubleGteLte GreaterThanEq LessThanEq
1551+ | RangeDoubleGteLt GreaterThanEq LessThan
1552+ | RangeDoubleGtLte GreaterThan LessThanEq
1553+ deriving (Eq, Read, Show, Generic, Typeable)
1554+
1555+rangeValueToPair :: RangeValue -> [Pair]
1556+rangeValueToPair rv = case rv of
1557+ RangeDateLte (LessThanEqD t) -> ["lte" .= t]
1558+ RangeDateGte (GreaterThanEqD t) -> ["gte" .= t]
1559+ RangeDateLt (LessThanD t) -> ["lt" .= t]
1560+ RangeDateGt (GreaterThanD t) -> ["gt" .= t]
1561+ RangeDateGteLte (GreaterThanEqD l) (LessThanEqD g) -> ["gte" .= l, "lte" .= g]
1562+ RangeDateGtLte (GreaterThanD l) (LessThanEqD g) -> ["gt" .= l, "lte" .= g]
1563+ RangeDateGteLt (GreaterThanEqD l) (LessThanD g) -> ["gte" .= l, "lt" .= g]
1564+ RangeDateGtLt (GreaterThanD l) (LessThanD g) -> ["gt" .= l, "lt" .= g]
1565+ RangeDoubleLte (LessThanEq t) -> ["lte" .= t]
1566+ RangeDoubleGte (GreaterThanEq t) -> ["gte" .= t]
1567+ RangeDoubleLt (LessThan t) -> ["lt" .= t]
1568+ RangeDoubleGt (GreaterThan t) -> ["gt" .= t]
1569+ RangeDoubleGteLte (GreaterThanEq l) (LessThanEq g) -> ["gte" .= l, "lte" .= g]
1570+ RangeDoubleGtLte (GreaterThan l) (LessThanEq g) -> ["gt" .= l, "lte" .= g]
1571+ RangeDoubleGteLt (GreaterThanEq l) (LessThan g) -> ["gte" .= l, "lt" .= g]
1572+ RangeDoubleGtLt (GreaterThan l) (LessThan g) -> ["gt" .= l, "lt" .= g]
1573+
1574+data Term = Term { termField :: Text
1575+ , termValue :: Text } deriving (Eq, Read, Show, Generic, Typeable)
1576+
1577+data BoolMatch = MustMatch Term Cache
1578+ | MustNotMatch Term Cache
1579+ | ShouldMatch [Term] Cache deriving (Eq, Read, Show, Generic, Typeable)
1580+
1581+-- "memory" or "indexed"
1582+data GeoFilterType = GeoFilterMemory
1583+ | GeoFilterIndexed deriving (Eq, Read, Show, Generic, Typeable)
1584+
1585+data LatLon = LatLon { lat :: Double
1586+ , lon :: Double } deriving (Eq, Read, Show, Generic, Typeable)
1587+
1588+data GeoBoundingBox =
1589+ GeoBoundingBox { topLeft :: LatLon
1590+ , bottomRight :: LatLon } deriving (Eq, Read, Show, Generic, Typeable)
1591+
1592+data GeoBoundingBoxConstraint =
1593+ GeoBoundingBoxConstraint { geoBBField :: FieldName
1594+ , constraintBox :: GeoBoundingBox
1595+ , bbConstraintcache :: Cache
1596+ , geoType :: GeoFilterType
1597+ } deriving (Eq, Read, Show, Generic, Typeable)
1598+
1599+data GeoPoint =
1600+ GeoPoint { geoField :: FieldName
1601+ , latLon :: LatLon} deriving (Eq, Read, Show, Generic, Typeable)
1602+
1603+data DistanceUnit = Miles
1604+ | Yards
1605+ | Feet
1606+ | Inches
1607+ | Kilometers
1608+ | Meters
1609+ | Centimeters
1610+ | Millimeters
1611+ | NauticalMiles deriving (Eq, Read, Show, Generic, Typeable)
1612+
1613+data DistanceType = Arc
1614+ | SloppyArc -- doesn't exist <1.0
1615+ | Plane deriving (Eq, Read, Show, Generic, Typeable)
1616+
1617+data OptimizeBbox = OptimizeGeoFilterType GeoFilterType
1618+ | NoOptimizeBbox deriving (Eq, Read, Show, Generic, Typeable)
1619+
1620+data Distance =
1621+ Distance { coefficient :: Double
1622+ , unit :: DistanceUnit } deriving (Eq, Read, Show, Generic, Typeable)
1623+
1624+data DistanceRange =
1625+ DistanceRange { distanceFrom :: Distance
1626+ , distanceTo :: Distance } deriving (Eq, Read, Show, Generic, Typeable)
1627+
1628+type TemplateQueryKey = Text
1629+type TemplateQueryValue = Text
1630+
1631+newtype TemplateQueryKeyValuePairs = TemplateQueryKeyValuePairs (HM.HashMap TemplateQueryKey TemplateQueryValue)
1632+ deriving (Eq, Read, Show, Generic)
1633+
1634+instance ToJSON TemplateQueryKeyValuePairs where
1635+ toJSON (TemplateQueryKeyValuePairs x) = Object $ HM.map toJSON x
1636+
1637+instance FromJSON TemplateQueryKeyValuePairs where
1638+ parseJSON (Object o) = pure . TemplateQueryKeyValuePairs $ HM.mapMaybe getValue o
1639+ where getValue (String x) = Just x
1640+ getValue _ = Nothing
1641+ parseJSON _ = fail "error parsing TemplateQueryKeyValuePairs"
1642+
1643+data TemplateQueryInline =
1644+ TemplateQueryInline { inline :: Query
1645+ , params :: TemplateQueryKeyValuePairs
1646+ }
1647+ deriving (Eq, Read, Show, Generic, Typeable)
1648+
1649+instance ToJSON TemplateQueryInline where
1650+ toJSON TemplateQueryInline{..} = object [ "inline" .= inline
1651+ , "params" .= params
1652+ ]
1653+
1654+instance FromJSON TemplateQueryInline where
1655+ parseJSON = withObject "TemplateQueryInline" parse
1656+ where parse o = TemplateQueryInline
1657+ <$> o .: "inline"
1658+ <*> o .: "params"
1659+
1660+data SearchResult a =
1661+ SearchResult { took :: Int
1662+ , timedOut :: Bool
1663+ , shards :: ShardResult
1664+ , searchHits :: SearchHits a
1665+ , aggregations :: Maybe AggregationResults
1666+ , scrollId :: Maybe ScrollId } deriving (Eq, Read, Show, Generic, Typeable)
1667+
1668+newtype ScrollId = ScrollId Text deriving (Eq, Read, Show, Generic, Ord, ToJSON, FromJSON)
1669+
1670+type Score = Maybe Double
1671+
1672+data SearchHits a =
1673+ SearchHits { hitsTotal :: Int
1674+ , maxScore :: Score
1675+ , hits :: [Hit a] } deriving (Eq, Read, Show, Generic, Typeable)
1676+
1677+instance Semigroup (SearchHits a) where
1678+ (SearchHits ta ma ha) <> (SearchHits tb mb hb) = SearchHits (ta + tb) (max ma mb) (ha <> hb)
1679+
1680+instance Monoid (SearchHits a) where
1681+ mempty = SearchHits 0 Nothing mempty
1682+ mappend = (<>)
1683+
1684+data Hit a =
1685+ Hit { hitIndex :: IndexName
1686+ , hitType :: MappingName
1687+ , hitDocId :: DocId
1688+ , hitScore :: Score
1689+ , hitSource :: Maybe a
1690+ , hitHighlight :: Maybe HitHighlight } deriving (Eq, Read, Show, Generic, Typeable)
1691+
1692+data ShardResult =
1693+ ShardResult { shardTotal :: Int
1694+ , shardsSuccessful :: Int
1695+ , shardsFailed :: Int } deriving (Eq, Read, Show, Generic, Typeable)
1696+
1697+type HitHighlight = M.Map Text [Text]
1698+
1699+showText :: Show a => a -> Text
1700+showText = T.pack . show
1701+
1702+readMay :: Read a => String -> Maybe a
1703+readMay s = case reads s of
1704+ (a, ""):_ -> Just a
1705+ _ -> Nothing
1706+
1707+parseReadText :: Read a => Text -> Parser a
1708+parseReadText = maybe mzero return . readMay . T.unpack
1709+
1710+type Aggregations = M.Map Text Aggregation
1711+
1712+emptyAggregations :: Aggregations
1713+emptyAggregations = M.empty
1714+
1715+mkAggregations :: Text -> Aggregation -> Aggregations
1716+mkAggregations name aggregation = M.insert name aggregation emptyAggregations
1717+
1718+data TermOrder = TermOrder{ termSortField :: Text
1719+ , termSortOrder :: SortOrder } deriving (Eq, Read, Show, Generic, Typeable)
1720+
1721+data TermInclusion = TermInclusion Text
1722+ | TermPattern Text Text deriving (Eq, Read, Show, Generic, Typeable)
1723+
1724+data CollectionMode = BreadthFirst
1725+ | DepthFirst deriving (Eq, Read, Show, Generic, Typeable)
1726+
1727+data ExecutionHint = Ordinals
1728+ | GlobalOrdinals
1729+ | GlobalOrdinalsHash
1730+ | GlobalOrdinalsLowCardinality
1731+ | Map deriving (Eq, Read, Show, Generic, Typeable)
1732+
1733+data TimeInterval = Weeks
1734+ | Days
1735+ | Hours
1736+ | Minutes
1737+ | Seconds deriving Eq
1738+
1739+data Interval = Year
1740+ | Quarter
1741+ | Month
1742+ | Week
1743+ | Day
1744+ | Hour
1745+ | Minute
1746+ | Second deriving (Eq, Read, Show, Generic, Typeable)
1747+
1748+data Aggregation = TermsAgg TermsAggregation
1749+ | CardinalityAgg CardinalityAggregation
1750+ | DateHistogramAgg DateHistogramAggregation
1751+ | ValueCountAgg ValueCountAggregation
1752+ | FilterAgg FilterAggregation
1753+ | DateRangeAgg DateRangeAggregation
1754+ | MissingAgg MissingAggregation
1755+ | TopHitsAgg TopHitsAggregation
1756+ | StatsAgg StatisticsAggregation
1757+ deriving (Eq, Read, Show, Generic, Typeable)
1758+
1759+data TopHitsAggregation = TopHitsAggregation
1760+ { taFrom :: Maybe From
1761+ , taSize :: Maybe Size
1762+ , taSort :: Maybe Sort
1763+ } deriving (Eq, Read, Show)
1764+
1765+data MissingAggregation = MissingAggregation
1766+ { maField :: Text
1767+ } deriving (Eq, Read, Show, Generic, Typeable)
1768+
1769+data TermsAggregation = TermsAggregation { term :: Either Text Text
1770+ , termInclude :: Maybe TermInclusion
1771+ , termExclude :: Maybe TermInclusion
1772+ , termOrder :: Maybe TermOrder
1773+ , termMinDocCount :: Maybe Int
1774+ , termSize :: Maybe Int
1775+ , termShardSize :: Maybe Int
1776+ , termCollectMode :: Maybe CollectionMode
1777+ , termExecutionHint :: Maybe ExecutionHint
1778+ , termAggs :: Maybe Aggregations
1779+ } deriving (Eq, Read, Show, Generic, Typeable)
1780+
1781+data CardinalityAggregation = CardinalityAggregation { cardinalityField :: FieldName,
1782+ precisionThreshold :: Maybe Int
1783+ } deriving (Eq, Read, Show, Generic, Typeable)
1784+
1785+data DateHistogramAggregation = DateHistogramAggregation { dateField :: FieldName
1786+ , dateInterval :: Interval
1787+ , dateFormat :: Maybe Text
1788+ -- pre and post deprecated in 1.5
1789+ , datePreZone :: Maybe Text
1790+ , datePostZone :: Maybe Text
1791+ , datePreOffset :: Maybe Text
1792+ , datePostOffset :: Maybe Text
1793+ , dateAggs :: Maybe Aggregations
1794+ } deriving (Eq, Read, Show, Generic, Typeable)
1795+
1796+
1797+data DateRangeAggregation = DateRangeAggregation { draField :: FieldName
1798+ , draFormat :: Maybe Text
1799+ , draRanges :: NonEmpty DateRangeAggRange
1800+ } deriving (Eq, Read, Show, Generic, Typeable)
1801+
1802+data DateRangeAggRange = DateRangeFrom DateMathExpr
1803+ | DateRangeTo DateMathExpr
1804+ | DateRangeFromAndTo DateMathExpr DateMathExpr deriving (Eq, Read, Show, Generic, Typeable)
1805+
1806+-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math> for more information.
1807+data DateMathExpr = DateMathExpr DateMathAnchor [DateMathModifier] deriving (Eq, Read, Show, Generic, Typeable)
1808+
1809+
1810+-- | Starting point for a date range. This along with the 'DateMathModifiers' gets you the date ES will start from.
1811+data DateMathAnchor = DMNow
1812+ | DMDate Day deriving (Eq, Read, Show, Generic, Typeable)
1813+
1814+data DateMathModifier = AddTime Int DateMathUnit
1815+ | SubtractTime Int DateMathUnit
1816+ | RoundDownTo DateMathUnit deriving (Eq, Read, Show, Generic, Typeable)
1817+
1818+data DateMathUnit = DMYear
1819+ | DMMonth
1820+ | DMWeek
1821+ | DMDay
1822+ | DMHour
1823+ | DMMinute
1824+ | DMSecond deriving (Eq, Read, Show, Generic, Typeable)
1825+
1826+-- | See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html> for more information.
1827+data ValueCountAggregation = FieldValueCount FieldName
1828+ | ScriptValueCount Script deriving (Eq, Read, Show, Generic, Typeable)
1829+
1830+-- | Single-bucket filter aggregations. See <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html#search-aggregations-bucket-filter-aggregation> for more information.
1831+data FilterAggregation = FilterAggregation { faFilter :: Filter
1832+ , faAggs :: Maybe Aggregations} deriving (Eq, Read, Show, Generic, Typeable)
1833+
1834+data StatisticsAggregation = StatisticsAggregation { statsType :: StatsType
1835+ , statsField :: FieldName } deriving (Eq, Read, Show, Generic, Typeable)
1836+
1837+data StatsType
1838+ = Basic
1839+ | Extended
1840+ deriving (Eq, Read, Show, Generic, Typeable)
1841+
1842+mkTermsAggregation :: Text -> TermsAggregation
1843+mkTermsAggregation t = TermsAggregation (Left t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1844+
1845+mkTermsScriptAggregation :: Text -> TermsAggregation
1846+mkTermsScriptAggregation t = TermsAggregation (Right t) Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
1847+
1848+mkDateHistogram :: FieldName -> Interval -> DateHistogramAggregation
1849+mkDateHistogram t i = DateHistogramAggregation t i Nothing Nothing Nothing Nothing Nothing Nothing
1850+
1851+mkCardinalityAggregation :: FieldName -> CardinalityAggregation
1852+mkCardinalityAggregation t = CardinalityAggregation t Nothing
1853+
1854+mkStatsAggregation :: FieldName -> StatisticsAggregation
1855+mkStatsAggregation = StatisticsAggregation Basic
1856+
1857+mkExtendedStatsAggregation :: FieldName -> StatisticsAggregation
1858+mkExtendedStatsAggregation = StatisticsAggregation Extended
1859+
1860+instance ToJSON Version where
1861+ toJSON Version {..} = object ["number" .= number
1862+ ,"build_hash" .= build_hash
1863+ ,"build_date" .= build_date
1864+ ,"build_snapshot" .= build_snapshot
1865+ ,"lucene_version" .= lucene_version]
1866+
1867+instance FromJSON Version where
1868+ parseJSON = withObject "Version" parse
1869+ where parse o = Version
1870+ <$> o .: "number"
1871+ <*> o .: "build_hash"
1872+ <*> o .: "build_date"
1873+ <*> o .: "build_snapshot"
1874+ <*> o .: "lucene_version"
1875+
1876+instance ToJSON VersionNumber where
1877+ toJSON = toJSON . Vers.showVersion . versionNumber
1878+
1879+instance FromJSON VersionNumber where
1880+ parseJSON = withText "VersionNumber" (parse . T.unpack)
1881+ where
1882+ parse s = case filter (null . snd)(RP.readP_to_S Vers.parseVersion s) of
1883+ [(v, _)] -> pure (VersionNumber v)
1884+ [] -> fail ("Invalid version string " ++ s)
1885+ xs -> fail ("Ambiguous version string " ++ s ++ " (" ++ intercalate ", " (Vers.showVersion . fst <$> xs) ++ ")")
1886+
1887+instance ToJSON TermOrder where
1888+ toJSON (TermOrder termSortField termSortOrder) = object [termSortField .= termSortOrder]
1889+
1890+instance ToJSON TermInclusion where
1891+ toJSON (TermInclusion x) = toJSON x
1892+ toJSON (TermPattern pattern flags) = omitNulls [ "pattern" .= pattern,
1893+ "flags" .= flags]
1894+
1895+instance ToJSON CollectionMode where
1896+ toJSON BreadthFirst = "breadth_first"
1897+ toJSON DepthFirst = "depth_first"
1898+
1899+instance ToJSON ExecutionHint where
1900+ toJSON Ordinals = "ordinals"
1901+ toJSON GlobalOrdinals = "global_ordinals"
1902+ toJSON GlobalOrdinalsHash = "global_ordinals_hash"
1903+ toJSON GlobalOrdinalsLowCardinality = "global_ordinals_low_cardinality"
1904+ toJSON Map = "map"
1905+
1906+instance ToJSON Interval where
1907+ toJSON Year = "year"
1908+ toJSON Quarter = "quarter"
1909+ toJSON Month = "month"
1910+ toJSON Week = "week"
1911+ toJSON Day = "day"
1912+ toJSON Hour = "hour"
1913+ toJSON Minute = "minute"
1914+ toJSON Second = "second"
1915+
1916+instance Show TimeInterval where
1917+ show Weeks = "w"
1918+ show Days = "d"
1919+ show Hours = "h"
1920+ show Minutes = "m"
1921+ show Seconds = "s"
1922+
1923+instance Read TimeInterval where
1924+ readPrec = f =<< TR.get
1925+ where
1926+ f 'w' = return Weeks
1927+ f 'd' = return Days
1928+ f 'h' = return Hours
1929+ f 'm' = return Minutes
1930+ f 's' = return Seconds
1931+ f _ = fail "TimeInterval expected one of w, d, h, m, s"
1932+
1933+instance ToJSON Aggregation where
1934+ toJSON (TermsAgg (TermsAggregation term include exclude order minDocCount size shardSize collectMode executionHint termAggs)) =
1935+ omitNulls ["terms" .= omitNulls [ toJSON' term,
1936+ "include" .= include,
1937+ "exclude" .= exclude,
1938+ "order" .= order,
1939+ "min_doc_count" .= minDocCount,
1940+ "size" .= size,
1941+ "shard_size" .= shardSize,
1942+ "collect_mode" .= collectMode,
1943+ "execution_hint" .= executionHint
1944+ ],
1945+ "aggs" .= termAggs ]
1946+ where
1947+ toJSON' x = case x of { Left y -> "field" .= y; Right y -> "script" .= y }
1948+
1949+ toJSON (CardinalityAgg (CardinalityAggregation field precisionThreshold)) =
1950+ object ["cardinality" .= omitNulls [ "field" .= field,
1951+ "precisionThreshold" .= precisionThreshold
1952+ ]
1953+ ]
1954+
1955+ toJSON (DateHistogramAgg (DateHistogramAggregation field interval format preZone postZone preOffset postOffset dateHistoAggs)) =
1956+ omitNulls ["date_histogram" .= omitNulls [ "field" .= field,
1957+ "interval" .= interval,
1958+ "format" .= format,
1959+ "pre_zone" .= preZone,
1960+ "post_zone" .= postZone,
1961+ "pre_offset" .= preOffset,
1962+ "post_offset" .= postOffset
1963+ ],
1964+ "aggs" .= dateHistoAggs ]
1965+ toJSON (ValueCountAgg a) = object ["value_count" .= v]
1966+ where v = case a of
1967+ (FieldValueCount (FieldName n)) -> object ["field" .= n]
1968+ (ScriptValueCount (Script s)) -> object ["script" .= s]
1969+ toJSON (FilterAgg (FilterAggregation filt ags)) =
1970+ omitNulls [ "filter" .= filt
1971+ , "aggs" .= ags]
1972+ toJSON (DateRangeAgg a) = object [ "date_range" .= a
1973+ ]
1974+ toJSON (MissingAgg (MissingAggregation{..})) =
1975+ object ["missing" .= object ["field" .= maField]]
1976+
1977+ toJSON (TopHitsAgg (TopHitsAggregation mfrom msize msort)) =
1978+ omitNulls ["top_hits" .= omitNulls [ "size" .= msize
1979+ , "from" .= mfrom
1980+ , "sort" .= msort
1981+ ]
1982+ ]
1983+
1984+ toJSON (StatsAgg (StatisticsAggregation typ field)) =
1985+ object [stType .= omitNulls [ "field" .= field ]]
1986+ where
1987+ stType | typ == Basic = "stats"
1988+ | otherwise = "extended_stats"
1989+
1990+instance ToJSON DateRangeAggregation where
1991+ toJSON DateRangeAggregation {..} =
1992+ omitNulls [ "field" .= draField
1993+ , "format" .= draFormat
1994+ , "ranges" .= toList draRanges
1995+ ]
1996+
1997+instance ToJSON DateRangeAggRange where
1998+ toJSON (DateRangeFrom e) = object [ "from" .= e ]
1999+ toJSON (DateRangeTo e) = object [ "to" .= e ]
2000+ toJSON (DateRangeFromAndTo f t) = object [ "from" .= f, "to" .= t ]
2001+
2002+instance ToJSON DateMathExpr where
2003+ toJSON (DateMathExpr a mods) = String (fmtA a <> mconcat (fmtMod <$> mods))
2004+ where fmtA DMNow = "now"
2005+ fmtA (DMDate date) = (T.pack $ showGregorian date) <> "||"
2006+ fmtMod (AddTime n u) = "+" <> showText n <> fmtU u
2007+ fmtMod (SubtractTime n u) = "-" <> showText n <> fmtU u
2008+ fmtMod (RoundDownTo u) = "/" <> fmtU u
2009+ fmtU DMYear = "y"
2010+ fmtU DMMonth = "M"
2011+ fmtU DMWeek = "w"
2012+ fmtU DMDay = "d"
2013+ fmtU DMHour = "h"
2014+ fmtU DMMinute = "m"
2015+ fmtU DMSecond = "s"
2016+
2017+
2018+type AggregationResults = M.Map Text Value
2019+
2020+class BucketAggregation a where
2021+ key :: a -> BucketValue
2022+ docCount :: a -> Int
2023+ aggs :: a -> Maybe AggregationResults
2024+
2025+
2026+data Bucket a = Bucket { buckets :: [a]} deriving (Read, Show)
2027+
2028+data BucketValue = TextValue Text
2029+ | ScientificValue Scientific
2030+ | BoolValue Bool deriving (Read, Show)
2031+
2032+data MissingResult = MissingResult { missingDocCount :: Int } deriving (Show)
2033+
2034+data TopHitResult a = TopHitResult { tarHits :: (SearchHits a)
2035+ } deriving Show
2036+
2037+data TermsResult = TermsResult { termKey :: BucketValue
2038+ , termsDocCount :: Int
2039+ , termsAggs :: Maybe AggregationResults } deriving (Read, Show)
2040+
2041+data DateHistogramResult = DateHistogramResult { dateKey :: Int
2042+ , dateKeyStr :: Maybe Text
2043+ , dateDocCount :: Int
2044+ , dateHistogramAggs :: Maybe AggregationResults } deriving (Read, Show)
2045+
2046+data DateRangeResult = DateRangeResult { dateRangeKey :: Text
2047+ , dateRangeFrom :: Maybe UTCTime
2048+ , dateRangeFromAsString :: Maybe Text
2049+ , dateRangeTo :: Maybe UTCTime
2050+ , dateRangeToAsString :: Maybe Text
2051+ , dateRangeDocCount :: Int
2052+ , dateRangeAggs :: Maybe AggregationResults } deriving (Read, Show, Eq, Generic, Typeable)
2053+
2054+toTerms :: Text -> AggregationResults -> Maybe (Bucket TermsResult)
2055+toTerms = toAggResult
2056+
2057+toDateHistogram :: Text -> AggregationResults -> Maybe (Bucket DateHistogramResult)
2058+toDateHistogram = toAggResult
2059+
2060+toMissing :: Text -> AggregationResults -> Maybe MissingResult
2061+toMissing = toAggResult
2062+
2063+toTopHits :: (FromJSON a) => Text -> AggregationResults -> Maybe (TopHitResult a)
2064+toTopHits = toAggResult
2065+
2066+toAggResult :: (FromJSON a) => Text -> AggregationResults -> Maybe a
2067+toAggResult t a = M.lookup t a >>= deserialize
2068+ where deserialize = parseMaybe parseJSON
2069+
2070+instance BucketAggregation TermsResult where
2071+ key = termKey
2072+ docCount = termsDocCount
2073+ aggs = termsAggs
2074+
2075+instance BucketAggregation DateHistogramResult where
2076+ key = TextValue . showText . dateKey
2077+ docCount = dateDocCount
2078+ aggs = dateHistogramAggs
2079+
2080+instance BucketAggregation DateRangeResult where
2081+ key = TextValue . dateRangeKey
2082+ docCount = dateRangeDocCount
2083+ aggs = dateRangeAggs
2084+
2085+instance (FromJSON a, BucketAggregation a) => FromJSON (Bucket a) where
2086+ parseJSON (Object v) = Bucket <$>
2087+ v .: "buckets"
2088+ parseJSON _ = mempty
2089+
2090+instance FromJSON BucketValue where
2091+ parseJSON (String t) = return $ TextValue t
2092+ parseJSON (Number s) = return $ ScientificValue s
2093+ parseJSON (Bool b) = return $ BoolValue b
2094+ parseJSON _ = mempty
2095+
2096+instance FromJSON MissingResult where
2097+ parseJSON = withObject "MissingResult" parse
2098+ where parse v = MissingResult <$> v .: "doc_count"
2099+
2100+instance FromJSON TermsResult where
2101+ parseJSON (Object v) = TermsResult <$>
2102+ v .: "key" <*>
2103+ v .: "doc_count" <*>
2104+ (pure $ getNamedSubAgg v ["key", "doc_count"])
2105+ parseJSON _ = mempty
2106+
2107+instance FromJSON DateHistogramResult where
2108+ parseJSON (Object v) = DateHistogramResult <$>
2109+ v .: "key" <*>
2110+ v .:? "key_as_string" <*>
2111+ v .: "doc_count" <*>
2112+ (pure $ getNamedSubAgg v [ "key"
2113+ , "doc_count"
2114+ , "key_as_string"
2115+ ]
2116+ )
2117+ parseJSON _ = mempty
2118+
2119+instance FromJSON DateRangeResult where
2120+ parseJSON = withObject "DateRangeResult" parse
2121+ where parse v = DateRangeResult <$>
2122+ v .: "key" <*>
2123+ (fmap posixMS <$> v .:? "from") <*>
2124+ v .:? "from_as_string" <*>
2125+ (fmap posixMS <$> v .:? "to") <*>
2126+ v .:? "to_as_string" <*>
2127+ v .: "doc_count" <*>
2128+ (pure $ getNamedSubAgg v [ "key"
2129+ , "from"
2130+ , "from_as_string"
2131+ , "to"
2132+ , "to_as_string"
2133+ , "doc_count"
2134+ ]
2135+ )
2136+
2137+instance FromJSON POSIXMS where
2138+ parseJSON = withScientific "POSIXMS" (return . parse)
2139+ where parse n = let n' = truncate n :: Integer
2140+ in POSIXMS (posixSecondsToUTCTime (fromInteger (n' `div` 1000)))
2141+
2142+instance (FromJSON a) => FromJSON (TopHitResult a) where
2143+ parseJSON (Object v) = TopHitResult <$>
2144+ v .: "hits"
2145+ parseJSON _ = fail "Failure in FromJSON (TopHitResult a)"
2146+
2147+fieldTagged :: Monad m => (FieldName -> Object -> m a) -> Object -> m a
2148+fieldTagged f o = case HM.toList o of
2149+ [(k, Object o')] -> f (FieldName k) o'
2150+ _ -> fail "Expected object with 1 field-named key"
2151+
2152+-- Try to get an AggregationResults when we don't know the
2153+-- field name. We filter out the known keys to try to minimize the noise.
2154+getNamedSubAgg :: Object -> [Text] -> Maybe AggregationResults
2155+getNamedSubAgg o knownKeys = maggRes
2156+ where unknownKeys = HM.filterWithKey (\k _ -> k `notElem` knownKeys) o
2157+ maggRes
2158+ | HM.null unknownKeys = Nothing
2159+ | otherwise = Just . M.fromList $ HM.toList unknownKeys
2160+
2161+instance ToJSON GeoPoint where
2162+ toJSON (GeoPoint (FieldName geoPointField) geoPointLatLon) =
2163+ object [ geoPointField .= geoPointLatLon ]
2164+
2165+
2166+instance ToJSON Query where
2167+ toJSON (TermQuery (Term termQueryField termQueryValue) boost) =
2168+ object [ "term" .=
2169+ object [termQueryField .= object merged]]
2170+ where
2171+ base = [ "value" .= termQueryValue ]
2172+ boosted = maybe [] (return . ("boost" .=)) boost
2173+ merged = mappend base boosted
2174+
2175+ toJSON (TermsQuery fieldName terms) =
2176+ object [ "terms" .= object conjoined ]
2177+ where conjoined = [fieldName .= terms]
2178+
2179+ toJSON (IdsQuery idsQueryMappingName docIds) =
2180+ object [ "ids" .= object conjoined ]
2181+ where conjoined = [ "type" .= idsQueryMappingName
2182+ , "values" .= fmap toJSON docIds ]
2183+
2184+ toJSON (QueryQueryStringQuery qQueryStringQuery) =
2185+ object [ "query_string" .= qQueryStringQuery ]
2186+
2187+ toJSON (QueryMatchQuery matchQuery) =
2188+ object [ "match" .= matchQuery ]
2189+
2190+ toJSON (QueryMultiMatchQuery multiMatchQuery) =
2191+ toJSON multiMatchQuery
2192+
2193+ toJSON (QueryBoolQuery boolQuery) =
2194+ object [ "bool" .= boolQuery ]
2195+
2196+ toJSON (QueryBoostingQuery boostingQuery) =
2197+ object [ "boosting" .= boostingQuery ]
2198+
2199+ toJSON (QueryCommonTermsQuery commonTermsQuery) =
2200+ object [ "common" .= commonTermsQuery ]
2201+
2202+ toJSON (ConstantScoreQuery query boost) =
2203+ object ["constant_score" .= object ["query" .= query
2204+ , "boost" .= boost]]
2205+
2206+ toJSON (QueryDisMaxQuery disMaxQuery) =
2207+ object [ "dis_max" .= disMaxQuery ]
2208+
2209+ toJSON (QueryFuzzyLikeThisQuery fuzzyQuery) =
2210+ object [ "fuzzy_like_this" .= fuzzyQuery ]
2211+
2212+ toJSON (QueryFuzzyLikeFieldQuery fuzzyFieldQuery) =
2213+ object [ "fuzzy_like_this_field" .= fuzzyFieldQuery ]
2214+
2215+ toJSON (QueryFuzzyQuery fuzzyQuery) =
2216+ object [ "fuzzy" .= fuzzyQuery ]
2217+
2218+ toJSON (QueryHasChildQuery childQuery) =
2219+ object [ "has_child" .= childQuery ]
2220+
2221+ toJSON (QueryHasParentQuery parentQuery) =
2222+ object [ "has_parent" .= parentQuery ]
2223+
2224+ toJSON (QueryIndicesQuery qIndicesQuery) =
2225+ object [ "indices" .= qIndicesQuery ]
2226+
2227+ toJSON (MatchAllQuery boost) =
2228+ object [ "match_all" .= omitNulls [ "boost" .= boost ] ]
2229+
2230+ toJSON (QueryMoreLikeThisQuery query) =
2231+ object [ "more_like_this" .= query ]
2232+
2233+ toJSON (QueryMoreLikeThisFieldQuery query) =
2234+ object [ "more_like_this_field" .= query ]
2235+
2236+ toJSON (QueryNestedQuery query) =
2237+ object [ "nested" .= query ]
2238+
2239+ toJSON (QueryPrefixQuery query) =
2240+ object [ "prefix" .= query ]
2241+
2242+ toJSON (QueryRangeQuery query) =
2243+ object [ "range" .= query ]
2244+
2245+ toJSON (QueryRegexpQuery query) =
2246+ object [ "regexp" .= query ]
2247+
2248+ toJSON (QuerySimpleQueryStringQuery query) =
2249+ object [ "simple_query_string" .= query ]
2250+
2251+ toJSON (QueryExistsQuery (FieldName fieldName)) =
2252+ object ["exists" .= object
2253+ ["field" .= fieldName]
2254+ ]
2255+ toJSON QueryMatchNoneQuery =
2256+ object ["match_none" .= object []]
2257+
2258+ toJSON (QueryTemplateQueryInline templateQuery) =
2259+ object [ "template" .= templateQuery ]
2260+
2261+instance FromJSON Query where
2262+ parseJSON v = withObject "Query" parse v
2263+ where parse o = termQuery `taggedWith` "term"
2264+ <|> termsQuery `taggedWith` "terms"
2265+ <|> idsQuery `taggedWith` "ids"
2266+ <|> queryQueryStringQuery `taggedWith` "query_string"
2267+ <|> queryMatchQuery `taggedWith` "match"
2268+ <|> queryMultiMatchQuery
2269+ <|> queryBoolQuery `taggedWith` "bool"
2270+ <|> queryBoostingQuery `taggedWith` "boosting"
2271+ <|> queryCommonTermsQuery `taggedWith` "common"
2272+ <|> constantScoreQuery `taggedWith` "constant_score"
2273+ <|> queryDisMaxQuery `taggedWith` "dis_max"
2274+ <|> queryFuzzyLikeThisQuery `taggedWith` "fuzzy_like_this"
2275+ <|> queryFuzzyLikeFieldQuery `taggedWith` "fuzzy_like_this_field"
2276+ <|> queryFuzzyQuery `taggedWith` "fuzzy"
2277+ <|> queryHasChildQuery `taggedWith` "has_child"
2278+ <|> queryHasParentQuery `taggedWith` "has_parent"
2279+ <|> queryIndicesQuery `taggedWith` "indices"
2280+ <|> matchAllQuery `taggedWith` "match_all"
2281+ <|> queryMoreLikeThisQuery `taggedWith` "more_like_this"
2282+ <|> queryMoreLikeThisFieldQuery `taggedWith` "more_like_this_field"
2283+ <|> queryNestedQuery `taggedWith` "nested"
2284+ <|> queryPrefixQuery `taggedWith` "prefix"
2285+ <|> queryRangeQuery `taggedWith` "range"
2286+ <|> queryRegexpQuery `taggedWith` "regexp"
2287+ <|> querySimpleQueryStringQuery `taggedWith` "simple_query_string"
2288+ <|> queryTemplateQueryInline `taggedWith` "template"
2289+ where taggedWith parser k = parser =<< o .: k
2290+ termQuery = fieldTagged $ \(FieldName fn) o ->
2291+ TermQuery <$> (Term fn <$> o .: "value") <*> o .:? "boost"
2292+ termsQuery o = case HM.toList o of
2293+ [(fn, vs)] -> do vals <- parseJSON vs
2294+ case vals of
2295+ x:xs -> return (TermsQuery fn (x :| xs))
2296+ _ -> fail "Expected non empty list of values"
2297+ _ -> fail "Expected object with 1 field-named key"
2298+ idsQuery o = IdsQuery <$> o .: "type"
2299+ <*> o .: "values"
2300+ queryQueryStringQuery = pure . QueryQueryStringQuery
2301+ queryMatchQuery = pure . QueryMatchQuery
2302+ queryMultiMatchQuery = QueryMultiMatchQuery <$> parseJSON v
2303+ queryBoolQuery = pure . QueryBoolQuery
2304+ queryBoostingQuery = pure . QueryBoostingQuery
2305+ queryCommonTermsQuery = pure . QueryCommonTermsQuery
2306+ constantScoreQuery o = case HM.lookup "query" o of
2307+ Just x -> ConstantScoreQuery <$> parseJSON x
2308+ <*> o .: "boost"
2309+ _ -> fail "Does not appear to be a ConstantScoreQuery"
2310+ queryDisMaxQuery = pure . QueryDisMaxQuery
2311+ queryFuzzyLikeThisQuery = pure . QueryFuzzyLikeThisQuery
2312+ queryFuzzyLikeFieldQuery = pure . QueryFuzzyLikeFieldQuery
2313+ queryFuzzyQuery = pure . QueryFuzzyQuery
2314+ queryHasChildQuery = pure . QueryHasChildQuery
2315+ queryHasParentQuery = pure . QueryHasParentQuery
2316+ queryIndicesQuery = pure . QueryIndicesQuery
2317+ matchAllQuery o = MatchAllQuery <$> o .:? "boost"
2318+ queryMoreLikeThisQuery = pure . QueryMoreLikeThisQuery
2319+ queryMoreLikeThisFieldQuery = pure . QueryMoreLikeThisFieldQuery
2320+ queryNestedQuery = pure . QueryNestedQuery
2321+ queryPrefixQuery = pure . QueryPrefixQuery
2322+ queryRangeQuery = pure . QueryRangeQuery
2323+ queryRegexpQuery = pure . QueryRegexpQuery
2324+ querySimpleQueryStringQuery = pure . QuerySimpleQueryStringQuery
2325+ queryExistsQuery o = QueryExistsQuery <$> o .: "field"
2326+ queryTemplateQueryInline = pure . QueryTemplateQueryInline
2327+
2328+
2329+omitNulls :: [(Text, Value)] -> Value
2330+omitNulls = object . filter notNull where
2331+ notNull (_, Null) = False
2332+ notNull (_, Array a) = (not . V.null) a
2333+ notNull _ = True
2334+
2335+instance ToJSON SimpleQueryStringQuery where
2336+ toJSON SimpleQueryStringQuery {..} =
2337+ omitNulls (base ++ maybeAdd)
2338+ where base = [ "query" .= simpleQueryStringQuery ]
2339+ maybeAdd = [ "fields" .= simpleQueryStringField
2340+ , "default_operator" .= simpleQueryStringOperator
2341+ , "analyzer" .= simpleQueryStringAnalyzer
2342+ , "flags" .= simpleQueryStringFlags
2343+ , "lowercase_expanded_terms" .= simpleQueryStringLowercaseExpanded
2344+ , "locale" .= simpleQueryStringLocale ]
2345+
2346+instance FromJSON SimpleQueryStringQuery where
2347+ parseJSON = withObject "SimpleQueryStringQuery" parse
2348+ where parse o = SimpleQueryStringQuery <$> o .: "query"
2349+ <*> o .:? "fields"
2350+ <*> o .:? "default_operator"
2351+ <*> o .:? "analyzer"
2352+ <*> (parseFlags <$> o .:? "flags")
2353+ <*> o .:? "lowercase_expanded_terms"
2354+ <*> o .:? "locale"
2355+ parseFlags (Just (x:xs)) = Just (x :| xs)
2356+ parseFlags _ = Nothing
2357+
2358+instance ToJSON FieldOrFields where
2359+ toJSON (FofField fieldName) =
2360+ toJSON fieldName
2361+ toJSON (FofFields fieldNames) =
2362+ toJSON fieldNames
2363+
2364+instance FromJSON FieldOrFields where
2365+ parseJSON v = FofField <$> parseJSON v
2366+ <|> FofFields <$> (parseNEJSON =<< parseJSON v)
2367+
2368+instance ToJSON SimpleQueryFlag where
2369+ toJSON SimpleQueryAll = "ALL"
2370+ toJSON SimpleQueryNone = "NONE"
2371+ toJSON SimpleQueryAnd = "AND"
2372+ toJSON SimpleQueryOr = "OR"
2373+ toJSON SimpleQueryPrefix = "PREFIX"
2374+ toJSON SimpleQueryPhrase = "PHRASE"
2375+ toJSON SimpleQueryPrecedence = "PRECEDENCE"
2376+ toJSON SimpleQueryEscape = "ESCAPE"
2377+ toJSON SimpleQueryWhitespace = "WHITESPACE"
2378+ toJSON SimpleQueryFuzzy = "FUZZY"
2379+ toJSON SimpleQueryNear = "NEAR"
2380+ toJSON SimpleQuerySlop = "SLOP"
2381+
2382+instance FromJSON SimpleQueryFlag where
2383+ parseJSON = withText "SimpleQueryFlag" parse
2384+ where parse "ALL" = pure SimpleQueryAll
2385+ parse "NONE" = pure SimpleQueryNone
2386+ parse "AND" = pure SimpleQueryAnd
2387+ parse "OR" = pure SimpleQueryOr
2388+ parse "PREFIX" = pure SimpleQueryPrefix
2389+ parse "PHRASE" = pure SimpleQueryPhrase
2390+ parse "PRECEDENCE" = pure SimpleQueryPrecedence
2391+ parse "ESCAPE" = pure SimpleQueryEscape
2392+ parse "WHITESPACE" = pure SimpleQueryWhitespace
2393+ parse "FUZZY" = pure SimpleQueryFuzzy
2394+ parse "NEAR" = pure SimpleQueryNear
2395+ parse "SLOP" = pure SimpleQuerySlop
2396+ parse f = fail ("Unexpected SimpleQueryFlag: " <> show f)
2397+
2398+instance ToJSON RegexpQuery where
2399+ toJSON (RegexpQuery (FieldName rqQueryField)
2400+ (Regexp regexpQueryQuery) rqQueryFlags
2401+ rqQueryBoost) =
2402+ object [ rqQueryField .= omitNulls base ]
2403+ where base = [ "value" .= regexpQueryQuery
2404+ , "flags" .= rqQueryFlags
2405+ , "boost" .= rqQueryBoost ]
2406+
2407+instance FromJSON RegexpQuery where
2408+ parseJSON = withObject "RegexpQuery" parse
2409+ where parse = fieldTagged $ \fn o ->
2410+ RegexpQuery fn
2411+ <$> o .: "value"
2412+ <*> o .: "flags"
2413+ <*> o .:? "boost"
2414+
2415+instance ToJSON QueryStringQuery where
2416+ toJSON (QueryStringQuery qsQueryString
2417+ qsDefaultField qsOperator
2418+ qsAnalyzer qsAllowWildcard
2419+ qsLowercaseExpanded qsEnablePositionIncrements
2420+ qsFuzzyMaxExpansions qsFuzziness
2421+ qsFuzzyPrefixLength qsPhraseSlop
2422+ qsBoost qsAnalyzeWildcard
2423+ qsGeneratePhraseQueries qsMinimumShouldMatch
2424+ qsLenient qsLocale) =
2425+ omitNulls base
2426+ where
2427+ base = [ "query" .= qsQueryString
2428+ , "default_field" .= qsDefaultField
2429+ , "default_operator" .= qsOperator
2430+ , "analyzer" .= qsAnalyzer
2431+ , "allow_leading_wildcard" .= qsAllowWildcard
2432+ , "lowercase_expanded_terms" .= qsLowercaseExpanded
2433+ , "enable_position_increments" .= qsEnablePositionIncrements
2434+ , "fuzzy_max_expansions" .= qsFuzzyMaxExpansions
2435+ , "fuzziness" .= qsFuzziness
2436+ , "fuzzy_prefix_length" .= qsFuzzyPrefixLength
2437+ , "phrase_slop" .= qsPhraseSlop
2438+ , "boost" .= qsBoost
2439+ , "analyze_wildcard" .= qsAnalyzeWildcard
2440+ , "auto_generate_phrase_queries" .= qsGeneratePhraseQueries
2441+ , "minimum_should_match" .= qsMinimumShouldMatch
2442+ , "lenient" .= qsLenient
2443+ , "locale" .= qsLocale ]
2444+
2445+instance FromJSON QueryStringQuery where
2446+ parseJSON = withObject "QueryStringQuery" parse
2447+ where parse o = QueryStringQuery
2448+ <$> o .: "query"
2449+ <*> o .:? "default_field"
2450+ <*> o .:? "default_operator"
2451+ <*> o .:? "analyzer"
2452+ <*> o .:? "allow_leading_wildcard"
2453+ <*> o .:? "lowercase_expanded_terms"
2454+ <*> o .:? "enable_position_increments"
2455+ <*> o .:? "fuzzy_max_expansions"
2456+ <*> o .:? "fuzziness"
2457+ <*> o .:? "fuzzy_prefix_length"
2458+ <*> o .:? "phrase_slop"
2459+ <*> o .:? "boost"
2460+ <*> o .:? "analyze_wildcard"
2461+ <*> o .:? "auto_generate_phrase_queries"
2462+ <*> o .:? "minimum_should_match"
2463+ <*> o .:? "lenient"
2464+ <*> o .:? "locale"
2465+
2466+instance ToJSON RangeQuery where
2467+ toJSON (RangeQuery (FieldName fieldName) range boost) =
2468+ object [ fieldName .= object conjoined ]
2469+ where conjoined = [ "boost" .= boost ] ++ (rangeValueToPair range)
2470+
2471+instance FromJSON RangeQuery where
2472+ parseJSON = withObject "RangeQuery" parse
2473+ where parse = fieldTagged $ \fn o ->
2474+ RangeQuery fn
2475+ <$> parseJSON (Object o)
2476+ <*> o .: "boost"
2477+
2478+instance FromJSON RangeValue where
2479+ parseJSON = withObject "RangeValue" parse
2480+ where parse o = parseDate o
2481+ <|> parseDouble o
2482+ parseDate o = do lt <- o .:? "lt"
2483+ lte <- o .:? "lte"
2484+ gt <- o .:? "gt"
2485+ gte <- o .:? "gte"
2486+ case (lt, lte, gt, gte) of
2487+ (Just a, _, Just b, _) -> return (RangeDateGtLt (GreaterThanD b) (LessThanD a))
2488+ (Just a, _, _, Just b)-> return (RangeDateGteLt (GreaterThanEqD b) (LessThanD a))
2489+ (_, Just a, Just b, _)-> return (RangeDateGtLte (GreaterThanD b) (LessThanEqD a))
2490+ (_, Just a, _, Just b)-> return (RangeDateGteLte (GreaterThanEqD b) (LessThanEqD a))
2491+ (_, _, Just a, _)-> return (RangeDateGt (GreaterThanD a))
2492+ (Just a, _, _, _)-> return (RangeDateLt (LessThanD a))
2493+ (_, _, _, Just a)-> return (RangeDateGte (GreaterThanEqD a))
2494+ (_, Just a, _, _)-> return (RangeDateLte (LessThanEqD a))
2495+ (Nothing, Nothing, Nothing, Nothing) -> mzero
2496+ parseDouble o = do lt <- o .:? "lt"
2497+ lte <- o .:? "lte"
2498+ gt <- o .:? "gt"
2499+ gte <- o .:? "gte"
2500+ case (lt, lte, gt, gte) of
2501+ (Just a, _, Just b, _) -> return (RangeDoubleGtLt (GreaterThan b) (LessThan a))
2502+ (Just a, _, _, Just b)-> return (RangeDoubleGteLt (GreaterThanEq b) (LessThan a))
2503+ (_, Just a, Just b, _)-> return (RangeDoubleGtLte (GreaterThan b) (LessThanEq a))
2504+ (_, Just a, _, Just b)-> return (RangeDoubleGteLte (GreaterThanEq b) (LessThanEq a))
2505+ (_, _, Just a, _)-> return (RangeDoubleGt (GreaterThan a))
2506+ (Just a, _, _, _)-> return (RangeDoubleLt (LessThan a))
2507+ (_, _, _, Just a)-> return (RangeDoubleGte (GreaterThanEq a))
2508+ (_, Just a, _, _)-> return (RangeDoubleLte (LessThanEq a))
2509+ (Nothing, Nothing, Nothing, Nothing) -> mzero
2510+
2511+instance ToJSON PrefixQuery where
2512+ toJSON (PrefixQuery (FieldName fieldName) queryValue boost) =
2513+ object [ fieldName .= omitNulls base ]
2514+ where base = [ "value" .= queryValue
2515+ , "boost" .= boost ]
2516+
2517+instance FromJSON PrefixQuery where
2518+ parseJSON = withObject "PrefixQuery" parse
2519+ where parse = fieldTagged $ \fn o ->
2520+ PrefixQuery fn
2521+ <$> o .: "value"
2522+ <*> o .:? "boost"
2523+
2524+instance ToJSON NestedQuery where
2525+ toJSON (NestedQuery nqPath nqScoreType nqQuery) =
2526+ object [ "path" .= nqPath
2527+ , "score_mode" .= nqScoreType
2528+ , "query" .= nqQuery ]
2529+
2530+instance FromJSON NestedQuery where
2531+ parseJSON = withObject "NestedQuery" parse
2532+ where parse o = NestedQuery
2533+ <$> o .: "path"
2534+ <*> o .: "score_mode"
2535+ <*> o .: "query"
2536+
2537+instance ToJSON MoreLikeThisFieldQuery where
2538+ toJSON (MoreLikeThisFieldQuery text (FieldName fieldName)
2539+ percent mtf mqt stopwords mindf maxdf
2540+ minwl maxwl boostTerms boost analyzer) =
2541+ object [ fieldName .= omitNulls base ]
2542+ where base = [ "like_text" .= text
2543+ , "percent_terms_to_match" .= percent
2544+ , "min_term_freq" .= mtf
2545+ , "max_query_terms" .= mqt
2546+ , "stop_words" .= stopwords
2547+ , "min_doc_freq" .= mindf
2548+ , "max_doc_freq" .= maxdf
2549+ , "min_word_length" .= minwl
2550+ , "max_word_length" .= maxwl
2551+ , "boost_terms" .= boostTerms
2552+ , "boost" .= boost
2553+ , "analyzer" .= analyzer ]
2554+
2555+instance FromJSON MoreLikeThisFieldQuery where
2556+ parseJSON = withObject "MoreLikeThisFieldQuery" parse
2557+ where parse = fieldTagged $ \fn o ->
2558+ MoreLikeThisFieldQuery
2559+ <$> o .: "like_text"
2560+ <*> pure fn
2561+ <*> o .:? "percent_terms_to_match"
2562+ <*> o .:? "min_term_freq"
2563+ <*> o .:? "max_query_terms"
2564+ -- <*> (optionalNE =<< o .:? "stop_words")
2565+ <*> o .:? "stop_words"
2566+ <*> o .:? "min_doc_freq"
2567+ <*> o .:? "max_doc_freq"
2568+ <*> o .:? "min_word_length"
2569+ <*> o .:? "max_word_length"
2570+ <*> o .:? "boost_terms"
2571+ <*> o .:? "boost"
2572+ <*> o .:? "analyzer"
2573+ -- optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
2574+
2575+instance ToJSON MoreLikeThisQuery where
2576+ toJSON (MoreLikeThisQuery text fields percent
2577+ mtf mqt stopwords mindf maxdf
2578+ minwl maxwl boostTerms boost analyzer) =
2579+ omitNulls base
2580+ where base = [ "like_text" .= text
2581+ , "fields" .= fields
2582+ , "percent_terms_to_match" .= percent
2583+ , "min_term_freq" .= mtf
2584+ , "max_query_terms" .= mqt
2585+ , "stop_words" .= stopwords
2586+ , "min_doc_freq" .= mindf
2587+ , "max_doc_freq" .= maxdf
2588+ , "min_word_length" .= minwl
2589+ , "max_word_length" .= maxwl
2590+ , "boost_terms" .= boostTerms
2591+ , "boost" .= boost
2592+ , "analyzer" .= analyzer ]
2593+
2594+instance FromJSON MoreLikeThisQuery where
2595+ parseJSON = withObject "MoreLikeThisQuery" parse
2596+ where parse o = MoreLikeThisQuery
2597+ <$> o .: "like_text"
2598+ -- <*> (optionalNE =<< o .:? "fields")
2599+ <*> o .:? "fields"
2600+ <*> o .:? "percent_terms_to_match"
2601+ <*> o .:? "min_term_freq"
2602+ <*> o .:? "max_query_terms"
2603+ -- <*> (optionalNE =<< o .:? "stop_words")
2604+ <*> o .:? "stop_words"
2605+ <*> o .:? "min_doc_freq"
2606+ <*> o .:? "max_doc_freq"
2607+ <*> o .:? "min_word_length"
2608+ <*> o .:? "max_word_length"
2609+ <*> o .:? "boost_terms"
2610+ <*> o .:? "boost"
2611+ <*> o .:? "analyzer"
2612+ -- optionalNE = maybe (pure Nothing) (fmap Just . parseNEJSON)
2613+
2614+instance ToJSON IndicesQuery where
2615+ toJSON (IndicesQuery indices query noMatch) =
2616+ omitNulls [ "indices" .= indices
2617+ , "no_match_query" .= noMatch
2618+ , "query" .= query ]
2619+
2620+instance FromJSON IndicesQuery where
2621+ parseJSON = withObject "IndicesQuery" parse
2622+ where parse o = IndicesQuery
2623+ <$> o .:? "indices" .!= []
2624+ <*> o .: "query"
2625+ <*> o .:? "no_match_query"
2626+
2627+instance ToJSON HasParentQuery where
2628+ toJSON (HasParentQuery queryType query scoreType) =
2629+ omitNulls [ "parent_type" .= queryType
2630+ , "score_type" .= scoreType
2631+ , "query" .= query ]
2632+
2633+instance FromJSON HasParentQuery where
2634+ parseJSON = withObject "HasParentQuery" parse
2635+ where parse o = HasParentQuery
2636+ <$> o .: "parent_type"
2637+ <*> o .: "query"
2638+ <*> o .:? "score_type"
2639+
2640+instance ToJSON HasChildQuery where
2641+ toJSON (HasChildQuery queryType query scoreType) =
2642+ omitNulls [ "query" .= query
2643+ , "score_type" .= scoreType
2644+ , "type" .= queryType ]
2645+
2646+instance FromJSON HasChildQuery where
2647+ parseJSON = withObject "HasChildQuery" parse
2648+ where parse o = HasChildQuery
2649+ <$> o .: "type"
2650+ <*> o .: "query"
2651+ <*> o .:? "score_type"
2652+
2653+instance ToJSON FuzzyQuery where
2654+ toJSON (FuzzyQuery (FieldName fieldName) queryText
2655+ prefixLength maxEx fuzziness boost) =
2656+ object [ fieldName .= omitNulls base ]
2657+ where base = [ "value" .= queryText
2658+ , "fuzziness" .= fuzziness
2659+ , "prefix_length" .= prefixLength
2660+ , "boost" .= boost
2661+ , "max_expansions" .= maxEx ]
2662+
2663+instance FromJSON FuzzyQuery where
2664+ parseJSON = withObject "FuzzyQuery" parse
2665+ where parse = fieldTagged $ \fn o ->
2666+ FuzzyQuery fn
2667+ <$> o .: "value"
2668+ <*> o .: "prefix_length"
2669+ <*> o .: "max_expansions"
2670+ <*> o .: "fuzziness"
2671+ <*> o .:? "boost"
2672+
2673+instance ToJSON FuzzyLikeFieldQuery where
2674+ toJSON (FuzzyLikeFieldQuery (FieldName fieldName)
2675+ fieldText maxTerms ignoreFreq fuzziness prefixLength
2676+ boost analyzer) =
2677+ object [ fieldName .=
2678+ omitNulls [ "like_text" .= fieldText
2679+ , "max_query_terms" .= maxTerms
2680+ , "ignore_tf" .= ignoreFreq
2681+ , "fuzziness" .= fuzziness
2682+ , "prefix_length" .= prefixLength
2683+ , "analyzer" .= analyzer
2684+ , "boost" .= boost ]]
2685+
2686+instance FromJSON FuzzyLikeFieldQuery where
2687+ parseJSON = withObject "FuzzyLikeFieldQuery" parse
2688+ where parse = fieldTagged $ \fn o ->
2689+ FuzzyLikeFieldQuery fn
2690+ <$> o .: "like_text"
2691+ <*> o .: "max_query_terms"
2692+ <*> o .: "ignore_tf"
2693+ <*> o .: "fuzziness"
2694+ <*> o .: "prefix_length"
2695+ <*> o .: "boost"
2696+ <*> o .:? "analyzer"
2697+
2698+instance ToJSON FuzzyLikeThisQuery where
2699+ toJSON (FuzzyLikeThisQuery fields text maxTerms
2700+ ignoreFreq fuzziness prefixLength boost analyzer) =
2701+ omitNulls base
2702+ where base = [ "fields" .= fields
2703+ , "like_text" .= text
2704+ , "max_query_terms" .= maxTerms
2705+ , "ignore_tf" .= ignoreFreq
2706+ , "fuzziness" .= fuzziness
2707+ , "prefix_length" .= prefixLength
2708+ , "analyzer" .= analyzer
2709+ , "boost" .= boost ]
2710+
2711+instance FromJSON FuzzyLikeThisQuery where
2712+ parseJSON = withObject "FuzzyLikeThisQuery" parse
2713+ where parse o = FuzzyLikeThisQuery
2714+ <$> o .:? "fields" .!= []
2715+ <*> o .: "like_text"
2716+ <*> o .: "max_query_terms"
2717+ <*> o .: "ignore_tf"
2718+ <*> o .: "fuzziness"
2719+ <*> o .: "prefix_length"
2720+ <*> o .: "boost"
2721+ <*> o .:? "analyzer"
2722+
2723+instance ToJSON DisMaxQuery where
2724+ toJSON (DisMaxQuery queries tiebreaker boost) =
2725+ omitNulls base
2726+ where base = [ "queries" .= queries
2727+ , "boost" .= boost
2728+ , "tie_breaker" .= tiebreaker ]
2729+
2730+instance FromJSON DisMaxQuery where
2731+ parseJSON = withObject "DisMaxQuery" parse
2732+ where parse o = DisMaxQuery
2733+ <$> o .:? "queries" .!= []
2734+ <*> o .: "tie_breaker"
2735+ <*> o .:? "boost"
2736+
2737+instance ToJSON CommonTermsQuery where
2738+ toJSON (CommonTermsQuery (FieldName fieldName)
2739+ (QueryString query) cf lfo hfo msm
2740+ boost analyzer disableCoord) =
2741+ object [fieldName .= omitNulls base ]
2742+ where base = [ "query" .= query
2743+ , "cutoff_frequency" .= cf
2744+ , "low_freq_operator" .= lfo
2745+ , "minimum_should_match" .= msm
2746+ , "boost" .= boost
2747+ , "analyzer" .= analyzer
2748+ , "disable_coord" .= disableCoord
2749+ , "high_freq_operator" .= hfo ]
2750+
2751+instance FromJSON CommonTermsQuery where
2752+ parseJSON = withObject "CommonTermsQuery" parse
2753+ where parse = fieldTagged $ \fn o ->
2754+ CommonTermsQuery fn
2755+ <$> o .: "query"
2756+ <*> o .: "cutoff_frequency"
2757+ <*> o .: "low_freq_operator"
2758+ <*> o .: "high_freq_operator"
2759+ <*> o .:? "minimum_should_match"
2760+ <*> o .:? "boost"
2761+ <*> o .:? "analyzer"
2762+ <*> o .:? "disable_coord"
2763+
2764+instance ToJSON CommonMinimumMatch where
2765+ toJSON (CommonMinimumMatch mm) = toJSON mm
2766+ toJSON (CommonMinimumMatchHighLow (MinimumMatchHighLow lowF highF)) =
2767+ object [ "low_freq" .= lowF
2768+ , "high_freq" .= highF ]
2769+
2770+instance FromJSON CommonMinimumMatch where
2771+ parseJSON v = parseMinimum v
2772+ <|> parseMinimumHighLow v
2773+ where parseMinimum = fmap CommonMinimumMatch . parseJSON
2774+ parseMinimumHighLow = fmap CommonMinimumMatchHighLow . withObject "CommonMinimumMatchHighLow" (\o ->
2775+ MinimumMatchHighLow
2776+ <$> o .: "low_freq"
2777+ <*> o .: "high_freq")
2778+
2779+
2780+instance ToJSON BoostingQuery where
2781+ toJSON (BoostingQuery bqPositiveQuery bqNegativeQuery bqNegativeBoost) =
2782+ object [ "positive" .= bqPositiveQuery
2783+ , "negative" .= bqNegativeQuery
2784+ , "negative_boost" .= bqNegativeBoost ]
2785+
2786+instance FromJSON BoostingQuery where
2787+ parseJSON = withObject "BoostingQuery" parse
2788+ where parse o = BoostingQuery
2789+ <$> o .: "positive"
2790+ <*> o .: "negative"
2791+ <*> o .: "negative_boost"
2792+
2793+instance ToJSON BoolQuery where
2794+ toJSON (BoolQuery mustM filterM notM shouldM bqMin boost disableCoord) =
2795+ omitNulls base
2796+ where base = [ "must" .= mustM
2797+ , "filter" .= filterM
2798+ , "must_not" .= notM
2799+ , "should" .= shouldM
2800+ , "minimum_should_match" .= bqMin
2801+ , "boost" .= boost
2802+ , "disable_coord" .= disableCoord ]
2803+
2804+instance FromJSON BoolQuery where
2805+ parseJSON = withObject "BoolQuery" parse
2806+ where parse o = BoolQuery
2807+ <$> o .:? "must" .!= []
2808+ <*> o .:? "filter" .!= []
2809+ <*> o .:? "must_not" .!= []
2810+ <*> o .:? "should" .!= []
2811+ <*> o .:? "minimum_should_match"
2812+ <*> o .:? "boost"
2813+ <*> o .:? "disable_coord"
2814+
2815+instance ToJSON MatchQuery where
2816+ toJSON (MatchQuery (FieldName fieldName)
2817+ (QueryString mqQueryString) booleanOperator
2818+ zeroTermsQuery cutoffFrequency matchQueryType
2819+ analyzer maxExpansions lenient boost) =
2820+ object [ fieldName .= omitNulls base ]
2821+ where base = [ "query" .= mqQueryString
2822+ , "operator" .= booleanOperator
2823+ , "zero_terms_query" .= zeroTermsQuery
2824+ , "cutoff_frequency" .= cutoffFrequency
2825+ , "type" .= matchQueryType
2826+ , "analyzer" .= analyzer
2827+ , "max_expansions" .= maxExpansions
2828+ , "lenient" .= lenient
2829+ , "boost" .= boost ]
2830+
2831+instance FromJSON MatchQuery where
2832+ parseJSON = withObject "MatchQuery" parse
2833+ where parse = fieldTagged $ \fn o ->
2834+ MatchQuery fn
2835+ <$> o .: "query"
2836+ <*> o .: "operator"
2837+ <*> o .: "zero_terms_query"
2838+ <*> o .:? "cutoff_frequency"
2839+ <*> o .:? "type"
2840+ <*> o .:? "analyzer"
2841+ <*> o .:? "max_expansions"
2842+ <*> o .:? "lenient"
2843+ <*> o .:? "boost"
2844+
2845+instance ToJSON MultiMatchQuery where
2846+ toJSON (MultiMatchQuery fields (QueryString query) boolOp
2847+ ztQ tb mmqt cf analyzer maxEx lenient) =
2848+ object ["multi_match" .= omitNulls base]
2849+ where base = [ "fields" .= fmap toJSON fields
2850+ , "query" .= query
2851+ , "operator" .= boolOp
2852+ , "zero_terms_query" .= ztQ
2853+ , "tiebreaker" .= tb
2854+ , "type" .= mmqt
2855+ , "cutoff_frequency" .= cf
2856+ , "analyzer" .= analyzer
2857+ , "max_expansions" .= maxEx
2858+ , "lenient" .= lenient ]
2859+
2860+instance FromJSON MultiMatchQuery where
2861+ parseJSON = withObject "MultiMatchQuery" parse
2862+ where parse raw = do o <- raw .: "multi_match"
2863+ MultiMatchQuery
2864+ <$> o .:? "fields" .!= []
2865+ <*> o .: "query"
2866+ <*> o .: "operator"
2867+ <*> o .: "zero_terms_query"
2868+ <*> o .:? "tiebreaker"
2869+ <*> o .:? "type"
2870+ <*> o .:? "cutoff_frequency"
2871+ <*> o .:? "analyzer"
2872+ <*> o .:? "max_expansions"
2873+ <*> o .:? "lenient"
2874+
2875+instance ToJSON MultiMatchQueryType where
2876+ toJSON MultiMatchBestFields = "best_fields"
2877+ toJSON MultiMatchMostFields = "most_fields"
2878+ toJSON MultiMatchCrossFields = "cross_fields"
2879+ toJSON MultiMatchPhrase = "phrase"
2880+ toJSON MultiMatchPhrasePrefix = "phrase_prefix"
2881+
2882+instance FromJSON MultiMatchQueryType where
2883+ parseJSON = withText "MultiMatchPhrasePrefix" parse
2884+ where parse "best_fields" = pure MultiMatchBestFields
2885+ parse "most_fields" = pure MultiMatchMostFields
2886+ parse "cross_fields" = pure MultiMatchCrossFields
2887+ parse "phrase" = pure MultiMatchPhrase
2888+ parse "phrase_prefix" = pure MultiMatchPhrasePrefix
2889+ parse t = fail ("Unexpected MultiMatchPhrasePrefix: " <> show t)
2890+
2891+instance ToJSON BooleanOperator where
2892+ toJSON And = String "and"
2893+ toJSON Or = String "or"
2894+
2895+instance FromJSON BooleanOperator where
2896+ parseJSON = withText "BooleanOperator" parse
2897+ where parse "and" = pure And
2898+ parse "or" = pure Or
2899+ parse o = fail ("Unexpected BooleanOperator: " <> show o)
2900+
2901+instance ToJSON ZeroTermsQuery where
2902+ toJSON ZeroTermsNone = String "none"
2903+ toJSON ZeroTermsAll = String "all"
2904+
2905+instance FromJSON ZeroTermsQuery where
2906+ parseJSON = withText "ZeroTermsQuery" parse
2907+ where parse "none" = pure ZeroTermsNone
2908+ parse "all" = pure ZeroTermsAll
2909+ parse q = fail ("Unexpected ZeroTermsQuery: " <> show q)
2910+
2911+instance ToJSON MatchQueryType where
2912+ toJSON MatchPhrase = "phrase"
2913+ toJSON MatchPhrasePrefix = "phrase_prefix"
2914+
2915+instance FromJSON MatchQueryType where
2916+ parseJSON = withText "MatchQueryType" parse
2917+ where parse "phrase" = pure MatchPhrase
2918+ parse "phrase_prefix" = pure MatchPhrasePrefix
2919+ parse t = fail ("Unexpected MatchQueryType: " <> show t)
2920+
2921+instance FromJSON Status where
2922+ parseJSON (Object v) = Status <$>
2923+ v .: "name" <*>
2924+ v .: "cluster_name" <*>
2925+ v .: "cluster_uuid" <*>
2926+ v .: "version" <*>
2927+ v .: "tagline"
2928+ parseJSON _ = empty
2929+
2930+
2931+instance ToJSON IndexSettings where
2932+ toJSON (IndexSettings s r) = object ["settings" .=
2933+ object ["index" .=
2934+ object ["number_of_shards" .= s, "number_of_replicas" .= r]
2935+ ]
2936+ ]
2937+
2938+instance FromJSON IndexSettings where
2939+ parseJSON = withObject "IndexSettings" parse
2940+ where parse o = do s <- o .: "settings"
2941+ i <- s .: "index"
2942+ IndexSettings <$> i .: "number_of_shards"
2943+ <*> i .: "number_of_replicas"
2944+
2945+instance ToJSON UpdatableIndexSetting where
2946+ toJSON (NumberOfReplicas x) = oPath ("index" :| ["number_of_replicas"]) x
2947+ toJSON (AutoExpandReplicas x) = oPath ("index" :| ["auto_expand_replicas"]) x
2948+ toJSON (RefreshInterval x) = oPath ("index" :| ["refresh_interval"]) (NominalDiffTimeJSON x)
2949+ toJSON (IndexConcurrency x) = oPath ("index" :| ["concurrency"]) x
2950+ toJSON (FailOnMergeFailure x) = oPath ("index" :| ["fail_on_merge_failure"]) x
2951+ toJSON (TranslogFlushThresholdOps x) = oPath ("index" :| ["translog", "flush_threshold_ops"]) x
2952+ toJSON (TranslogFlushThresholdSize x) = oPath ("index" :| ["translog", "flush_threshold_size"]) x
2953+ toJSON (TranslogFlushThresholdPeriod x) = oPath ("index" :| ["translog", "flush_threshold_period"]) (NominalDiffTimeJSON x)
2954+ toJSON (TranslogDisableFlush x) = oPath ("index" :| ["translog", "disable_flush"]) x
2955+ toJSON (CacheFilterMaxSize x) = oPath ("index" :| ["cache", "filter", "max_size"]) x
2956+ toJSON (CacheFilterExpire x) = oPath ("index" :| ["cache", "filter", "expire"]) (NominalDiffTimeJSON <$> x)
2957+ toJSON (GatewaySnapshotInterval x) = oPath ("index" :| ["gateway", "snapshot_interval"]) (NominalDiffTimeJSON x)
2958+ toJSON (RoutingAllocationInclude fs) = oPath ("index" :| ["routing", "allocation", "include"]) (attrFilterJSON fs)
2959+ toJSON (RoutingAllocationExclude fs) = oPath ("index" :| ["routing", "allocation", "exclude"]) (attrFilterJSON fs)
2960+ toJSON (RoutingAllocationRequire fs) = oPath ("index" :| ["routing", "allocation", "require"]) (attrFilterJSON fs)
2961+ toJSON (RoutingAllocationEnable x) = oPath ("index" :| ["routing", "allocation", "enable"]) x
2962+ toJSON (RoutingAllocationShardsPerNode x) = oPath ("index" :| ["routing", "allocation", "total_shards_per_node"]) x
2963+ toJSON (RecoveryInitialShards x) = oPath ("index" :| ["recovery", "initial_shards"]) x
2964+ toJSON (GCDeletes x) = oPath ("index" :| ["gc_deletes"]) (NominalDiffTimeJSON x)
2965+ toJSON (TTLDisablePurge x) = oPath ("index" :| ["ttl", "disable_purge"]) x
2966+ toJSON (TranslogFSType x) = oPath ("index" :| ["translog", "fs", "type"]) x
2967+ toJSON (IndexCompoundFormat x) = oPath ("index" :| ["compound_format"]) x
2968+ toJSON (IndexCompoundOnFlush x) = oPath ("index" :| ["compound_on_flush"]) x
2969+ toJSON (WarmerEnabled x) = oPath ("index" :| ["warmer", "enabled"]) x
2970+ toJSON (BlocksReadOnly x) = oPath ("blocks" :| ["read_only"]) x
2971+ toJSON (BlocksRead x) = oPath ("blocks" :| ["read"]) x
2972+ toJSON (BlocksWrite x) = oPath ("blocks" :| ["write"]) x
2973+ toJSON (BlocksMetaData x) = oPath ("blocks" :| ["metadata"]) x
2974+
2975+instance FromJSON UpdatableIndexSetting where
2976+ parseJSON = withObject "UpdatableIndexSetting" parse
2977+ where parse o = numberOfReplicas `taggedAt` ["index", "number_of_replicas"]
2978+ <|> autoExpandReplicas `taggedAt` ["index", "auto_expand_replicas"]
2979+ <|> refreshInterval `taggedAt` ["index", "refresh_interval"]
2980+ <|> indexConcurrency `taggedAt` ["index", "concurrency"]
2981+ <|> failOnMergeFailure `taggedAt` ["index", "fail_on_merge_failure"]
2982+ <|> translogFlushThresholdOps `taggedAt` ["index", "translog", "flush_threshold_ops"]
2983+ <|> translogFlushThresholdSize `taggedAt` ["index", "translog", "flush_threshold_size"]
2984+ <|> translogFlushThresholdPeriod `taggedAt` ["index", "translog", "flush_threshold_period"]
2985+ <|> translogDisableFlush `taggedAt` ["index", "translog", "disable_flush"]
2986+ <|> cacheFilterMaxSize `taggedAt` ["index", "cache", "filter", "max_size"]
2987+ <|> cacheFilterExpire `taggedAt` ["index", "cache", "filter", "expire"]
2988+ <|> gatewaySnapshotInterval `taggedAt` ["index", "gateway", "snapshot_interval"]
2989+ <|> routingAllocationInclude `taggedAt` ["index", "routing", "allocation", "include"]
2990+ <|> routingAllocationExclude `taggedAt` ["index", "routing", "allocation", "exclude"]
2991+ <|> routingAllocationRequire `taggedAt` ["index", "routing", "allocation", "require"]
2992+ <|> routingAllocationEnable `taggedAt` ["index", "routing", "allocation", "enable"]
2993+ <|> routingAllocationShardsPerNode `taggedAt` ["index", "routing", "allocation", "total_shards_per_node"]
2994+ <|> recoveryInitialShards `taggedAt` ["index", "recovery", "initial_shards"]
2995+ <|> gcDeletes `taggedAt` ["index", "gc_deletes"]
2996+ <|> ttlDisablePurge `taggedAt` ["index", "ttl", "disable_purge"]
2997+ <|> translogFSType `taggedAt` ["index", "translog", "fs", "type"]
2998+ <|> compoundFormat `taggedAt` ["index", "compound_format"]
2999+ <|> compoundOnFlush `taggedAt` ["index", "compound_on_flush"]
3000+ <|> warmerEnabled `taggedAt` ["index", "warmer", "enabled"]
3001+ <|> blocksReadOnly `taggedAt` ["blocks", "read_only"]
3002+ <|> blocksRead `taggedAt` ["blocks", "read"]
3003+ <|> blocksWrite `taggedAt` ["blocks", "write"]
3004+ <|> blocksMetaData `taggedAt` ["blocks", "metadata"]
3005+ where taggedAt f ks = taggedAt' f (Object o) ks
3006+ taggedAt' f v [] = f =<< (parseJSON v <|> (parseJSON (unStringlyTypeJSON v)))
3007+ taggedAt' f v (k:ks) = withObject "Object" (\o -> do v' <- o .: k
3008+ taggedAt' f v' ks) v
3009+ numberOfReplicas = pure . NumberOfReplicas
3010+ autoExpandReplicas = pure . AutoExpandReplicas
3011+ refreshInterval = pure . RefreshInterval . ndtJSON
3012+ indexConcurrency = pure . IndexConcurrency
3013+ failOnMergeFailure = pure . FailOnMergeFailure
3014+ translogFlushThresholdOps = pure . TranslogFlushThresholdOps
3015+ translogFlushThresholdSize = pure . TranslogFlushThresholdSize
3016+ translogFlushThresholdPeriod = pure . TranslogFlushThresholdPeriod . ndtJSON
3017+ translogDisableFlush = pure . TranslogDisableFlush
3018+ cacheFilterMaxSize = pure . CacheFilterMaxSize
3019+ cacheFilterExpire = pure . CacheFilterExpire . fmap ndtJSON
3020+ gatewaySnapshotInterval = pure . GatewaySnapshotInterval . ndtJSON
3021+ routingAllocationInclude = fmap RoutingAllocationInclude . parseAttrFilter
3022+ routingAllocationExclude = fmap RoutingAllocationExclude . parseAttrFilter
3023+ routingAllocationRequire = fmap RoutingAllocationRequire . parseAttrFilter
3024+ routingAllocationEnable = pure . RoutingAllocationEnable
3025+ routingAllocationShardsPerNode = pure . RoutingAllocationShardsPerNode
3026+ recoveryInitialShards = pure . RecoveryInitialShards
3027+ gcDeletes = pure . GCDeletes . ndtJSON
3028+ ttlDisablePurge = pure . TTLDisablePurge
3029+ translogFSType = pure . TranslogFSType
3030+ compoundFormat = pure . IndexCompoundFormat
3031+ compoundOnFlush = pure . IndexCompoundOnFlush
3032+ warmerEnabled = pure . WarmerEnabled
3033+ blocksReadOnly = pure . BlocksReadOnly
3034+ blocksRead = pure . BlocksRead
3035+ blocksWrite = pure . BlocksWrite
3036+ blocksMetaData = pure . BlocksMetaData
3037+
3038+instance FromJSON IndexSettingsSummary where
3039+ parseJSON = withObject "IndexSettingsSummary" parse
3040+ where parse o = case HM.toList o of
3041+ [(ixn, v@(Object o'))] -> IndexSettingsSummary (IndexName ixn)
3042+ <$> parseJSON v
3043+ <*> (fmap (filter (not . redundant)) . parseSettings =<< o' .: "settings")
3044+ _ -> fail "Expected single-key object with index name"
3045+ redundant (NumberOfReplicas _) = True
3046+ redundant _ = False
3047+
3048+-- | For some reason in several settings APIs, all leaf values get returned
3049+-- as strings. This function attepmts to recover from this for all
3050+-- non-recursive JSON types. If nothing can be done, the value is left alone.
3051+unStringlyTypeJSON :: Value -> Value
3052+unStringlyTypeJSON (String "true") = Bool True
3053+unStringlyTypeJSON (String "false") = Bool False
3054+unStringlyTypeJSON (String "null") = Null
3055+unStringlyTypeJSON v@(String t) = case readMay (T.unpack t) of
3056+ Just n -> Number n
3057+ Nothing -> v
3058+unStringlyTypeJSON v = v
3059+
3060+
3061+parseSettings :: Object -> Parser [UpdatableIndexSetting]
3062+parseSettings o = do
3063+ o' <- o .: "index"
3064+ -- slice the index object into singleton hashmaps and try to parse each
3065+ parses <- forM (HM.toList o') $ \(k, v) -> do
3066+ -- blocks are now nested into the "index" key, which is not how they're serialized
3067+ let atRoot = Object (HM.singleton k v)
3068+ let atIndex = Object (HM.singleton "index" atRoot)
3069+ optional (parseJSON atRoot <|> parseJSON atIndex)
3070+ return (catMaybes parses)
3071+
3072+oPath :: ToJSON a => NonEmpty Text -> a -> Value
3073+oPath (k :| []) v = object [k .= v]
3074+oPath (k:| (h:t)) v = object [k .= oPath (h :| t) v]
3075+
3076+attrFilterJSON :: NonEmpty NodeAttrFilter -> Value
3077+attrFilterJSON fs = object [ n .= T.intercalate "," (toList vs)
3078+ | NodeAttrFilter (NodeAttrName n) vs <- toList fs]
3079+
3080+parseAttrFilter :: Value -> Parser (NonEmpty NodeAttrFilter)
3081+parseAttrFilter = withObject "NonEmpty NodeAttrFilter" parse
3082+ where parse o = case HM.toList o of
3083+ [] -> fail "Expected non-empty list of NodeAttrFilters"
3084+ x:xs -> DT.mapM (uncurry parse') (x :| xs)
3085+ parse' n = withText "Text" $ \t ->
3086+ case T.splitOn "," t of
3087+ fv:fvs -> return (NodeAttrFilter (NodeAttrName n) (fv :| fvs))
3088+ [] -> fail "Expected non-empty list of filter values"
3089+
3090+instance ToJSON ReplicaBounds where
3091+ toJSON (ReplicasBounded a b) = String (showText a <> "-" <> showText b)
3092+ toJSON (ReplicasLowerBounded a) = String (showText a <> "-all")
3093+ toJSON ReplicasUnbounded = Bool False
3094+
3095+instance FromJSON ReplicaBounds where
3096+ parseJSON v = withText "ReplicaBounds" parseText v
3097+ <|> withBool "ReplicaBounds" parseBool v
3098+ where parseText t = case T.splitOn "-" t of
3099+ [a, "all"] -> ReplicasLowerBounded <$> parseReadText a
3100+ [a, b] -> ReplicasBounded <$> parseReadText a
3101+ <*> parseReadText b
3102+ _ -> fail ("Could not parse ReplicaBounds: " <> show t)
3103+ parseBool False = pure ReplicasUnbounded
3104+ parseBool _ = fail "ReplicasUnbounded cannot be represented with True"
3105+
3106+instance ToJSON AllocationPolicy where
3107+ toJSON AllocAll = String "all"
3108+ toJSON AllocPrimaries = String "primaries"
3109+ toJSON AllocNewPrimaries = String "new_primaries"
3110+ toJSON AllocNone = String "none"
3111+
3112+instance FromJSON AllocationPolicy where
3113+ parseJSON = withText "AllocationPolicy" parse
3114+ where parse "all" = pure AllocAll
3115+ parse "primaries" = pure AllocPrimaries
3116+ parse "new_primaries" = pure AllocNewPrimaries
3117+ parse "none" = pure AllocNone
3118+ parse t = fail ("Invlaid AllocationPolicy: " <> show t)
3119+
3120+instance ToJSON InitialShardCount where
3121+ toJSON QuorumShards = String "quorum"
3122+ toJSON QuorumMinus1Shards = String "quorum-1"
3123+ toJSON FullShards = String "full"
3124+ toJSON FullMinus1Shards = String "full-1"
3125+ toJSON (ExplicitShards x) = toJSON x
3126+
3127+instance FromJSON InitialShardCount where
3128+ parseJSON v = withText "InitialShardCount" parseText v
3129+ <|> ExplicitShards <$> parseJSON v
3130+ where parseText "quorum" = pure QuorumShards
3131+ parseText "quorum-1" = pure QuorumMinus1Shards
3132+ parseText "full" = pure FullShards
3133+ parseText "full-1" = pure FullMinus1Shards
3134+ parseText _ = mzero
3135+
3136+instance ToJSON FSType where
3137+ toJSON FSSimple = "simple"
3138+ toJSON FSBuffered = "buffered"
3139+
3140+instance FromJSON FSType where
3141+ parseJSON = withText "FSType" parse
3142+ where parse "simple" = pure FSSimple
3143+ parse "buffered" = pure FSBuffered
3144+ parse t = fail ("Invalid FSType: " <> show t)
3145+
3146+instance ToJSON CompoundFormat where
3147+ toJSON (CompoundFileFormat x) = Bool x
3148+ toJSON (MergeSegmentVsTotalIndex x) = toJSON x
3149+
3150+instance FromJSON CompoundFormat where
3151+ parseJSON v = CompoundFileFormat <$> parseJSON v
3152+ <|> MergeSegmentVsTotalIndex <$> parseJSON v
3153+
3154+instance ToJSON NominalDiffTimeJSON where
3155+ toJSON (NominalDiffTimeJSON t) = String (showText (round t :: Integer) <> "s")
3156+
3157+instance FromJSON NominalDiffTimeJSON where
3158+ parseJSON = withText "NominalDiffTime" parse
3159+ where parse t = case T.takeEnd 1 t of
3160+ "s" -> NominalDiffTimeJSON . fromInteger <$> parseReadText (T.dropEnd 1 t)
3161+ _ -> fail "Invalid or missing NominalDiffTime unit (expected s)"
3162+
3163+instance ToJSON IndexTemplate where
3164+ toJSON (IndexTemplate p s m) = merge
3165+ (object [ "template" .= p
3166+ , "mappings" .= foldl' merge (object []) m
3167+ ])
3168+ (toJSON s)
3169+ where
3170+ merge (Object o1) (Object o2) = toJSON $ HM.union o1 o2
3171+ merge o Null = o
3172+ merge _ _ = undefined
3173+
3174+instance (FromJSON a) => FromJSON (EsResult a) where
3175+ parseJSON jsonVal@(Object v) = do
3176+ found <- v .:? "found" .!= False
3177+ fr <- if found
3178+ then parseJSON jsonVal
3179+ else return Nothing
3180+ EsResult <$> v .: "_index" <*>
3181+ v .: "_type" <*>
3182+ v .: "_id" <*>
3183+ pure fr
3184+ parseJSON _ = empty
3185+
3186+instance (FromJSON a) => FromJSON (EsResultFound a) where
3187+ parseJSON (Object v) = EsResultFound <$>
3188+ v .: "_version" <*>
3189+ v .: "_source"
3190+ parseJSON _ = empty
3191+
3192+instance FromJSON EsError where
3193+ parseJSON (Object v) = EsError <$>
3194+ v .: "status" <*>
3195+ (v .: "error" <|> (v .: "error" >>= (.: "reason")))
3196+ parseJSON _ = empty
3197+
3198+instance FromJSON IndexAliasesSummary where
3199+ parseJSON = withObject "IndexAliasesSummary" parse
3200+ where parse o = IndexAliasesSummary . mconcat <$> mapM (uncurry go) (HM.toList o)
3201+ go ixn = withObject "index aliases" $ \ia -> do
3202+ aliases <- ia .:? "aliases" .!= mempty
3203+ forM (HM.toList aliases) $ \(aName, v) -> do
3204+ let indexAlias = IndexAlias (IndexName ixn) (IndexAliasName (IndexName aName))
3205+ IndexAliasSummary indexAlias <$> parseJSON v
3206+
3207+
3208+instance ToJSON IndexAliasAction where
3209+ toJSON (AddAlias ia opts) = object ["add" .= (iaObj <> optsObj)]
3210+ where Object iaObj = toJSON ia
3211+ Object optsObj = toJSON opts
3212+ toJSON (RemoveAlias ia) = object ["remove" .= iaObj]
3213+ where Object iaObj = toJSON ia
3214+
3215+instance ToJSON IndexAlias where
3216+ toJSON IndexAlias {..} = object ["index" .= srcIndex
3217+ , "alias" .= indexAlias
3218+ ]
3219+
3220+instance ToJSON IndexAliasCreate where
3221+ toJSON IndexAliasCreate {..} = Object (filterObj <> routingObj)
3222+ where filterObj = maybe mempty (HM.singleton "filter" . toJSON) aliasCreateFilter
3223+ Object routingObj = maybe (Object mempty) toJSON aliasCreateRouting
3224+
3225+instance ToJSON AliasRouting where
3226+ toJSON (AllAliasRouting v) = object ["routing" .= v]
3227+ toJSON (GranularAliasRouting srch idx) = object (catMaybes prs)
3228+ where prs = [("search_routing" .=) <$> srch
3229+ ,("index_routing" .=) <$> idx]
3230+
3231+instance FromJSON AliasRouting where
3232+ parseJSON = withObject "AliasRouting" parse
3233+ where parse o = parseAll o <|> parseGranular o
3234+ parseAll o = AllAliasRouting <$> o .: "routing"
3235+ parseGranular o = do
3236+ sr <- o .:? "search_routing"
3237+ ir <- o .:? "index_routing"
3238+ if isNothing sr && isNothing ir
3239+ then fail "Both search_routing and index_routing can't be blank"
3240+ else return (GranularAliasRouting sr ir)
3241+
3242+instance FromJSON IndexAliasCreate where
3243+ parseJSON v = withObject "IndexAliasCreate" parse v
3244+ where parse o = IndexAliasCreate <$> optional (parseJSON v)
3245+ <*> o .:? "filter"
3246+
3247+instance ToJSON SearchAliasRouting where
3248+ toJSON (SearchAliasRouting rvs) = toJSON (T.intercalate "," (routingValue <$> toList rvs))
3249+
3250+instance FromJSON SearchAliasRouting where
3251+ parseJSON = withText "SearchAliasRouting" parse
3252+ where parse t = SearchAliasRouting <$> parseNEJSON (String <$> T.splitOn "," t)
3253+
3254+instance ToJSON Search where
3255+ toJSON (Search mquery sFilter sort searchAggs highlight sTrackSortScores sFrom sSize _ sFields sSource) =
3256+ omitNulls [ "query" .= query'
3257+ , "sort" .= sort
3258+ , "aggregations" .= searchAggs
3259+ , "highlight" .= highlight
3260+ , "from" .= sFrom
3261+ , "size" .= sSize
3262+ , "track_scores" .= sTrackSortScores
3263+ , "fields" .= sFields
3264+ , "_source" .= sSource]
3265+
3266+ where query' = case sFilter of
3267+ Nothing -> mquery
3268+ Just x -> Just . QueryBoolQuery $ mkBoolQuery (maybeToList mquery) [x] [] []
3269+
3270+instance ToJSON Source where
3271+ toJSON NoSource = toJSON False
3272+ toJSON (SourcePatterns patterns) = toJSON patterns
3273+ toJSON (SourceIncludeExclude incl excl) = object [ "includes" .= incl, "excludes" .= excl ]
3274+
3275+instance ToJSON PatternOrPatterns where
3276+ toJSON (PopPattern pattern) = toJSON pattern
3277+ toJSON (PopPatterns patterns) = toJSON patterns
3278+
3279+instance ToJSON Include where
3280+ toJSON (Include patterns) = toJSON patterns
3281+
3282+instance ToJSON Exclude where
3283+ toJSON (Exclude patterns) = toJSON patterns
3284+
3285+instance ToJSON Pattern where
3286+ toJSON (Pattern pattern) = toJSON pattern
3287+
3288+
3289+instance ToJSON FieldHighlight where
3290+ toJSON (FieldHighlight (FieldName fName) (Just fSettings)) =
3291+ object [ fName .= fSettings ]
3292+ toJSON (FieldHighlight (FieldName fName) Nothing) =
3293+ object [ fName .= emptyObject ]
3294+
3295+instance ToJSON Highlights where
3296+ toJSON (Highlights global fields) =
3297+ omitNulls (("fields" .= fields)
3298+ : highlightSettingsPairs global)
3299+
3300+instance ToJSON HighlightSettings where
3301+ toJSON hs = omitNulls (highlightSettingsPairs (Just hs))
3302+
3303+highlightSettingsPairs :: Maybe HighlightSettings -> [Pair]
3304+highlightSettingsPairs Nothing = []
3305+highlightSettingsPairs (Just (Plain plh)) = plainHighPairs (Just plh)
3306+highlightSettingsPairs (Just (Postings ph)) = postHighPairs (Just ph)
3307+highlightSettingsPairs (Just (FastVector fvh)) = fastVectorHighPairs (Just fvh)
3308+
3309+
3310+plainHighPairs :: Maybe PlainHighlight -> [Pair]
3311+plainHighPairs Nothing = []
3312+plainHighPairs (Just (PlainHighlight plCom plNonPost)) =
3313+ [ "type" .= String "plain"]
3314+ ++ commonHighlightPairs plCom
3315+ ++ nonPostingsToPairs plNonPost
3316+
3317+postHighPairs :: Maybe PostingsHighlight -> [Pair]
3318+postHighPairs Nothing = []
3319+postHighPairs (Just (PostingsHighlight pCom)) =
3320+ ("type" .= String "postings")
3321+ : commonHighlightPairs pCom
3322+
3323+fastVectorHighPairs :: Maybe FastVectorHighlight -> [Pair]
3324+fastVectorHighPairs Nothing = []
3325+fastVectorHighPairs (Just
3326+ (FastVectorHighlight fvCom fvNonPostSettings fvBoundChars
3327+ fvBoundMaxScan fvFragOff fvMatchedFields
3328+ fvPhraseLim)) =
3329+ [ "type" .= String "fvh"
3330+ , "boundary_chars" .= fvBoundChars
3331+ , "boundary_max_scan" .= fvBoundMaxScan
3332+ , "fragment_offset" .= fvFragOff
3333+ , "matched_fields" .= fvMatchedFields
3334+ , "phraseLimit" .= fvPhraseLim]
3335+ ++ commonHighlightPairs fvCom
3336+ ++ nonPostingsToPairs fvNonPostSettings
3337+
3338+deleteSeveral :: (Eq k, Hashable k) => [k] -> HM.HashMap k v -> HM.HashMap k v
3339+deleteSeveral ks hm = foldr HM.delete hm ks
3340+
3341+commonHighlightPairs :: Maybe CommonHighlight -> [Pair]
3342+commonHighlightPairs Nothing = []
3343+commonHighlightPairs (Just (CommonHighlight chScore chForceSource chTag chEncoder
3344+ chNoMatchSize chHighlightQuery
3345+ chRequireFieldMatch)) =
3346+ [ "order" .= chScore
3347+ , "force_source" .= chForceSource
3348+ , "encoder" .= chEncoder
3349+ , "no_match_size" .= chNoMatchSize
3350+ , "highlight_query" .= chHighlightQuery
3351+ , "require_fieldMatch" .= chRequireFieldMatch]
3352+ ++ highlightTagToPairs chTag
3353+
3354+
3355+nonPostingsToPairs :: Maybe NonPostings -> [Pair]
3356+nonPostingsToPairs Nothing = []
3357+nonPostingsToPairs (Just (NonPostings npFragSize npNumOfFrags)) =
3358+ [ "fragment_size" .= npFragSize
3359+ , "number_of_fragments" .= npNumOfFrags]
3360+
3361+parseNEJSON :: (FromJSON a) => [Value] -> Parser (NonEmpty a)
3362+parseNEJSON [] = fail "Expected non-empty list"
3363+parseNEJSON (x:xs) = DT.mapM parseJSON (x :| xs)
3364+
3365+
3366+instance ToJSON HighlightEncoder where
3367+ toJSON DefaultEncoder = String "default"
3368+ toJSON HTMLEncoder = String "html"
3369+
3370+highlightTagToPairs :: Maybe HighlightTag -> [Pair]
3371+highlightTagToPairs (Just (TagSchema _)) = [ "scheme" .= String "default"]
3372+highlightTagToPairs (Just (CustomTags (pre, post))) = [ "pre_tags" .= pre
3373+ , "post_tags" .= post]
3374+highlightTagToPairs Nothing = []
3375+
3376+instance ToJSON SortSpec where
3377+ toJSON (DefaultSortSpec
3378+ (DefaultSort (FieldName dsSortFieldName) dsSortOrder dsIgnoreUnmapped
3379+ dsSortMode dsMissingSort dsNestedFilter)) =
3380+ object [dsSortFieldName .= omitNulls base] where
3381+ base = [ "order" .= dsSortOrder
3382+ , "unmapped_type" .= dsIgnoreUnmapped
3383+ , "mode" .= dsSortMode
3384+ , "missing" .= dsMissingSort
3385+ , "nested_filter" .= dsNestedFilter ]
3386+
3387+ toJSON (GeoDistanceSortSpec gdsSortOrder (GeoPoint (FieldName field) gdsLatLon) units) =
3388+ object [ "unit" .= units
3389+ , field .= gdsLatLon
3390+ , "order" .= gdsSortOrder ]
3391+
3392+
3393+instance ToJSON SortOrder where
3394+ toJSON Ascending = String "asc"
3395+ toJSON Descending = String "desc"
3396+
3397+
3398+instance ToJSON SortMode where
3399+ toJSON SortMin = String "min"
3400+ toJSON SortMax = String "max"
3401+ toJSON SortSum = String "sum"
3402+ toJSON SortAvg = String "avg"
3403+
3404+
3405+instance ToJSON Missing where
3406+ toJSON LastMissing = String "_last"
3407+ toJSON FirstMissing = String "_first"
3408+ toJSON (CustomMissing txt) = String txt
3409+
3410+
3411+instance ToJSON ScoreType where
3412+ toJSON ScoreTypeMax = "max"
3413+ toJSON ScoreTypeAvg = "avg"
3414+ toJSON ScoreTypeSum = "sum"
3415+ toJSON ScoreTypeNone = "none"
3416+
3417+instance FromJSON ScoreType where
3418+ parseJSON = withText "ScoreType" parse
3419+ where parse "max" = pure ScoreTypeMax
3420+ parse "avg" = pure ScoreTypeAvg
3421+ parse "sum" = pure ScoreTypeSum
3422+ parse "none" = pure ScoreTypeNone
3423+ parse t = fail ("Unexpected ScoreType: " <> show t)
3424+
3425+instance ToJSON Distance where
3426+ toJSON (Distance dCoefficient dUnit) =
3427+ String boltedTogether where
3428+ coefText = showText dCoefficient
3429+ (String unitText) = toJSON dUnit
3430+ boltedTogether = mappend coefText unitText
3431+
3432+instance FromJSON Distance where
3433+ parseJSON = withText "Distance" parse
3434+ where parse t = Distance <$> parseCoeff nT
3435+ <*> parseJSON (String unitT)
3436+ where (nT, unitT) = T.span validForNumber t
3437+ -- may be a better way to do this
3438+ validForNumber '-' = True
3439+ validForNumber '.' = True
3440+ validForNumber 'e' = True
3441+ validForNumber c = isNumber c
3442+ parseCoeff "" = fail "Empty string cannot be parsed as number"
3443+ parseCoeff s = return (read (T.unpack s))
3444+
3445+instance ToJSON DistanceUnit where
3446+ toJSON Miles = String "mi"
3447+ toJSON Yards = String "yd"
3448+ toJSON Feet = String "ft"
3449+ toJSON Inches = String "in"
3450+ toJSON Kilometers = String "km"
3451+ toJSON Meters = String "m"
3452+ toJSON Centimeters = String "cm"
3453+ toJSON Millimeters = String "mm"
3454+ toJSON NauticalMiles = String "nmi"
3455+
3456+
3457+instance FromJSON DistanceUnit where
3458+ parseJSON = withText "DistanceUnit" parse
3459+ where parse "mi" = pure Miles
3460+ parse "yd" = pure Yards
3461+ parse "ft" = pure Feet
3462+ parse "in" = pure Inches
3463+ parse "km" = pure Kilometers
3464+ parse "m" = pure Meters
3465+ parse "cm" = pure Centimeters
3466+ parse "mm" = pure Millimeters
3467+ parse "nmi" = pure NauticalMiles
3468+ parse u = fail ("Unrecognized DistanceUnit: " <> show u)
3469+
3470+instance ToJSON DistanceType where
3471+ toJSON Arc = String "arc"
3472+ toJSON SloppyArc = String "sloppy_arc"
3473+ toJSON Plane = String "plane"
3474+
3475+instance FromJSON DistanceType where
3476+ parseJSON = withText "DistanceType" parse
3477+ where parse "arc" = pure Arc
3478+ parse "sloppy_arc" = pure SloppyArc
3479+ parse "plane" = pure Plane
3480+ parse t = fail ("Unrecognized DistanceType: " <> show t)
3481+
3482+
3483+instance ToJSON OptimizeBbox where
3484+ toJSON NoOptimizeBbox = String "none"
3485+ toJSON (OptimizeGeoFilterType gft) = toJSON gft
3486+
3487+instance FromJSON OptimizeBbox where
3488+ parseJSON v = withText "NoOptimizeBbox" parseNoOptimize v
3489+ <|> parseOptimize v
3490+ where parseNoOptimize "none" = pure NoOptimizeBbox
3491+ parseNoOptimize _ = mzero
3492+ parseOptimize = fmap OptimizeGeoFilterType . parseJSON
3493+
3494+instance ToJSON GeoBoundingBoxConstraint where
3495+ toJSON (GeoBoundingBoxConstraint
3496+ (FieldName gbbcGeoBBField) gbbcConstraintBox cache type') =
3497+ object [gbbcGeoBBField .= gbbcConstraintBox
3498+ , "_cache" .= cache
3499+ , "type" .= type']
3500+
3501+instance FromJSON GeoBoundingBoxConstraint where
3502+ parseJSON = withObject "GeoBoundingBoxConstraint" parse
3503+ where parse o = case HM.toList (deleteSeveral ["type", "_cache"] o) of
3504+ [(fn, v)] -> GeoBoundingBoxConstraint (FieldName fn)
3505+ <$> parseJSON v
3506+ <*> o .:? "_cache" .!= defaultCache
3507+ <*> o .: "type"
3508+ _ -> fail "Could not find field name for GeoBoundingBoxConstraint"
3509+
3510+instance ToJSON GeoFilterType where
3511+ toJSON GeoFilterMemory = String "memory"
3512+ toJSON GeoFilterIndexed = String "indexed"
3513+
3514+instance FromJSON GeoFilterType where
3515+ parseJSON = withText "GeoFilterType" parse
3516+ where parse "memory" = pure GeoFilterMemory
3517+ parse "indexed" = pure GeoFilterIndexed
3518+ parse t = fail ("Unrecognized GeoFilterType: " <> show t)
3519+
3520+instance ToJSON GeoBoundingBox where
3521+ toJSON (GeoBoundingBox gbbTopLeft gbbBottomRight) =
3522+ object ["top_left" .= gbbTopLeft
3523+ , "bottom_right" .= gbbBottomRight]
3524+
3525+instance FromJSON GeoBoundingBox where
3526+ parseJSON = withObject "GeoBoundingBox" parse
3527+ where parse o = GeoBoundingBox
3528+ <$> o .: "top_left"
3529+ <*> o .: "bottom_right"
3530+
3531+instance ToJSON LatLon where
3532+ toJSON (LatLon lLat lLon) =
3533+ object ["lat" .= lLat
3534+ , "lon" .= lLon]
3535+
3536+instance FromJSON LatLon where
3537+ parseJSON = withObject "LatLon" parse
3538+ where parse o = LatLon <$> o .: "lat"
3539+ <*> o .: "lon"
3540+
3541+-- index for smaller ranges, fielddata for longer ranges
3542+instance ToJSON RangeExecution where
3543+ toJSON RangeExecutionIndex = "index"
3544+ toJSON RangeExecutionFielddata = "fielddata"
3545+
3546+
3547+instance FromJSON RangeExecution where
3548+ parseJSON = withText "RangeExecution" parse
3549+ where parse "index" = pure RangeExecutionIndex
3550+ parse "fielddata" = pure RangeExecutionFielddata
3551+ parse t = error ("Unrecognized RangeExecution " <> show t)
3552+
3553+instance ToJSON RegexpFlags where
3554+ toJSON AllRegexpFlags = String "ALL"
3555+ toJSON NoRegexpFlags = String "NONE"
3556+ toJSON (SomeRegexpFlags (h :| fs)) = String $ T.intercalate "|" flagStrs
3557+ where flagStrs = map flagStr . nub $ h:fs
3558+ flagStr AnyString = "ANYSTRING"
3559+ flagStr Automaton = "AUTOMATON"
3560+ flagStr Complement = "COMPLEMENT"
3561+ flagStr Empty = "EMPTY"
3562+ flagStr Intersection = "INTERSECTION"
3563+ flagStr Interval = "INTERVAL"
3564+
3565+instance FromJSON RegexpFlags where
3566+ parseJSON = withText "RegexpFlags" parse
3567+ where parse "ALL" = pure AllRegexpFlags
3568+ parse "NONE" = pure NoRegexpFlags
3569+ parse t = SomeRegexpFlags <$> parseNEJSON (String <$> T.splitOn "|" t)
3570+
3571+instance FromJSON RegexpFlag where
3572+ parseJSON = withText "RegexpFlag" parse
3573+ where parse "ANYSTRING" = pure AnyString
3574+ parse "AUTOMATON" = pure Automaton
3575+ parse "COMPLEMENT" = pure Complement
3576+ parse "EMPTY" = pure Empty
3577+ parse "INTERSECTION" = pure Intersection
3578+ parse "INTERVAL" = pure Interval
3579+ parse f = fail ("Unknown RegexpFlag: " <> show f)
3580+
3581+instance ToJSON Term where
3582+ toJSON (Term field value) = object ["term" .= object
3583+ [field .= value]]
3584+
3585+instance FromJSON Term where
3586+ parseJSON = withObject "Term" parse
3587+ where parse o = do termObj <- o .: "term"
3588+ case HM.toList termObj of
3589+ [(fn, v)] -> Term fn <$> parseJSON v
3590+ _ -> fail "Expected object with 1 field-named key"
3591+
3592+instance ToJSON BoolMatch where
3593+ toJSON (MustMatch term cache) = object ["must" .= term,
3594+ "_cache" .= cache]
3595+ toJSON (MustNotMatch term cache) = object ["must_not" .= term,
3596+ "_cache" .= cache]
3597+ toJSON (ShouldMatch terms cache) = object ["should" .= fmap toJSON terms,
3598+ "_cache" .= cache]
3599+
3600+instance FromJSON BoolMatch where
3601+ parseJSON = withObject "BoolMatch" parse
3602+ where parse o = mustMatch `taggedWith` "must"
3603+ <|> mustNotMatch `taggedWith` "must_not"
3604+ <|> shouldMatch `taggedWith` "should"
3605+ where taggedWith parser k = parser =<< o .: k
3606+ mustMatch t = MustMatch t <$> o .:? "_cache" .!= defaultCache
3607+ mustNotMatch t = MustNotMatch t <$> o .:? "_cache" .!= defaultCache
3608+ shouldMatch t = ShouldMatch t <$> o .:? "_cache" .!= defaultCache
3609+
3610+instance (FromJSON a) => FromJSON (SearchResult a) where
3611+ parseJSON (Object v) = SearchResult <$>
3612+ v .: "took" <*>
3613+ v .: "timed_out" <*>
3614+ v .: "_shards" <*>
3615+ v .: "hits" <*>
3616+ v .:? "aggregations" <*>
3617+ v .:? "_scroll_id"
3618+ parseJSON _ = empty
3619+
3620+instance (FromJSON a) => FromJSON (SearchHits a) where
3621+ parseJSON (Object v) = SearchHits <$>
3622+ v .: "total" <*>
3623+ v .: "max_score" <*>
3624+ v .: "hits"
3625+ parseJSON _ = empty
3626+
3627+instance (FromJSON a) => FromJSON (Hit a) where
3628+ parseJSON (Object v) = Hit <$>
3629+ v .: "_index" <*>
3630+ v .: "_type" <*>
3631+ v .: "_id" <*>
3632+ v .: "_score" <*>
3633+ v .:? "_source" <*>
3634+ v .:? "highlight"
3635+ parseJSON _ = empty
3636+
3637+instance FromJSON ShardResult where
3638+ parseJSON (Object v) = ShardResult <$>
3639+ v .: "total" <*>
3640+ v .: "successful" <*>
3641+ v .: "failed"
3642+ parseJSON _ = empty
3643+
3644+
3645+instance FromJSON DocVersion where
3646+ parseJSON v = do
3647+ i <- parseJSON v
3648+ maybe (fail "DocVersion out of range") return $ mkDocVersion i
3649+
3650+-- This insanity is because ES *sometimes* returns Replica/Shard counts as strings
3651+instance FromJSON ReplicaCount where
3652+ parseJSON v = parseAsInt v
3653+ <|> parseAsString v
3654+ where parseAsInt = fmap ReplicaCount . parseJSON
3655+ parseAsString = withText "ReplicaCount" (fmap ReplicaCount . parseReadText)
3656+
3657+instance FromJSON ShardCount where
3658+ parseJSON v = parseAsInt v
3659+ <|> parseAsString v
3660+ where parseAsInt = fmap ShardCount . parseJSON
3661+ parseAsString = withText "ShardCount" (fmap ShardCount . parseReadText)
3662+
3663+instance Bounded DocVersion where
3664+ minBound = DocVersion 1
3665+ maxBound = DocVersion 9200000000000000000 -- 9.2e+18
3666+
3667+instance Enum DocVersion where
3668+ succ x
3669+ | x /= maxBound = DocVersion (succ $ docVersionNumber x)
3670+ | otherwise = succError "DocVersion"
3671+ pred x
3672+ | x /= minBound = DocVersion (pred $ docVersionNumber x)
3673+ | otherwise = predError "DocVersion"
3674+ toEnum i =
3675+ fromMaybe (error $ show i ++ " out of DocVersion range") $ mkDocVersion i
3676+ fromEnum = docVersionNumber
3677+ enumFrom = boundedEnumFrom
3678+ enumFromThen = boundedEnumFromThen
3679+
3680+-- | Username type used for HTTP Basic authentication. See 'basicAuthHook'.
3681+newtype EsUsername = EsUsername { esUsername :: Text } deriving (Read, Show, Eq)
3682+
3683+-- | Password type used for HTTP Basic authentication. See 'basicAuthHook'.
3684+newtype EsPassword = EsPassword { esPassword :: Text } deriving (Read, Show, Eq)
3685+
3686+
3687+data SnapshotRepoSelection = SnapshotRepoList (NonEmpty SnapshotRepoPattern)
3688+ | AllSnapshotRepos deriving (Eq, Generic, Show, Typeable)
3689+
3690+
3691+-- | Either specifies an exact repo name or one with globs in it,
3692+-- e.g. @RepoPattern "foo*"@ __NOTE__: Patterns are not supported on ES < 1.7
3693+data SnapshotRepoPattern = ExactRepo SnapshotRepoName
3694+ | RepoPattern Text
3695+ deriving (Eq, Generic, Show, Typeable)
3696+
3697+-- | The unique name of a snapshot repository.
3698+newtype SnapshotRepoName = SnapshotRepoName { snapshotRepoName :: Text }
3699+ deriving (Eq, Ord, Generic, Show, Typeable, ToJSON, FromJSON)
3700+
3701+
3702+-- | A generic representation of a snapshot repo. This is what gets
3703+-- sent to and parsed from the server. For repo types enabled by
3704+-- plugins that aren't exported by this library, consider making a
3705+-- custom type which implements 'SnapshotRepo'. If it is a common repo
3706+-- type, consider submitting a pull request to have it included in the
3707+-- library proper
3708+data GenericSnapshotRepo = GenericSnapshotRepo {
3709+ gSnapshotRepoName :: SnapshotRepoName
3710+ , gSnapshotRepoType :: SnapshotRepoType
3711+ , gSnapshotRepoSettings :: GenericSnapshotRepoSettings
3712+ } deriving (Eq, Generic, Show, Typeable)
3713+
3714+
3715+instance SnapshotRepo GenericSnapshotRepo where
3716+ toGSnapshotRepo = id
3717+ fromGSnapshotRepo = Right
3718+
3719+
3720+newtype SnapshotRepoType = SnapshotRepoType { snapshotRepoType :: Text }
3721+ deriving (Eq, Ord, Generic, Show, Typeable, ToJSON, FromJSON)
3722+
3723+
3724+-- | Opaque representation of snapshot repo settings. Instances of
3725+-- 'SnapshotRepo' will produce this.
3726+newtype GenericSnapshotRepoSettings = GenericSnapshotRepoSettings { gSnapshotRepoSettingsObject :: Object }
3727+ deriving (Eq, Generic, Show, Typeable, ToJSON)
3728+
3729+
3730+ -- Regardless of whether you send strongly typed json, my version of
3731+ -- ES sends back stringly typed json in the settings, e.g. booleans
3732+ -- as strings, so we'll try to convert them.
3733+instance FromJSON GenericSnapshotRepoSettings where
3734+ parseJSON = fmap (GenericSnapshotRepoSettings . fmap unStringlyTypeJSON). parseJSON
3735+
3736+-- | The result of running 'verifySnapshotRepo'.
3737+newtype SnapshotVerification = SnapshotVerification { snapshotNodeVerifications :: [SnapshotNodeVerification] }
3738+ deriving (Eq, Generic, Show, Typeable)
3739+
3740+
3741+instance FromJSON SnapshotVerification where
3742+ parseJSON = withObject "SnapshotVerification" parse
3743+ where
3744+ parse o = do
3745+ o2 <- o .: "nodes"
3746+ SnapshotVerification <$> mapM (uncurry parse') (HM.toList o2)
3747+ parse' rawFullId = withObject "SnapshotNodeVerification" $ \o ->
3748+ SnapshotNodeVerification (FullNodeId rawFullId) <$> o .: "name"
3749+
3750+
3751+-- | A node that has verified a snapshot
3752+data SnapshotNodeVerification = SnapshotNodeVerification {
3753+ snvFullId :: FullNodeId
3754+ , snvNodeName :: NodeName
3755+ } deriving (Eq, Generic, Show, Typeable)
3756+
3757+
3758+-- | Unique, automatically-generated name assigned to nodes that are
3759+-- usually returned in node-oriented APIs.
3760+newtype FullNodeId = FullNodeId { fullNodeId :: Text }
3761+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
3762+
3763+
3764+-- | A human-readable node name that is supplied by the user in the
3765+-- node config or automatically generated by ElasticSearch.
3766+newtype NodeName = NodeName { nodeName :: Text }
3767+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
3768+
3769+newtype ClusterName = ClusterName { clusterName :: Text }
3770+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
3771+
3772+data NodesInfo = NodesInfo {
3773+ nodesInfo :: [NodeInfo]
3774+ , nodesClusterName :: ClusterName
3775+ } deriving (Eq, Show, Generic, Typeable)
3776+
3777+data NodesStats = NodesStats {
3778+ nodesStats :: [NodeStats]
3779+ , nodesStatsClusterName :: ClusterName
3780+ } deriving (Eq, Show, Generic, Typeable)
3781+
3782+data NodeStats = NodeStats {
3783+ nodeStatsName :: NodeName
3784+ , nodeStatsFullId :: FullNodeId
3785+ , nodeStatsBreakersStats :: Maybe NodeBreakersStats
3786+ , nodeStatsHTTP :: NodeHTTPStats
3787+ , nodeStatsTransport :: NodeTransportStats
3788+ , nodeStatsFS :: NodeFSStats
3789+ , nodeStatsNetwork :: Maybe NodeNetworkStats
3790+ , nodeStatsThreadPool :: NodeThreadPoolsStats
3791+ , nodeStatsJVM :: NodeJVMStats
3792+ , nodeStatsProcess :: NodeProcessStats
3793+ , nodeStatsOS :: NodeOSStats
3794+ , nodeStatsIndices :: NodeIndicesStats
3795+ } deriving (Eq, Show, Generic, Typeable)
3796+
3797+data NodeBreakersStats = NodeBreakersStats {
3798+ nodeStatsParentBreaker :: NodeBreakerStats
3799+ , nodeStatsRequestBreaker :: NodeBreakerStats
3800+ , nodeStatsFieldDataBreaker :: NodeBreakerStats
3801+ } deriving (Eq, Show, Generic, Typeable)
3802+
3803+data NodeBreakerStats = NodeBreakerStats {
3804+ nodeBreakersTripped :: Int
3805+ , nodeBreakersOverhead :: Double
3806+ , nodeBreakersEstSize :: Bytes
3807+ , nodeBreakersLimitSize :: Bytes
3808+ } deriving (Eq, Show, Generic, Typeable)
3809+
3810+data NodeHTTPStats = NodeHTTPStats {
3811+ nodeHTTPTotalOpened :: Int
3812+ , nodeHTTPCurrentOpen :: Int
3813+ } deriving (Eq, Show, Generic, Typeable)
3814+
3815+data NodeTransportStats = NodeTransportStats {
3816+ nodeTransportTXSize :: Bytes
3817+ , nodeTransportCount :: Int
3818+ , nodeTransportRXSize :: Bytes
3819+ , nodeTransportRXCount :: Int
3820+ , nodeTransportServerOpen :: Int
3821+ } deriving (Eq, Show, Generic, Typeable)
3822+
3823+data NodeFSStats = NodeFSStats {
3824+ nodeFSDataPaths :: [NodeDataPathStats]
3825+ , nodeFSTotal :: NodeFSTotalStats
3826+ , nodeFSTimestamp :: UTCTime
3827+ } deriving (Eq, Show, Generic, Typeable)
3828+
3829+data NodeDataPathStats = NodeDataPathStats {
3830+ nodeDataPathDiskServiceTime :: Maybe Double
3831+ , nodeDataPathDiskQueue :: Maybe Double
3832+ , nodeDataPathIOSize :: Maybe Bytes
3833+ , nodeDataPathWriteSize :: Maybe Bytes
3834+ , nodeDataPathReadSize :: Maybe Bytes
3835+ , nodeDataPathIOOps :: Maybe Int
3836+ , nodeDataPathWrites :: Maybe Int
3837+ , nodeDataPathReads :: Maybe Int
3838+ , nodeDataPathAvailable :: Bytes
3839+ , nodeDataPathFree :: Bytes
3840+ , nodeDataPathTotal :: Bytes
3841+ , nodeDataPathType :: Maybe Text
3842+ , nodeDataPathDevice :: Maybe Text
3843+ , nodeDataPathMount :: Text
3844+ , nodeDataPathPath :: Text
3845+ } deriving (Eq, Show, Generic, Typeable)
3846+
3847+data NodeFSTotalStats = NodeFSTotalStats {
3848+ nodeFSTotalDiskServiceTime :: Maybe Double
3849+ , nodeFSTotalDiskQueue :: Maybe Double
3850+ , nodeFSTotalIOSize :: Maybe Bytes
3851+ , nodeFSTotalWriteSize :: Maybe Bytes
3852+ , nodeFSTotalReadSize :: Maybe Bytes
3853+ , nodeFSTotalIOOps :: Maybe Int
3854+ , nodeFSTotalWrites :: Maybe Int
3855+ , nodeFSTotalReads :: Maybe Int
3856+ , nodeFSTotalAvailable :: Bytes
3857+ , nodeFSTotalFree :: Bytes
3858+ , nodeFSTotalTotal :: Bytes
3859+ } deriving (Eq, Show, Generic, Typeable)
3860+
3861+data NodeNetworkStats = NodeNetworkStats {
3862+ nodeNetTCPOutRSTs :: Int
3863+ , nodeNetTCPInErrs :: Int
3864+ , nodeNetTCPAttemptFails :: Int
3865+ , nodeNetTCPEstabResets :: Int
3866+ , nodeNetTCPRetransSegs :: Int
3867+ , nodeNetTCPOutSegs :: Int
3868+ , nodeNetTCPInSegs :: Int
3869+ , nodeNetTCPCurrEstab :: Int
3870+ , nodeNetTCPPassiveOpens :: Int
3871+ , nodeNetTCPActiveOpens :: Int
3872+ } deriving (Eq, Show, Generic, Typeable)
3873+
3874+data NodeThreadPoolsStats = NodeThreadPoolsStats {
3875+ nodeThreadPoolsStatsSnapshot :: NodeThreadPoolStats
3876+ , nodeThreadPoolsStatsBulk :: NodeThreadPoolStats
3877+ , nodeThreadPoolsStatsMerge :: NodeThreadPoolStats
3878+ , nodeThreadPoolsStatsGet :: NodeThreadPoolStats
3879+ , nodeThreadPoolsStatsManagement :: NodeThreadPoolStats
3880+ , nodeThreadPoolsStatsFetchShardStore :: Maybe NodeThreadPoolStats
3881+ , nodeThreadPoolsStatsOptimize :: Maybe NodeThreadPoolStats
3882+ , nodeThreadPoolsStatsFlush :: NodeThreadPoolStats
3883+ , nodeThreadPoolsStatsSearch :: NodeThreadPoolStats
3884+ , nodeThreadPoolsStatsWarmer :: NodeThreadPoolStats
3885+ , nodeThreadPoolsStatsGeneric :: NodeThreadPoolStats
3886+ , nodeThreadPoolsStatsSuggest :: Maybe NodeThreadPoolStats
3887+ , nodeThreadPoolsStatsRefresh :: NodeThreadPoolStats
3888+ , nodeThreadPoolsStatsIndex :: NodeThreadPoolStats
3889+ , nodeThreadPoolsStatsListener :: Maybe NodeThreadPoolStats
3890+ , nodeThreadPoolsStatsFetchShardStarted :: Maybe NodeThreadPoolStats
3891+ , nodeThreadPoolsStatsPercolate :: Maybe NodeThreadPoolStats
3892+ } deriving (Eq, Show, Generic, Typeable)
3893+
3894+data NodeThreadPoolStats = NodeThreadPoolStats {
3895+ nodeThreadPoolCompleted :: Int
3896+ , nodeThreadPoolLargest :: Int
3897+ , nodeThreadPoolRejected :: Int
3898+ , nodeThreadPoolActive :: Int
3899+ , nodeThreadPoolQueue :: Int
3900+ , nodeThreadPoolThreads :: Int
3901+ } deriving (Eq, Show, Generic, Typeable)
3902+
3903+data NodeJVMStats = NodeJVMStats {
3904+ nodeJVMStatsMappedBufferPool :: JVMBufferPoolStats
3905+ , nodeJVMStatsDirectBufferPool :: JVMBufferPoolStats
3906+ , nodeJVMStatsGCOldCollector :: JVMGCStats
3907+ , nodeJVMStatsGCYoungCollector :: JVMGCStats
3908+ , nodeJVMStatsPeakThreadsCount :: Int
3909+ , nodeJVMStatsThreadsCount :: Int
3910+ , nodeJVMStatsOldPool :: JVMPoolStats
3911+ , nodeJVMStatsSurvivorPool :: JVMPoolStats
3912+ , nodeJVMStatsYoungPool :: JVMPoolStats
3913+ , nodeJVMStatsNonHeapCommitted :: Bytes
3914+ , nodeJVMStatsNonHeapUsed :: Bytes
3915+ , nodeJVMStatsHeapMax :: Bytes
3916+ , nodeJVMStatsHeapCommitted :: Bytes
3917+ , nodeJVMStatsHeapUsedPercent :: Int
3918+ , nodeJVMStatsHeapUsed :: Bytes
3919+ , nodeJVMStatsUptime :: NominalDiffTime
3920+ , nodeJVMStatsTimestamp :: UTCTime
3921+ } deriving (Eq, Show, Generic, Typeable)
3922+
3923+data JVMBufferPoolStats = JVMBufferPoolStats {
3924+ jvmBufferPoolStatsTotalCapacity :: Bytes
3925+ , jvmBufferPoolStatsUsed :: Bytes
3926+ , jvmBufferPoolStatsCount :: Int
3927+ } deriving (Eq, Show, Generic, Typeable)
3928+
3929+data JVMGCStats = JVMGCStats {
3930+ jvmGCStatsCollectionTime :: NominalDiffTime
3931+ , jvmGCStatsCollectionCount :: Int
3932+ } deriving (Eq, Show, Generic, Typeable)
3933+
3934+data JVMPoolStats = JVMPoolStats {
3935+ jvmPoolStatsPeakMax :: Bytes
3936+ , jvmPoolStatsPeakUsed :: Bytes
3937+ , jvmPoolStatsMax :: Bytes
3938+ , jvmPoolStatsUsed :: Bytes
3939+ } deriving (Eq, Show, Generic, Typeable)
3940+
3941+data NodeProcessStats = NodeProcessStats {
3942+ nodeProcessTimestamp :: UTCTime
3943+ , nodeProcessOpenFDs :: Int
3944+ , nodeProcessMaxFDs :: Int
3945+ , nodeProcessCPUPercent :: Int
3946+ , nodeProcessCPUTotal :: NominalDiffTime
3947+ , nodeProcessMemTotalVirtual :: Bytes
3948+ } deriving (Eq, Show, Generic, Typeable)
3949+
3950+data NodeOSStats = NodeOSStats {
3951+ nodeOSTimestamp :: UTCTime
3952+ , nodeOSCPUPercent :: Int
3953+ , nodeOSLoad :: Maybe LoadAvgs
3954+ , nodeOSMemTotal :: Bytes
3955+ , nodeOSMemFree :: Bytes
3956+ , nodeOSMemFreePercent :: Int
3957+ , nodeOSMemUsed :: Bytes
3958+ , nodeOSMemUsedPercent :: Int
3959+ , nodeOSSwapTotal :: Bytes
3960+ , nodeOSSwapFree :: Bytes
3961+ , nodeOSSwapUsed :: Bytes
3962+ } deriving (Eq, Show, Generic, Typeable)
3963+
3964+data LoadAvgs = LoadAvgs {
3965+ loadAvg1Min :: Double
3966+ , loadAvg5Min :: Double
3967+ , loadAvg15Min :: Double
3968+ } deriving (Eq, Show, Generic, Typeable)
3969+
3970+data NodeIndicesStats = NodeIndicesStats {
3971+ nodeIndicesStatsRecoveryThrottleTime :: Maybe NominalDiffTime
3972+ , nodeIndicesStatsRecoveryCurrentAsTarget :: Maybe Int
3973+ , nodeIndicesStatsRecoveryCurrentAsSource :: Maybe Int
3974+ , nodeIndicesStatsQueryCacheMisses :: Maybe Int
3975+ , nodeIndicesStatsQueryCacheHits :: Maybe Int
3976+ , nodeIndicesStatsQueryCacheEvictions :: Maybe Int
3977+ , nodeIndicesStatsQueryCacheSize :: Maybe Bytes
3978+ , nodeIndicesStatsSuggestCurrent :: Maybe Int
3979+ , nodeIndicesStatsSuggestTime :: Maybe NominalDiffTime
3980+ , nodeIndicesStatsSuggestTotal :: Maybe Int
3981+ , nodeIndicesStatsTranslogSize :: Bytes
3982+ , nodeIndicesStatsTranslogOps :: Int
3983+ , nodeIndicesStatsSegFixedBitSetMemory :: Maybe Bytes
3984+ , nodeIndicesStatsSegVersionMapMemory :: Bytes
3985+ , nodeIndicesStatsSegIndexWriterMaxMemory :: Maybe Bytes
3986+ , nodeIndicesStatsSegIndexWriterMemory :: Bytes
3987+ , nodeIndicesStatsSegMemory :: Bytes
3988+ , nodeIndicesStatsSegCount :: Int
3989+ , nodeIndicesStatsCompletionSize :: Bytes
3990+ , nodeIndicesStatsPercolateQueries :: Maybe Int
3991+ , nodeIndicesStatsPercolateMemory :: Maybe Bytes
3992+ , nodeIndicesStatsPercolateCurrent :: Maybe Int
3993+ , nodeIndicesStatsPercolateTime :: Maybe NominalDiffTime
3994+ , nodeIndicesStatsPercolateTotal :: Maybe Int
3995+ , nodeIndicesStatsFieldDataEvictions :: Int
3996+ , nodeIndicesStatsFieldDataMemory :: Bytes
3997+ , nodeIndicesStatsWarmerTotalTime :: NominalDiffTime
3998+ , nodeIndicesStatsWarmerTotal :: Int
3999+ , nodeIndicesStatsWarmerCurrent :: Int
4000+ , nodeIndicesStatsFlushTotalTime :: NominalDiffTime
4001+ , nodeIndicesStatsFlushTotal :: Int
4002+ , nodeIndicesStatsRefreshTotalTime :: NominalDiffTime
4003+ , nodeIndicesStatsRefreshTotal :: Int
4004+ , nodeIndicesStatsMergesTotalSize :: Bytes
4005+ , nodeIndicesStatsMergesTotalDocs :: Int
4006+ , nodeIndicesStatsMergesTotalTime :: NominalDiffTime
4007+ , nodeIndicesStatsMergesTotal :: Int
4008+ , nodeIndicesStatsMergesCurrentSize :: Bytes
4009+ , nodeIndicesStatsMergesCurrentDocs :: Int
4010+ , nodeIndicesStatsMergesCurrent :: Int
4011+ , nodeIndicesStatsSearchFetchCurrent :: Int
4012+ , nodeIndicesStatsSearchFetchTime :: NominalDiffTime
4013+ , nodeIndicesStatsSearchFetchTotal :: Int
4014+ , nodeIndicesStatsSearchQueryCurrent :: Int
4015+ , nodeIndicesStatsSearchQueryTime :: NominalDiffTime
4016+ , nodeIndicesStatsSearchQueryTotal :: Int
4017+ , nodeIndicesStatsSearchOpenContexts :: Int
4018+ , nodeIndicesStatsGetCurrent :: Int
4019+ , nodeIndicesStatsGetMissingTime :: NominalDiffTime
4020+ , nodeIndicesStatsGetMissingTotal :: Int
4021+ , nodeIndicesStatsGetExistsTime :: NominalDiffTime
4022+ , nodeIndicesStatsGetExistsTotal :: Int
4023+ , nodeIndicesStatsGetTime :: NominalDiffTime
4024+ , nodeIndicesStatsGetTotal :: Int
4025+ , nodeIndicesStatsIndexingThrottleTime :: Maybe NominalDiffTime
4026+ , nodeIndicesStatsIndexingIsThrottled :: Maybe Bool
4027+ , nodeIndicesStatsIndexingNoopUpdateTotal :: Maybe Int
4028+ , nodeIndicesStatsIndexingDeleteCurrent :: Int
4029+ , nodeIndicesStatsIndexingDeleteTime :: NominalDiffTime
4030+ , nodeIndicesStatsIndexingDeleteTotal :: Int
4031+ , nodeIndicesStatsIndexingIndexCurrent :: Int
4032+ , nodeIndicesStatsIndexingIndexTime :: NominalDiffTime
4033+ , nodeIndicesStatsIndexingTotal :: Int
4034+ , nodeIndicesStatsStoreThrottleTime :: NominalDiffTime
4035+ , nodeIndicesStatsStoreSize :: Bytes
4036+ , nodeIndicesStatsDocsDeleted :: Int
4037+ , nodeIndicesStatsDocsCount :: Int
4038+ } deriving (Eq, Show, Generic, Typeable)
4039+
4040+-- | A quirky address format used throughout ElasticSearch. An example
4041+-- would be inet[/1.1.1.1:9200]. inet may be a placeholder for a
4042+-- <https://en.wikipedia.org/wiki/Fully_qualified_domain_name FQDN>.
4043+newtype EsAddress = EsAddress { esAddress :: Text }
4044+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
4045+
4046+-- | Typically a 7 character hex string.
4047+newtype BuildHash = BuildHash { buildHash :: Text }
4048+ deriving (Eq, Ord, Generic, Read, Show, Typeable, FromJSON, ToJSON)
4049+
4050+newtype PluginName = PluginName { pluginName :: Text }
4051+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
4052+
4053+data NodeInfo = NodeInfo {
4054+ nodeInfoHTTPAddress :: Maybe EsAddress
4055+ , nodeInfoBuild :: BuildHash
4056+ , nodeInfoESVersion :: VersionNumber
4057+ , nodeInfoIP :: Server
4058+ , nodeInfoHost :: Server
4059+ , nodeInfoTransportAddress :: EsAddress
4060+ , nodeInfoName :: NodeName
4061+ , nodeInfoFullId :: FullNodeId
4062+ , nodeInfoPlugins :: [NodePluginInfo]
4063+ , nodeInfoHTTP :: NodeHTTPInfo
4064+ , nodeInfoTransport :: NodeTransportInfo
4065+ , nodeInfoNetwork :: Maybe NodeNetworkInfo
4066+ , nodeInfoThreadPool :: NodeThreadPoolsInfo
4067+ , nodeInfoJVM :: NodeJVMInfo
4068+ , nodeInfoProcess :: NodeProcessInfo
4069+ , nodeInfoOS :: NodeOSInfo
4070+ , nodeInfoSettings :: Object
4071+ -- ^ The members of the settings objects are not consistent,
4072+ -- dependent on plugins, etc.
4073+ } deriving (Eq, Show, Generic, Typeable)
4074+
4075+data NodePluginInfo = NodePluginInfo {
4076+ nodePluginSite :: Maybe Bool
4077+ -- ^ Is this a site plugin?
4078+ , nodePluginJVM :: Maybe Bool
4079+ -- ^ Is this plugin running on the JVM
4080+ , nodePluginDescription :: Text
4081+ , nodePluginVersion :: MaybeNA VersionNumber
4082+ , nodePluginName :: PluginName
4083+ } deriving (Eq, Show, Generic, Typeable)
4084+
4085+data NodeHTTPInfo = NodeHTTPInfo {
4086+ nodeHTTPMaxContentLength :: Bytes
4087+ , nodeHTTPTransportAddress :: BoundTransportAddress
4088+ } deriving (Eq, Show, Generic, Typeable)
4089+
4090+data NodeTransportInfo = NodeTransportInfo {
4091+ nodeTransportProfiles :: [BoundTransportAddress]
4092+ , nodeTransportAddress :: BoundTransportAddress
4093+ } deriving (Eq, Show, Generic, Typeable)
4094+
4095+data BoundTransportAddress = BoundTransportAddress {
4096+ publishAddress :: EsAddress
4097+ , boundAddress :: [EsAddress]
4098+ } deriving (Eq, Show, Generic, Typeable)
4099+
4100+data NodeNetworkInfo = NodeNetworkInfo {
4101+ nodeNetworkPrimaryInterface :: NodeNetworkInterface
4102+ , nodeNetworkRefreshInterval :: NominalDiffTime
4103+ } deriving (Eq, Show, Generic, Typeable)
4104+
4105+newtype MacAddress = MacAddress { macAddress :: Text }
4106+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
4107+
4108+newtype NetworkInterfaceName = NetworkInterfaceName { networkInterfaceName :: Text }
4109+ deriving (Eq, Ord, Generic, Show, Typeable, FromJSON)
4110+
4111+data NodeNetworkInterface = NodeNetworkInterface {
4112+ nodeNetIfaceMacAddress :: MacAddress
4113+ , nodeNetIfaceName :: NetworkInterfaceName
4114+ , nodeNetIfaceAddress :: Server
4115+ } deriving (Eq, Show, Generic, Typeable)
4116+
4117+data NodeThreadPoolsInfo = NodeThreadPoolsInfo {
4118+ nodeThreadPoolsRefresh :: NodeThreadPoolInfo
4119+ , nodeThreadPoolsManagement :: NodeThreadPoolInfo
4120+ , nodeThreadPoolsPercolate :: Maybe NodeThreadPoolInfo
4121+ , nodeThreadPoolsListener :: Maybe NodeThreadPoolInfo
4122+ , nodeThreadPoolsFetchShardStarted :: Maybe NodeThreadPoolInfo
4123+ , nodeThreadPoolsSearch :: NodeThreadPoolInfo
4124+ , nodeThreadPoolsFlush :: NodeThreadPoolInfo
4125+ , nodeThreadPoolsWarmer :: NodeThreadPoolInfo
4126+ , nodeThreadPoolsOptimize :: Maybe NodeThreadPoolInfo
4127+ , nodeThreadPoolsBulk :: NodeThreadPoolInfo
4128+ , nodeThreadPoolsSuggest :: Maybe NodeThreadPoolInfo
4129+ , nodeThreadPoolsMerge :: NodeThreadPoolInfo
4130+ , nodeThreadPoolsSnapshot :: NodeThreadPoolInfo
4131+ , nodeThreadPoolsGet :: NodeThreadPoolInfo
4132+ , nodeThreadPoolsFetchShardStore :: Maybe NodeThreadPoolInfo
4133+ , nodeThreadPoolsIndex :: NodeThreadPoolInfo
4134+ , nodeThreadPoolsGeneric :: NodeThreadPoolInfo
4135+ } deriving (Eq, Show, Generic, Typeable)
4136+
4137+data NodeThreadPoolInfo = NodeThreadPoolInfo {
4138+ nodeThreadPoolQueueSize :: ThreadPoolSize
4139+ , nodeThreadPoolKeepalive :: Maybe NominalDiffTime
4140+ , nodeThreadPoolMin :: Maybe Int
4141+ , nodeThreadPoolMax :: Maybe Int
4142+ , nodeThreadPoolType :: ThreadPoolType
4143+ } deriving (Eq, Show, Generic, Typeable)
4144+
4145+data ThreadPoolSize = ThreadPoolBounded Int
4146+ | ThreadPoolUnbounded
4147+ deriving (Eq, Show, Generic, Typeable)
4148+
4149+data ThreadPoolType = ThreadPoolScaling
4150+ | ThreadPoolFixed
4151+ | ThreadPoolCached
4152+ deriving (Eq, Show, Generic, Typeable)
4153+
4154+data NodeJVMInfo = NodeJVMInfo {
4155+ nodeJVMInfoMemoryPools :: [JVMMemoryPool]
4156+ , nodeJVMInfoMemoryPoolsGCCollectors :: [JVMGCCollector]
4157+ , nodeJVMInfoMemoryInfo :: JVMMemoryInfo
4158+ , nodeJVMInfoStartTime :: UTCTime
4159+ , nodeJVMInfoVMVendor :: Text
4160+ , nodeJVMVMVersion :: VersionNumber
4161+ -- ^ JVM doesn't seme to follow normal version conventions
4162+ , nodeJVMVMName :: Text
4163+ , nodeJVMVersion :: VersionNumber
4164+ , nodeJVMPID :: PID
4165+ } deriving (Eq, Show, Generic, Typeable)
4166+
4167+-- | Handles quirks in the way JVM versions are rendered (1.7.0_101 -> 1.7.0.101)
4168+newtype JVMVersion = JVMVersion { unJVMVersion :: VersionNumber }
4169+
4170+data JVMMemoryInfo = JVMMemoryInfo {
4171+ jvmMemoryInfoDirectMax :: Bytes
4172+ , jvmMemoryInfoNonHeapMax :: Bytes
4173+ , jvmMemoryInfoNonHeapInit :: Bytes
4174+ , jvmMemoryInfoHeapMax :: Bytes
4175+ , jvmMemoryInfoHeapInit :: Bytes
4176+ } deriving (Eq, Show, Generic, Typeable)
4177+
4178+newtype JVMMemoryPool = JVMMemoryPool {
4179+ jvmMemoryPool :: Text
4180+ } deriving (Eq, Show, Generic, Typeable, FromJSON)
4181+
4182+newtype JVMGCCollector = JVMGCCollector {
4183+ jvmGCCollector :: Text
4184+ } deriving (Eq, Show, Generic, Typeable, FromJSON)
4185+
4186+newtype PID = PID {
4187+ pid :: Int
4188+ } deriving (Eq, Show, Generic, Typeable, FromJSON)
4189+
4190+data NodeOSInfo = NodeOSInfo {
4191+ nodeOSRefreshInterval :: NominalDiffTime
4192+ , nodeOSName :: Text
4193+ , nodeOSArch :: Text
4194+ , nodeOSVersion :: VersionNumber
4195+ , nodeOSAvailableProcessors :: Int
4196+ , nodeOSAllocatedProcessors :: Int
4197+ } deriving (Eq, Show, Generic, Typeable)
4198+
4199+data CPUInfo = CPUInfo {
4200+ cpuCacheSize :: Bytes
4201+ , cpuCoresPerSocket :: Int
4202+ , cpuTotalSockets :: Int
4203+ , cpuTotalCores :: Int
4204+ , cpuMHZ :: Int
4205+ , cpuModel :: Text
4206+ , cpuVendor :: Text
4207+ } deriving (Eq, Show, Generic, Typeable)
4208+
4209+data NodeProcessInfo = NodeProcessInfo {
4210+ nodeProcessMLockAll :: Bool
4211+ -- ^ See <https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
4212+ , nodeProcessMaxFileDescriptors :: Maybe Int
4213+ , nodeProcessId :: PID
4214+ , nodeProcessRefreshInterval :: NominalDiffTime
4215+ } deriving (Eq, Show, Generic, Typeable)
4216+
4217+data SnapshotRepoUpdateSettings = SnapshotRepoUpdateSettings {
4218+ repoUpdateVerify :: Bool
4219+ -- ^ After creation/update, synchronously check that nodes can
4220+ -- write to this repo. Defaults to True. You may use False if you
4221+ -- need a faster response and plan on verifying manually later
4222+ -- with 'verifySnapshotRepo'.
4223+ } deriving (Eq, Show, Generic, Typeable)
4224+
4225+
4226+-- | Reasonable defaults for repo creation/update
4227+--
4228+-- * repoUpdateVerify True
4229+defaultSnapshotRepoUpdateSettings :: SnapshotRepoUpdateSettings
4230+defaultSnapshotRepoUpdateSettings = SnapshotRepoUpdateSettings True
4231+
4232+
4233+-- | A filesystem-based snapshot repo that ships with
4234+-- ElasticSearch. This is an instance of 'SnapshotRepo' so it can be
4235+-- used with 'updateSnapshotRepo'
4236+data FsSnapshotRepo = FsSnapshotRepo {
4237+ fsrName :: SnapshotRepoName
4238+ , fsrLocation :: FilePath
4239+ , fsrCompressMetadata :: Bool
4240+ , fsrChunkSize :: Maybe Bytes
4241+ -- ^ Size by which to split large files during snapshotting.
4242+ , fsrMaxRestoreBytesPerSec :: Maybe Bytes
4243+ -- ^ Throttle node restore rate. If not supplied, defaults to 40mb/sec
4244+ , fsrMaxSnapshotBytesPerSec :: Maybe Bytes
4245+ -- ^ Throttle node snapshot rate. If not supplied, defaults to 40mb/sec
4246+ } deriving (Eq, Generic, Show, Typeable)
4247+
4248+
4249+instance SnapshotRepo FsSnapshotRepo where
4250+ toGSnapshotRepo FsSnapshotRepo {..} =
4251+ GenericSnapshotRepo fsrName fsRepoType (GenericSnapshotRepoSettings settings)
4252+ where
4253+ Object settings = object $ [ "location" .= fsrLocation
4254+ , "compress" .= fsrCompressMetadata
4255+ ] ++ optionalPairs
4256+ optionalPairs = catMaybes [ ("chunk_size" .=) <$> fsrChunkSize
4257+ , ("max_restore_bytes_per_sec" .=) <$> fsrMaxRestoreBytesPerSec
4258+ , ("max_snapshot_bytes_per_sec" .=) <$> fsrMaxSnapshotBytesPerSec
4259+ ]
4260+ fromGSnapshotRepo GenericSnapshotRepo {..}
4261+ | gSnapshotRepoType == fsRepoType = do
4262+ let o = gSnapshotRepoSettingsObject gSnapshotRepoSettings
4263+ parseRepo $ do
4264+ FsSnapshotRepo gSnapshotRepoName <$> o .: "location"
4265+ <*> o .:? "compress" .!= False
4266+ <*> o .:? "chunk_size"
4267+ <*> o .:? "max_restore_bytes_per_sec"
4268+ <*> o .:? "max_snapshot_bytes_per_sec"
4269+ | otherwise = Left (RepoTypeMismatch fsRepoType gSnapshotRepoType)
4270+
4271+
4272+parseRepo :: Parser a -> Either SnapshotRepoConversionError a
4273+parseRepo parser = case parseEither (const parser) () of
4274+ Left e -> Left (OtherRepoConversionError (T.pack e))
4275+ Right a -> Right a
4276+
4277+
4278+fsRepoType :: SnapshotRepoType
4279+fsRepoType = SnapshotRepoType "fs"
4280+
4281+-- | Law: fromGSnapshotRepo (toGSnapshotRepo r) == Right r
4282+class SnapshotRepo r where
4283+ toGSnapshotRepo :: r -> GenericSnapshotRepo
4284+ fromGSnapshotRepo :: GenericSnapshotRepo -> Either SnapshotRepoConversionError r
4285+
4286+
4287+data SnapshotRepoConversionError = RepoTypeMismatch SnapshotRepoType SnapshotRepoType
4288+ -- ^ Expected type and actual type
4289+ | OtherRepoConversionError Text
4290+ deriving (Show, Eq, Generic, Typeable)
4291+
4292+
4293+instance Exception SnapshotRepoConversionError
4294+
4295+
4296+data SnapshotCreateSettings = SnapshotCreateSettings {
4297+ snapWaitForCompletion :: Bool
4298+ -- ^ Should the API call return immediately after initializing
4299+ -- the snapshot or wait until completed? Note that if this is
4300+ -- enabled it could wait a long time, so you should adjust your
4301+ -- 'ManagerSettings' accordingly to set long timeouts or
4302+ -- explicitly handle timeouts.
4303+ , snapIndices :: Maybe IndexSelection
4304+ -- ^ Nothing will snapshot all indices. Just [] is permissable and
4305+ -- will essentially be a no-op snapshot.
4306+ , snapIgnoreUnavailable :: Bool
4307+ -- ^ If set to True, any matched indices that don't exist will be
4308+ -- ignored. Otherwise it will be an error and fail.
4309+ , snapIncludeGlobalState :: Bool
4310+ , snapPartial :: Bool
4311+ -- ^ If some indices failed to snapshot (e.g. if not all primary
4312+ -- shards are available), should the process proceed?
4313+ } deriving (Eq, Generic, Show, Typeable)
4314+
4315+
4316+-- | Reasonable defaults for snapshot creation
4317+--
4318+-- * snapWaitForCompletion False
4319+-- * snapIndices Nothing
4320+-- * snapIgnoreUnavailable False
4321+-- * snapIncludeGlobalState True
4322+-- * snapPartial False
4323+defaultSnapshotCreateSettings :: SnapshotCreateSettings
4324+defaultSnapshotCreateSettings = SnapshotCreateSettings {
4325+ snapWaitForCompletion = False
4326+ , snapIndices = Nothing
4327+ , snapIgnoreUnavailable = False
4328+ , snapIncludeGlobalState = True
4329+ , snapPartial = False
4330+ }
4331+
4332+
4333+data SnapshotSelection = SnapshotList (NonEmpty SnapshotPattern)
4334+ | AllSnapshots deriving (Eq, Generic, Show, Typeable)
4335+
4336+
4337+-- | Either specifies an exact snapshot name or one with globs in it,
4338+-- e.g. @SnapPattern "foo*"@ __NOTE__: Patterns are not supported on
4339+-- ES < 1.7
4340+data SnapshotPattern = ExactSnap SnapshotName
4341+ | SnapPattern Text
4342+ deriving (Eq, Generic, Show, Typeable)
4343+
4344+
4345+-- | General information about the state of a snapshot. Has some
4346+-- redundancies with 'SnapshotStatus'
4347+data SnapshotInfo = SnapshotInfo {
4348+ snapInfoShards :: ShardResult
4349+ , snapInfoFailures :: [SnapshotShardFailure]
4350+ , snapInfoDuration :: NominalDiffTime
4351+ , snapInfoEndTime :: UTCTime
4352+ , snapInfoStartTime :: UTCTime
4353+ , snapInfoState :: SnapshotState
4354+ , snapInfoIndices :: [IndexName]
4355+ , snapInfoName :: SnapshotName
4356+ } deriving (Eq, Generic, Show, Typeable)
4357+
4358+
4359+instance FromJSON SnapshotInfo where
4360+ parseJSON = withObject "SnapshotInfo" parse
4361+ where
4362+ parse o = SnapshotInfo <$> o .: "shards"
4363+ <*> o .: "failures"
4364+ <*> (unMS <$> o .: "duration_in_millis")
4365+ <*> (posixMS <$> o .: "end_time_in_millis")
4366+ <*> (posixMS <$> o .: "start_time_in_millis")
4367+ <*> o .: "state"
4368+ <*> o .: "indices"
4369+ <*> o .: "snapshot"
4370+
4371+data SnapshotShardFailure = SnapshotShardFailure {
4372+ snapShardFailureIndex :: IndexName
4373+ , snapShardFailureNodeId :: Maybe NodeName -- I'm not 100% sure this isn't actually 'FullNodeId'
4374+ , snapShardFailureReason :: Text
4375+ , snapShardFailureShardId :: ShardId
4376+ } deriving (Eq, Show, Generic, Typeable)
4377+
4378+
4379+instance FromJSON SnapshotShardFailure where
4380+ parseJSON = withObject "SnapshotShardFailure" parse
4381+ where
4382+ parse o = SnapshotShardFailure <$> o .: "index"
4383+ <*> o .:? "node_id"
4384+ <*> o .: "reason"
4385+ <*> o .: "shard_id"
4386+
4387+
4388+newtype ShardId = ShardId { shardId :: Int }
4389+ deriving (Eq, Show, Generic, Typeable, FromJSON)
4390+
4391+-- | Milliseconds
4392+newtype MS = MS NominalDiffTime
4393+
4394+
4395+-- keeps the unexported constructor warnings at bay
4396+unMS :: MS -> NominalDiffTime
4397+unMS (MS t) = t
4398+
4399+
4400+instance FromJSON MS where
4401+ parseJSON = withScientific "MS" (return . MS . parse)
4402+ where
4403+ parse n = fromInteger ((truncate n) * 1000)
4404+
4405+
4406+data SnapshotState = SnapshotInit
4407+ | SnapshotStarted
4408+ | SnapshotSuccess
4409+ | SnapshotFailed
4410+ | SnapshotAborted
4411+ | SnapshotMissing
4412+ | SnapshotWaiting
4413+ deriving (Show, Eq, Generic, Typeable)
4414+
4415+instance FromJSON SnapshotState where
4416+ parseJSON = withText "SnapshotState" parse
4417+ where
4418+ parse "INIT" = return SnapshotInit
4419+ parse "STARTED" = return SnapshotStarted
4420+ parse "SUCCESS" = return SnapshotSuccess
4421+ parse "FAILED" = return SnapshotFailed
4422+ parse "ABORTED" = return SnapshotAborted
4423+ parse "MISSING" = return SnapshotMissing
4424+ parse "WAITING" = return SnapshotWaiting
4425+ parse t = fail ("Invalid snapshot state " <> T.unpack t)
4426+
4427+
4428+newtype SnapshotName = SnapshotName { snapshotName :: Text }
4429+ deriving (Show, Eq, Ord, Generic, Typeable, ToJSON, FromJSON)
4430+
4431+
4432+data SnapshotRestoreSettings = SnapshotRestoreSettings {
4433+ snapRestoreWaitForCompletion :: Bool
4434+ -- ^ Should the API call return immediately after initializing
4435+ -- the restore or wait until completed? Note that if this is
4436+ -- enabled, it could wait a long time, so you should adjust your
4437+ -- 'ManagerSettings' accordingly to set long timeouts or
4438+ -- explicitly handle timeouts.
4439+ , snapRestoreIndices :: Maybe IndexSelection
4440+ -- ^ Nothing will restore all indices in the snapshot. Just [] is
4441+ -- permissable and will essentially be a no-op restore.
4442+ , snapRestoreIgnoreUnavailable :: Bool
4443+ -- ^ If set to True, any indices that do not exist will be ignored
4444+ -- during snapshot rather than failing the restore.
4445+ , snapRestoreIncludeGlobalState :: Bool
4446+ -- ^ If set to false, will ignore any global state in the snapshot
4447+ -- and will not restore it.
4448+ , snapRestoreRenamePattern :: Maybe RestoreRenamePattern
4449+ -- ^ A regex pattern for matching indices. Used with
4450+ -- 'snapRestoreRenameReplacement', the restore can reference the
4451+ -- matched index and create a new index name upon restore.
4452+ , snapRestoreRenameReplacement :: Maybe (NonEmpty RestoreRenameToken)
4453+ -- ^ Expression of how index renames should be constructed.
4454+ , snapRestorePartial :: Bool
4455+ -- ^ If some indices fail to restore, should the process proceed?
4456+ , snapRestoreIncludeAliases :: Bool
4457+ -- ^ Should the restore also restore the aliases captured in the
4458+ -- snapshot.
4459+ , snapRestoreIndexSettingsOverrides :: Maybe RestoreIndexSettings
4460+ -- ^ Settings to apply during the restore process. __NOTE:__ This
4461+ -- option is not supported in ES < 1.5 and should be set to
4462+ -- Nothing in that case.
4463+ , snapRestoreIgnoreIndexSettings :: Maybe (NonEmpty Text)
4464+ -- ^ This type could be more rich but it isn't clear which
4465+ -- settings are allowed to be ignored during restore, so we're
4466+ -- going with including this feature in a basic form rather than
4467+ -- omitting it. One example here would be
4468+ -- "index.refresh_interval". Any setting specified here will
4469+ -- revert back to the server default during the restore process.
4470+ } deriving (Eq, Generic, Show, Typeable)
4471+
4472+-- | Regex-stype pattern, e.g. "index_(.+)" to match index names
4473+newtype RestoreRenamePattern = RestoreRenamePattern { rrPattern :: Text }
4474+ deriving (Show, Eq, Generic, Typeable, Ord, ToJSON)
4475+
4476+
4477+-- | A single token in a index renaming scheme for a restore. These
4478+-- are concatenated into a string before being sent to
4479+-- ElasticSearch. Check out these Java
4480+-- <https://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html docs> to find out more if you're into that sort of thing.
4481+data RestoreRenameToken = RRTLit Text
4482+ -- ^ Just a literal string of characters
4483+ | RRSubWholeMatch
4484+ -- ^ Equivalent to $0. The entire matched pattern, not any subgroup
4485+ | RRSubGroup RRGroupRefNum
4486+ -- ^ A specific reference to a group number
4487+ deriving (Show, Eq, Generic, Typeable)
4488+
4489+
4490+-- | A group number for regex matching. Only values from 1-9 are
4491+-- supported. Construct with 'mkRRGroupRefNum'
4492+newtype RRGroupRefNum = RRGroupRefNum { rrGroupRefNum :: Int }
4493+ deriving (Show, Eq, Generic, Typeable, Ord)
4494+
4495+instance Bounded RRGroupRefNum where
4496+ minBound = RRGroupRefNum 1
4497+ maxBound = RRGroupRefNum 9
4498+
4499+
4500+-- | Only allows valid group number references (1-9).
4501+mkRRGroupRefNum :: Int -> Maybe RRGroupRefNum
4502+mkRRGroupRefNum i
4503+ | i >= (rrGroupRefNum minBound) && i <= (rrGroupRefNum maxBound) =
4504+ Just $ RRGroupRefNum i
4505+ | otherwise = Nothing
4506+
4507+
4508+-- | Reasonable defaults for snapshot restores
4509+--
4510+-- * snapRestoreWaitForCompletion False
4511+-- * snapRestoreIndices Nothing
4512+-- * snapRestoreIgnoreUnavailable False
4513+-- * snapRestoreIncludeGlobalState True
4514+-- * snapRestoreRenamePattern Nothing
4515+-- * snapRestoreRenameReplacement Nothing
4516+-- * snapRestorePartial False
4517+-- * snapRestoreIncludeAliases True
4518+-- * snapRestoreIndexSettingsOverrides Nothing
4519+-- * snapRestoreIgnoreIndexSettings Nothing
4520+defaultSnapshotRestoreSettings :: SnapshotRestoreSettings
4521+defaultSnapshotRestoreSettings = SnapshotRestoreSettings {
4522+ snapRestoreWaitForCompletion = False
4523+ , snapRestoreIndices = Nothing
4524+ , snapRestoreIgnoreUnavailable = False
4525+ , snapRestoreIncludeGlobalState = True
4526+ , snapRestoreRenamePattern = Nothing
4527+ , snapRestoreRenameReplacement = Nothing
4528+ , snapRestorePartial = False
4529+ , snapRestoreIncludeAliases = True
4530+ , snapRestoreIndexSettingsOverrides = Nothing
4531+ , snapRestoreIgnoreIndexSettings = Nothing
4532+ }
4533+
4534+
4535+-- | Index settings that can be overridden. The docs only mention you
4536+-- can update number of replicas, but there may be more. You
4537+-- definitely cannot override shard count.
4538+data RestoreIndexSettings = RestoreIndexSettings {
4539+ restoreOverrideReplicas :: Maybe ReplicaCount
4540+ } deriving (Show, Eq, Generic, Typeable)
4541+
4542+
4543+instance ToJSON RestoreIndexSettings where
4544+ toJSON RestoreIndexSettings {..} = object prs
4545+ where
4546+ prs = catMaybes [("index.number_of_replicas" .=) <$> restoreOverrideReplicas]
4547+
4548+
4549+instance FromJSON NodesInfo where
4550+ parseJSON = withObject "NodesInfo" parse
4551+ where
4552+ parse o = do
4553+ nodes <- o .: "nodes"
4554+ infos <- forM (HM.toList nodes) $ \(fullNID, v) -> do
4555+ node <- parseJSON v
4556+ parseNodeInfo (FullNodeId fullNID) node
4557+ cn <- o .: "cluster_name"
4558+ return (NodesInfo infos cn)
4559+
4560+instance FromJSON NodesStats where
4561+ parseJSON = withObject "NodesStats" parse
4562+ where
4563+ parse o = do
4564+ nodes <- o .: "nodes"
4565+ stats <- forM (HM.toList nodes) $ \(fullNID, v) -> do
4566+ node <- parseJSON v
4567+ parseNodeStats (FullNodeId fullNID) node
4568+ cn <- o .: "cluster_name"
4569+ return (NodesStats stats cn)
4570+
4571+instance FromJSON NodeBreakerStats where
4572+ parseJSON = withObject "NodeBreakerStats" parse
4573+ where
4574+ parse o = NodeBreakerStats <$> o .: "tripped"
4575+ <*> o .: "overhead"
4576+ <*> o .: "estimated_size_in_bytes"
4577+ <*> o .: "limit_size_in_bytes"
4578+
4579+instance FromJSON NodeHTTPStats where
4580+ parseJSON = withObject "NodeHTTPStats" parse
4581+ where
4582+ parse o = NodeHTTPStats <$> o .: "total_opened"
4583+ <*> o .: "current_open"
4584+
4585+instance FromJSON NodeTransportStats where
4586+ parseJSON = withObject "NodeTransportStats" parse
4587+ where
4588+ parse o = NodeTransportStats <$> o .: "tx_size_in_bytes"
4589+ <*> o .: "tx_count"
4590+ <*> o .: "rx_size_in_bytes"
4591+ <*> o .: "rx_count"
4592+ <*> o .: "server_open"
4593+
4594+instance FromJSON NodeFSStats where
4595+ parseJSON = withObject "NodeFSStats" parse
4596+ where
4597+ parse o = NodeFSStats <$> o .: "data"
4598+ <*> o .: "total"
4599+ <*> (posixMS <$> o .: "timestamp")
4600+
4601+instance FromJSON NodeDataPathStats where
4602+ parseJSON = withObject "NodeDataPathStats" parse
4603+ where
4604+ parse o =
4605+ NodeDataPathStats <$> (fmap unStringlyTypedDouble <$> o .:? "disk_service_time")
4606+ <*> (fmap unStringlyTypedDouble <$> o .:? "disk_queue")
4607+ <*> o .:? "disk_io_size_in_bytes"
4608+ <*> o .:? "disk_write_size_in_bytes"
4609+ <*> o .:? "disk_read_size_in_bytes"
4610+ <*> o .:? "disk_io_op"
4611+ <*> o .:? "disk_writes"
4612+ <*> o .:? "disk_reads"
4613+ <*> o .: "available_in_bytes"
4614+ <*> o .: "free_in_bytes"
4615+ <*> o .: "total_in_bytes"
4616+ <*> o .:? "type"
4617+ <*> o .:? "dev"
4618+ <*> o .: "mount"
4619+ <*> o .: "path"
4620+
4621+newtype StringlyTypedDouble = StringlyTypedDouble { unStringlyTypedDouble :: Double }
4622+
4623+
4624+instance FromJSON StringlyTypedDouble where
4625+ parseJSON = fmap StringlyTypedDouble . parseJSON . unStringlyTypeJSON
4626+
4627+
4628+instance FromJSON NodeFSTotalStats where
4629+ parseJSON = withObject "NodeFSTotalStats" parse
4630+ where
4631+ parse o = NodeFSTotalStats <$> (fmap unStringlyTypedDouble <$> o .:? "disk_service_time")
4632+ <*> (fmap unStringlyTypedDouble <$> o .:? "disk_queue")
4633+ <*> o .:? "disk_io_size_in_bytes"
4634+ <*> o .:? "disk_write_size_in_bytes"
4635+ <*> o .:? "disk_read_size_in_bytes"
4636+ <*> o .:? "disk_io_op"
4637+ <*> o .:? "disk_writes"
4638+ <*> o .:? "disk_reads"
4639+ <*> o .: "available_in_bytes"
4640+ <*> o .: "free_in_bytes"
4641+ <*> o .: "total_in_bytes"
4642+
4643+instance FromJSON NodeNetworkStats where
4644+ parseJSON = withObject "NodeNetworkStats" parse
4645+ where
4646+ parse o = do
4647+ tcp <- o .: "tcp"
4648+ NodeNetworkStats <$> tcp .: "out_rsts"
4649+ <*> tcp .: "in_errs"
4650+ <*> tcp .: "attempt_fails"
4651+ <*> tcp .: "estab_resets"
4652+ <*> tcp .: "retrans_segs"
4653+ <*> tcp .: "out_segs"
4654+ <*> tcp .: "in_segs"
4655+ <*> tcp .: "curr_estab"
4656+ <*> tcp .: "passive_opens"
4657+ <*> tcp .: "active_opens"
4658+
4659+instance FromJSON NodeThreadPoolsStats where
4660+ parseJSON = withObject "NodeThreadPoolsStats" parse
4661+ where
4662+ parse o = NodeThreadPoolsStats <$> o .: "snapshot"
4663+ <*> o .: "bulk"
4664+ <*> o .: "force_merge"
4665+ <*> o .: "get"
4666+ <*> o .: "management"
4667+ <*> o .:? "fetch_shard_store"
4668+ <*> o .:? "optimize"
4669+ <*> o .: "flush"
4670+ <*> o .: "search"
4671+ <*> o .: "warmer"
4672+ <*> o .: "generic"
4673+ <*> o .:? "suggest"
4674+ <*> o .: "refresh"
4675+ <*> o .: "index"
4676+ <*> o .:? "listener"
4677+ <*> o .:? "fetch_shard_started"
4678+ <*> o .:? "percolate"
4679+instance FromJSON NodeThreadPoolStats where
4680+ parseJSON = withObject "NodeThreadPoolStats" parse
4681+ where
4682+ parse o = NodeThreadPoolStats <$> o .: "completed"
4683+ <*> o .: "largest"
4684+ <*> o .: "rejected"
4685+ <*> o .: "active"
4686+ <*> o .: "queue"
4687+ <*> o .: "threads"
4688+
4689+instance FromJSON NodeJVMStats where
4690+ parseJSON = withObject "NodeJVMStats" parse
4691+ where
4692+ parse o = do
4693+ bufferPools <- o .: "buffer_pools"
4694+ mapped <- bufferPools .: "mapped"
4695+ direct <- bufferPools .: "direct"
4696+ gc <- o .: "gc"
4697+ collectors <- gc .: "collectors"
4698+ oldC <- collectors .: "old"
4699+ youngC <- collectors .: "young"
4700+ threads <- o .: "threads"
4701+ mem <- o .: "mem"
4702+ pools <- mem .: "pools"
4703+ oldM <- pools .: "old"
4704+ survivorM <- pools .: "survivor"
4705+ youngM <- pools .: "young"
4706+ NodeJVMStats <$> pure mapped
4707+ <*> pure direct
4708+ <*> pure oldC
4709+ <*> pure youngC
4710+ <*> threads .: "peak_count"
4711+ <*> threads .: "count"
4712+ <*> pure oldM
4713+ <*> pure survivorM
4714+ <*> pure youngM
4715+ <*> mem .: "non_heap_committed_in_bytes"
4716+ <*> mem .: "non_heap_used_in_bytes"
4717+ <*> mem .: "heap_max_in_bytes"
4718+ <*> mem .: "heap_committed_in_bytes"
4719+ <*> mem .: "heap_used_percent"
4720+ <*> mem .: "heap_used_in_bytes"
4721+ <*> (unMS <$> o .: "uptime_in_millis")
4722+ <*> (posixMS <$> o .: "timestamp")
4723+
4724+instance FromJSON JVMBufferPoolStats where
4725+ parseJSON = withObject "JVMBufferPoolStats" parse
4726+ where
4727+ parse o = JVMBufferPoolStats <$> o .: "total_capacity_in_bytes"
4728+ <*> o .: "used_in_bytes"
4729+ <*> o .: "count"
4730+
4731+instance FromJSON JVMGCStats where
4732+ parseJSON = withObject "JVMGCStats" parse
4733+ where
4734+ parse o = JVMGCStats <$> (unMS <$> o .: "collection_time_in_millis")
4735+ <*> o .: "collection_count"
4736+
4737+instance FromJSON JVMPoolStats where
4738+ parseJSON = withObject "JVMPoolStats" parse
4739+ where
4740+ parse o = JVMPoolStats <$> o .: "peak_max_in_bytes"
4741+ <*> o .: "peak_used_in_bytes"
4742+ <*> o .: "max_in_bytes"
4743+ <*> o .: "used_in_bytes"
4744+
4745+instance FromJSON NodeProcessStats where
4746+ parseJSON = withObject "NodeProcessStats" parse
4747+ where
4748+ parse o = do
4749+ mem <- o .: "mem"
4750+ cpu <- o .: "cpu"
4751+ NodeProcessStats <$> (posixMS <$> o .: "timestamp")
4752+ <*> o .: "open_file_descriptors"
4753+ <*> o .: "max_file_descriptors"
4754+ <*> cpu .: "percent"
4755+ <*> (unMS <$> cpu .: "total_in_millis")
4756+ <*> mem .: "total_virtual_in_bytes"
4757+
4758+instance FromJSON NodeOSStats where
4759+ parseJSON = withObject "NodeOSStats" parse
4760+ where
4761+ parse o = do
4762+ swap <- o .: "swap"
4763+ mem <- o .: "mem"
4764+ cpu <- o .: "cpu"
4765+ load <- o .:? "load_average"
4766+ NodeOSStats <$> (posixMS <$> o .: "timestamp")
4767+ <*> cpu .: "percent"
4768+ <*> pure load
4769+ <*> mem .: "total_in_bytes"
4770+ <*> mem .: "free_in_bytes"
4771+ <*> mem .: "free_percent"
4772+ <*> mem .: "used_in_bytes"
4773+ <*> mem .: "used_percent"
4774+ <*> swap .: "total_in_bytes"
4775+ <*> swap .: "free_in_bytes"
4776+ <*> swap .: "used_in_bytes"
4777+
4778+instance FromJSON LoadAvgs where
4779+ parseJSON = withArray "LoadAvgs" parse
4780+ where
4781+ parse v = case V.toList v of
4782+ [one, five, fifteen] -> LoadAvgs <$> parseJSON one
4783+ <*> parseJSON five
4784+ <*> parseJSON fifteen
4785+ _ -> fail "Expecting a triple of Doubles"
4786+
4787+instance FromJSON NodeIndicesStats where
4788+ parseJSON = withObject "NodeIndicesStats" parse
4789+ where
4790+ parse o = do
4791+ let (.::) mv k = case mv of
4792+ Just v -> Just <$> v .: k
4793+ Nothing -> pure Nothing
4794+ mRecovery <- o .:? "recovery"
4795+ mQueryCache <- o .:? "query_cache"
4796+ mSuggest <- o .:? "suggest"
4797+ translog <- o .: "translog"
4798+ segments <- o .: "segments"
4799+ completion <- o .: "completion"
4800+ mPercolate <- o .:? "percolate"
4801+ fielddata <- o .: "fielddata"
4802+ warmer <- o .: "warmer"
4803+ flush <- o .: "flush"
4804+ refresh <- o .: "refresh"
4805+ merges <- o .: "merges"
4806+ search <- o .: "search"
4807+ getStats <- o .: "get"
4808+ indexing <- o .: "indexing"
4809+ store <- o .: "store"
4810+ docs <- o .: "docs"
4811+ NodeIndicesStats <$> (fmap unMS <$> mRecovery .:: "throttle_time_in_millis")
4812+ <*> mRecovery .:: "current_as_target"
4813+ <*> mRecovery .:: "current_as_source"
4814+ <*> mQueryCache .:: "miss_count"
4815+ <*> mQueryCache .:: "hit_count"
4816+ <*> mQueryCache .:: "evictions"
4817+ <*> mQueryCache .:: "memory_size_in_bytes"
4818+ <*> mSuggest .:: "current"
4819+ <*> (fmap unMS <$> mSuggest .:: "time_in_millis")
4820+ <*> mSuggest .:: "total"
4821+ <*> translog .: "size_in_bytes"
4822+ <*> translog .: "operations"
4823+ <*> segments .:? "fixed_bit_set_memory_in_bytes"
4824+ <*> segments .: "version_map_memory_in_bytes"
4825+ <*> segments .:? "index_writer_max_memory_in_bytes"
4826+ <*> segments .: "index_writer_memory_in_bytes"
4827+ <*> segments .: "memory_in_bytes"
4828+ <*> segments .: "count"
4829+ <*> completion .: "size_in_bytes"
4830+ <*> mPercolate .:: "queries"
4831+ <*> mPercolate .:: "memory_size_in_bytes"
4832+ <*> mPercolate .:: "current"
4833+ <*> (fmap unMS <$> mPercolate .:: "time_in_millis")
4834+ <*> mPercolate .:: "total"
4835+ <*> fielddata .: "evictions"
4836+ <*> fielddata .: "memory_size_in_bytes"
4837+ <*> (unMS <$> warmer .: "total_time_in_millis")
4838+ <*> warmer .: "total"
4839+ <*> warmer .: "current"
4840+ <*> (unMS <$> flush .: "total_time_in_millis")
4841+ <*> flush .: "total"
4842+ <*> (unMS <$> refresh .: "total_time_in_millis")
4843+ <*> refresh .: "total"
4844+ <*> merges .: "total_size_in_bytes"
4845+ <*> merges .: "total_docs"
4846+ <*> (unMS <$> merges .: "total_time_in_millis")
4847+ <*> merges .: "total"
4848+ <*> merges .: "current_size_in_bytes"
4849+ <*> merges .: "current_docs"
4850+ <*> merges .: "current"
4851+ <*> search .: "fetch_current"
4852+ <*> (unMS <$> search .: "fetch_time_in_millis")
4853+ <*> search .: "fetch_total"
4854+ <*> search .: "query_current"
4855+ <*> (unMS <$> search .: "query_time_in_millis")
4856+ <*> search .: "query_total"
4857+ <*> search .: "open_contexts"
4858+ <*> getStats .: "current"
4859+ <*> (unMS <$> getStats .: "missing_time_in_millis")
4860+ <*> getStats .: "missing_total"
4861+ <*> (unMS <$> getStats .: "exists_time_in_millis")
4862+ <*> getStats .: "exists_total"
4863+ <*> (unMS <$> getStats .: "time_in_millis")
4864+ <*> getStats .: "total"
4865+ <*> (fmap unMS <$> indexing .:? "throttle_time_in_millis")
4866+ <*> indexing .:? "is_throttled"
4867+ <*> indexing .:? "noop_update_total"
4868+ <*> indexing .: "delete_current"
4869+ <*> (unMS <$> indexing .: "delete_time_in_millis")
4870+ <*> indexing .: "delete_total"
4871+ <*> indexing .: "index_current"
4872+ <*> (unMS <$> indexing .: "index_time_in_millis")
4873+ <*> indexing .: "index_total"
4874+ <*> (unMS <$> store .: "throttle_time_in_millis")
4875+ <*> store .: "size_in_bytes"
4876+ <*> docs .: "deleted"
4877+ <*> docs .: "count"
4878+
4879+instance FromJSON NodeBreakersStats where
4880+ parseJSON = withObject "NodeBreakersStats" parse
4881+ where
4882+ parse o = NodeBreakersStats <$> o .: "parent"
4883+ <*> o .: "request"
4884+ <*> o .: "fielddata"
4885+
4886+parseNodeStats :: FullNodeId -> Object -> Parser NodeStats
4887+parseNodeStats fnid o = do
4888+ NodeStats <$> o .: "name"
4889+ <*> pure fnid
4890+ <*> o .:? "breakers"
4891+ <*> o .: "http"
4892+ <*> o .: "transport"
4893+ <*> o .: "fs"
4894+ <*> o .:? "network"
4895+ <*> o .: "thread_pool"
4896+ <*> o .: "jvm"
4897+ <*> o .: "process"
4898+ <*> o .: "os"
4899+ <*> o .: "indices"
4900+
4901+parseNodeInfo :: FullNodeId -> Object -> Parser NodeInfo
4902+parseNodeInfo nid o =
4903+ NodeInfo <$> o .:? "http_address"
4904+ <*> o .: "build_hash"
4905+ <*> o .: "version"
4906+ <*> o .: "ip"
4907+ <*> o .: "host"
4908+ <*> o .: "transport_address"
4909+ <*> o .: "name"
4910+ <*> pure nid
4911+ <*> o .: "plugins"
4912+ <*> o .: "http"
4913+ <*> o .: "transport"
4914+ <*> o .:? "network"
4915+ <*> o .: "thread_pool"
4916+ <*> o .: "jvm"
4917+ <*> o .: "process"
4918+ <*> o .: "os"
4919+ <*> o .: "settings"
4920+
4921+instance FromJSON NodePluginInfo where
4922+ parseJSON = withObject "NodePluginInfo" parse
4923+ where
4924+ parse o = NodePluginInfo <$> o .:? "site"
4925+ <*> o .:? "jvm"
4926+ <*> o .: "description"
4927+ <*> o .: "version"
4928+ <*> o .: "name"
4929+
4930+instance FromJSON NodeHTTPInfo where
4931+ parseJSON = withObject "NodeHTTPInfo" parse
4932+ where
4933+ parse o = NodeHTTPInfo <$> o .: "max_content_length_in_bytes"
4934+ <*> parseJSON (Object o)
4935+
4936+instance FromJSON BoundTransportAddress where
4937+ parseJSON = withObject "BoundTransportAddress" parse
4938+ where
4939+ parse o = BoundTransportAddress <$> o .: "publish_address"
4940+ <*> o .: "bound_address"
4941+
4942+instance FromJSON NodeOSInfo where
4943+ parseJSON = withObject "NodeOSInfo" parse
4944+ where
4945+ parse o = do
4946+ NodeOSInfo <$> (unMS <$> o .: "refresh_interval_in_millis")
4947+ <*> o .: "name"
4948+ <*> o .: "arch"
4949+ <*> o .: "version"
4950+ <*> o .: "available_processors"
4951+ <*> o .: "allocated_processors"
4952+
4953+
4954+instance FromJSON CPUInfo where
4955+ parseJSON = withObject "CPUInfo" parse
4956+ where
4957+ parse o = CPUInfo <$> o .: "cache_size_in_bytes"
4958+ <*> o .: "cores_per_socket"
4959+ <*> o .: "total_sockets"
4960+ <*> o .: "total_cores"
4961+ <*> o .: "mhz"
4962+ <*> o .: "model"
4963+ <*> o .: "vendor"
4964+
4965+instance FromJSON NodeProcessInfo where
4966+ parseJSON = withObject "NodeProcessInfo" parse
4967+ where
4968+ parse o = NodeProcessInfo <$> o .: "mlockall"
4969+ <*> o .:? "max_file_descriptors"
4970+ <*> o .: "id"
4971+ <*> (unMS <$> o .: "refresh_interval_in_millis")
4972+
4973+instance FromJSON NodeJVMInfo where
4974+ parseJSON = withObject "NodeJVMInfo" parse
4975+ where
4976+ parse o = NodeJVMInfo <$> o .: "memory_pools"
4977+ <*> o .: "gc_collectors"
4978+ <*> o .: "mem"
4979+ <*> (posixMS <$> o .: "start_time_in_millis")
4980+ <*> o .: "vm_vendor"
4981+ <*> o .: "vm_version"
4982+ <*> o .: "vm_name"
4983+ <*> (unJVMVersion <$> o .: "version")
4984+ <*> o .: "pid"
4985+
4986+instance FromJSON JVMVersion where
4987+ parseJSON (String t) =
4988+ JVMVersion <$> parseJSON (String (T.replace "_" "." t))
4989+ parseJSON v = JVMVersion <$> parseJSON v
4990+
4991+instance FromJSON JVMMemoryInfo where
4992+ parseJSON = withObject "JVMMemoryInfo" parse
4993+ where
4994+ parse o = JVMMemoryInfo <$> o .: "direct_max_in_bytes"
4995+ <*> o .: "non_heap_max_in_bytes"
4996+ <*> o .: "non_heap_init_in_bytes"
4997+ <*> o .: "heap_max_in_bytes"
4998+ <*> o .: "heap_init_in_bytes"
4999+
5000+instance FromJSON NodeThreadPoolsInfo where
5001+ parseJSON = withObject "NodeThreadPoolsInfo" parse
5002+ where
5003+ parse o = NodeThreadPoolsInfo <$> o .: "refresh"
5004+ <*> o .: "management"
5005+ <*> o .:? "percolate"
5006+ <*> o .:? "listener"
5007+ <*> o .:? "fetch_shard_started"
5008+ <*> o .: "search"
5009+ <*> o .: "flush"
5010+ <*> o .: "warmer"
5011+ <*> o .:? "optimize"
5012+ <*> o .: "bulk"
5013+ <*> o .:? "suggest"
5014+ <*> o .: "force_merge"
5015+ <*> o .: "snapshot"
5016+ <*> o .: "get"
5017+ <*> o .:? "fetch_shard_store"
5018+ <*> o .: "index"
5019+ <*> o .: "generic"
5020+
5021+instance FromJSON NodeThreadPoolInfo where
5022+ parseJSON = withObject "NodeThreadPoolInfo" parse
5023+ where
5024+ parse o = do
5025+ ka <- maybe (return Nothing) (fmap Just . parseStringInterval) =<< o .:? "keep_alive"
5026+ NodeThreadPoolInfo <$> (parseJSON . unStringlyTypeJSON =<< o .: "queue_size")
5027+ <*> pure ka
5028+ <*> o .:? "min"
5029+ <*> o .:? "max"
5030+ <*> o .: "type"
5031+
5032+parseStringInterval :: (Monad m) => String -> m NominalDiffTime
5033+parseStringInterval s = case span isNumber s of
5034+ ("", _) -> fail "Invalid interval"
5035+ (nS, unitS) -> case (readMay nS, readMay unitS) of
5036+ (Just n, Just unit) -> return (fromInteger (n * unitNDT unit))
5037+ (Nothing, _) -> fail "Invalid interval number"
5038+ (_, Nothing) -> fail "Invalid interval unit"
5039+ where
5040+ unitNDT Seconds = 1
5041+ unitNDT Minutes = 60
5042+ unitNDT Hours = 60 * 60
5043+ unitNDT Days = 24 * 60 * 60
5044+ unitNDT Weeks = 7 * 24 * 60 * 60
5045+
5046+instance FromJSON ThreadPoolSize where
5047+ parseJSON v = parseAsNumber v <|> parseAsString v
5048+ where
5049+ parseAsNumber = parseAsInt <=< parseJSON
5050+ parseAsInt (-1) = return ThreadPoolUnbounded
5051+ parseAsInt n
5052+ | n >= 0 = return (ThreadPoolBounded n)
5053+ | otherwise = fail "Thread pool size must be >= -1."
5054+ parseAsString = withText "ThreadPoolSize" $ \t ->
5055+ case first (readMay . T.unpack) (T.span isNumber t) of
5056+ (Just n, "k") -> return (ThreadPoolBounded (n * 1000))
5057+ (Just n, "") -> return (ThreadPoolBounded n)
5058+ _ -> fail ("Invalid thread pool size " <> T.unpack t)
5059+
5060+instance FromJSON ThreadPoolType where
5061+ parseJSON = withText "ThreadPoolType" parse
5062+ where
5063+ parse "scaling" = return ThreadPoolScaling
5064+ parse "fixed" = return ThreadPoolFixed
5065+ parse "cached" = return ThreadPoolCached
5066+ parse e = fail ("Unexpected thread pool type" <> T.unpack e)
5067+
5068+instance FromJSON NodeTransportInfo where
5069+ parseJSON = withObject "NodeTransportInfo" parse
5070+ where
5071+ parse o = NodeTransportInfo <$> (maybe (return mempty) parseProfiles =<< o .:? "profiles")
5072+ <*> parseJSON (Object o)
5073+ parseProfiles (Object o) | HM.null o = return []
5074+ parseProfiles v@(Array _) = parseJSON v
5075+ parseProfiles Null = return []
5076+ parseProfiles _ = fail "Could not parse profiles"
5077+
5078+instance FromJSON NodeNetworkInfo where
5079+ parseJSON = withObject "NodeNetworkInfo" parse
5080+ where
5081+ parse o = NodeNetworkInfo <$> o .: "primary_interface"
5082+ <*> (unMS <$> o .: "refresh_interval_in_millis")
5083+
5084+
5085+instance FromJSON NodeNetworkInterface where
5086+ parseJSON = withObject "NodeNetworkInterface" parse
5087+ where
5088+ parse o = NodeNetworkInterface <$> o .: "mac_address"
5089+ <*> o .: "name"
5090+ <*> o .: "address"
5091+
5092+newtype MaybeNA a = MaybeNA { unMaybeNA :: Maybe a }
5093+ deriving (Show, Eq)
5094+
5095+instance FromJSON a => FromJSON (MaybeNA a) where
5096+ parseJSON (String "NA") = pure $ MaybeNA Nothing
5097+ parseJSON o = MaybeNA . Just <$> parseJSON o
src/Database/V5/Bloodhound/Types/Class.hsnew file+17
@@ -0,0 +1,17 @@
1+{-# LANGUAGE CPP #-}
2+module Database.V5.Bloodhound.Types.Class
3+ ( Seminearring(..) )
4+ where
5+
6+#if !MIN_VERSION_base(4,8,0)
7+import Data.Monoid
8+#endif
9+
10+class Monoid a => Seminearring a where
11+ -- 0, +, *
12+ (<||>) :: a -> a -> a
13+ (<&&>) :: a -> a -> a
14+ (<&&>) = mappend
15+
16+infixr 5 <||>
17+infixr 5 <&&>
src/Database/V5/Bloodhound/Types/Internal.hsnew file+57
@@ -0,0 +1,57 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+
6+-------------------------------------------------------------------------------
7+-- |
8+-- Module : Database.Bloodhound.Types.Internal
9+-- Copyright : (C) 2014 Chris Allen
10+-- License : BSD-style (see the file LICENSE)
11+-- Maintainer : Chris Allen <cma@bitemyapp.com>
12+-- Stability : provisional
13+-- Portability : DeriveGeneric, RecordWildCards
14+--
15+-- Internal data types for Bloodhound. These types may change without
16+-- notice so import at your own risk.
17+-------------------------------------------------------------------------------
18+module Database.V5.Bloodhound.Types.Internal
19+ ( BHEnv(..)
20+ , Server(..)
21+ , MonadBH(..)
22+ ) where
23+
24+
25+import Control.Applicative as A
26+import Control.Monad.Reader
27+import Data.Aeson
28+import Data.Text (Text)
29+import Data.Typeable (Typeable)
30+import GHC.Generics (Generic)
31+import Network.HTTP.Client
32+
33+{-| Common environment for Elasticsearch calls. Connections will be
34+ pipelined according to the provided HTTP connection manager.
35+-}
36+data BHEnv = BHEnv { bhServer :: Server
37+ , bhManager :: Manager
38+ , bhRequestHook :: Request -> IO Request
39+ -- ^ Low-level hook that is run before every request is sent. Used to implement custom authentication strategies. Defaults to 'return' with 'mkBHEnv'.
40+ }
41+
42+instance (Functor m, Applicative m, MonadIO m) => MonadBH (ReaderT BHEnv m) where
43+ getBHEnv = ask
44+
45+{-| 'Server' is used with the client functions to point at the ES instance
46+-}
47+newtype Server = Server Text deriving (Eq, Show, Generic, Typeable, FromJSON)
48+
49+{-| All API calls to Elasticsearch operate within
50+ MonadBH
51+ . The idea is that it can be easily embedded in your
52+ own monad transformer stack. A default instance for a ReaderT and
53+ alias 'BH' is provided for the simple case.
54+-}
55+class (Functor m, A.Applicative m, MonadIO m) => MonadBH m where
56+ getBHEnv :: m BHEnv
57+
stack-7.10.yamlnew file+17
@@ -0,0 +1,17 @@
1+flags:
2+ bloodhound-examples:
3+ werror: true
4+packages:
5+- '.'
6+- './examples'
7+extra-deps:
8+- http-client-0.5.0
9+- aeson-0.11.1.0
10+- fail-4.9.0.0
11+- http-types-0.9
12+- attoparsec-0.13.0.1
13+- quickcheck-properties-0.1
14+- semigroups-0.18.0.1
15+- uri-bytestring-0.1.9
16+- temporary-resourcet-0.1.0.0
17+resolver: lts-6.12
stack-7.8.yamlnew file+21
@@ -0,0 +1,21 @@
1+flags:
2+ aeson:
3+ old-locale: true
4+ bloodhound-examples:
5+ werror: true
6+packages:
7+- '.'
8+- './examples'
9+extra-deps:
10+- aeson-0.11.1.0
11+- fail-4.9.0.0
12+- http-types-0.9
13+- http-client-0.5.0
14+- attoparsec-0.13.0.1
15+- quickcheck-properties-0.1
16+- semigroups-0.18.0.1
17+- tagged-0.8.3
18+- uri-bytestring-0.1.9
19+- void-0.7.1
20+- generics-sop-0.2.2.0
21+resolver: lts-2.18
stack-8.0.yamldeleted−12
@@ -1,12 +0,0 @@
1−flags:
2− bloodhound-examples:
3− werror: true
4−
5−packages:
6−- '.'
7−- './examples'
8−extra-deps:
9− - quickcheck-properties-0.1
10− - quickcheck-arbitrary-template-0.2.0.0
11−
12−resolver: lts-8.14
stack-8.2.yamldeleted−15
@@ -1,15 +0,0 @@
1−flags:
2− bloodhound-examples:
3− werror: true
4−
5−packages:
6− - '.'
7− - './examples'
8−
9−extra-deps:
10− - aeson-1.3.0.0
11− - http-types-0.12.1
12− - quickcheck-arbitrary-template-0.2.0.0
13− - quickcheck-properties-0.1
14−
15−resolver: lts-11.6
stack-8.2.yaml.lockdeleted−40
@@ -1,40 +0,0 @@
1−# This file was autogenerated by Stack.
2−# You should not edit this file by hand.
3−# For more information, please see the documentation at:
4−# https://docs.haskellstack.org/en/stable/lock_files
5−
6−packages:
7−- completed:
8− hackage: aeson-1.3.0.0@sha256:23c353da98033eab7dca65d496df6c79c929f8d1413119648d1de01e703fbd3b,6539
9− pantry-tree:
10− size: 39767
11− sha256: 68512381071165c48e123cf515c1098ee455ec75b56724e31f6d570d3c865c47
12− original:
13− hackage: aeson-1.3.0.0
14−- completed:
15− hackage: http-types-0.12.1@sha256:12ce2fc081fde3cf219b630c2fb3815183a64eee236ba7aa735f666eab949610,2035
16− pantry-tree:
17− size: 832
18− sha256: c66c35b8f4f1b8b82f99cf8de3711027b5d0cc64bd031ab84375100038f81310
19− original:
20− hackage: http-types-0.12.1
21−- completed:
22− hackage: quickcheck-arbitrary-template-0.2.0.0@sha256:c8afad9c6e1f2a1d9813bb6c94158a99f7d6edeae8bb696b29d6c450f67811ec,2025
23− pantry-tree:
24− size: 625
25− sha256: 64262d8f3365305c35babbb2f5727ea3287c3fad61d3b66c3a2c68185fe92c5a
26− original:
27− hackage: quickcheck-arbitrary-template-0.2.0.0
28−- completed:
29− hackage: quickcheck-properties-0.1@sha256:3bd0890e84ec4cf1c1e6e475152c2c0d7c85915a051cf4e8bf9ec70c32df735c,1268
30− pantry-tree:
31− size: 550
32− sha256: 12264d8a89883b42e78c313792f8cba9af51c8fda915fce34e9780ff6972cf7e
33− original:
34− hackage: quickcheck-properties-0.1
35−snapshots:
36−- completed:
37− size: 507208
38− url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/11/6.yaml
39− sha256: 99d52f009dc5a53ac61ffc4e6044f910ca7b28b4596b6962135a48e8c7d3d093
40− original: lts-11.6
stack-8.4.yamldeleted−15
@@ -1,15 +0,0 @@
1−flags:
2− bloodhound-examples:
3− werror: true
4−
5−packages:
6− - '.'
7− - './examples'
8−
9−# extra-deps:
10−# - aeson-1.3.0.0
11−# - http-types-0.12.1
12−# - quickcheck-arbitrary-template-0.2.0.0
13−# - quickcheck-properties-0.1
14−
15−resolver: lts-12.6
stack-8.4.yaml.lockdeleted−12
@@ -1,12 +0,0 @@
1−# This file was autogenerated by Stack.
2−# You should not edit this file by hand.
3−# For more information, please see the documentation at:
4−# https://docs.haskellstack.org/en/stable/lock_files
5−
6−packages: []
7−snapshots:
8−- completed:
9− size: 502349
10− url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/12/6.yaml
11− sha256: fbce9f11b2f1c637855eb29fe952f3ac16c35d46836df0dee0cc54a85e1e33d6
12− original: lts-12.6
stack-8.6.yamldeleted−14
@@ -1,14 +0,0 @@
1−flags:
2− bloodhound-examples:
3− werror: true
4−
5−packages:
6− - '.'
7− - './examples'
8−
9−extra-deps:
10− - git: https://github.com/bitemyapp/quickcheck-arbitrary-template.git
11− commit: bf9cee96a88ecce2ff920ce74c166a38712e5ed2
12− - quickcheck-properties-0.1
13−
14−resolver: lts-13.30
stack-8.6.yaml.lockdeleted−33
@@ -1,33 +0,0 @@
1−# This file was autogenerated by Stack.
2−# You should not edit this file by hand.
3−# For more information, please see the documentation at:
4−# https://docs.haskellstack.org/en/stable/lock_files
5−
6−packages:
7−- completed:
8− cabal-file:
9− size: 2026
10− sha256: d340ae2f3fd0b54b2f899d482072be24f41eddac8ea081f11d9db260b14264c8
11− name: quickcheck-arbitrary-template
12− version: 0.2.0.0
13− git: https://github.com/bitemyapp/quickcheck-arbitrary-template.git
14− pantry-tree:
15− size: 859
16− sha256: a6c70780465af154ada9481c0a5ffcda1b069d249522634c578218c3c390cbf1
17− commit: bf9cee96a88ecce2ff920ce74c166a38712e5ed2
18− original:
19− git: https://github.com/bitemyapp/quickcheck-arbitrary-template.git
20− commit: bf9cee96a88ecce2ff920ce74c166a38712e5ed2
21−- completed:
22− hackage: quickcheck-properties-0.1@sha256:3bd0890e84ec4cf1c1e6e475152c2c0d7c85915a051cf4e8bf9ec70c32df735c,1268
23− pantry-tree:
24− size: 550
25− sha256: 12264d8a89883b42e78c313792f8cba9af51c8fda915fce34e9780ff6972cf7e
26− original:
27− hackage: quickcheck-properties-0.1
28−snapshots:
29−- completed:
30− size: 500539
31− url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/13/30.yaml
32− sha256: 59ad6b944c9903847fecdc1d4815e8500c1f9999d80fd1b4d2d66e408faec44b
33− original: lts-13.30
stack.yamlmode 120000 → 100644+13−1
@@ -1 +0,0 @@
1−stack-8.6.yaml
@@ -0,0 +1,13 @@
1+flags:
2+ bloodhound-examples:
3+ werror: true
4+packages:
5+- '.'
6+- './examples'
7+extra-deps:
8+- hspec-2.4.1
9+- hspec-core-2.4.1
10+- hspec-discover-2.4.1
11+- quickcheck-properties-0.1
12+- vector-0.12.0.0
13+resolver: nightly-2017-01-30
stack.yaml.lockdeleted−12
@@ -1,12 +0,0 @@
1−# This file was autogenerated by Stack.
2−# You should not edit this file by hand.
3−# For more information, please see the documentation at:
4−# https://docs.haskellstack.org/en/stable/lock_files
5−
6−packages: []
7−snapshots:
8−- completed:
9− size: 500539
10− url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/13/30.yaml
11− sha256: 59ad6b944c9903847fecdc1d4815e8500c1f9999d80fd1b4d2d66e408faec44b
12− original: lts-13.30
tests/Test/Aggregation.hsdeleted−147
@@ -1,147 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−
3−module Test.Aggregation (spec) where
4−
5−import Test.Common
6−import Test.Import
7−
8−import Control.Error (fmapL, note)
9−import qualified Data.Map as M
10−import qualified Database.Bloodhound
11−
12−spec :: Spec
13−spec =
14− describe "Aggregation API" $ do
15− it "returns term aggregation results" $ withTestEnv $ do
16− _ <- insertData
17− let terms = TermsAgg $ mkTermsAggregation "user"
18− let search = mkAggregateSearch Nothing $ mkAggregations "users" terms
19− searchExpectAggs search
20− searchValidBucketAgg search "users" toTerms
21−
22− it "return sub-aggregation results" $ withTestEnv $ do
23− _ <- insertData
24− let subaggs = mkAggregations "age_agg" . TermsAgg $ mkTermsAggregation "age"
25− agg = TermsAgg $ (mkTermsAggregation "user") { termAggs = Just subaggs}
26− search = mkAggregateSearch Nothing $ mkAggregations "users" agg
27− reply <- searchByIndex testIndex search
28− let result = decode (responseBody reply) :: Maybe (SearchResult Tweet)
29− usersAggResults = result >>= aggregations >>= toTerms "users"
30− subAggResults = usersAggResults >>= (listToMaybe . buckets) >>= termsAggs >>= toTerms "age_agg"
31− subAddResultsExists = isJust subAggResults
32− liftIO $ subAddResultsExists `shouldBe` True
33−
34− it "returns cardinality aggregation results" $ withTestEnv $ do
35− _ <- insertData
36− let cardinality = CardinalityAgg $ mkCardinalityAggregation $ FieldName "user"
37− let search = mkAggregateSearch Nothing $ mkAggregations "users" cardinality
38− let search' = search { Database.Bloodhound.from = From 0, size = Size 0 }
39− searchExpectAggs search'
40− let docCountPair k n = (k, object ["value" .= Number n])
41− res <- searchTweets search'
42− liftIO $
43− fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "users" 1]))
44−
45− it "returns stats aggregation results" $ withTestEnv $ do
46− _ <- insertData
47− let stats = StatsAgg $ mkStatsAggregation $ FieldName "age"
48− let search = mkAggregateSearch Nothing $ mkAggregations "users" stats
49− let search' = search { Database.Bloodhound.from = From 0, size = Size 0 }
50− searchExpectAggs search'
51− let statsAggRes k n = (k, object [ "max" .= Number n
52− , "avg" .= Number n
53− , "count" .= Number 1
54− , "min" .= Number n
55− , "sum" .= Number n])
56− res <- searchTweets search'
57− liftIO $
58− fmap aggregations res `shouldBe` Right (Just (M.fromList [ statsAggRes "users" 10000]))
59−
60− it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
61− _ <- insertData
62− let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }
63− let search = mkAggregateSearch Nothing $ mkAggregations "users" terms
64− searchExpectAggs search
65− searchValidBucketAgg search "users" toTerms
66−
67− -- One of these fails with 1.7.3
68− it "can give execution hint parameters to term aggregations" $ when' (atmost es11) $ withTestEnv $ do
69− _ <- insertData
70− searchTermsAggHint [Map, Ordinals]
71−
72− it "can give execution hint parameters to term aggregations" $ when' (is es12) $ withTestEnv $ do
73− _ <- insertData
74− searchTermsAggHint [GlobalOrdinals, GlobalOrdinalsHash, GlobalOrdinalsLowCardinality, Map, Ordinals]
75−
76− it "can give execution hint parameters to term aggregations" $ when' (atleast es12) $ withTestEnv $ do
77− _ <- insertData
78− searchTermsAggHint [GlobalOrdinals, GlobalOrdinalsHash, GlobalOrdinalsLowCardinality, Map]
79− -- One of the above.
80−
81− it "can execute value_count aggregations" $ withTestEnv $ do
82− _ <- insertData
83− _ <- insertOther
84− let ags = mkAggregations "user_count" (ValueCountAgg (FieldValueCount (FieldName "user"))) <>
85− mkAggregations "bogus_count" (ValueCountAgg (FieldValueCount (FieldName "bogus")))
86− let search = mkAggregateSearch Nothing ags
87− let docCountPair k n = (k, object ["value" .= Number n])
88− res <- searchTweets search
89− liftIO $
90− fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "user_count" 2
91− , docCountPair "bogus_count" 0
92− ]))
93−
94− it "can execute date_range aggregations" $ withTestEnv $ do
95− let now = fromGregorian 2015 3 14
96− let ltAMonthAgo = UTCTime (fromGregorian 2015 3 1) 0
97− let ltAWeekAgo = UTCTime (fromGregorian 2015 3 10) 0
98− let oldDoc = exampleTweet { postDate = ltAMonthAgo }
99− let newDoc = exampleTweet { postDate = ltAWeekAgo }
100− _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings oldDoc (DocId "1")
101− _ <- indexDocument testIndex testMapping defaultIndexDocumentSettings newDoc (DocId "2")
102− _ <- refreshIndex testIndex
103− let thisMonth = DateRangeFrom (DateMathExpr (DMDate now) [SubtractTime 1 DMMonth])
104− let thisWeek = DateRangeFrom (DateMathExpr (DMDate now) [SubtractTime 1 DMWeek])
105− let agg = DateRangeAggregation (FieldName "postDate") Nothing (thisMonth :| [thisWeek])
106− let ags = mkAggregations "date_ranges" (DateRangeAgg agg)
107− let search = mkAggregateSearch Nothing ags
108− res <- searchTweets search
109− liftIO $ hitsTotal . searchHits <$> res `shouldBe` Right 2
110− let bucks = do magrs <- fmapL show (aggregations <$> res)
111− agrs <- note "no aggregations returned" magrs
112− rawBucks <- note "no date_ranges aggregation" $ M.lookup "date_ranges" agrs
113− parseEither parseJSON rawBucks
114− let fromMonthT = UTCTime (fromGregorian 2015 2 14) 0
115− let fromWeekT = UTCTime (fromGregorian 2015 3 7) 0
116− liftIO $ buckets <$> bucks `shouldBe` Right [ DateRangeResult "2015-02-14T00:00:00.000Z-*"
117− (Just fromMonthT)
118− (Just "2015-02-14T00:00:00.000Z")
119− Nothing
120− Nothing
121− 2
122− Nothing
123− , DateRangeResult "2015-03-07T00:00:00.000Z-*"
124− (Just fromWeekT)
125− (Just "2015-03-07T00:00:00.000Z")
126− Nothing
127− Nothing
128− 1
129− Nothing
130− ]
131−
132− it "returns date histogram aggregation results" $ withTestEnv $ do
133− _ <- insertData
134− let histogram = DateHistogramAgg $ mkDateHistogram (FieldName "postDate") Minute
135− let search = mkAggregateSearch Nothing (mkAggregations "byDate" histogram)
136− searchExpectAggs search
137− searchValidBucketAgg search "byDate" toDateHistogram
138−
139− it "can execute missing aggregations" $ withTestEnv $ do
140− _ <- insertData
141− _ <- insertExtra
142− let ags = mkAggregations "missing_agg" (MissingAgg (MissingAggregation "extra"))
143− let search = mkAggregateSearch Nothing ags
144− let docCountPair k n = (k, object ["doc_count" .= Number n])
145− res <- searchTweets search
146− liftIO $
147− fmap aggregations res `shouldBe` Right (Just (M.fromList [docCountPair "missing_agg" 1]))
tests/Test/ApproxEq.hsdeleted−63
@@ -1,63 +0,0 @@
1−{-# LANGUAGE DefaultSignatures #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−
4−module Test.ApproxEq where
5−
6−import Database.Bloodhound
7−
8−import Test.Import
9−
10−import qualified Data.List.NonEmpty as NE
11−
12−-- | Typeclass for "equal where it matters". Use this to specify
13−-- less-strict equivalence for things such as lists that can wind up
14−-- in an unpredictable order
15−class ApproxEq a where
16− (=~) :: a -> a -> Bool
17−
18− showApproxEq :: a -> String
19− default showApproxEq :: (Show a) => a -> String
20− showApproxEq = show
21−
22−(==~) :: (ApproxEq a) => a -> a -> Property
23−a ==~ b = counterexample (showApproxEq a ++ " !=~ " ++ showApproxEq b) (a =~ b)
24−
25−instance ApproxEq NominalDiffTime where (=~) = (==)
26−instance ApproxEq Bool where (=~) = (==)
27−instance ApproxEq Int where (=~) = (==)
28−instance (Eq a, Show a) => ApproxEq (Maybe a) where (=~) = (==)
29−instance ApproxEq Char where
30− (=~) = (==)
31−
32−instance ApproxEq NodeAttrFilter where (=~) = (==)
33−instance ApproxEq NodeAttrName where (=~) = (==)
34−instance (Eq a, Show a) => ApproxEq (NonEmpty a) where (=~) = (==)
35−instance (ApproxEq l, Show l, ApproxEq r, Show r) => ApproxEq (Either l r) where
36− Left a =~ Left b = a =~ b
37− Right a =~ Right b = a =~ b
38− _ =~ _ = False
39− showApproxEq (Left x) = "Left " <> showApproxEq x
40− showApproxEq (Right x) = "Right " <> showApproxEq x
41−instance (ApproxEq a, Show a) => ApproxEq [a] where
42− as =~ bs = and (zipWith (=~) as bs)
43−instance ApproxEq ReplicaCount where (=~) = (==)
44−instance ApproxEq ReplicaBounds where (=~) = (==)
45−instance ApproxEq Bytes where (=~) = (==)
46−instance ApproxEq AllocationPolicy where (=~) = (==)
47−instance ApproxEq InitialShardCount where (=~) = (==)
48−instance ApproxEq FSType where (=~) = (==)
49−
50−-- | Due to the way nodeattrfilters get serialized here, they may come
51−-- out in a different order, but they are morally equivalent
52−instance ApproxEq UpdatableIndexSetting where
53− RoutingAllocationInclude a =~ RoutingAllocationInclude b =
54− NE.sort a =~ NE.sort b
55− RoutingAllocationExclude a =~ RoutingAllocationExclude b =
56− NE.sort a =~ NE.sort b
57− RoutingAllocationRequire a =~ RoutingAllocationRequire b =
58− NE.sort a =~ NE.sort b
59− a =~ b = a == b
60− showApproxEq (RoutingAllocationInclude xs) = show (RoutingAllocationInclude (NE.sort xs))
61− showApproxEq (RoutingAllocationExclude xs) = show (RoutingAllocationExclude (NE.sort xs))
62− showApproxEq (RoutingAllocationRequire xs) = show (RoutingAllocationRequire (NE.sort xs))
63− showApproxEq x = show x
tests/Test/BulkAPI.hsdeleted−95
@@ -1,95 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−
3−module Test.BulkAPI (spec) where
4−
5−import Test.Common
6−import Test.Import
7−
8−import qualified Data.Vector as V
9−import qualified Lens.Micro.Aeson as LMA
10−
11−newtype BulkTest =
12− BulkTest Text
13− deriving (Eq, Show)
14−
15−instance ToJSON BulkTest where
16− toJSON (BulkTest name') =
17− object ["name" .= name']
18−
19−instance FromJSON BulkTest where
20− parseJSON = withObject "BulkTest" parse
21− where
22− parse o = do
23− t <- o .: "name"
24− BulkTest <$> parseJSON t
25−
26−spec :: Spec
27−spec =
28− describe "Bulk API" $
29− it "inserts all documents we request" $ withTestEnv $ do
30− _ <- insertData
31− let firstTest = BulkTest "blah"
32− let secondTest = BulkTest "bloo"
33− let thirdTest = BulkTest "graffle"
34− let fourthTest = BulkTest "garabadoo"
35− let fifthTest = BulkTest "serenity"
36− let firstDoc = BulkIndex testIndex
37− testMapping (DocId "2") (toJSON firstTest)
38− let secondDoc = BulkCreate testIndex
39− testMapping (DocId "3") (toJSON secondTest)
40− let thirdDoc = BulkCreateEncoding testIndex
41− testMapping (DocId "4") (toEncoding thirdTest)
42− let fourthDoc = BulkIndexAuto testIndex
43− testMapping (toJSON fourthTest)
44− let fifthDoc = BulkIndexEncodingAuto testIndex
45− testMapping (toEncoding fifthTest)
46− let stream = V.fromList [firstDoc, secondDoc, thirdDoc, fourthDoc, fifthDoc]
47− _ <- bulk stream
48− -- liftIO $ pPrint bulkResp
49− _ <- refreshIndex testIndex
50− -- liftIO $ pPrint refreshResp
51− fDoc <- getDocument testIndex testMapping (DocId "2")
52− sDoc <- getDocument testIndex testMapping (DocId "3")
53− tDoc <- getDocument testIndex testMapping (DocId "4")
54− -- note that we cannot query for fourthDoc and fifthDoc since we
55− -- do not know their autogenerated ids.
56− let maybeFirst =
57− eitherDecode
58− $ responseBody fDoc
59− :: Either String (EsResult BulkTest)
60− let maybeSecond =
61− eitherDecode
62− $ responseBody sDoc
63− :: Either String (EsResult BulkTest)
64− let maybeThird =
65− eitherDecode
66− $ responseBody tDoc
67− :: Either String (EsResult BulkTest)
68− -- liftIO $ pPrint [maybeFirst, maybeSecond, maybeThird]
69− liftIO $ do
70− fmap getSource maybeFirst `shouldBe` Right (Just firstTest)
71− fmap getSource maybeSecond `shouldBe` Right (Just secondTest)
72− fmap getSource maybeThird `shouldBe` Right (Just thirdTest)
73− -- Since we can't get the docs by doc id, we check for their existence in
74− -- a match all query.
75− let query = MatchAllQuery Nothing
76− let search = mkSearch (Just query) Nothing
77− resp <- searchByIndex testIndex search
78− parsed <- parseEsResponse resp :: BH IO (Either EsError (SearchResult Value))
79− case parsed of
80− Left e ->
81− liftIO $ expectationFailure ("Expected a script-transformed result but got: " <> show e)
82− (Right sr) -> do
83− liftIO $
84− hitsTotal (searchHits sr) `shouldBe` 6
85− let nameList :: [Text]
86− nameList =
87− hits (searchHits sr)
88− ^.. traverse
89− . to hitSource
90− . _Just
91− . LMA.key "name"
92− . _String
93− liftIO $
94− nameList
95− `shouldBe` ["blah","bloo","graffle","garabadoo","serenity"]
tests/Test/Common.hsdeleted−292

This diff is too big to show here.

tests/Test/Documents.hsdeleted−51

This diff is too big to show here.

tests/Test/Generators.hsdeleted−563

This diff is too big to show here.

tests/Test/Highlights.hsdeleted−32

This diff is too big to show here.

tests/Test/Import.hsdeleted−58

This diff is too big to show here.

tests/Test/Indices.hsdeleted−173

This diff is too big to show here.

tests/Test/JSON.hsdeleted−213

This diff is too big to show here.

tests/Test/Query.hsdeleted−132

This diff is too big to show here.

tests/Test/Script.hsdeleted−36

This diff is too big to show here.

tests/Test/Snapshots.hsdeleted−201

This diff is too big to show here.

tests/Test/Sorting.hsdeleted−22

This diff is too big to show here.

tests/Test/SourceFiltering.hsdeleted−38

This diff is too big to show here.

tests/Test/Suggest.hsdeleted−23

This diff is too big to show here.

tests/Test/Templates.hsdeleted−26

This diff is too big to show here.

tests/V1/tests.hsnew file+1905

This diff is too big to show here.

tests/V5/tests.hsnew file+1689

This diff is too big to show here.

tests/tests.hsdeleted−136

This diff is too big to show here.