Skip to content

Add queryName to support named queries #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.7.1

- Add support for named queries

# 0.7.0

- Add support for filter and nested aggregations, thanks @nzcoffeem !
Expand Down Expand Up @@ -38,4 +42,4 @@
# 0.1.0

- Initial release
- Targets Elastic Search version 2.2.x
- Targets Elastic Search version 2.2.x
2 changes: 2 additions & 0 deletions src/main/kotlin/mbuhot/eskotlin/query/CommonQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import org.elasticsearch.index.query.AbstractQueryBuilder

open class QueryData {
var boost: Float? = null
var queryName: String? = null
}

fun AbstractQueryBuilder<*>.initQuery(data: QueryData) {
data.boost?.let { this.boost(it) }
data.queryName?.let { this.queryName(it) }
}
30 changes: 30 additions & 0 deletions src/test/kotlin/mbuhot/eskotlin/query/QueryDataTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mbuhot.eskotlin.query

import org.elasticsearch.index.query.BoolQueryBuilder
import org.junit.Test

/**
* Created on 02/24/2021
* @author Vishwanath Sundareshan ([email protected])
*/
internal class QueryDataTest{
@Test
fun `boost and query name is applied`() {
val queryData = QueryData()
queryData.boost = 1f
queryData.queryName = "myquery"

val boolQuery = BoolQueryBuilder()
boolQuery.initQuery(queryData)

boolQuery.toString() should_render_as """
{
"bool" : {
"adjust_pure_negative" : true,
"boost" : 1.0,
"_name" : "myquery"
}
}
"""
}
}
22 changes: 22 additions & 0 deletions src/test/kotlin/mbuhot/eskotlin/query/term/TermTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,27 @@ class TermTest {
}
"""
}

@Test
fun `test term with boost, name and queryName`() {
val query = term {
"status" {
value = "urgent"
queryName = "myquery"
boost = 2.0f
}
}
query should_render_as """
{
"term" : {
"status" : {
"value" : "urgent",
"boost" : 2.0,
"_name": "myquery"
}
}
}
"""
}
}