-
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
Support index pattern #316
Support index pattern #316
Conversation
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.
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)) |
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.
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 |
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.
I would use toSelector
or asSelector
.
object IndexSelector { | ||
|
||
implicit object IndexNameSelector extends IndexSelector[IndexName] { | ||
def selectorString(a: IndexName): String = IndexName.unwrap(a) |
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.
I would use toSelector(name: IndexName)
...
} | ||
|
||
implicit object IndexPatternSelector extends IndexSelector[IndexPattern] { | ||
def selectorString(a: IndexPattern): String = IndexPattern.unwrap(a) |
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.
pattern
instead of a
.
} | ||
|
||
implicit object MultiIndexSelector extends IndexSelector[MultiIndex] { | ||
def selectorString(a: MultiIndex): String = a.indices.mkString(",") |
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.
Probably multi
instead of a
.
} | ||
|
||
final case class MultiIndex private (indices: Chunk[String]) { self => | ||
def names(indexNames: IndexName*): MultiIndex = |
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.
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 = |
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.
Same here.
} | ||
|
||
object MultiIndex { | ||
def names(indexNames: IndexName*): MultiIndex = |
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.
Same here.
def names(indexNames: IndexName*): MultiIndex = | ||
new MultiIndex(Chunk.fromIterable(indexNames.map(IndexName.unwrap))) | ||
|
||
def patterns(indexPatterns: IndexPattern*): MultiIndex = |
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.
Same here.
for { | ||
_ <- Executor.execute(ElasticRequest.createIndex(createIndexTestName)) | ||
res <- Executor.execute(ElasticRequest.refresh(MultiIndex.names(index, createIndexTestName))) | ||
} yield assert(res)(equalTo(true)) |
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.
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) |
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.
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") |
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.
Can we use a constant?
) | ||
) | ||
} | ||
}) |
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.
Missing new line.
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")) | ||
``` | ||
|
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.
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") |
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.
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) |
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.
Put implementation in the new line.
} | ||
|
||
implicit object IndexPatternSelector extends IndexSelector[IndexPattern] { | ||
def toSelector(pattern: IndexPattern): String = IndexPattern.unwrap(pattern) |
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.
Same here.
} | ||
|
||
implicit object MultiIndexSelector extends IndexSelector[MultiIndex] { | ||
def toSelector(multi: MultiIndex): String = multi.indices.mkString(",") |
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.
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))) |
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.
Omit new.
|
||
def patterns(pattern: IndexPattern, patterns: IndexPattern*): MultiIndex = | ||
new MultiIndex(Chunk.fromIterable(pattern.toString +: patterns.map(IndexPattern.unwrap))) | ||
} |
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.
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 = |
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.
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 = |
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.
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]( |
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.
Also here? (index or more indices)
GetById request adjusted back to supporting only |
for { | ||
_ <- Executor.execute(ElasticRequest.deleteByQuery(firstSearchIndex, matchAll)) | ||
_ <- Executor.execute(ElasticRequest.deleteByQuery(secondSearchIndex, matchAll)) | ||
firstDocumentCopy = firstDocument.copy(stringField = s"this is test") |
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.
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") |
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.
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") |
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.
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") |
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.
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, |
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.
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.
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.
Maybe we should consider using both "selector" and "selectors" for places we have one index, and places where we can have more than one.
@@ -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` |
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.
// 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` |
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.
// 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). |
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.
Can you add new line at the end of the file?
|
||
} | ||
|
||
final case class MultiIndex private[elasticsearch] (indices: Chunk[String]) { self => |
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.
Can you make this private[elasticsearch]?
object IndexPatternValidator | ||
extends Validator[String](pattern => { |
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.
Can this be in one line?
|
||
} | ||
|
||
final case class MultiIndex private[elasticsearch] (indices: Chunk[String]) { self => |
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.
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 |
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.
* the name of the index or more indices to be refreshed | |
* the name of the index or more indices to aggregate on |
assert(getByIdRequest)( | ||
equalTo(GetById(index = Index, id = DocId, refresh = None, routing = None)) |
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.
Revert this to one line.
equalTo( | ||
GetById(index = Index, id = DocId, refresh = Some(true), routing = Some(RoutingValue)) | ||
) |
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.
Same here.
Closes #310
Refresh, Search and GetById requests currently adjusted for handling IndexSelectors, i am not sure about GetById completely.