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

Support index pattern #316

Merged
merged 11 commits into from
Sep 20, 2023
Merged

Conversation

milicns
Copy link
Contributor

@milicns milicns commented Aug 30, 2023

Closes #310

Refresh, Search and GetById requests currently adjusted for handling IndexSelectors, i am not sure about GetById completely.

@milicns milicns changed the title Task support index pattern Support index pattern Aug 30, 2023
Copy link
Member

@drmarjanovic drmarjanovic left a comment

Choose a reason for hiding this comment

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

Can you provide more integration tests with using multiple indexes?

} yield assert(res)(equalTo(true))
},
test("successfully refresh all indices") {
assertZIO(Executor.execute(ElasticRequest.refresh(IndexPattern("_all"))))(equalTo(true))
Copy link
Member

Choose a reason for hiding this comment

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

You can use IndexPattern.All or whatever we choose to refactor it to.

import zio.prelude.Newtype

trait IndexSelector[A] {
def selectorString(a: A): String
Copy link
Member

Choose a reason for hiding this comment

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

I would use toSelector or asSelector.

object IndexSelector {

implicit object IndexNameSelector extends IndexSelector[IndexName] {
def selectorString(a: IndexName): String = IndexName.unwrap(a)
Copy link
Member

Choose a reason for hiding this comment

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

I would use toSelector(name: IndexName)...

}

implicit object IndexPatternSelector extends IndexSelector[IndexPattern] {
def selectorString(a: IndexPattern): String = IndexPattern.unwrap(a)
Copy link
Member

Choose a reason for hiding this comment

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

pattern instead of a.

}

implicit object MultiIndexSelector extends IndexSelector[MultiIndex] {
def selectorString(a: MultiIndex): String = a.indices.mkString(",")
Copy link
Member

Choose a reason for hiding this comment

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

Probably multi instead of a.

}

final case class MultiIndex private (indices: Chunk[String]) { self =>
def names(indexNames: IndexName*): MultiIndex =
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to support empty? Maybe def names(name: IndexName, names: IndexNames).

def names(indexNames: IndexName*): MultiIndex =
self.copy(indices = indices ++ Chunk.fromIterable(indexNames.map(IndexName.unwrap)))

def patterns(indexPatterns: IndexPattern*): MultiIndex =
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

}

object MultiIndex {
def names(indexNames: IndexName*): MultiIndex =
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

def names(indexNames: IndexName*): MultiIndex =
new MultiIndex(Chunk.fromIterable(indexNames.map(IndexName.unwrap)))

def patterns(indexPatterns: IndexPattern*): MultiIndex =
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

for {
_ <- Executor.execute(ElasticRequest.createIndex(createIndexTestName))
res <- Executor.execute(ElasticRequest.refresh(MultiIndex.names(index, createIndexTestName)))
} yield assert(res)(equalTo(true))
Copy link
Member

Choose a reason for hiding this comment

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

Let's replace all equalTo(true) with isTrue.

} yield assert(res)(equalTo(true))
},
test("successfully refresh all indices") {
assertZIO(Executor.execute(ElasticRequest.refresh(IndexPatternAll)))(equalTo(true))
},
test("return false if index does not exists") {
assertZIO(Executor.execute(ElasticRequest.refresh(refreshFailIndex)))(
equalTo(false)
Copy link
Member

Choose a reason for hiding this comment

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

I guess you can use isFalse here.

@@ -54,6 +54,8 @@ trait IntegrationSpec extends ZIOSpecDefault {

val refreshFailIndex: IndexName = IndexName("refresh-fail")

val IndexPatternAll: IndexPattern = IndexPattern("_all")
Copy link
Member

Choose a reason for hiding this comment

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

Can we use a constant?

)
)
}
})
Copy link
Member

Choose a reason for hiding this comment

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

Missing new line.

Comment on lines 33 to 37
If you want to create `GetById` request with `IndexPattern`, you can do that in the following manner:
```scala
val requestWithIndexPattern: GetByIdRequest = getById(index = IndexPattern("index*"), id = DocumentId("111"))
```

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe put this after this part To create a GetById request do the following:.

for {
_ <- Executor.execute(ElasticRequest.deleteByQuery(firstSearchIndex, matchAll))
_ <- Executor.execute(ElasticRequest.deleteByQuery(secondSearchIndex, matchAll))
document = firstDocument.copy(stringField = s"this is test")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since you named copy of secondDocument: secondDocumentCopy, you should call this one firstDocumentCopy.

object IndexSelector {

implicit object IndexNameSelector extends IndexSelector[IndexName] {
def toSelector(name: IndexName): String = IndexName.unwrap(name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Put implementation in the new line.

}

implicit object IndexPatternSelector extends IndexSelector[IndexPattern] {
def toSelector(pattern: IndexPattern): String = IndexPattern.unwrap(pattern)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

}

implicit object MultiIndexSelector extends IndexSelector[MultiIndex] {
def toSelector(multi: MultiIndex): String = multi.indices.mkString(",")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

new MultiIndex(Chunk.fromIterable(name.toString +: names.map(IndexName.unwrap)))

def patterns(pattern: IndexPattern, patterns: IndexPattern*): MultiIndex =
new MultiIndex(Chunk.fromIterable(pattern.toString +: patterns.map(IndexPattern.unwrap)))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Omit new.


def patterns(pattern: IndexPattern, patterns: IndexPattern*): MultiIndex =
new MultiIndex(Chunk.fromIterable(pattern.toString +: patterns.map(IndexPattern.unwrap)))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Put an empty line at the end.

@@ -211,19 +212,19 @@ object ElasticRequest {
* @return
* an instance of [[GetByIdRequest]] that represents get by id operation to be performed.
*/
final def getById(index: IndexName, id: DocumentId): GetByIdRequest =
GetById(index = index, id = id, refresh = None, routing = None)
final def getById[I: IndexSelector](index: I, id: DocumentId): GetByIdRequest =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we update scaladoc for this method too? (index or more indices)

@@ -235,11 +236,11 @@ object ElasticRequest {
* @return
* an instance of [[SearchRequest]] that represents search operation to be performed.
*/
final def search(index: IndexName, query: ElasticQuery[_]): SearchRequest =
final def search[I: IndexSelector](index: I, query: ElasticQuery[_]): SearchRequest =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also here? (index or more indices)

@@ -261,16 +262,16 @@ object ElasticRequest {
* @return
* an instance of [[SearchAndAggregateRequest]] that represents search and aggregate operations to be performed.
*/
final def search(
index: IndexName,
final def search[I: IndexSelector](
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also here? (index or more indices)

dbulaja98

This comment was marked as resolved.

@milicns
Copy link
Contributor Author

milicns commented Aug 31, 2023

GetById request adjusted back to supporting only IndexName, because it seems that it doesn't support IndexPattern https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-get.html

for {
_ <- Executor.execute(ElasticRequest.deleteByQuery(firstSearchIndex, matchAll))
_ <- Executor.execute(ElasticRequest.deleteByQuery(secondSearchIndex, matchAll))
firstDocumentCopy = firstDocument.copy(stringField = s"this is test")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
firstDocumentCopy = firstDocument.copy(stringField = s"this is test")
firstDocumentCopy = firstDocument.copy(stringField = "this is test")

.upsert[TestDocument](firstSearchIndex, firstDocumentId, firstDocumentCopy)
.refreshTrue
)
secondDocumentCopy = secondDocument.copy(stringField = s"this is test")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
secondDocumentCopy = secondDocument.copy(stringField = s"this is test")
secondDocumentCopy = secondDocument.copy(stringField = "this is test")

for {
_ <- Executor.execute(ElasticRequest.deleteByQuery(firstSearchIndex, matchAll))
_ <- Executor.execute(ElasticRequest.deleteByQuery(secondSearchIndex, matchAll))
firstDocumentCopy = firstDocument.copy(stringField = s"this is test")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
firstDocumentCopy = firstDocument.copy(stringField = s"this is test")
firstDocumentCopy = firstDocument.copy(stringField = "this is test")

.upsert[TestDocument](firstSearchIndex, firstDocumentId, firstDocumentCopy)
.refreshTrue
)
secondDocumentCopy = secondDocument.copy(stringField = s"this is test")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
secondDocumentCopy = secondDocument.copy(stringField = s"this is test")
secondDocumentCopy = secondDocument.copy(stringField = "this is test")

@@ -717,7 +718,7 @@ object ElasticRequest {
aggregation: ElasticAggregation,
excluded: Chunk[String],
included: Chunk[String],
index: IndexName,
index: String,
Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking about renaming this to "selector" or "selectors" and using Chunk[String] instead. Also, I would probably use the same name and type for all underlying private classes in the ElasticRequest.

@arnoldlacko @dbulaja98

Copy link
Collaborator

@dbulaja98 dbulaja98 Sep 1, 2023

Choose a reason for hiding this comment

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

Maybe we should consider using both "selector" and "selectors" for places we have one index, and places where we can have more than one.

drmarjanovic
drmarjanovic previously approved these changes Sep 2, 2023
@@ -9,11 +9,21 @@ To create a `Aggregate` request do the following:
```scala
import zio.elasticsearch.ElasticRequest.AggregateRequest
import zio.elasticsearch.ElasticRequest.aggregate
// this import is required for using `IndexName`
// this import is required for using `IndexName, IndexPattern, MultiIndex`
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
// this import is required for using `IndexName, IndexPattern, MultiIndex`
// this import is required for using `IndexName, IndexPattern and MultiIndex`

@@ -13,11 +13,11 @@ To create a `Search` request do the following:
```scala
import zio.elasticsearch.ElasticRequest.SearchRequest
import zio.elasticsearch.ElasticRequest.search
// this import is required for using `IndexName`
// this import is required for using `IndexName, IndexPattern, MultiIndex`
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
// this import is required for using `IndexName, IndexPattern, MultiIndex`
// this import is required for using `IndexName, IndexPattern and MultiIndex`


If you want to create `Search` request with `MultiIndex`, do the following:
```scala
val requestWithMultiIndex: SearchRequest = search(selectors = MultiIndex.names(IndexName("index1"), IndexName("index2")), query = matchAll)
```

You can find more information about `Search` and `SearchAndAggregate` requests [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-search.html).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add new line at the end of the file?


}

final case class MultiIndex private[elasticsearch] (indices: Chunk[String]) { self =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you make this private[elasticsearch]?

Comment on lines +24 to +25
object IndexPatternValidator
extends Validator[String](pattern => {
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?


}

final case class MultiIndex private[elasticsearch] (indices: Chunk[String]) { self =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here - private[es].

* @param index
* the name of the Elasticsearch index to aggregate on
* @param selectors
* the name of the index or more indices to be refreshed
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 name of the index or more indices to be refreshed
* the name of the index or more indices to aggregate on

Comment on lines +255 to +256
assert(getByIdRequest)(
equalTo(GetById(index = Index, id = DocId, refresh = None, routing = None))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Revert this to one line.

Comment on lines +264 to +266
equalTo(
GetById(index = Index, id = DocId, refresh = Some(true), routing = Some(RoutingValue))
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

@dbulaja98 dbulaja98 merged commit a03ace4 into lambdaworks:main Sep 20, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to do an Index Pattern search/lookup
3 participants