Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions docs/overview/queries/elastic_query_match_phrase_prefix.md
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `MatchPhrasePrefix` returns documents that contain the words of a provided text, in the same order as provided.
The `MatchPhrasePrefix` query 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).
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Up @@ -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))
Copy link
Collaborator

@dbulaja98 dbulaja98 Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can add && !contains(...) for other document?

}
} @@ 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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* the value to be matched, represented by an instance of type `String`
* the text value to be matched

* @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`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* the value to be matched, represented by an instance of type `String`
* the text value to be matched

* @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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
Expand Down
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
'overview/queries/elastic_query_match',
'overview/queries/elastic_query_match_all',
'overview/queries/elastic_query_match_phrase',
'overview/queries/elastic_query_match_phrase_prefix',
'overview/queries/elastic_query_nested',
'overview/queries/elastic_query_prefix',
'overview/queries/elastic_query_range',
Expand Down