Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ See the src/test directory for more examples.

## Span Queries - Contributions welcome!

+ Span Term
+ [Span Term](./src/test/kotlin/mbuhot/eskotlin/span/SpanTermTest.kt)
+ Span Multi Term
+ Span First
+ Span Near
+ [Span Near](./src/test/kotlin/mbuhot/eskotlin/span/SpanNearTest.kt)
+ Span Or
+ Span Not
+ Span Containing
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

group 'mbuhot'
version '0.8.0'
version '0.9.0'

apply plugin: 'kotlin'
apply plugin: 'com.jfrog.bintray'
Expand All @@ -15,15 +15,15 @@ buildscript {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.+'
}
}

dependencies {
compile "org.elasticsearch:elasticsearch:7.5.1"
compile "org.elasticsearch.plugin:parent-join-client:7.5.1"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.3.61"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.4.10"
testCompile "org.apache.logging.log4j:log4j-api:2.13.0"
testCompile "org.apache.logging.log4j:log4j-core:2.13.0"
testCompile 'com.fasterxml.jackson.core:jackson-databind:2.10.2'
Expand Down
33 changes: 33 additions & 0 deletions src/main/kotlin/mbuhot/eskotlin/span/SpanNear.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mbuhot.eskotlin.span

import org.elasticsearch.index.query.SpanNearQueryBuilder
import org.elasticsearch.index.query.SpanQueryBuilder

data class SpanNearData(
var clauses: List<SpanQueryBuilder>,
var slop: Int = 0,
var in_order: Boolean = false
) {

fun clauses(f: () -> SpanQueryBuilder) {
clauses = listOf(f())
}
}

fun span_near(init: SpanNearData.() -> Unit): SpanNearQueryBuilder? {
val params = SpanNearData(listOf()).apply(init)

if (params.clauses.isEmpty())
throw IllegalArgumentException("[span_near] must include at least one clause")

return SpanNearQueryBuilder(params.clauses.first(), params.slop).apply {
if (params.clauses.size > 1) {
params.clauses.forEachIndexed { index, spanQueryBuilder ->
if (index > 0)
addClause(spanQueryBuilder)
}
}

inOrder(params.in_order)
}
}
22 changes: 22 additions & 0 deletions src/main/kotlin/mbuhot/eskotlin/span/SpanTerm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mbuhot.eskotlin.span

import mbuhot.eskotlin.query.QueryData
import mbuhot.eskotlin.query.initQuery
import org.elasticsearch.index.query.SpanTermQueryBuilder

class SpanTermBlock {
class SpanTermData(
var name: String? = null,
var value: String? = null) : QueryData()

infix fun String.to(value: String): SpanTermData {
return SpanTermData(name = this, value = value)
}
}

fun span_term(init: SpanTermBlock.() -> SpanTermBlock.SpanTermData): SpanTermQueryBuilder {
val params = SpanTermBlock().init()
return SpanTermQueryBuilder(params.name, params.value).apply {
initQuery(params)
}
}
43 changes: 43 additions & 0 deletions src/test/kotlin/mbuhot/eskotlin/span/SpanNearTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package mbuhot.eskotlin.span

import mbuhot.eskotlin.query.should_render_as
import org.junit.Test

class SpanNearTest {
@Test(expected = IllegalArgumentException::class)
fun `span term should throw exception when clauses is empty`() {
span_near {
slop = 12
in_order = true
}
}

@Test
fun `span term near test should`() {
val spanNear = span_near {
slop = 12
in_order = true
clauses = listOf(
span_term { "field" to "value1" },
span_term { "field" to "value2" },
span_term { "field" to "value3" }
)
}

spanNear should_render_as """
{
"span_near": {
"clauses": [
{ "span_term": { "field": { "value": "value1", "boost": 1.0 } } },
{ "span_term": { "field": { "value": "value2", "boost": 1.0 } } },
{ "span_term": { "field": { "value": "value3", "boost": 1.0 } } }
],
"slop": 12,
"in_order": true,
"boost": 1.0
}
}
""".trimIndent()
}
}

24 changes: 24 additions & 0 deletions src/test/kotlin/mbuhot/eskotlin/span/SpanTermTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mbuhot.eskotlin.span

import mbuhot.eskotlin.query.should_render_as
import org.junit.Test

class SpanTermTest {
@Test
fun `span term test`() {
val query = span_term {
"field" to "value1"
}

query should_render_as """
{
"span_term": {
"field": {
"value": "value1",
"boost": 1.0
}
}
}
""".trimIndent()
}
}