-
Notifications
You must be signed in to change notification settings - Fork 18
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
(dsl): Support prefix query #258
Merged
drmarjanovic
merged 6 commits into
lambdaworks:main
from
petarcurcin:task-support-prefix-query
Jun 24, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1afdcca
Add PrefixQuery trait and provide implementation
petarcurcin 6e1103b
Provide unit and integration tests
petarcurcin 381abe8
Fix code remarks
petarcurcin ae04900
Add documentation for prefix query
petarcurcin aedad4d
Fix code remarks
petarcurcin 91b9ad1
Add constructing test
petarcurcin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
id: elastic_query_prefix | ||
title: "Prefix Query" | ||
--- | ||
|
||
The `Prefix` query returns documents that contain a specific prefix in a provided field. | ||
|
||
In order to use the `Prefix` query import the following: | ||
```scala | ||
import zio.elasticsearch.query.PrefixQuery | ||
import zio.elasticsearch.ElasticQuery._ | ||
``` | ||
|
||
You can create a `Prefix` query using the `prefix` method this way: | ||
```scala | ||
val query: PrefixQuery = prefix(field = Document.name, value = "test") | ||
``` | ||
|
||
You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `Prefix` query using the `prefix` method this way: | ||
```scala | ||
val query: PrefixQuery = prefix(field = Document.name, value = "test") | ||
``` | ||
|
||
If you want to change the `case_insensitive`, you can use `caseInsensitive`, `caseInsensitiveFalse` or `caseInsensitiveTrue` method: | ||
```scala | ||
val queryWithCaseInsensitive: PrefixQuery = prefix(field = Document.name, value = "test").caseInsensitive(true) | ||
val queryWithCaseInsensitiveFalse: PrefixQuery = prefix(field = Document.name, value = "test").caseInsensitiveFalse | ||
val queryWithCaseInsensitiveTrue: PrefixQuery = prefix(field = Document.name, value = "test").caseInsensitiveTrue | ||
``` | ||
|
||
You can find more information about `Prefix` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-prefix-query.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1007,6 +1007,29 @@ object ElasticQuerySpec extends ZIOSpecDefault { | |
) | ||
) | ||
}, | ||
test("prefix") { | ||
val query = prefix("stringField", "test") | ||
val queryTs = prefix(TestDocument.stringField, "test") | ||
val queryWithSuffix = prefix(TestDocument.stringField.keyword, "test") | ||
val queryWithCaseInsensitive = prefix(TestDocument.stringField, "test").caseInsensitiveTrue | ||
|
||
assert(query)( | ||
equalTo(Prefix[Any](field = "stringField", value = "test", caseInsensitive = None)) | ||
) && | ||
assert(queryTs)( | ||
equalTo(Prefix[TestDocument](field = "stringField", value = "test", caseInsensitive = None)) | ||
) && | ||
assert(queryWithSuffix)( | ||
equalTo( | ||
Prefix[TestDocument](field = "stringField.keyword", value = "test", caseInsensitive = None) | ||
) | ||
) && | ||
assert(queryWithCaseInsensitive)( | ||
equalTo( | ||
Prefix[TestDocument](field = "stringField", value = "test", caseInsensitive = Some(true)) | ||
) | ||
) | ||
}, | ||
test("range") { | ||
val query = range("testField") | ||
val queryString = range(TestDocument.stringField) | ||
|
@@ -2531,6 +2554,36 @@ object ElasticQuerySpec extends ZIOSpecDefault { | |
assert(queryWithScoreMode.toJson(fieldPath = None))(equalTo(expectedWithScoreMode.toJson)) && | ||
assert(queryWithAllParams.toJson(fieldPath = None))(equalTo(expectedWithAllParams.toJson)) | ||
}, | ||
test("prefix") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide |
||
val query = prefix(TestDocument.stringField, "test") | ||
val queryWithCaseInsensitive = prefix(TestDocument.stringField, "test").caseInsensitiveTrue | ||
|
||
val expected = | ||
""" | ||
|{ | ||
| "prefix": { | ||
| "stringField": { | ||
| "value": "test" | ||
| } | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
val expectedWithCaseInsensitive = | ||
""" | ||
|{ | ||
| "prefix": { | ||
| "stringField": { | ||
| "value": "test", | ||
| "case_insensitive": true | ||
| } | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
assert(query.toJson(fieldPath = None))(equalTo(expected.toJson)) && | ||
assert(queryWithCaseInsensitive.toJson(fieldPath = None))(equalTo(expectedWithCaseInsensitive.toJson)) | ||
}, | ||
test("range") { | ||
val queryEmpty = range(TestDocument.intField) | ||
val queryEmptyWithBoost = range(TestDocument.intField).boost(3.14) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.