├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── kotlin │ └── mbuhot │ └── eskotlin │ ├── aggregation │ ├── FilterAggregation.kt │ └── NestedAggregation.kt │ └── query │ ├── CommonQuery.kt │ ├── compound │ ├── Bool.kt │ ├── Boosting.kt │ ├── ConstantScore.kt │ ├── DisMax.kt │ └── FunctionScore.kt │ ├── fulltext │ ├── Common.kt │ ├── Match.kt │ ├── MatchPhrase.kt │ ├── MatchPhrasePrefix.kt │ └── MultiMatch.kt │ ├── joining │ ├── HasChild.kt │ ├── HasParent.kt │ └── Nested.kt │ └── term │ ├── Exists.kt │ ├── Fuzzy.kt │ ├── Ids.kt │ ├── MatchAll.kt │ ├── Prefix.kt │ ├── Range.kt │ ├── Regexp.kt │ ├── StringQuery.kt │ ├── Term.kt │ ├── Terms.kt │ ├── Type.kt │ └── Wildcard.kt └── test └── kotlin └── mbuhot └── eskotlin ├── aggregation ├── FilterAggregationTest.kt └── NestedAggregationTest.kt └── query ├── Util.kt ├── compound ├── BoolTest.kt ├── BoostingTest.kt ├── ConstantScoreTest.kt ├── DisMaxTest.kt └── FunctionScoreTest.kt ├── fulltext ├── CommonTest.kt ├── MatchPhrasePrefixTest.kt ├── MatchPhraseTest.kt ├── MatchTest.kt └── MultiMatchTest.kt ├── joining ├── HasChildTest.kt ├── HasParentTest.kt └── NestedTest.kt └── term ├── ExistsTest.kt ├── FuzzyTest.kt ├── IdsTest.kt ├── PrefixTest.kt ├── RangeTest.kt ├── RegexpTest.kt ├── StringQueryTest.kt ├── TermTest.kt ├── TermsTest.kt ├── TypeTest.kt └── WildcardTest.kt /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | .idea 4 | build/ 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | before_cache: 4 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 5 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 6 | 7 | cache: 8 | directories: 9 | - $HOME/.gradle/caches/ 10 | - $HOME/.gradle/wrapper/ 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 0.7.0 2 | 3 | - Add support for filter and nested aggregations, thanks @nzcoffeem ! 4 | - Update build script to Gradle 5 5 | 6 | # 0.6.0 7 | 8 | - Update elasticsearch version to 6.3 9 | - `IndicesQueryBuilder` removed, 10 | - `disable_coord` property of `CommonTermsQueryBuilder` removed 11 | - Seed for `randomFunction` is no longer accepted as constructor parameter 12 | 13 | # 0.5.0 14 | 15 | - New syntax available for string keyed objects, no longer requires using `to` when the right side is initialized with a lambda. 16 | - Update to kotlin 1.2.60 17 | 18 | Thanks to @vincentlauvlwj and @roschlau for contributions! 19 | 20 | # 0.4.0 21 | 22 | - Add TermsLookup support for `terms` query 23 | - Update Gradle version to 4.5.1 24 | - Update Kotlin version to 1.2.30 25 | - Update elasticsearch version to 5.6.5 26 | - Import HasChild/HasParent from org.elasticsearch.plugin:parent-join-client module 27 | 28 | # 0.3.0 29 | 30 | - Update Kotlin version to 1.1.3-2 31 | - Adding the ability to accept field boost for multi match 32 | 33 | # 0.2.0 34 | 35 | - Added match_phrase & match_phrase_prefix 36 | - Updated to elasticsearch 5.0.0 37 | 38 | # 0.1.0 39 | 40 | - Initial release 41 | - Targets Elastic Search version 2.2.x -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Michael Buhot (m.buhot@gmail.com) 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/mbuhot/eskotlin.svg?branch=master)](https://travis-ci.org/mbuhot/eskotlin) 2 | 3 | # ES Kotlin 4 | Elasticsearch Query DSL for Kotlin. 5 | 6 | This library aims to minimize the gap between the Elasticsearch JSON query DSL, and the API used when writing kotlin applications. 7 | This integrates with the existing java API, only providing a nicer syntax to build the queries. 8 | 9 | 10 | # Getting Started 11 | 12 | ## Gradle 13 | 14 | ```groovy 15 | repositories { 16 | mavenCentral() 17 | ... 18 | maven { 19 | url "http://dl.bintray.com/mbuhot/maven" 20 | } 21 | } 22 | dependencies { 23 | compile 'mbuhot:eskotlin:0.7.0' 24 | ... 25 | } 26 | ``` 27 | 28 | See [CHANGELOG](./CHANGELOG.md) for older versions supporting previous elasticsearch client versions. 29 | 30 | ## Maven 31 | 32 | Full details on [bintray](https://bintray.com/mbuhot/maven/eskotlin/) 33 | 34 | ```xml 35 | 36 | mbuhot 37 | eskotlin 38 | 0.7.0 39 | pom 40 | 41 | ``` 42 | 43 | 44 | # Examples 45 | 46 | ## Term Query 47 | 48 | JSON: 49 | ```json 50 | { 51 | "term" : { "user" : "Kimchy" } 52 | } 53 | ``` 54 | 55 | Kotlin: 56 | ```kotlin 57 | val query = term { 58 | "user" to "Kimchy" 59 | } 60 | ``` 61 | 62 | ## Bool Query 63 | 64 | JSON: 65 | ```json 66 | { 67 | "bool" : { 68 | "must" : { 69 | "term" : { "user" : "kimchy" } 70 | }, 71 | "filter": { 72 | "term" : { "tag" : "tech" } 73 | }, 74 | "must_not" : { 75 | "range" : { 76 | "age" : { "from" : 10, "to" : 20 } 77 | } 78 | }, 79 | "should" : [ 80 | { 81 | "term" : { "tag" : "wow" } 82 | }, 83 | { 84 | "term" : { "tag" : "elasticsearch" } 85 | } 86 | ], 87 | "minimum_should_match" : 1, 88 | "boost" : 1.0 89 | } 90 | } 91 | ``` 92 | 93 | Kotlin: 94 | ```kotlin 95 | val query = bool { 96 | must { 97 | term { "user" to "kimchy" } 98 | } 99 | filter { 100 | term { "tag" to "tech" } 101 | } 102 | must_not { 103 | range { 104 | "age" { 105 | from = 10 106 | to = 20 107 | } 108 | } 109 | } 110 | should = listOf( 111 | term { "tag" to "wow" }, 112 | term { "tag" to "elasticsearch" }) 113 | minimum_should_match = 1 114 | boost = 1.0f 115 | } 116 | 117 | ``` 118 | 119 | ## Function Score Query 120 | 121 | JSON: 122 | ```json 123 | { 124 | "function_score": { 125 | "query": { 126 | "match_all": {} 127 | }, 128 | "functions": [ 129 | { 130 | "filter": { 131 | "term": { 132 | "foo": "bar" 133 | } 134 | }, 135 | "gauss": { 136 | "baz": { 137 | "scale": 1.0 138 | } 139 | } 140 | }, 141 | { 142 | "filter": { 143 | "match_all": {} 144 | }, 145 | "random_score": { 146 | "seed": 234 147 | } 148 | }, 149 | { 150 | "exp": { 151 | "qux": { 152 | "scale": 2.3 153 | } 154 | } 155 | } 156 | ], 157 | "score_mode": "max", 158 | "boost_mode": "multiply", 159 | "max_boost": 5.0, 160 | "boost": 1.2, 161 | "min_score": 0.001 162 | } 163 | } 164 | ``` 165 | 166 | Kotlin: 167 | ```kotlin 168 | val query = function_score { 169 | query = match_all { } 170 | functions = listOf( 171 | term { "foo" to "bar" } to gaussDecayFunction("baz", 1.0), 172 | match_all { } to randomFunction(234L), 173 | null to exponentialDecayFunction("qux", 2.3)) 174 | 175 | boost = 1.2f 176 | boost_mode = "multiply" 177 | score_mode = "max" 178 | max_boost = 5.0f 179 | min_score = 0.001f 180 | } 181 | ``` 182 | 183 | See the src/test directory for more examples. 184 | 185 | 186 | # API Coverage 187 | 188 | ## Full Text Queries - Done 189 | 190 | + [Match](./src/test/kotlin/mbuhot/eskotlin/query/fulltext/MatchTest.kt) 191 | + [Multi Match](./src/test/kotlin/mbuhot/eskotlin/query/fulltext/MultiMatchTest.kt) 192 | + [Common Terms](./src/test/kotlin/mbuhot/eskotlin/query/fulltext/CommonTest.kt) 193 | 194 | ## Term Queries - Done 195 | 196 | + [Term](./src/test/kotlin/mbuhot/eskotlin/query/term/TermTest.kt) 197 | + [Terms](./src/test/kotlin/mbuhot/eskotlin/query/term/TermsTest.kt) 198 | + [Range](./src/test/kotlin/mbuhot/eskotlin/query/term/RangeTest.kt) 199 | + [Exists](./src/test/kotlin/mbuhot/eskotlin/query/term/ExistsTest.kt) 200 | + [Prefix](./src/test/kotlin/mbuhot/eskotlin/query/term/PrefixTest.kt) 201 | + [Wildcard](./src/test/kotlin/mbuhot/eskotlin/query/term/WildcardTest.kt) 202 | + [Regex](./src/test/kotlin/mbuhot/eskotlin/query/term/RegexpTest.kt) 203 | + [Fuzzy](./src/test/kotlin/mbuhot/eskotlin/query/term/FuzzyTest.kt) 204 | + [Type](./src/test/kotlin/mbuhot/eskotlin/query/term/TypeTest.kt) 205 | + [Ids](./src/test/kotlin/mbuhot/eskotlin/query/term/IdsTest.kt) 206 | 207 | ## Compound Queries - Done 208 | 209 | + [Constant Score](./src/test/kotlin/mbuhot/eskotlin/query/compound/ConstantScoreTest.kt) 210 | + [Bool](./src/test/kotlin/mbuhot/eskotlin/query/compound/BoolTest.kt) 211 | + [Dis Max](./src/test/kotlin/mbuhot/eskotlin/query/compound/DisMaxTest.kt) 212 | + [Function Score](./src/test/kotlin/mbuhot/eskotlin/query/compound/FunctionScoreTest.kt) 213 | + [Boosting](./src/test/kotlin/mbuhot/eskotlin/query/compound/BoostingTest.kt) 214 | 215 | ## Joining Queries - Done 216 | 217 | + [Nested](./src/test/kotlin/mbuhot/eskotlin/query/joining/NestedTest.kt) 218 | + [Has Child](./src/test/kotlin/mbuhot/eskotlin/query/joining/HasChildTest.kt) 219 | + [Has Parent](./src/test/kotlin/mbuhot/eskotlin/query/joining/HasParentTest.kt) 220 | 221 | ## Geo Queries - Contributions welcome! 222 | 223 | + Geo Shape 224 | + Geo Bounding Box 225 | + Geo Distance Range 226 | + Geo Polygon 227 | + Geohash Cell 228 | 229 | ## Specialized Queries - Contributions welcome! 230 | 231 | + More Like This 232 | + Template 233 | + Script 234 | 235 | ## Span Queries - Contributions welcome! 236 | 237 | + Span Term 238 | + Span Multi Term 239 | + Span First 240 | + Span Near 241 | + Span Or 242 | + Span Not 243 | + Span Containing 244 | + Span Within 245 | 246 | ## Aggregations - Contributions welcome! 247 | 248 | + [Nested](./src/test/kotlin/mbuhot/eskotlin/aggregation/NestedAggregationTest.kt) 249 | + [Filter](./src/test/kotlin/mbuhot/eskotlin/aggregation/FilterAggregationTest.kt) 250 | + Metrics Aggregations 251 | + Bucket Aggregations 252 | + Pipeline Aggregations 253 | + Matrix Aggregations 254 | 255 | # License 256 | 257 | MIT - See LICENSE file for full text. 258 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | group 'mbuhot' 6 | version '0.8.0' 7 | 8 | apply plugin: 'kotlin' 9 | apply plugin: 'com.jfrog.bintray' 10 | apply plugin: 'maven-publish' 11 | 12 | buildscript { 13 | repositories { 14 | mavenCentral() 15 | jcenter() 16 | } 17 | dependencies { 18 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61" 19 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.+' 20 | } 21 | } 22 | 23 | dependencies { 24 | compile "org.elasticsearch:elasticsearch:7.5.1" 25 | compile "org.elasticsearch.plugin:parent-join-client:7.5.1" 26 | compile "org.jetbrains.kotlin:kotlin-stdlib:1.3.61" 27 | testCompile "org.apache.logging.log4j:log4j-api:2.13.0" 28 | testCompile "org.apache.logging.log4j:log4j-core:2.13.0" 29 | testCompile 'com.fasterxml.jackson.core:jackson-databind:2.10.2' 30 | testCompile "junit:junit:4.13" 31 | } 32 | 33 | repositories { 34 | mavenCentral() 35 | } 36 | 37 | task sourcesJar(type: Jar) { 38 | archiveClassifier = 'sources' 39 | from sourceSets.main.allSource 40 | } 41 | 42 | publishing { 43 | publications { 44 | mavenJava(MavenPublication) { 45 | from components.java 46 | artifact sourcesJar 47 | } 48 | } 49 | } 50 | 51 | bintray { 52 | user = System.getenv('BINTRAY_USER') 53 | key = System.getenv('BINTRAY_KEY') 54 | publications = ['mavenJava'] 55 | dryRun = false 56 | publish = true 57 | pkg { 58 | repo = 'maven' 59 | name = 'eskotlin' 60 | desc = 'Elasticsearch Query DSL for Kotlin' 61 | websiteUrl = 'https://github.com/mbuhot/eskotlin' 62 | issueTrackerUrl = 'https://github.com/mbuhot/eskotlin/issues' 63 | vcsUrl = 'https://github.com/mbuhot/eskotlin.git' 64 | licenses = ['MIT'] 65 | labels = ['kotlin', 'elasticseasrch'] 66 | publicDownloadNumbers = true 67 | version { 68 | name = project.version 69 | released = new Date() 70 | vcsTag = project.version 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbuhot/eskotlin/091d0cdedfaeb9b35ee18f4c9596f14403c14321/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin, switch paths to Windows format before running java 129 | if $cygwin ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=$((i+1)) 158 | done 159 | case $i in 160 | (0) set -- ;; 161 | (1) set -- "$args0" ;; 162 | (2) set -- "$args0" "$args1" ;; 163 | (3) set -- "$args0" "$args1" "$args2" ;; 164 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=$(save "$@") 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 184 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 185 | cd "$(dirname "$0")" 186 | fi 187 | 188 | exec "$JAVACMD" "$@" 189 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem http://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 34 | 35 | @rem Find java.exe 36 | if defined JAVA_HOME goto findJavaFromJavaHome 37 | 38 | set JAVA_EXE=java.exe 39 | %JAVA_EXE% -version >NUL 2>&1 40 | if "%ERRORLEVEL%" == "0" goto init 41 | 42 | echo. 43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 44 | echo. 45 | echo Please set the JAVA_HOME variable in your environment to match the 46 | echo location of your Java installation. 47 | 48 | goto fail 49 | 50 | :findJavaFromJavaHome 51 | set JAVA_HOME=%JAVA_HOME:"=% 52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 53 | 54 | if exist "%JAVA_EXE%" goto init 55 | 56 | echo. 57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 58 | echo. 59 | echo Please set the JAVA_HOME variable in your environment to match the 60 | echo location of your Java installation. 61 | 62 | goto fail 63 | 64 | :init 65 | @rem Get command-line arguments, handling Windows variants 66 | 67 | if not "%OS%" == "Windows_NT" goto win9xME_args 68 | 69 | :win9xME_args 70 | @rem Slurp the command line arguments. 71 | set CMD_LINE_ARGS= 72 | set _SKIP=2 73 | 74 | :win9xME_args_slurp 75 | if "x%~1" == "x" goto execute 76 | 77 | set CMD_LINE_ARGS=%* 78 | 79 | :execute 80 | @rem Setup the command line 81 | 82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 83 | 84 | @rem Execute Gradle 85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 86 | 87 | :end 88 | @rem End local scope for the variables with windows NT shell 89 | if "%ERRORLEVEL%"=="0" goto mainEnd 90 | 91 | :fail 92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 93 | rem the _cmd.exe /c_ return code! 94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 95 | exit /b 1 96 | 97 | :mainEnd 98 | if "%OS%"=="Windows_NT" endlocal 99 | 100 | :omega 101 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | rootProject.name = 'eskotlin' 6 | 7 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/aggregation/FilterAggregation.kt: -------------------------------------------------------------------------------- 1 | package mbuhot.eskotlin.aggregation 2 | 3 | import org.elasticsearch.index.query.QueryBuilder 4 | import org.elasticsearch.search.aggregations.AggregationBuilder 5 | import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder 6 | 7 | /** 8 | */ 9 | 10 | data class FilterAggregationData( 11 | var name: String? = null, 12 | var filter: QueryBuilder? = null 13 | ) 14 | 15 | fun filterAggregation(init: FilterAggregationData.() -> Unit): AggregationBuilder { 16 | val params = FilterAggregationData().apply(init) 17 | return FilterAggregationBuilder( 18 | params.name, 19 | params.filter 20 | ) 21 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/aggregation/NestedAggregation.kt: -------------------------------------------------------------------------------- 1 | package mbuhot.eskotlin.aggregation 2 | 3 | import org.elasticsearch.search.aggregations.AggregationBuilder 4 | import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder 5 | 6 | data class NestedAggregationData( 7 | var name: String? = null, 8 | var path: String? = null, 9 | var sub_aggregation: List? = null) { 10 | fun sub_aggregation(f: () -> AggregationBuilder) { 11 | sub_aggregation = listOf(f()) 12 | } 13 | } 14 | 15 | fun nested_aggregation(init: NestedAggregationData.() -> Unit): NestedAggregationBuilder { 16 | val params = NestedAggregationData().apply(init) 17 | return NestedAggregationBuilder(params.name, params.path).apply { 18 | params.sub_aggregation?.forEach { 19 | subAggregation(it) 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/CommonQuery.kt: -------------------------------------------------------------------------------- 1 | package mbuhot.eskotlin.query 2 | 3 | import org.elasticsearch.index.query.AbstractQueryBuilder 4 | 5 | /** 6 | * Created on 8/11/16. 7 | * @author Ryan Murfitt (rmurfitt@gmail.com) 8 | */ 9 | 10 | open class QueryData { 11 | var boost: Float? = null 12 | } 13 | 14 | fun AbstractQueryBuilder<*>.initQuery(data: QueryData) { 15 | data.boost?.let { this.boost(it) } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/compound/Bool.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import org.elasticsearch.index.query.BoolQueryBuilder 8 | import org.elasticsearch.index.query.QueryBuilder 9 | 10 | 11 | data class BoolData( 12 | var must: List? = null, 13 | var filter: List? = null, 14 | var must_not: List? = null, 15 | var should: List? = null, 16 | var minimum_should_match: Int? = null, 17 | var boost: Float? = null) { 18 | 19 | fun must(f: () -> QueryBuilder) { 20 | must = listOf(f()) 21 | } 22 | 23 | fun must_not(f: () -> QueryBuilder) { 24 | must_not = listOf(f()) 25 | } 26 | 27 | fun filter(f: () -> QueryBuilder) { 28 | filter = listOf(f()) 29 | } 30 | 31 | fun should(f: () -> QueryBuilder) { 32 | should = listOf(f()) 33 | } 34 | } 35 | 36 | fun bool(init: BoolData.() -> Unit): BoolQueryBuilder { 37 | val params = BoolData().apply(init) 38 | return BoolQueryBuilder().apply { 39 | params.must?.forEach { must(it) } 40 | params.filter?.forEach { filter(it) } 41 | params.must_not?.forEach { mustNot(it) } 42 | params.should?.forEach { should(it) } 43 | params.minimum_should_match?.let { minimumShouldMatch(it) } 44 | params.boost?.let { boost(it) } 45 | } 46 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/compound/Boosting.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import org.elasticsearch.index.query.BoostingQueryBuilder 8 | import org.elasticsearch.index.query.QueryBuilder 9 | 10 | data class BoostingData( 11 | var positive: QueryBuilder? = null, 12 | var negative: QueryBuilder? = null, 13 | var negative_boost: Float? = null) { 14 | 15 | fun positive(f: () -> QueryBuilder) { 16 | positive = f() 17 | } 18 | 19 | fun negative(f: () -> QueryBuilder) { 20 | negative = f() 21 | } 22 | } 23 | 24 | fun boosting(init: BoostingData.() -> Unit): BoostingQueryBuilder { 25 | val params = BoostingData().apply(init) 26 | return BoostingQueryBuilder(params.positive, params.negative).apply { 27 | params.negative_boost?.let { negativeBoost(it) } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/compound/ConstantScore.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import org.elasticsearch.index.query.ConstantScoreQueryBuilder 8 | import org.elasticsearch.index.query.QueryBuilder 9 | 10 | data class ConstantScoreData( 11 | var filter: QueryBuilder? = null, 12 | var boost: Float? = null) { 13 | fun filter(f: () -> QueryBuilder) { 14 | filter = f() 15 | } 16 | } 17 | 18 | fun constant_score(init: ConstantScoreData.() -> Unit): ConstantScoreQueryBuilder { 19 | val params = ConstantScoreData().apply(init) 20 | return ConstantScoreQueryBuilder(params.filter).apply { 21 | params.boost?.let { boost(it) } 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/compound/DisMax.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import org.elasticsearch.index.query.DisMaxQueryBuilder 8 | import org.elasticsearch.index.query.QueryBuilder 9 | 10 | data class DisMaxData( 11 | var tie_breaker: Float? = null, 12 | var boost: Float? = null, 13 | var queries: List? = null) 14 | 15 | fun dis_max(init: DisMaxData.() -> Unit): DisMaxQueryBuilder { 16 | val params = DisMaxData().apply(init) 17 | return DisMaxQueryBuilder().apply { 18 | params.tie_breaker?.let { tieBreaker(it) } 19 | params.boost?.let { boost(it) } 20 | params.queries?.forEach { add(it) } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/compound/FunctionScore.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import org.elasticsearch.common.lucene.search.function.CombineFunction 8 | import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction 9 | import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery 10 | import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder 11 | import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder 12 | import org.elasticsearch.index.query.MatchAllQueryBuilder 13 | import org.elasticsearch.index.query.QueryBuilder 14 | import org.elasticsearch.index.query.functionscore.FieldValueFactorFunctionBuilder 15 | import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders 16 | 17 | data class FunctionScoreData( 18 | var query: QueryBuilder = MatchAllQueryBuilder(), 19 | var boost: Float? = null, 20 | var functions: List>> = emptyList(), 21 | var field_value_factor: FieldValueFactorFunctionBuilder? = null, 22 | var max_boost: Float? = null, 23 | var score_mode: String? = null, 24 | var boost_mode: String? = null, 25 | var min_score: Float? = null) 26 | 27 | fun function_score(init: FunctionScoreData.() -> Unit): FunctionScoreQueryBuilder { 28 | val params = FunctionScoreData().apply(init) 29 | val factorWrapper = listOf(Pair(null as QueryBuilder?, params.field_value_factor as ScoreFunctionBuilder<*>?)) 30 | val merged = factorWrapper + params.functions; 31 | 32 | val filterFunctions = merged.filter { it.second != null }.map { 33 | if (it.first == null) { 34 | FunctionScoreQueryBuilder.FilterFunctionBuilder(it.second) 35 | } else { 36 | FunctionScoreQueryBuilder.FilterFunctionBuilder(it.first, it.second) 37 | } 38 | } 39 | 40 | val builder = FunctionScoreQueryBuilder(params.query, filterFunctions.toTypedArray()) 41 | 42 | return builder.apply { 43 | params.boost?.let { boost(it) } 44 | params.max_boost?.let { maxBoost(it) } 45 | params.score_mode?.let { scoreMode(FunctionScoreQuery.ScoreMode.fromString(it)) } 46 | params.boost_mode?.let { boostMode(CombineFunction.fromString(it)) } 47 | params.min_score?.let { setMinScore(it) } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/fulltext/Common.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import org.elasticsearch.index.query.CommonTermsQueryBuilder 8 | import org.elasticsearch.index.query.Operator 9 | 10 | 11 | /** 12 | * common 13 | */ 14 | class CommonBlock { 15 | 16 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 17 | infix fun String.to(init: CommonData.() -> Unit): CommonData { 18 | return this.invoke(init) 19 | } 20 | 21 | operator fun String.invoke(init: CommonData.() -> Unit): CommonData { 22 | return CommonData(name = this).apply(init) 23 | } 24 | 25 | data class CommonData( 26 | var name: String, 27 | var query: Any? = null, 28 | var high_freq_operator: String? = null, 29 | var low_freq_operator: String? = null, 30 | var analyzer: String? = null, 31 | var boost: Float? = null, 32 | var cutoff_frequency: Float? = null, 33 | val minimum_should_match: MinimumShouldMatchData = MinimumShouldMatchData()) { 34 | 35 | fun minimum_should_match(init: MinimumShouldMatchData.() -> Unit) { 36 | this.minimum_should_match.init() 37 | } 38 | 39 | infix fun MinimumShouldMatchData.to(numOrStr: Any) { 40 | this.low_freq = numOrStr 41 | } 42 | } 43 | 44 | data class MinimumShouldMatchData(var low_freq: Any? = null, var high_freq: Any? = null) 45 | } 46 | 47 | fun common(init: CommonBlock.() -> CommonBlock.CommonData): CommonTermsQueryBuilder { 48 | val params = CommonBlock().init() 49 | return CommonTermsQueryBuilder(params.name, params.query).apply { 50 | params.high_freq_operator?.let { highFreqOperator(Operator.fromString(it)) } 51 | params.low_freq_operator?.let { lowFreqOperator(Operator.fromString(it)) } 52 | params.analyzer?.let { analyzer(it) } 53 | params.boost?.let { boost(it) } 54 | params.cutoff_frequency?.let { cutoffFrequency(it) } 55 | params.minimum_should_match.low_freq?.toString()?.let { lowFreqMinimumShouldMatch(it) } 56 | params.minimum_should_match.high_freq?.toString()?.let { highFreqMinimumShouldMatch(it) } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/fulltext/Match.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import org.elasticsearch.common.unit.Fuzziness 8 | import org.elasticsearch.index.query.MatchQueryBuilder 9 | import org.elasticsearch.index.query.Operator 10 | import org.elasticsearch.index.search.MatchQuery 11 | 12 | 13 | /** 14 | * match 15 | */ 16 | 17 | class MatchBlock { 18 | 19 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 20 | infix fun String.to(init: MatchData.() -> Unit) = this.invoke(init) 21 | 22 | operator fun String.invoke(init: MatchData.() -> Unit) = MatchData(name = this).apply(init) 23 | 24 | infix fun String.to(query: Any) = MatchData(name = this, query = query) 25 | 26 | data class MatchData( 27 | var name: String, 28 | var query: Any? = null, 29 | var operator: String? = null, 30 | var analyzer: String? = null, 31 | var boost: Float? = null, 32 | var fuzziness: Fuzziness? = null, 33 | var prefix_length: Int? = null, 34 | var max_expansions: Int? = null, 35 | var minimum_should_match: String? = null, 36 | var fuzzy_rewrite: String? = null, 37 | var lenient: Boolean? = null, 38 | var fuzzy_transpositions: Boolean? = null, 39 | var zero_terms_query: String? = null, 40 | var cutoff_frequency: Float? = null) 41 | } 42 | 43 | fun match(init: MatchBlock.() -> MatchBlock.MatchData): MatchQueryBuilder { 44 | val params = MatchBlock().init() 45 | return MatchQueryBuilder(params.name, params.query).apply { 46 | params.analyzer?.let { analyzer(it) } 47 | params.boost?.let { boost(it) } 48 | params.cutoff_frequency?.let { cutoffFrequency(it) } 49 | params.fuzziness?.let { fuzziness(it) } 50 | params.fuzzy_rewrite?.let { fuzzyRewrite(it) } 51 | params.fuzzy_transpositions?.let { fuzzyTranspositions(it) } 52 | params.lenient?.let { lenient(it) } 53 | params.max_expansions?.let { maxExpansions(it) } 54 | params.minimum_should_match?.let { minimumShouldMatch(it) } 55 | params.operator?.let { operator(Operator.fromString(it)) } 56 | params.prefix_length?.let { prefixLength(it) } 57 | params.zero_terms_query?.let { zeroTermsQuery(MatchQuery.ZeroTermsQuery.valueOf(it.toUpperCase())) } 58 | } 59 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/fulltext/MatchPhrase.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Ryan Murfitt rmurfitt@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import org.elasticsearch.index.query.MatchPhraseQueryBuilder 8 | 9 | 10 | /** 11 | * match_phrase 12 | */ 13 | 14 | class MatchPhraseBlock { 15 | 16 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 17 | infix fun String.to(init: MatchPhraseData.() -> Unit) = this.invoke(init) 18 | 19 | operator fun String.invoke(init: MatchPhraseData.() -> Unit) = MatchPhraseData(name = this).apply(init) 20 | 21 | infix fun String.to(query: Any) = MatchPhraseData(name = this, query = query) 22 | 23 | data class MatchPhraseData( 24 | var name: String, 25 | var query: Any? = null, 26 | var analyzer: String? = null, 27 | var slop: Int? = null) 28 | } 29 | 30 | fun match_phrase(init: MatchPhraseBlock.() -> MatchPhraseBlock.MatchPhraseData): MatchPhraseQueryBuilder { 31 | val params = MatchPhraseBlock().init() 32 | return MatchPhraseQueryBuilder(params.name, params.query).apply { 33 | params.analyzer?.let { analyzer(it) } 34 | params.slop?.let { slop(it) } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/fulltext/MatchPhrasePrefix.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Ryan Murfitt rmurfitt@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import org.elasticsearch.index.query.MatchPhrasePrefixQueryBuilder 8 | 9 | 10 | /** 11 | * match_phrase_prefix 12 | */ 13 | 14 | class MatchPhrasePrefixBlock { 15 | 16 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 17 | infix fun String.to(init: MatchPhrasePrefixData.() -> Unit) = this.invoke(init) 18 | 19 | operator fun String.invoke(init: MatchPhrasePrefixData.() -> Unit) = MatchPhrasePrefixData(name = this).apply(init) 20 | 21 | infix fun String.to(query: Any) = MatchPhrasePrefixData(name = this, query = query) 22 | 23 | data class MatchPhrasePrefixData( 24 | var name: String, 25 | var query: Any? = null, 26 | var analyzer: String? = null, 27 | var slop: Int? = null, 28 | var max_expansions: Int? = null) 29 | } 30 | 31 | fun match_phrase_prefix(init: MatchPhrasePrefixBlock.() -> MatchPhrasePrefixBlock.MatchPhrasePrefixData): MatchPhrasePrefixQueryBuilder { 32 | val params = MatchPhrasePrefixBlock().init() 33 | return MatchPhrasePrefixQueryBuilder(params.name, params.query).apply { 34 | params.analyzer?.let { analyzer(it) } 35 | params.slop?.let { slop(it) } 36 | params.max_expansions?.let { maxExpansions(it) } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/fulltext/MultiMatch.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import org.elasticsearch.common.unit.Fuzziness 8 | import org.elasticsearch.index.query.MultiMatchQueryBuilder 9 | import org.elasticsearch.index.query.Operator 10 | import org.elasticsearch.index.search.MatchQuery 11 | 12 | data class MultiMatchData( 13 | var query: Any? = null, 14 | var fields: List? = null, 15 | var type: String? = null, 16 | var operator: String? = null, 17 | var analyzer: String? = null, 18 | var boost: Float? = null, 19 | var slop: Int? = null, 20 | var fuzziness: Fuzziness? = null, 21 | var prefix_length: Int? = null, 22 | var max_expansions: Int? = null, 23 | var minimum_should_match: String? = null, 24 | var fuzzy_rewrite: String? = null, 25 | var tie_breaker: Float? = null, 26 | var lenient: Boolean? = null, 27 | var cutoff_frequency: Float? = null, 28 | var zero_terms_query: MatchQuery.ZeroTermsQuery? = null) 29 | 30 | private fun String.splitFieldBoost() : Pair { 31 | val parts = this.split("^", limit = 2) 32 | return when (parts.size) { 33 | 1 -> parts[0] to 1.0f 34 | else -> parts[0] to parts[1].toFloat() 35 | } 36 | } 37 | 38 | private fun MultiMatchData.boostedFields() = 39 | fields!!.map { it.splitFieldBoost() }.toMap() 40 | 41 | fun multi_match(init: MultiMatchData.() -> Unit): MultiMatchQueryBuilder { 42 | val params = MultiMatchData().apply(init) 43 | return MultiMatchQueryBuilder(params.query).apply { 44 | fields(params.boostedFields()) 45 | params.type?.let { type(it) } 46 | params.operator?.let { operator(Operator.fromString(it)) } 47 | params.analyzer?.let { analyzer(it) } 48 | params.boost?.let { boost(it) } 49 | params.slop?.let { slop(it) } 50 | params.fuzziness?.let { fuzziness(it) } 51 | params.prefix_length?.let { prefixLength(it) } 52 | params.max_expansions?.let { maxExpansions(it) } 53 | params.minimum_should_match?.let { minimumShouldMatch(it) } 54 | params.fuzzy_rewrite?.let { fuzzyRewrite(it) } 55 | params.tie_breaker?.let { tieBreaker(it) } 56 | params.lenient?.let { lenient(it) } 57 | params.cutoff_frequency?.let { cutoffFrequency(it) } 58 | params.zero_terms_query?.let { zeroTermsQuery(it) } 59 | } 60 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/joining/HasChild.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.joining 6 | 7 | import org.apache.lucene.search.join.ScoreMode 8 | import org.elasticsearch.join.query.HasChildQueryBuilder 9 | import org.elasticsearch.index.query.InnerHitBuilder 10 | import org.elasticsearch.index.query.QueryBuilder 11 | 12 | data class HasChildData( 13 | var query: QueryBuilder? = null, 14 | var type: String? = null, 15 | var boost: Float? = null, 16 | var score_mode: ScoreMode = ScoreMode.None, 17 | var min_children: Int = HasChildQueryBuilder.DEFAULT_MIN_CHILDREN, 18 | var max_children: Int = HasChildQueryBuilder.DEFAULT_MAX_CHILDREN, 19 | var inner_hits: InnerHitBuilder? = null) { 20 | 21 | fun query(f: () -> QueryBuilder) { 22 | query = f() 23 | } 24 | } 25 | 26 | fun has_child(init: HasChildData.() -> Unit): HasChildQueryBuilder { 27 | val params = HasChildData().apply(init) 28 | return HasChildQueryBuilder(params.type, params.query, params.score_mode).apply { 29 | params.boost?.let { boost(it) } 30 | minMaxChildren(params.min_children, params.max_children) 31 | params.inner_hits?.let { innerHit(it) } 32 | } 33 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/joining/HasParent.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.joining 6 | 7 | import org.elasticsearch.join.query.HasParentQueryBuilder 8 | import org.elasticsearch.index.query.InnerHitBuilder 9 | import org.elasticsearch.index.query.QueryBuilder 10 | 11 | data class HasParentData( 12 | var query: QueryBuilder? = null, 13 | var parent_type: String? = null, 14 | var score: Boolean = false, 15 | var boost: Float? = null, 16 | var inner_hits: InnerHitBuilder? = null) { 17 | 18 | fun query(f: () -> QueryBuilder) { 19 | query = f() 20 | } 21 | } 22 | 23 | fun has_parent(init: HasParentData.() -> Unit): HasParentQueryBuilder { 24 | val params = HasParentData().apply(init) 25 | return HasParentQueryBuilder(params.parent_type, params.query, params.score).apply { 26 | params.boost?.let { boost(it) } 27 | params.inner_hits?.let { innerHit(it) } 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/joining/Nested.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.joining 6 | 7 | import org.apache.lucene.search.join.ScoreMode 8 | import org.elasticsearch.index.query.InnerHitBuilder 9 | import org.elasticsearch.index.query.NestedQueryBuilder 10 | import org.elasticsearch.index.query.QueryBuilder 11 | 12 | 13 | data class NestedData( 14 | var query: QueryBuilder? = null, 15 | var path: String? = null, 16 | var score_mode: ScoreMode = ScoreMode.Avg, 17 | var boost: Float? = null, 18 | var inner_hits: InnerHitBuilder? = null) { 19 | 20 | fun query(f: () -> QueryBuilder) { 21 | query = f() 22 | } 23 | } 24 | 25 | fun nested(init: NestedData.() -> Unit): NestedQueryBuilder { 26 | val params = NestedData().apply(init) 27 | return NestedQueryBuilder(params.path, params.query, params.score_mode).apply { 28 | params.boost?.let { boost(it) } 29 | params.inner_hits?.let { innerHit(it) } 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Exists.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.ExistsQueryBuilder 10 | 11 | class ExistsData(var field: String? = null) : QueryData() 12 | 13 | fun exists(init: ExistsData.() -> Unit): ExistsQueryBuilder { 14 | val params = ExistsData().apply(init) 15 | return ExistsQueryBuilder(params.field).apply { 16 | initQuery(params) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Fuzzy.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.common.unit.Fuzziness 10 | import org.elasticsearch.index.query.FuzzyQueryBuilder 11 | 12 | class FuzzyBlock { 13 | class FuzzyData( 14 | val name: String, 15 | var value: Any? = null, 16 | var fuzziness: Fuzziness? = null, 17 | var prefix_length: Int? = null, 18 | var max_expansions: Int? = null, 19 | var transpositions: Boolean? = null) : QueryData() 20 | 21 | infix fun String.to(value: Any) = FuzzyData(name = this, value = value) 22 | 23 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 24 | infix fun String.to(init: FuzzyData.() -> Unit) = this.invoke(init) 25 | 26 | operator fun String.invoke(init: FuzzyData.() -> Unit) = FuzzyData(name = this).apply(init) 27 | } 28 | 29 | @Suppress("DEPRECATION") 30 | fun fuzzy(init: FuzzyBlock.() -> FuzzyBlock.FuzzyData): FuzzyQueryBuilder { 31 | val params = FuzzyBlock().init() 32 | return FuzzyQueryBuilder(params.name, params.value).apply { 33 | initQuery(params) 34 | params.fuzziness?.let { fuzziness(it) } 35 | params.prefix_length?.let { prefixLength(it) } 36 | params.max_expansions?.let { maxExpansions(it) } 37 | transpositions(params.transpositions ?: false) 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Ids.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.IdsQueryBuilder 10 | 11 | class IdsData( 12 | var types: List = emptyList(), 13 | var values: List = emptyList()) : QueryData() { 14 | 15 | var type: String 16 | get() = types[0] 17 | set(value) { 18 | types = listOf(value) 19 | } 20 | } 21 | 22 | 23 | fun ids(init: IdsData.() -> Unit): IdsQueryBuilder { 24 | val params = IdsData().apply(init) 25 | return IdsQueryBuilder().apply { 26 | initQuery(params) 27 | addIds(*params.values.toTypedArray()) 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/MatchAll.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import org.elasticsearch.index.query.MatchAllQueryBuilder 8 | 9 | 10 | /** 11 | * match_all 12 | */ 13 | fun match_all(init: MatchAllQueryBuilder.() -> Unit) = MatchAllQueryBuilder().apply(init) 14 | 15 | var MatchAllQueryBuilder.boost: Float 16 | get() = error("write-only property") 17 | set(value) { 18 | this.boost(value) 19 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Prefix.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.PrefixQueryBuilder 10 | 11 | class PrefixBlock { 12 | class PrefixData( 13 | val name: String, 14 | var prefix: String? = null) : QueryData() 15 | 16 | infix fun String.to(prefix: String) = PrefixData(name = this, prefix = prefix) 17 | 18 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 19 | infix fun String.to(init: PrefixData.() -> Unit) = this.invoke(init) 20 | 21 | operator fun String.invoke(init: PrefixData.() -> Unit) = PrefixData(name = this).apply(init) 22 | } 23 | 24 | fun prefix(init: PrefixBlock.() -> PrefixBlock.PrefixData): PrefixQueryBuilder { 25 | val params = PrefixBlock().init() 26 | return PrefixQueryBuilder(params.name, params.prefix).apply { 27 | initQuery(params) 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Range.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.RangeQueryBuilder 10 | 11 | class RangeBlock { 12 | class RangeData( 13 | var name: String, 14 | var from: Any? = null, 15 | var to: Any? = null, 16 | var include_upper: Boolean? = null, 17 | var include_lower: Boolean? = null, 18 | var format: String? = null, 19 | var time_zone: String? = null) : QueryData() { 20 | 21 | var gte: Any? 22 | get() = this.from 23 | set(value) { 24 | from = value 25 | include_lower = true 26 | } 27 | 28 | var gt: Any? 29 | get() = this.from 30 | set(value) { 31 | from = value 32 | include_lower = false 33 | } 34 | 35 | var lte: Any? 36 | get() = this.to 37 | set(value) { 38 | to = value 39 | include_upper = true 40 | } 41 | 42 | var lt: Any? 43 | get() = this.to 44 | set(value) { 45 | to = value 46 | include_upper = false 47 | } 48 | } 49 | 50 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 51 | infix fun String.to(init: RangeData.() -> Unit): RangeData { 52 | return this.invoke(init) 53 | } 54 | 55 | operator fun String.invoke(init: RangeData.() -> Unit): RangeData { 56 | return RangeData(name = this).apply(init) 57 | } 58 | } 59 | 60 | fun range(init: RangeBlock.() -> RangeBlock.RangeData): RangeQueryBuilder { 61 | val params = RangeBlock().init() 62 | return RangeQueryBuilder(params.name).apply { 63 | initQuery(params) 64 | params.from?.let { from(it) } 65 | params.to?.let { to(it) } 66 | params.include_lower?.let { includeLower(it) } 67 | params.include_upper?.let { includeUpper(it) } 68 | params.boost?.let { boost(it) } 69 | params.format?.let { format(it) } 70 | params.time_zone?.let { timeZone(it) } 71 | } 72 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Regexp.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.RegexpFlag 10 | import org.elasticsearch.index.query.RegexpQueryBuilder 11 | 12 | class RegexpBlock { 13 | class RegexpData( 14 | val name: String, 15 | var value: String? = null, 16 | var flags: List? = null, 17 | var max_determinized_states: Int? = null) : QueryData() 18 | 19 | infix fun String.to(value: String) = RegexpData(name = this, value = value) 20 | 21 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 22 | infix fun String.to(init: RegexpData.() -> Unit) = this.invoke(init) 23 | 24 | operator fun String.invoke(init: RegexpData.() -> Unit) = RegexpData(name = this).apply(init) 25 | } 26 | 27 | fun regexp(init: RegexpBlock.() -> RegexpBlock.RegexpData): RegexpQueryBuilder { 28 | val params = RegexpBlock().init() 29 | return RegexpQueryBuilder(params.name, params.value).apply { 30 | initQuery(params) 31 | params.flags?.let { flags(*it.toTypedArray()) } 32 | params.max_determinized_states?.let { maxDeterminizedStates(it) } 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/StringQuery.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021. Ehsan Souri ehsansouri23@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.Operator 10 | import org.elasticsearch.index.query.QueryStringQueryBuilder 11 | 12 | class StringBlock { 13 | class StringData( 14 | var searchText: String, 15 | var field: String, 16 | var defaultOperator: Operator = Operator.AND 17 | ) : QueryData() 18 | 19 | infix fun String.startsWith(searchText: String): StringData = StringData("$searchText*", this) 20 | infix fun String.endsWith(searchText: String): StringData = StringData("*$searchText", this) 21 | infix fun String.equalsTo(searchText: String): StringData = StringData(searchText, this) 22 | infix fun String.contains(searchText: String): StringData = StringData("*$searchText*", this) 23 | } 24 | 25 | fun string(init: StringBlock.() -> StringBlock.StringData): QueryStringQueryBuilder { 26 | val params = StringBlock().init() 27 | return QueryStringQueryBuilder(params.searchText).field(params.field).analyzeWildcard(true) 28 | .apply { 29 | defaultOperator(params.defaultOperator) 30 | initQuery(params) 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Term.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.TermQueryBuilder 10 | 11 | class TermBlock { 12 | class TermData( 13 | var name: String? = null, 14 | var value: String? = null) : QueryData() 15 | 16 | infix fun String.to(value: String): TermData { 17 | return TermData(name = this, value = value) 18 | } 19 | 20 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 21 | infix fun String.to(init: TermData.() -> Unit): TermData { 22 | return this.invoke(init) 23 | } 24 | 25 | operator fun String.invoke(init: TermData.() -> Unit): TermData { 26 | return TermData(name = this).apply(init) 27 | } 28 | } 29 | 30 | fun term(init: TermBlock.() -> TermBlock.TermData): TermQueryBuilder { 31 | val params = TermBlock().init() 32 | return TermQueryBuilder(params.name, params.value).apply { 33 | initQuery(params) 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Terms.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.TermsQueryBuilder 10 | import org.elasticsearch.indices.TermsLookup 11 | 12 | class TermsBlock { 13 | class TermsData( 14 | var name: String, 15 | var values: List = emptyList(), 16 | var termsLookup: TermsLookup? = null) : QueryData() 17 | 18 | infix fun String.to(values: List): TermsData { 19 | return TermsData(name = this, values = values) 20 | } 21 | 22 | infix fun String.to(lookup: TermsLookup): TermsData { 23 | return TermsData(name = this, termsLookup = lookup) 24 | } 25 | 26 | class TermsLookupData( 27 | var index: String? = null, 28 | var type: String? = null, 29 | var id: String? = null, 30 | var path: String? = null 31 | ) 32 | 33 | operator fun String.invoke(init: TermsLookupData.() -> Unit): TermsData { 34 | val lookup = TermsLookupData().run { 35 | init() 36 | TermsLookup(index, type, id, path) 37 | } 38 | return TermsData(name = this, termsLookup = lookup) 39 | } 40 | } 41 | 42 | fun terms(init: TermsBlock.() -> TermsBlock.TermsData): TermsQueryBuilder { 43 | val params = TermsBlock().init() 44 | return when { 45 | params.termsLookup is TermsLookup -> 46 | TermsQueryBuilder(params.name, params.termsLookup) 47 | 48 | else -> 49 | TermsQueryBuilder(params.name, params.values) 50 | } 51 | .apply { 52 | initQuery(params) 53 | } 54 | } -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Type.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.TypeQueryBuilder 10 | 11 | class TypeData( 12 | var value: String? = null) : QueryData() 13 | 14 | fun type(init: TypeData.() -> Unit): TypeQueryBuilder { 15 | val params = TypeData().apply(init) 16 | return TypeQueryBuilder(params.value).apply { 17 | initQuery(params) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/kotlin/mbuhot/eskotlin/query/term/Wildcard.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.QueryData 8 | import mbuhot.eskotlin.query.initQuery 9 | import org.elasticsearch.index.query.WildcardQueryBuilder 10 | 11 | class WildcardBlock { 12 | class WildcardData( 13 | val name: String, 14 | var wildcard: String? = null) : QueryData() 15 | 16 | infix fun String.to(wildcard: String) = WildcardData(name = this, wildcard = wildcard) 17 | 18 | @Deprecated(message = "Use invoke operator instead.", replaceWith = ReplaceWith("invoke(init)")) 19 | infix fun String.to(init: WildcardData.() -> Unit) = this.invoke(init) 20 | 21 | operator fun String.invoke(init: WildcardData.() -> Unit) = WildcardData(name = this).apply(init) 22 | } 23 | 24 | fun wildcard(init: WildcardBlock.() -> WildcardBlock.WildcardData): WildcardQueryBuilder { 25 | val params = WildcardBlock().init() 26 | return WildcardQueryBuilder(params.name, params.wildcard).apply { 27 | initQuery(params) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/aggregation/FilterAggregationTest.kt: -------------------------------------------------------------------------------- 1 | package mbuhot.eskotlin.aggregation 2 | 3 | import mbuhot.eskotlin.query.compound.bool 4 | import mbuhot.eskotlin.query.should_render_as 5 | import mbuhot.eskotlin.query.term.exists 6 | import org.junit.Test 7 | 8 | class FilterAggregationTest { 9 | @Test 10 | fun `filter aggregation should`() { 11 | val agg = filterAggregation { 12 | name = "expiry" 13 | filter = bool { 14 | must_not { exists { field = "expiryDate" } } 15 | } 16 | } 17 | 18 | agg should_render_as """ 19 | { 20 | "expiry" : { 21 | "filter" : { 22 | "bool" : { 23 | "must_not" : [ 24 | { 25 | "exists" : { 26 | "field" : "expiryDate", 27 | "boost" : 1.0 28 | } 29 | } 30 | ], 31 | "adjust_pure_negative" : true, 32 | "boost" : 1.0 33 | } 34 | } 35 | } 36 | } 37 | """ 38 | } 39 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/aggregation/NestedAggregationTest.kt: -------------------------------------------------------------------------------- 1 | package mbuhot.eskotlin.aggregation 2 | 3 | import mbuhot.eskotlin.query.compound.bool 4 | import mbuhot.eskotlin.query.should_render_as 5 | import mbuhot.eskotlin.query.term.exists 6 | import mbuhot.eskotlin.query.term.term 7 | import org.junit.Test 8 | 9 | class NestedAggregationTest { 10 | @Test 11 | fun `nested_aggregation should`() { 12 | val agg = nested_aggregation { 13 | name = "nested_aggregation" 14 | path = "path" 15 | sub_aggregation = listOf( 16 | filterAggregation { 17 | name = "expiry" 18 | filter = bool { 19 | must_not { exists { field = "expiryDate" } } 20 | } 21 | }, 22 | filterAggregation { 23 | name = "status" 24 | filter = term { 25 | "user" to "Kimchy" 26 | } 27 | } 28 | ) 29 | } 30 | 31 | agg should_render_as """ 32 | { 33 | "nested_aggregation": { 34 | "nested": { 35 | "path": "path" 36 | }, 37 | "aggregations": { 38 | "expiry": { 39 | "filter": { 40 | "bool": { 41 | "must_not": [ 42 | { 43 | "exists": { 44 | "field": "expiryDate", 45 | "boost": 1.0 46 | } 47 | } 48 | ], 49 | "adjust_pure_negative": true, 50 | "boost": 1.0 51 | } 52 | } 53 | }, 54 | "status": { 55 | "filter": { 56 | "term": { 57 | "user": { 58 | "value": "Kimchy", 59 | "boost": 1.0 60 | } 61 | } 62 | } 63 | } 64 | } 65 | } 66 | } 67 | """.trimIndent() 68 | } 69 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/Util.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | package mbuhot.eskotlin.query 5 | 6 | import com.fasterxml.jackson.databind.ObjectMapper 7 | import org.junit.Assert.assertEquals 8 | 9 | 10 | val jsonMapper = ObjectMapper() 11 | fun json_normalize(str: String) : String = jsonMapper.readTree(str).let { jsonMapper.writeValueAsString(it) } 12 | 13 | infix fun T.should_render_as(jsonStr: String) { 14 | assertEquals(json_normalize(this.toString()), json_normalize(jsonStr)) 15 | } 16 | 17 | -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/compound/BoolTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import mbuhot.eskotlin.query.term.match_all 9 | import mbuhot.eskotlin.query.term.range 10 | import mbuhot.eskotlin.query.term.term 11 | import org.junit.Test 12 | 13 | /** 14 | * Created on 3/03/2016 15 | 16 | * @author Michael Buhot (m.buhot@gmail.com) 17 | */ 18 | class BoolTest { 19 | @Test 20 | fun `test bool single should`() { 21 | val query = bool { 22 | should { 23 | match_all { } 24 | } 25 | } 26 | 27 | query should_render_as """ 28 | { 29 | "bool" : { 30 | "should" : [{ 31 | "match_all" : { 32 | "boost": 1.0 33 | } 34 | }], 35 | "adjust_pure_negative":true, 36 | "boost":1.0 37 | } 38 | } 39 | """ 40 | } 41 | 42 | 43 | @Test 44 | fun `test bool`() { 45 | val query = bool { 46 | must { 47 | term { "user" to "kimchy" } 48 | } 49 | filter { 50 | term { "tag" to "tech" } 51 | } 52 | must_not { 53 | range { 54 | "age" { 55 | from = 10 56 | to = 20 57 | } 58 | } 59 | } 60 | should = listOf( 61 | term { "tag" to "wow" }, 62 | term { "tag" to "elasticsearch" }) 63 | minimum_should_match = 1 64 | boost = 1.0f 65 | } 66 | 67 | query should_render_as """ 68 | { 69 | "bool": { 70 | "must": [{ 71 | "term": { 72 | "user": { 73 | "value": "kimchy", 74 | "boost": 1.0 75 | } 76 | } 77 | }], 78 | "filter": [{ 79 | "term": { 80 | "tag": { 81 | "value": "tech", 82 | "boost": 1.0 83 | } 84 | } 85 | }], 86 | "must_not": [{ 87 | "range": { 88 | "age": { 89 | "from": 10, 90 | "to": 20, 91 | "include_lower": true, 92 | "include_upper": true, 93 | "boost": 1.0 94 | } 95 | } 96 | }], 97 | "should": [{ 98 | "term": { 99 | "tag": { 100 | "value": "wow", 101 | "boost": 1.0 102 | } 103 | } 104 | }, { 105 | "term": { 106 | "tag": { 107 | "value": "elasticsearch", 108 | "boost": 1.0 109 | } 110 | } 111 | }], 112 | "adjust_pure_negative": true, 113 | "minimum_should_match": "1", 114 | "boost": 1.0 115 | } 116 | } 117 | """ 118 | } 119 | 120 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/compound/BoostingTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import mbuhot.eskotlin.query.term.term 9 | import org.junit.Test 10 | 11 | /** 12 | * Created on 3/03/2016 13 | 14 | * @author Michael Buhot (m.buhot@gmail.com) 15 | */ 16 | class BoostingTest { 17 | 18 | @Test 19 | fun `test boosting`() { 20 | val query = boosting { 21 | positive { 22 | term { 23 | "field1" to "value1" 24 | } 25 | } 26 | negative { 27 | term { 28 | "field2" to "value2" 29 | } 30 | } 31 | negative_boost = 0.2f 32 | } 33 | 34 | query should_render_as """ 35 | { 36 | "boosting": { 37 | "positive": { 38 | "term": { 39 | "field1": { 40 | "value": "value1", 41 | "boost": 1.0 42 | } 43 | } 44 | }, 45 | "negative": { 46 | "term": { 47 | "field2": { 48 | "value": "value2", 49 | "boost": 1.0 50 | } 51 | } 52 | }, 53 | "negative_boost": 0.2, 54 | "boost": 1.0 55 | } 56 | } 57 | """ 58 | } 59 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/compound/ConstantScoreTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import mbuhot.eskotlin.query.term.term 9 | import org.junit.Test 10 | 11 | /** 12 | * Created on 3/03/2016 13 | 14 | * @author Michael Buhot (m.buhot@gmail.com) 15 | */ 16 | class ConstantScoreTest { 17 | @Test 18 | fun `test constant_score`() { 19 | val query = constant_score { 20 | filter { 21 | term { 22 | "user" to "kimchy" 23 | } 24 | } 25 | boost = 1.2f 26 | } 27 | 28 | query should_render_as """ 29 | { 30 | "constant_score": { 31 | "filter": { 32 | "term": { 33 | "user": { 34 | "value": "kimchy", 35 | "boost": 1.0 36 | } 37 | } 38 | }, 39 | "boost": 1.2 40 | } 41 | } 42 | """ 43 | } 44 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/compound/DisMaxTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import mbuhot.eskotlin.query.term.term 9 | import org.junit.Test 10 | 11 | /** 12 | * Created on 3/03/2016 13 | 14 | * @author Michael Buhot (m.buhot@gmail.com) 15 | */ 16 | class DisMaxTest { 17 | 18 | @Test 19 | fun `test dis_max`() { 20 | val query = dis_max { 21 | tie_breaker = 0.7f 22 | boost = 1.2f 23 | queries = listOf( 24 | term { "age" to "34" }, 25 | term { "age" to "35" } 26 | ) 27 | } 28 | 29 | query should_render_as """ 30 | { 31 | "dis_max": { 32 | "tie_breaker": 0.7, 33 | "queries": [{ 34 | "term": { 35 | "age": { 36 | "value": "34", 37 | "boost": 1.0 38 | } 39 | } 40 | }, { 41 | "term": { 42 | "age": { 43 | "value": "35", 44 | "boost": 1.0 45 | } 46 | } 47 | }], 48 | "boost": 1.2 49 | } 50 | } 51 | """ 52 | } 53 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/compound/FunctionScoreTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.compound 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import mbuhot.eskotlin.query.term.match_all 9 | import mbuhot.eskotlin.query.term.term 10 | import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction 11 | import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.* 12 | import org.junit.Test 13 | 14 | /** 15 | * Created on 3/03/2016 16 | 17 | * @author Michael Buhot (m.buhot@gmail.com) 18 | */ 19 | class FunctionScoreTest { 20 | 21 | @Test 22 | fun `test function_score`() { 23 | val query = function_score { 24 | query = match_all { } 25 | functions = listOf( 26 | term { "foo" to "bar" } to gaussDecayFunction("baz", 1.0, "1d"), 27 | match_all { } to randomFunction().seed(234L), 28 | null to exponentialDecayFunction("qux", 2.3, "10km")) 29 | 30 | boost = 1.2f 31 | boost_mode = "multiply" 32 | score_mode = "max" 33 | max_boost = 5.0f 34 | min_score = 0.001f 35 | } 36 | 37 | query should_render_as """{ 38 | "function_score": { 39 | "query": { 40 | "match_all": { 41 | "boost": 1.0 42 | } 43 | }, 44 | "functions": [{ 45 | "filter": { 46 | "term": { 47 | "foo": { 48 | "value": "bar", 49 | "boost": 1.0 50 | } 51 | } 52 | }, 53 | "gauss": { 54 | "baz": { 55 | "origin": 1.0, 56 | "scale": "1d", 57 | "decay": 0.5 58 | }, 59 | "multi_value_mode": "MIN" 60 | } 61 | }, { 62 | "filter": { 63 | "match_all": { 64 | "boost": 1.0 65 | } 66 | }, 67 | "random_score": { 68 | "seed": 234 69 | } 70 | }, { 71 | "filter": { 72 | "match_all": { 73 | "boost": 1.0 74 | } 75 | }, 76 | "exp": { 77 | "qux": { 78 | "origin": 2.3, 79 | "scale": "10km", 80 | "decay": 0.5 81 | }, 82 | "multi_value_mode": "MIN" 83 | } 84 | }], 85 | "score_mode": "max", 86 | "boost_mode": "multiply", 87 | "max_boost": 5.0, 88 | "min_score": 0.001, 89 | "boost": 1.2 90 | } 91 | } 92 | """ 93 | } 94 | 95 | @Test 96 | fun `test field_value_factor`() { 97 | val query = function_score { 98 | query = match_all { } 99 | field_value_factor = fieldValueFactorFunction("someField") 100 | boost = 1.2f 101 | boost_mode = "multiply" 102 | score_mode = "max" 103 | max_boost = 5.0f 104 | min_score = 0.001f 105 | } 106 | 107 | query should_render_as """{ 108 | "function_score" : { 109 | "query" : { 110 | "match_all" : { 111 | "boost" : 1.0 112 | } 113 | }, 114 | "functions" : [ 115 | { 116 | "filter" : { 117 | "match_all" : { 118 | "boost" : 1.0 119 | } 120 | }, 121 | "field_value_factor" : { 122 | "field" : "someField", 123 | "factor" : 1.0, 124 | "modifier" : "none" 125 | } 126 | } 127 | ], 128 | "score_mode" : "max", 129 | "boost_mode" : "multiply", 130 | "max_boost" : 5.0, 131 | "min_score" : 0.001, 132 | "boost" : 1.2 133 | } 134 | } 135 | """ 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/fulltext/CommonTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | 11 | class CommonTest { 12 | @Test 13 | fun `test common terms query`() { 14 | val query = common { 15 | "body" { 16 | query = "this is bonsai cool" 17 | cutoff_frequency = 0.001f 18 | } 19 | } 20 | 21 | query should_render_as """ 22 | { 23 | "common": { 24 | "body": { 25 | "query": "this is bonsai cool", 26 | "high_freq_operator": "OR", 27 | "low_freq_operator": "OR", 28 | "cutoff_frequency": 0.001, 29 | "boost": 1.0 30 | } 31 | } 32 | } 33 | """ 34 | } 35 | 36 | @Test 37 | fun `test common terms with low_frequency operator`() { 38 | val query = common { 39 | "body" { 40 | query = "nelly the elephant as a cartoon" 41 | cutoff_frequency = 0.001f 42 | low_freq_operator = "and" 43 | } 44 | } 45 | 46 | query should_render_as """ 47 | { 48 | "common": { 49 | "body": { 50 | "query": "nelly the elephant as a cartoon", 51 | "high_freq_operator": "OR", 52 | "low_freq_operator": "AND", 53 | "cutoff_frequency": 0.001, 54 | "boost": 1.0 55 | } 56 | } 57 | } 58 | """ 59 | } 60 | 61 | @Test 62 | fun `test common terms with minimum_should_match`() { 63 | val query = common { 64 | "body" { 65 | query = "nelly the elephant as a cartoon" 66 | cutoff_frequency = 0.001f 67 | minimum_should_match.low_freq = 2 68 | } 69 | } 70 | query should_render_as """ 71 | { 72 | "common": { 73 | "body": { 74 | "query": "nelly the elephant as a cartoon", 75 | "high_freq_operator": "OR", 76 | "low_freq_operator": "OR", 77 | "cutoff_frequency": 0.001, 78 | "minimum_should_match": { 79 | "low_freq": "2" 80 | }, 81 | "boost": 1.0 82 | } 83 | } 84 | } 85 | """ 86 | } 87 | 88 | @Test 89 | fun `test common terms with separate minimum_should_match high and low`() { 90 | val query = common { 91 | "body" { 92 | query = "nelly the elephant not as a cartoon" 93 | cutoff_frequency = 0.001f 94 | minimum_should_match { 95 | low_freq = 2 96 | high_freq = 3 97 | } 98 | } 99 | } 100 | 101 | query should_render_as """ 102 | { 103 | "common": { 104 | "body": { 105 | "query": "nelly the elephant not as a cartoon", 106 | "high_freq_operator": "OR", 107 | "low_freq_operator": "OR", 108 | "cutoff_frequency": 0.001, 109 | "minimum_should_match": { 110 | "low_freq": "2", 111 | "high_freq": "3" 112 | }, 113 | "boost": 1.0 114 | } 115 | } 116 | } 117 | """ 118 | } 119 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/fulltext/MatchPhrasePrefixTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | /** 11 | * Created on 2/03/2016 12 | 13 | * @author Michael Buhot (m.buhot@gmail.com) 14 | */ 15 | 16 | 17 | class MatchPhrasePrefixTest { 18 | 19 | @Test 20 | fun `test match_phrase_prefix`() { 21 | val query = match_phrase_prefix { 22 | "message" to "this is a test" 23 | } 24 | 25 | query should_render_as """ 26 | { 27 | "match_phrase_prefix" : { 28 | "message" : { 29 | "query" : "this is a test", 30 | "slop":0, 31 | "max_expansions":50, 32 | "boost":1.0 33 | } 34 | } 35 | } 36 | """ 37 | } 38 | 39 | @Test 40 | fun `test match_phrase_prefix with max_expansions`() { 41 | val query = match_phrase_prefix { 42 | "message" { 43 | query = "this is a test" 44 | max_expansions = 10 45 | } 46 | } 47 | 48 | query should_render_as """ 49 | { 50 | "match_phrase_prefix" : { 51 | "message" : { 52 | "query" : "this is a test", 53 | "slop":0, 54 | "max_expansions":10, 55 | "boost":1.0 56 | } 57 | } 58 | } 59 | """ 60 | } 61 | 62 | @Test 63 | fun `test match_phrase_prefix with max_expansions and slop`() { 64 | val query = match_phrase_prefix { 65 | "message" { 66 | query = "this is a test" 67 | max_expansions = 10 68 | slop = 2 69 | } 70 | } 71 | 72 | query should_render_as """ 73 | { 74 | "match_phrase_prefix" : { 75 | "message" : { 76 | "query" : "this is a test", 77 | "slop":2, 78 | "max_expansions":10, 79 | "boost":1.0 80 | } 81 | } 82 | } 83 | """ 84 | } 85 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/fulltext/MatchPhraseTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | /** 11 | * Created on 2/03/2016 12 | 13 | * @author Michael Buhot (m.buhot@gmail.com) 14 | */ 15 | 16 | 17 | class MatchPhraseTest { 18 | 19 | @Test 20 | fun `test match_phrase`() { 21 | val query = match_phrase { 22 | "message" to "this is a test" 23 | } 24 | 25 | query should_render_as """ 26 | { 27 | "match_phrase" : { 28 | "message" : { 29 | "query" : "this is a test", 30 | "slop" : 0, 31 | "zero_terms_query" : "NONE", 32 | "boost" : 1.0 33 | } 34 | } 35 | } 36 | """ 37 | } 38 | 39 | @Test 40 | fun `test match_phrase with analyzer`() { 41 | val query = match_phrase { 42 | "message" { 43 | query = "this is a test" 44 | analyzer = "my_analyzer" 45 | } 46 | } 47 | query should_render_as """ 48 | { 49 | "match_phrase" : { 50 | "message" : { 51 | "query" : "this is a test", 52 | "analyzer" : "my_analyzer", 53 | "slop" : 0, 54 | "zero_terms_query": "NONE", 55 | "boost" : 1.0 56 | } 57 | } 58 | } 59 | """ 60 | } 61 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/fulltext/MatchTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import mbuhot.eskotlin.query.term.boost 9 | import mbuhot.eskotlin.query.term.match_all 10 | import org.junit.Test 11 | 12 | /** 13 | * Created on 2/03/2016 14 | 15 | * @author Michael Buhot (m.buhot@gmail.com) 16 | */ 17 | 18 | 19 | class MatchTest { 20 | @Test 21 | fun `test match_all`() { 22 | val query = match_all { 23 | boost = 1.2f 24 | } 25 | query should_render_as """{ "match_all": { "boost" : 1.2 }}""" 26 | } 27 | 28 | @Test 29 | fun `test simple match`() { 30 | val query = match { "message" to "this is a test" } 31 | query should_render_as """ 32 | { 33 | "match": { 34 | "message": { 35 | "query": "this is a test", 36 | "operator": "OR", 37 | "prefix_length": 0, 38 | "max_expansions": 50, 39 | "fuzzy_transpositions": true, 40 | "lenient": false, 41 | "zero_terms_query": "NONE", 42 | "auto_generate_synonyms_phrase_query": true, 43 | "boost": 1.0 44 | } 45 | } 46 | } 47 | """ 48 | } 49 | 50 | @Test 51 | fun `test match with query and operator`() { 52 | 53 | 54 | val query = match { 55 | "message" { 56 | query = "this is a test" 57 | operator = "and" 58 | } 59 | } 60 | 61 | query should_render_as """ 62 | { 63 | "match": { 64 | "message": { 65 | "query": "this is a test", 66 | "operator": "AND", 67 | "prefix_length": 0, 68 | "max_expansions": 50, 69 | "fuzzy_transpositions": true, 70 | "lenient": false, 71 | "zero_terms_query": "NONE", 72 | "auto_generate_synonyms_phrase_query": true, 73 | "boost": 1.0 74 | } 75 | } 76 | } 77 | """ 78 | } 79 | 80 | @Test 81 | fun `test match zero terms query`() { 82 | 83 | val query = match { 84 | "message" { 85 | query = "to be or not to be" 86 | operator = "and" 87 | zero_terms_query = "all" 88 | } 89 | } 90 | query should_render_as """ 91 | { 92 | "match": { 93 | "message": { 94 | "query": "to be or not to be", 95 | "operator": "AND", 96 | "prefix_length": 0, 97 | "max_expansions": 50, 98 | "fuzzy_transpositions": true, 99 | "lenient": false, 100 | "zero_terms_query": "ALL", 101 | "auto_generate_synonyms_phrase_query": true, 102 | "boost": 1.0 103 | } 104 | } 105 | } 106 | """ 107 | } 108 | 109 | 110 | @Test 111 | fun `test match cutoff frequency`() { 112 | val query = match { 113 | "message" { 114 | query = "to be or not to be" 115 | cutoff_frequency = 0.001f 116 | } 117 | } 118 | 119 | query should_render_as """ 120 | { 121 | "match": { 122 | "message": { 123 | "query": "to be or not to be", 124 | "operator": "OR", 125 | "prefix_length": 0, 126 | "max_expansions": 50, 127 | "fuzzy_transpositions": true, 128 | "lenient": false, 129 | "zero_terms_query": "NONE", 130 | "cutoff_frequency": 0.001, 131 | "auto_generate_synonyms_phrase_query": true, 132 | "boost": 1.0 133 | } 134 | } 135 | } 136 | """ 137 | } 138 | 139 | @Test 140 | fun `test match type phrase`() { 141 | val query = match { 142 | "message" { 143 | query = "this is a test" 144 | } 145 | } 146 | 147 | query should_render_as """ 148 | { 149 | "match": { 150 | "message": { 151 | "query": "this is a test", 152 | "operator": "OR", 153 | "prefix_length": 0, 154 | "max_expansions": 50, 155 | "fuzzy_transpositions": true, 156 | "lenient": false, 157 | "zero_terms_query": "NONE", 158 | "auto_generate_synonyms_phrase_query": true, 159 | "boost": 1.0 160 | } 161 | } 162 | } 163 | """ 164 | } 165 | 166 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/fulltext/MultiMatchTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.fulltext 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | 11 | class MultiMatchTest { 12 | 13 | @Test 14 | fun `test multi_match`() { 15 | val query = multi_match { 16 | query = "this is a test" 17 | fields = listOf("subject", "message") 18 | } 19 | query should_render_as """ 20 | { 21 | "multi_match": { 22 | "query": "this is a test", 23 | "fields": ["message^1.0", "subject^1.0"], 24 | "type": "best_fields", 25 | "operator": "OR", 26 | "slop": 0, 27 | "prefix_length": 0, 28 | "max_expansions": 50, 29 | "zero_terms_query": "NONE", 30 | "auto_generate_synonyms_phrase_query": true, 31 | "fuzzy_transpositions": true, 32 | "boost": 1.0 33 | } 34 | } 35 | """ 36 | } 37 | 38 | @Test 39 | fun `test multi_match with best_fields type`() { 40 | val query = multi_match { 41 | query = "brown fox" 42 | type = "best_fields" 43 | fields = listOf("subject", "message") 44 | tie_breaker = 0.3f 45 | } 46 | query should_render_as """ 47 | { 48 | "multi_match": { 49 | "query": "brown fox", 50 | "fields": ["message^1.0", "subject^1.0"], 51 | "type": "best_fields", 52 | "operator": "OR", 53 | "slop": 0, 54 | "prefix_length": 0, 55 | "max_expansions": 50, 56 | "tie_breaker": 0.3, 57 | "zero_terms_query": "NONE", 58 | "auto_generate_synonyms_phrase_query": true, 59 | "fuzzy_transpositions": true, 60 | "boost": 1.0 61 | } 62 | } 63 | """ 64 | } 65 | 66 | @Test 67 | fun `test multi_match with operator`() { 68 | val query = multi_match { 69 | query = "Will Smith" 70 | type = "cross_fields" 71 | fields = listOf("first_name", "last_name") 72 | operator = "and" 73 | } 74 | 75 | query should_render_as """ 76 | { 77 | "multi_match": { 78 | "query": "Will Smith", 79 | "fields": ["first_name^1.0", "last_name^1.0"], 80 | "type": "cross_fields", 81 | "operator": "AND", 82 | "slop": 0, 83 | "prefix_length": 0, 84 | "max_expansions": 50, 85 | "zero_terms_query": "NONE", 86 | "auto_generate_synonyms_phrase_query": true, 87 | "fuzzy_transpositions": true, 88 | "boost": 1.0 89 | } 90 | } 91 | """ 92 | } 93 | 94 | @Test 95 | fun `test multi_match with analyzer`() { 96 | val query = multi_match { 97 | query = "Jon" 98 | type = "cross_fields" 99 | analyzer = "standard" 100 | fields = listOf("first", "last", "*.edge") 101 | } 102 | 103 | query should_render_as """ 104 | { 105 | "multi_match": { 106 | "query": "Jon", 107 | "fields": ["*.edge^1.0", "first^1.0", "last^1.0"], 108 | "type": "cross_fields", 109 | "operator": "OR", 110 | "analyzer": "standard", 111 | "slop": 0, 112 | "prefix_length": 0, 113 | "max_expansions": 50, 114 | "zero_terms_query": "NONE", 115 | "auto_generate_synonyms_phrase_query": true, 116 | "fuzzy_transpositions": true, 117 | "boost": 1.0 118 | } 119 | } 120 | """ 121 | } 122 | 123 | @Test 124 | fun `test multi_match with boost`() { 125 | val query = multi_match { 126 | query = "this is a test" 127 | fields = listOf("subject","message^2.0") 128 | } 129 | query should_render_as """ 130 | { 131 | "multi_match": { 132 | "query": "this is a test", 133 | "fields": ["message^2.0", "subject^1.0"], 134 | "type": "best_fields", 135 | "operator": "OR", 136 | "slop": 0, 137 | "prefix_length": 0, 138 | "max_expansions": 50, 139 | "zero_terms_query": "NONE", 140 | "auto_generate_synonyms_phrase_query": true, 141 | "fuzzy_transpositions": true, 142 | "boost": 1.0 143 | } 144 | } 145 | """ 146 | } 147 | 148 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/joining/HasChildTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.joining 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import mbuhot.eskotlin.query.term.term 9 | import org.apache.lucene.search.join.ScoreMode 10 | import org.elasticsearch.index.query.InnerHitBuilder 11 | import org.junit.Test 12 | 13 | /** 14 | * Created on 3/03/2016 15 | 16 | * @author Michael Buhot (m.buhot@gmail.com) 17 | */ 18 | class HasChildTest { 19 | 20 | @Test 21 | fun `test has_child`() { 22 | val query = has_child { 23 | type = "blog_tag" 24 | score_mode = ScoreMode.Total 25 | min_children = 2 26 | max_children = 10 27 | query { 28 | term { 29 | "tag" to "something" 30 | } 31 | } 32 | inner_hits = InnerHitBuilder() 33 | } 34 | query should_render_as """ 35 | { 36 | "has_child": { 37 | "query": { 38 | "term": { 39 | "tag": { 40 | "value": "something", 41 | "boost": 1.0 42 | } 43 | } 44 | }, 45 | "type": "blog_tag", 46 | "score_mode": "sum", 47 | "min_children": 2, 48 | "max_children": 10, 49 | "ignore_unmapped": false, 50 | "boost": 1.0, 51 | "inner_hits": { 52 | "ignore_unmapped": false, 53 | "from": 0, 54 | "size": 3, 55 | "version": false, 56 | "seq_no_primary_term": false, 57 | "explain": false, 58 | "track_scores": false 59 | } 60 | } 61 | } 62 | """ 63 | } 64 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/joining/HasParentTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.joining 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import mbuhot.eskotlin.query.term.term 9 | import org.elasticsearch.index.query.InnerHitBuilder 10 | import org.junit.Test 11 | 12 | /** 13 | * Created on 3/03/2016 14 | 15 | * @author Michael Buhot (m.buhot@gmail.com) 16 | */ 17 | class HasParentTest { 18 | 19 | @Test 20 | fun `test has_parent`() { 21 | 22 | val query = has_parent { 23 | parent_type = "blog" 24 | query { 25 | term { 26 | "tag" to "something" 27 | } 28 | } 29 | inner_hits = InnerHitBuilder() 30 | } 31 | 32 | query should_render_as """ 33 | { 34 | "has_parent": { 35 | "query": { 36 | "term": { 37 | "tag": { 38 | "value": "something", 39 | "boost": 1.0 40 | } 41 | } 42 | }, 43 | "parent_type": "blog", 44 | "score": false, 45 | "ignore_unmapped": false, 46 | "boost": 1.0, 47 | "inner_hits": { 48 | "ignore_unmapped": false, 49 | "from": 0, 50 | "size": 3, 51 | "version": false, 52 | "seq_no_primary_term": false, 53 | "explain": false, 54 | "track_scores": false 55 | } 56 | } 57 | } 58 | """ 59 | } 60 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/joining/NestedTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.joining 6 | 7 | import mbuhot.eskotlin.query.compound.bool 8 | import mbuhot.eskotlin.query.fulltext.match 9 | import mbuhot.eskotlin.query.should_render_as 10 | import mbuhot.eskotlin.query.term.range 11 | import org.apache.lucene.search.join.ScoreMode 12 | import org.junit.Test 13 | 14 | /** 15 | * Created on 3/03/2016 16 | 17 | * @author Michael Buhot (m.buhot@gmail.com) 18 | */ 19 | class NestedTest { 20 | 21 | @Test 22 | fun `test nested`() { 23 | val query = nested { 24 | path = "obj1" 25 | score_mode = ScoreMode.Avg 26 | query { 27 | bool { 28 | must = listOf( 29 | match { "obj1.name" to "blue" }, 30 | range { "obj1.count" { gt = 5 } } 31 | ) 32 | } 33 | } 34 | } 35 | 36 | query should_render_as """ 37 | { 38 | "nested" : { 39 | "query" : { 40 | "bool" : { 41 | "must" : [ 42 | { 43 | "match" : { 44 | "obj1.name" : { 45 | "query": "blue", 46 | "operator": "OR", 47 | "prefix_length": 0, 48 | "max_expansions": 50, 49 | "fuzzy_transpositions": true, 50 | "lenient": false, 51 | "zero_terms_query": "NONE", 52 | "auto_generate_synonyms_phrase_query": true, 53 | "boost": 1.0 54 | } 55 | } 56 | }, 57 | { 58 | "range" : { 59 | "obj1.count" : { 60 | "from" : 5, 61 | "to" : null, 62 | "include_lower" : false, 63 | "include_upper" : true, 64 | "boost": 1.0 65 | } 66 | } 67 | } 68 | ], 69 | "adjust_pure_negative":true, 70 | "boost":1.0 71 | } 72 | }, 73 | "path" : "obj1", 74 | "ignore_unmapped":false, 75 | "score_mode" : "avg", 76 | "boost": 1.0 77 | } 78 | } 79 | """ 80 | } 81 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/ExistsTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | /** 11 | * Created on 3/03/2016 12 | 13 | * @author Michael Buhot (m.buhot@gmail.com) 14 | */ 15 | class ExistsTest { 16 | @Test 17 | fun `test exists`() { 18 | val query = exists { 19 | field = "user" 20 | } 21 | 22 | query should_render_as """ 23 | { 24 | "exists" : { 25 | "field" : "user", 26 | "boost" : 1.0 27 | } 28 | } 29 | """ 30 | } 31 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/FuzzyTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.elasticsearch.common.unit.Fuzziness 9 | import org.junit.Test 10 | 11 | /** 12 | * Created on 3/03/2016 13 | 14 | * @author Michael Buhot (m.buhot@gmail.com) 15 | */ 16 | class FuzzyTest { 17 | @Test 18 | fun `test fuzzy`() { 19 | val query = fuzzy { 20 | "user" to "ki" 21 | } 22 | query should_render_as """ 23 | { 24 | "fuzzy" : { 25 | "user" : { 26 | "value" : "ki", 27 | "fuzziness" : "AUTO", 28 | "prefix_length" : 0, 29 | "max_expansions" : 50, 30 | "transpositions" : false, 31 | "boost" : 1.0 32 | } 33 | } 34 | } 35 | """ 36 | } 37 | 38 | @Test 39 | fun `test advanced fuzzy`() { 40 | val query = fuzzy { 41 | "user" { 42 | value = "ki" 43 | boost = 2.0f 44 | fuzziness = Fuzziness.TWO 45 | prefix_length = 0 46 | max_expansions = 100 47 | } 48 | } 49 | query should_render_as """ 50 | { 51 | "fuzzy" : { 52 | "user" : { 53 | "value" : "ki", 54 | "fuzziness" : "2", 55 | "prefix_length" : 0, 56 | "max_expansions": 100, 57 | "transpositions" : false, 58 | "boost" : 2.0 59 | } 60 | } 61 | } 62 | """ 63 | } 64 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/IdsTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | /** 11 | * Created on 3/03/2016 12 | 13 | * @author Michael Buhot (m.buhot@gmail.com) 14 | */ 15 | class IdsTest { 16 | @Test 17 | fun `test ids`() { 18 | val query = ids { 19 | values = listOf("1", "100", "4") 20 | } 21 | 22 | query should_render_as """ 23 | { 24 | "ids" : { 25 | "values" : ["1", "100", "4"], 26 | "boost": 1.0 27 | } 28 | } 29 | """ 30 | } 31 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/PrefixTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | /** 11 | * Created on 3/03/2016 12 | 13 | * @author Michael Buhot (m.buhot@gmail.com) 14 | */ 15 | class PrefixTest { 16 | @Test 17 | fun `test prefix`() { 18 | val query = prefix { 19 | "user" to "ki" 20 | } 21 | 22 | query should_render_as """ 23 | { 24 | "prefix" : { 25 | "user" : { 26 | "value": "ki", 27 | "boost": 1.0 28 | } 29 | } 30 | }""" 31 | } 32 | 33 | @Test 34 | fun `test prefix with boost`() { 35 | val query = prefix { 36 | "user" { 37 | prefix = "ki" 38 | boost = 2.0f 39 | } 40 | } 41 | query should_render_as """ 42 | { 43 | "prefix" : { 44 | "user" : { 45 | "value" : "ki", 46 | "boost" : 2.0 47 | } 48 | } 49 | } 50 | """ 51 | } 52 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/RangeTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | /** 11 | * Created on 3/03/2016 12 | 13 | * @author Michael Buhot (m.buhot@gmail.com) 14 | */ 15 | class RangeTest { 16 | 17 | @Test 18 | fun `test range`() { 19 | val query = range { 20 | "age" { 21 | gte = 10 22 | lte = 20 23 | boost = 2.0f 24 | } 25 | } 26 | query should_render_as """ 27 | { 28 | "range" : { 29 | "age" : { 30 | "from" : 10, 31 | "to" : 20, 32 | "include_lower" : true, 33 | "include_upper" : true, 34 | "boost" : 2.0 35 | } 36 | } 37 | } 38 | """ 39 | } 40 | 41 | @Test 42 | fun `test range with date format and time_zone`() { 43 | val query = range { 44 | "born" { 45 | gt = "01/01/2012" 46 | lt = "2013" 47 | format = "dd/MM/yyyy||yyyy" 48 | time_zone = "+01:00" 49 | } 50 | } 51 | 52 | query should_render_as """ 53 | { 54 | "range" : { 55 | "born" : { 56 | "from" : "01/01/2012", 57 | "to" : "2013", 58 | "include_lower" : false, 59 | "include_upper" : false, 60 | "time_zone" : "+01:00", 61 | "format" : "dd/MM/yyyy||yyyy", 62 | "boost": 1.0 63 | } 64 | } 65 | } 66 | """ 67 | } 68 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/RegexpTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.elasticsearch.index.query.RegexpFlag.* 9 | import org.junit.Test 10 | 11 | /** 12 | * Created on 3/03/2016 13 | 14 | * @author Michael Buhot (m.buhot@gmail.com) 15 | */ 16 | class RegexpTest { 17 | 18 | @Test 19 | fun `test regexp`() { 20 | val query = regexp { 21 | "name.first" to "s.*y" 22 | } 23 | 24 | query should_render_as """ 25 | { 26 | "regexp" : { 27 | "name.first": { 28 | "value": "s.*y", 29 | "flags_value": ${ALL.value()}, 30 | "max_determinized_states" : 10000, 31 | "boost": 1.0 32 | } 33 | } 34 | } 35 | """ 36 | } 37 | 38 | @Test 39 | fun `test regexp with boost and flags`() { 40 | val query = regexp { 41 | "name.first" { 42 | value = "s.*y" 43 | boost = 1.2f 44 | flags = listOf(INTERSECTION, COMPLEMENT, EMPTY) 45 | } 46 | } 47 | query should_render_as """ 48 | { 49 | "regexp" : { 50 | "name.first" : { 51 | "value" : "s.*y", 52 | "flags_value" : ${INTERSECTION.value() or COMPLEMENT.value() or EMPTY.value()}, 53 | "max_determinized_states" : 10000, 54 | "boost" : 1.2 55 | } 56 | } 57 | } 58 | """ 59 | } 60 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/StringQueryTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021. Ehsan Souri ehsansouri23@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | class StringQueryTest { 11 | 12 | @Test 13 | fun `test string contains query`() { 14 | val query = string { 15 | "field" contains "f" 16 | } 17 | 18 | query should_render_as """ 19 | { 20 | "query_string":{ 21 | "query":"*f*", 22 | "fields":[ 23 | "field^1.0" 24 | ], 25 | "type":"best_fields", 26 | "default_operator":"and", 27 | "max_determinized_states":10000, 28 | "enable_position_increments":true, 29 | "fuzziness":"AUTO", 30 | "fuzzy_prefix_length":0, 31 | "fuzzy_max_expansions":50, 32 | "phrase_slop":0, 33 | "analyze_wildcard":true, 34 | "escape":false, 35 | "auto_generate_synonyms_phrase_query":true, 36 | "fuzzy_transpositions":true, 37 | "boost":1.0 38 | } 39 | } 40 | """.trimIndent() 41 | } 42 | 43 | @Test 44 | fun `test string starts with query`() { 45 | val query = string { 46 | "field" startsWith "f" 47 | } 48 | 49 | query should_render_as """ 50 | { 51 | "query_string":{ 52 | "query":"f*", 53 | "fields":[ 54 | "field^1.0" 55 | ], 56 | "type":"best_fields", 57 | "default_operator":"and", 58 | "max_determinized_states":10000, 59 | "enable_position_increments":true, 60 | "fuzziness":"AUTO", 61 | "fuzzy_prefix_length":0, 62 | "fuzzy_max_expansions":50, 63 | "phrase_slop":0, 64 | "analyze_wildcard":true, 65 | "escape":false, 66 | "auto_generate_synonyms_phrase_query":true, 67 | "fuzzy_transpositions":true, 68 | "boost":1.0 69 | } 70 | } 71 | """.trimIndent() 72 | } 73 | 74 | @Test 75 | fun `test string ends with query`() { 76 | val query = string { 77 | "field" endsWith "f" 78 | } 79 | 80 | query should_render_as """ 81 | { 82 | "query_string":{ 83 | "query":"*f", 84 | "fields":[ 85 | "field^1.0" 86 | ], 87 | "type":"best_fields", 88 | "default_operator":"and", 89 | "max_determinized_states":10000, 90 | "enable_position_increments":true, 91 | "fuzziness":"AUTO", 92 | "fuzzy_prefix_length":0, 93 | "fuzzy_max_expansions":50, 94 | "phrase_slop":0, 95 | "analyze_wildcard":true, 96 | "escape":false, 97 | "auto_generate_synonyms_phrase_query":true, 98 | "fuzzy_transpositions":true, 99 | "boost":1.0 100 | } 101 | } 102 | """.trimIndent() 103 | } 104 | 105 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/TermTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | class TermTest { 11 | 12 | @Test 13 | fun `test simple term`() { 14 | val query = term { 15 | "user" to "Kimchy" 16 | } 17 | 18 | query should_render_as """ 19 | { 20 | "term" : { 21 | "user" : { 22 | "value": "Kimchy", 23 | "boost": 1.0 24 | } 25 | } 26 | } 27 | """ 28 | } 29 | 30 | @Test 31 | fun `test term with boost and name`() { 32 | val query = term { 33 | "status" { 34 | value = "urgent" 35 | boost = 2.0f 36 | } 37 | } 38 | query should_render_as """ 39 | { 40 | "term" : { 41 | "status" : { 42 | "value" : "urgent", 43 | "boost" : 2.0 44 | } 45 | } 46 | } 47 | """ 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/TermsTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.elasticsearch.indices.TermsLookup 9 | import org.junit.Test 10 | 11 | /** 12 | * Created on 3/03/2016 13 | 14 | * @author Michael Buhot (m.buhot@gmail.com) 15 | */ 16 | class TermsTest { 17 | 18 | @Test 19 | fun `test terms`() { 20 | val query = terms { 21 | "user" to listOf("kimchy", "elasticsearch") 22 | } 23 | 24 | query should_render_as """ 25 | { 26 | "terms" : { 27 | "user" : ["kimchy", "elasticsearch"], 28 | "boost": 1.0 29 | } 30 | } 31 | """ 32 | } 33 | 34 | @Test 35 | fun `test terms lookup`() { 36 | val query = terms { 37 | "user" to TermsLookup("users", "user", "2", "followers") 38 | } 39 | 40 | query should_render_as """ 41 | { 42 | "terms" : { 43 | "user" : { 44 | "index" : "users", 45 | "type" : "user", 46 | "id" : "2", 47 | "path" : "followers" 48 | }, 49 | "boost": 1.0 50 | } 51 | } 52 | """ 53 | } 54 | 55 | @Test 56 | fun `test terms lookup dsl`() { 57 | val query = terms { 58 | "user" { 59 | index = "users" 60 | type = "user" 61 | id = "2" 62 | path = "followers" 63 | } 64 | } 65 | 66 | query should_render_as """ 67 | { 68 | "terms" : { 69 | "user" : { 70 | "index" : "users", 71 | "type" : "user", 72 | "id" : "2", 73 | "path" : "followers" 74 | }, 75 | "boost": 1.0 76 | } 77 | } 78 | """ 79 | } 80 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/TypeTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | /** 11 | * Created on 3/03/2016 12 | 13 | * @author Michael Buhot (m.buhot@gmail.com) 14 | */ 15 | class TypeTest { 16 | @Test 17 | fun `test type`() { 18 | val query = type { 19 | value = "my_type" 20 | } 21 | query should_render_as """ 22 | { 23 | "type": { 24 | "value" : "my_type", 25 | "boost" : 1.0 26 | } 27 | } 28 | """ 29 | } 30 | 31 | @Test 32 | fun `test type with boost`() { 33 | val query = type { 34 | value = "my_type" 35 | boost = 2.0f 36 | } 37 | query should_render_as """ 38 | { 39 | "type": { 40 | "value" : "my_type", 41 | "boost" : 2.0 42 | } 43 | } 44 | """ 45 | } 46 | } -------------------------------------------------------------------------------- /src/test/kotlin/mbuhot/eskotlin/query/term/WildcardTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. Michael Buhot m.buhot@gmail.com 3 | */ 4 | 5 | package mbuhot.eskotlin.query.term 6 | 7 | import mbuhot.eskotlin.query.should_render_as 8 | import org.junit.Test 9 | 10 | /** 11 | * Created on 3/03/2016 12 | 13 | * @author Michael Buhot (m.buhot@gmail.com) 14 | */ 15 | class WildcardTest { 16 | @Test 17 | fun `test wildcard`() { 18 | val query = wildcard { 19 | "user" to "ki*y" 20 | } 21 | 22 | query should_render_as """ 23 | { 24 | "wildcard" : { 25 | "user" : { 26 | "wildcard" : "ki*y", 27 | "boost" : 1.0 28 | } 29 | } 30 | }""" 31 | } 32 | 33 | @Test 34 | fun `test wildcard with boost`() { 35 | val query = wildcard { 36 | "user" { 37 | wildcard = "ki*y" 38 | boost = 2.0f 39 | } 40 | } 41 | 42 | query should_render_as """ 43 | { 44 | "wildcard" : { 45 | "user" : { 46 | "wildcard" : "ki*y", 47 | "boost" : 2.0 48 | } 49 | } 50 | } 51 | """ 52 | } 53 | } --------------------------------------------------------------------------------