-
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 matchPhrasePrefix
query
#301
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
id: elastic_query_match_phrase_prefix | ||
title: "Match Phrase Prefix Query" | ||
--- | ||
|
||
The `MatchPhrasePrefix` returns documents that contain the words of a provided text, in the same order as provided. | ||
The last term of the provided text is treated as a prefix, matching any words that begin with that term. | ||
|
||
In order to use the `MatchPhrasePrefix` query import the following: | ||
```scala | ||
import zio.elasticsearch.query.MatchPhrasePrefixQuery | ||
import zio.elasticsearch.ElasticQuery._ | ||
``` | ||
|
||
You can create a `MatchPhrasePrefix` query using the `matchPhrasePrefix` method this way: | ||
```scala | ||
val query: MatchPhrasePrefixQuery = matchPhrasePrefix(field = "stringField", value = "test") | ||
``` | ||
|
||
You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `MatchPhrasePrefix` query using the `matchPhrasePrefix` method this way: | ||
```scala | ||
val query: MatchPhrasePrefixQuery = matchPhrasePrefix(field = Document.stringField, value = "test") | ||
``` | ||
|
||
You can find more information about `MatchPhrasePrefix` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-match-query-phrase-prefix.html). | ||
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. Put empty line at the end of the file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -941,6 +941,30 @@ object HttpExecutorSpec extends IntegrationSpec { | |
Executor.execute(ElasticRequest.createIndex(firstSearchIndex)), | ||
Executor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie | ||
), | ||
test("search for a document using a match phrase prefix query") { | ||
checkOnce(genDocumentId, genTestDocument, genDocumentId, genTestDocument) { | ||
(firstDocumentId, firstDocument, secondDocumentId, secondDocument) => | ||
for { | ||
_ <- Executor.execute(ElasticRequest.deleteByQuery(firstSearchIndex, matchAll)) | ||
document = firstDocument.copy(stringField = s"${firstDocument.stringField} test") | ||
_ <- | ||
Executor.execute(ElasticRequest.upsert[TestDocument](firstSearchIndex, firstDocumentId, document)) | ||
_ <- Executor.execute( | ||
ElasticRequest | ||
.upsert[TestDocument](firstSearchIndex, secondDocumentId, secondDocument) | ||
.refreshTrue | ||
) | ||
query = matchPhrasePrefix( | ||
field = TestDocument.stringField, | ||
value = s"${firstDocument.stringField} te" | ||
) | ||
res <- Executor.execute(ElasticRequest.search(firstSearchIndex, query)).documentAs[TestDocument] | ||
} yield assert(res)(Assertion.contains(document)) | ||
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. Maybe you can add |
||
} | ||
} @@ around( | ||
Executor.execute(ElasticRequest.createIndex(firstSearchIndex)), | ||
Executor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie | ||
), | ||
test("search for a document using a terms query") { | ||
checkOnce(genDocumentId, genTestDocument, genDocumentId, genTestDocument, genDocumentId, genTestDocument) { | ||
(firstDocumentId, firstDocument, secondDocumentId, secondDocument, thirdDocumentId, thirdDocument) => | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -447,6 +447,42 @@ object ElasticQuery { | |||||
final def matchPhrase(field: String, value: String): MatchPhraseQuery[Any] = | ||||||
MatchPhrase(field = field, value = value, boost = None) | ||||||
|
||||||
/** | ||||||
* Constructs a type-safe instance of [[zio.elasticsearch.query.MatchPhrasePrefixQuery]] using the specified | ||||||
* parameters. [[zio.elasticsearch.query.MatchPhrasePrefixQuery]] returns documents that contain the words of a | ||||||
* provided text, in the same order as provided. The last term of the provided text is treated as a prefix, matching | ||||||
* any words that begin with that term. | ||||||
* | ||||||
* @param field | ||||||
* the type-safe field for which query is specified for | ||||||
* @param value | ||||||
* the value to be matched, represented by an instance of type `String` | ||||||
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.
Suggested change
|
||||||
* @tparam S | ||||||
* document for which field query is executed | ||||||
* @return | ||||||
* an instance of [[zio.elasticsearch.query.MatchPhrasePrefixQuery]] that represents the match phrase prefix query | ||||||
* to be performed. | ||||||
*/ | ||||||
final def matchPhrasePrefix[S](field: Field[S, String], value: String): MatchPhrasePrefixQuery[S] = | ||||||
MatchPhrasePrefix(field = field.toString, value = value) | ||||||
|
||||||
/** | ||||||
* Constructs an instance of [[zio.elasticsearch.query.MatchPhrasePrefixQuery]] using the specified parameters. | ||||||
* [[zio.elasticsearch.query.MatchPhrasePrefixQuery]] returns documents that contain the words of a provided text, in | ||||||
* the same order as provided. The last term of the provided text is treated as a prefix, matching any words that | ||||||
* begin with that term. | ||||||
* | ||||||
* @param field | ||||||
* the field for which query is specified for | ||||||
* @param value | ||||||
* the value to be matched, represented by an instance of type `String` | ||||||
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.
Suggested change
|
||||||
* @return | ||||||
* an instance of [[zio.elasticsearch.query.MatchPhrasePrefixQuery]] that represents the match phrase prefix query | ||||||
* to be performed. | ||||||
*/ | ||||||
final def matchPhrasePrefix(field: String, value: String): MatchPhrasePrefixQuery[Any] = | ||||||
MatchPhrasePrefix(field = field, value = value) | ||||||
|
||||||
/** | ||||||
* Constructs a type-safe instance of [[zio.elasticsearch.query.BoolQuery]] with queries that must satisfy the | ||||||
* criteria using the specified parameters. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -584,6 +584,14 @@ private[elasticsearch] final case class MatchPhrase[S](field: String, value: Str | |
) | ||
} | ||
|
||
sealed trait MatchPhrasePrefixQuery[S] extends ElasticQuery[S] | ||
|
||
private[elasticsearch] final case class MatchPhrasePrefix[S](field: String, value: String) | ||
extends MatchPhrasePrefixQuery[S] { | ||
private[elasticsearch] def toJson(fieldPath: Option[String]): Json = | ||
Obj("match_phrase_prefix" -> Obj(fieldPath.foldRight(field)(_ + "." + _) -> value.toJson)) | ||
} | ||
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. Put empty line between. |
||
|
||
sealed trait NestedQuery[S] | ||
extends ElasticQuery[S] | ||
with HasIgnoreUnmapped[NestedQuery[S]] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -890,6 +890,15 @@ object ElasticQuerySpec extends ZIOSpecDefault { | |
equalTo(MatchPhrase[TestDocument](field = "stringField", value = "this is a test", boost = Some(3))) | ||
) | ||
}, | ||
test("matchPhrasePrefix") { | ||
val query = matchPhrasePrefix("stringField", "test") | ||
val queryTs = matchPhrasePrefix(TestDocument.stringField, "test") | ||
|
||
assert(query)(equalTo(MatchPhrasePrefix[Any](field = "stringField", value = "test"))) && | ||
assert(queryTs)( | ||
equalTo(MatchPhrasePrefix[TestDocument](field = "stringField", value = "test")) | ||
) | ||
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. Can this be in one line? |
||
}, | ||
test("nested") { | ||
val query = nested("testField", matchAll) | ||
val queryTs = nested(TestDocument.subDocumentList, matchAll) | ||
|
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.