diff --git a/docs/overview/queries/elastic_query_bool.md b/docs/overview/queries/elastic_query_bool.md index 8200ed14c..7f4577a30 100644 --- a/docs/overview/queries/elastic_query_bool.md +++ b/docs/overview/queries/elastic_query_bool.md @@ -55,4 +55,4 @@ If you want to change the `minimum_should_match` parameter, you can use the `min val queryWithMinimumShouldMatch: BoolQuery = should(contains(field = Document.name, value = "a")).minimumShouldMatch(2) ``` -You can find more information about Boolean Query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-bool-query.html). +You can find more information about Boolean query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-bool-query.html). diff --git a/docs/overview/queries/elastic_query_range.md b/docs/overview/queries/elastic_query_range.md index 997369e96..ee679beb2 100644 --- a/docs/overview/queries/elastic_query_range.md +++ b/docs/overview/queries/elastic_query_range.md @@ -51,4 +51,4 @@ If you want to change `lte` (less than or equal to), you can use the `lte` metho val queryWithLte: RangeQuery = range(field = Document.intField).lte(100) ``` -You can find more information about Range Query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-range-query.html). +You can find more information about Range query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-range-query.html). diff --git a/docs/overview/queries/elastic_query_term.md b/docs/overview/queries/elastic_query_term.md index d87593a8f..63ade9c25 100644 --- a/docs/overview/queries/elastic_query_term.md +++ b/docs/overview/queries/elastic_query_term.md @@ -33,4 +33,4 @@ val queryWithCaseInsensitiveFalse: TermQuery = term(field = Document.name, value val queryWithCaseInsensitiveTrue: TermQuery = term(field = Document.name, value = "test").caseInsensitiveTrue ``` -You can find more information about `Term` Query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-term-query.html). +You can find more information about `Term` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-term-query.html). diff --git a/docs/overview/queries/elastic_query_terms.md b/docs/overview/queries/elastic_query_terms.md index 4acbf7814..e8115d5f0 100644 --- a/docs/overview/queries/elastic_query_terms.md +++ b/docs/overview/queries/elastic_query_terms.md @@ -27,4 +27,4 @@ If you want to change the `boost`, you can use `boost` method: val queryWithBoost: TermsQuery = terms(field = "name", "a", "b", "c").boost(2.0) ``` -You can find more information about `Terms` Query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-terms-query.html). +You can find more information about `Terms` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-terms-query.html). diff --git a/docs/overview/queries/elastic_query_wildcard.md b/docs/overview/queries/elastic_query_wildcard.md index c32d4d1de..c96189ccb 100644 --- a/docs/overview/queries/elastic_query_wildcard.md +++ b/docs/overview/queries/elastic_query_wildcard.md @@ -41,4 +41,4 @@ val queryWithCaseInsensitiveFalse: WildcardQuery = contains(field = Document.nam val queryWithCaseInsensitiveTrue: WildcardQuery = contains(field = Document.name, value = "a").caseInsensitiveTrue ``` -You can find more information about `Wildcard` Query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-wildcard-query.html#query-dsl-wildcard-query). +You can find more information about `Wildcard` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-wildcard-query.html#query-dsl-wildcard-query). diff --git a/docs/overview/requests/elastic_request_aggregate.md b/docs/overview/requests/elastic_request_aggregate.md index 517d26dc4..a24fb8f90 100644 --- a/docs/overview/requests/elastic_request_aggregate.md +++ b/docs/overview/requests/elastic_request_aggregate.md @@ -3,4 +3,17 @@ id: elastic_request_aggregate title: "Aggregation Request" --- -TBD +This request is used to create aggregations which summarizes your data as metrics, statistics, or other analytics. + +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` +import zio.elasticsearch._ +import zio.elasticsearch.ElasticAggregation._ + +val request: AggregateRequest = aggregate(name = IndexName("index"), aggregation = maxAggregation(name = "aggregation", field = "intField")) +``` + +You can find more information about `Aggregate` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations.html). diff --git a/docs/overview/requests/elastic_request_bulk.md b/docs/overview/requests/elastic_request_bulk.md index 2bd075a60..4a8f9fcb1 100644 --- a/docs/overview/requests/elastic_request_bulk.md +++ b/docs/overview/requests/elastic_request_bulk.md @@ -3,4 +3,40 @@ id: elastic_request_bulk title: "Bulk Request" --- -TBD +The `Bulk` request performs multiple indexing or delete operations in a single API call. This reduces overhead and can greatly increase indexing speed. + +In order to use the `Bulk` request import the following: +```scala +import zio.elasticsearch.ElasticRequest.BulkRequest +import zio.elasticsearch.ElasticRequest.bulk +``` + +You can create a `Bulk` request using the `bulk` method this way: +```scala +// this import is required for using `IndexName` and `DocumentId` +import zio.elasticsearch._ + +val index = Index("index") + +val document1 = new Document(id = DocumentId("111"), intField = 1, stringField = "stringField1") +val document2 = new Document(id = DocumentId("222"), intField = 2, stringField = "stringField2") + +val request: BulkRequest = bulk(create(index = index, doc = document1), upsert(index = index, id = DocumentId("111"), doc = document2)) +``` + +If you want to change the `refresh`, you can use `refresh`, `refreshFalse` or `refreshTrue` method: +```scala +val requestWithRefresh: BulkRequest = bulk(create(index = index, doc = document1), upsert(index = index, id = DocumentId("111"), doc = document2)).refresh(true) +val requestWithRefreshFalse: BulkRequest = bulk(create(index = index, doc = document1), upsert(index = index, id = DocumentId("111"), doc = document2)).refreshFalse +val requestWithRefreshTrue: BulkRequest = bulk(create(index = index, doc = document1), upsert(index = index, id = DocumentId("111"), doc = document2)).refreshTrue +``` + +If you want to change the `routing`, you can use the `routing` method: +```scala +// this import is required for using `Routing` also +import zio.elasticsearch._ + +val requestWithRouting: BulkRequest = bulk(create(index = index, doc = document1), upsert(index = index, id = DocumentId("111"), doc = document2)).routing(Routing("routing")) +``` + +You can find more information about `Bulk` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-bulk.html). diff --git a/docs/overview/requests/elastic_request_count.md b/docs/overview/requests/elastic_request_count.md index e147667f5..f56c13512 100644 --- a/docs/overview/requests/elastic_request_count.md +++ b/docs/overview/requests/elastic_request_count.md @@ -3,4 +3,35 @@ id: elastic_request_count title: "Count Request" --- -TBD +The `Count` request is used for getting the number of matches for a search query. If no query is specified, `matchAll` query will be used to count all the documents. + +In order to use the `Count` request import the following: +```scala +import zio.elasticsearch.ElasticRequest.CountRequest +import zio.elasticsearch.ElasticRequest.count +``` + +You can create a `Count` request using the `count` method without specifying query this way: +```scala +// this import is required for using `IndexName` +import zio.elasticsearch._ + +val request: CountRequest = count(index = IndexName("index")) +``` + +You can create a `Count` request using the `count` method with specified query this way: +```scala +import zio.elasticsearch.ElasticQuery._ + +val request: CountRequest = count(index = IndexName("index"), query = contains(field = Document.name, value = "test")) +``` + +If you want to change the `routing`, you can use the `routing` method: +```scala +// this import is required for using `Routing` also +import zio.elasticsearch._ + +val requestWithRouting: CountRequest = count(index = Index("index")).routing(Routing("routing")) +``` + +You can find more information about `Count` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-count.html). diff --git a/docs/overview/requests/elastic_request_create.md b/docs/overview/requests/elastic_request_create.md index c67ca33aa..05c616b86 100644 --- a/docs/overview/requests/elastic_request_create.md +++ b/docs/overview/requests/elastic_request_create.md @@ -1,6 +1,82 @@ --- id: elastic_request_create -title: "Create Request" +title: "Create Request, CreateWithId Request and CreateOrUpdate Request" --- -TBD +The `Create`, the `CreateWithId` and the `CreateOrUpdate` requests add a JSON document to the specified data stream and make it searchable. + +There are three ways of adding documents to the Elasticsearch index: +1. By using `Create` request - creates a JSON document without specifying ID (Elasticsearch creates one) +2. By using `CreateWithId` request - creates a JSON document with specified ID +3. By using `CreateOrUpdate` request - creates JSON document with specified ID, or updates the document (if it already exists) + +In order to use the `Create` request import the following: +```scala +import zio.elasticsearch.ElasticRequest.CreateRequest +import zio.elasticsearch.ElasticRequest.create +``` + +In order to use the `CreateWithId` request import the following: +```scala +import zio.elasticsearch.ElasticRequest.CreateWithIdRequest +import zio.elasticsearch.ElasticRequest.create +``` + +In order to use the `CreateOrUpdate` request import the following: +```scala +import zio.elasticsearch.ElasticRequest.CreateOrUpdateRequest +import zio.elasticsearch.ElasticRequest.upsert +``` + +Except imports, you must specify a document you want to create, with its implicit schema. +```scala +import zio.schema.Schema +// example of document +final case class User(id: String, username: String) + +val user: User = User(id = "1", username = "johndoe") + +implicit val schema: Schema.CaseClass2[String, String, User] = DeriveSchema.gen[GitHubRepo] +``` + +You can create a `Create` request using the `create` method this way: +```scala +// this import is required for using `IndexName` +import zio.elasticsearch._ + +val request: CreateRequest = create(index = IndexName("index"), doc = user) +``` + +You can create a `CreateWithId` request using the `create` method this way: +```scala +// this import is required for using `DocumentId` also +import zio.elasticsearch._ + +val request: CreateWithIdRequest = create(index = IndexName("index"), id = DocumentId("documentId"), doc = user) +``` + +You can create a `CreateOrUpdate` request using the `upsert` method this way: +```scala +import zio.elasticsearch._ + +val request: CreateOrUpdateRequest = upsert(index = IndexName("index"), id = DocumentId("documentId"), doc = user) +``` + +If you want to change the `refresh`, you can use `refresh`, `refreshFalse` or `refreshTrue` method on any of previously mentioned requests: +```scala +val requestWithRefresh: CreateRequest = create(index = IndexName("index"), doc = user).refresh(true) +val requestWithRefreshFalse: CreateWithIdRequest = create(index = IndexName("index"), id = DocumentId("documentId"), doc = user).refreshFalse +val requestWithRefreshTrue: CreateOrUpdateRequest = upsert(index = IndexName("index"), id = DocumentId("documentId"), doc = user).refreshTrue +``` + +If you want to change the `routing`, you can use the `routing` method on any of previously mentioned requests: +```scala +// this import is required for using `Routing` also +import zio.elasticsearch._ + +val request1WithRouting: CreateRequest = create(index = IndexName("index"), doc = user).routing(Routing("routing")) +val request2WithRouting: CreateWithIdRequest = create(index = IndexName("index"), id = DocumentId("documentId"), doc = user).routing(Routing("routing")) +val request3WithRouting: CreateOrUpdateRequest = upsert(index = IndexName("index"), id = DocumentId("documentId"), doc = user).routing(Routing("routing")) +``` + +You can find more information about `Create`, `CreateWithId`, `CreateOrUpdate` requests [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-index_.html). diff --git a/docs/overview/requests/elastic_request_create_index.md b/docs/overview/requests/elastic_request_create_index.md index b96b8632e..41fc0808d 100644 --- a/docs/overview/requests/elastic_request_create_index.md +++ b/docs/overview/requests/elastic_request_create_index.md @@ -3,4 +3,25 @@ id: elastic_request_create_index title: "Create Index Request" --- -TBD +This request creates a new Elasticsearch index. + +In order to use the `CreateIndex` request import the following: +```scala +import zio.elasticsearch.ElasticRequest.CreateIndexRequest +import zio.elasticsearch.ElasticRequest.createIndex +``` + +You can create a `CreateIndex` request using the `createIndex` method in the following manner: +```scala +// this import is required for using `IndexName` +import zio.elasticsearch._ + +val request: CreateIndexRequest = createIndex(name = IndexName("index")) +``` + +You can also create a `CreateIndex` request using the `createIndex` method with the specific definition in the following manner: +```scala +val request: CreateIndexRequest = createIndex(name = IndexName("index"), definition = """{ "mappings": { "properties": { "subDocumentList": { "type": "nested" } } } }""") +``` + +You can find more information about `CreateIndex` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/indices-create-index.html). diff --git a/docs/overview/requests/elastic_request_delete_by_id.md b/docs/overview/requests/elastic_request_delete_by_id.md index b38f6fd30..559222839 100644 --- a/docs/overview/requests/elastic_request_delete_by_id.md +++ b/docs/overview/requests/elastic_request_delete_by_id.md @@ -3,4 +3,31 @@ id: elastic_request_delete_by_id title: "Delete By ID Request" --- -TBD +This query removes a JSON document from the specified index. + +To create a `DeleteById` request do the following: +```scala +import zio.elasticsearch.ElasticRequest.DeleteByIdRequest +import zio.elasticsearch.ElasticRequest.deleteById +// this import is required for using `IndexName` and `DocumentId` +import zio.elasticsearch._ + +val request: DeleteByIdRequest = deleteById(index = IndexName("index"), id = DocumentId("111")) +``` + +If you want to change the `refresh`, you can use `refresh`, `refreshFalse` or `refreshTrue` method: +```scala +val requestWithRefresh: DeleteByIdRequest = deleteById(index = IndexName("index"), id = DocumentId("111")).refresh(true) +val requestWithRefreshFalse: DeleteByIdRequest = deleteById(index = IndexName("index"), id = DocumentId("111")).refreshFalse +val requestWithRefreshTrue: DeleteByIdRequest = deleteById(index = IndexName("index"), id = DocumentId("111")).refreshTrue +``` + +If you want to change the `routing`, you can use the `routing` method: +```scala +// this import is required for `Routing` also +import zio.elasticsearch._ + +val requestWithRouting: DeleteByIdRequest = deleteById(index = IndexName("index"), id = DocumentId("111")).routing(Routing("routing")) +``` + +You can find more information about `DeleteById` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-delete.html). diff --git a/docs/overview/requests/elastic_request_delete_by_query.md b/docs/overview/requests/elastic_request_delete_by_query.md index d5741e2a8..7fa56085a 100644 --- a/docs/overview/requests/elastic_request_delete_by_query.md +++ b/docs/overview/requests/elastic_request_delete_by_query.md @@ -3,4 +3,32 @@ id: elastic_request_delete_by_query title: "Delete By Query Request" --- -TBD +The `DeleteByQuery` request deletes documents that match the specified query. + +To create a `DeleteById` request do the following: +```scala +import zio.elasticsearch.ElasticRequest.DeleteByQueryRequest +import zio.elasticsearch.ElasticRequest.deleteByQuery +// this import is required for using `IndexName` +import zio.elasticsearch._ +import zio.elasticsearch.ElasticQuery._ + +val request: DeleteByQueryRequest = deleteByQuery(index = IndexName("index"), query = contains(field = Document.name, value = "test")) +``` + +If you want to change the `refresh`, you can use `refresh`, `refreshFalse` or `refreshTrue` method: +```scala +val requestWithRefresh: DeleteByQueryRequest = deleteByQuery(index = IndexName("index"), query = contains(field = Document.name, value = "test")).refresh(true) +val requestWithRefreshFalse: DeleteByQueryRequest = deleteByQuery(index = IndexName("index"), query = contains(field = Document.name, value = "test")).refreshFalse +val requestWithRefreshTrue: DeleteByQueryRequest = deleteByQuery(index = IndexName("index"), query = contains(field = Document.name, value = "test")).refreshTrue +``` + +If you want to change the `routing`, you can use the `routing` method: +```scala +// this import is required for `Routing` also +import zio.elasticsearch._ + +val requestWithRouting: DeleteByQueryRequest = deleteByQuery(index = IndexName("index"), query = contains(field = Document.name, value = "test")).routing(Routing("routing")) +``` + +You can find more information about `DeleteByQuery` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-delete-by-query.html). diff --git a/docs/overview/requests/elastic_request_delete_index.md b/docs/overview/requests/elastic_request_delete_index.md index fec4fb18b..cabeca6cb 100644 --- a/docs/overview/requests/elastic_request_delete_index.md +++ b/docs/overview/requests/elastic_request_delete_index.md @@ -3,4 +3,16 @@ id: elastic_request_delete_index title: "Delete Index Request" --- -TBD +This request deletes specified Elasticsearch index. + +To create a `DeleteById` request do the following: +```scala +import zio.elasticsearch.ElasticRequest.DeleteIndexRequest +import zio.elasticsearch.ElasticRequest.deleteIndex +// this import is required for using `IndexName` +import zio.elasticsearch._ + +val request: DeleteIndexRequest = deleteIndex(name = IndexName("index")) +``` + +You can find more information about `DeleteIndex` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/indices-delete-index.html). diff --git a/docs/overview/requests/elastic_request_exists.md b/docs/overview/requests/elastic_request_exists.md index 7e3de64c2..cc7701263 100644 --- a/docs/overview/requests/elastic_request_exists.md +++ b/docs/overview/requests/elastic_request_exists.md @@ -3,4 +3,24 @@ id: elastic_request_exists title: "Exists Request" --- -TBD +This request is used for checking whether document exists. + +To create a `Exists` request do the following: +```scala +import zio.elasticsearch.ElasticRequest.ExistsRequest +import zio.elasticsearch.ElasticRequest.exists +// this import is required for using `IndexName` and `DocumentId` +import zio.elasticsearch._ + +val request: ExistsRequest = exists(index = IndexName("index"), id = DocumentId("111")) +``` + +If you want to change the `routing`, you can use the `routing` method: +```scala +// this import is required for `Routing` also +import zio.elasticsearch._ + +val requestWithRouting: ExistsRequest = exists(index = IndexName("index"), id = DocumentId("111")).routing(Routing("routing")) +``` + +You can find more information about `Exists` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/indices-exists.html#indices-exists). diff --git a/docs/overview/requests/elastic_request_get_by_id.md b/docs/overview/requests/elastic_request_get_by_id.md index 294f51ab7..5325ff588 100644 --- a/docs/overview/requests/elastic_request_get_by_id.md +++ b/docs/overview/requests/elastic_request_get_by_id.md @@ -3,4 +3,31 @@ id: elastic_request_get_by_id title: "Get By ID Request" --- -TBD +The `GetById` request retrieves the specified JSON document from an Elasticsearch index. + +To create a `GetById` request do the following: +```scala +import zio.elasticsearch.ElasticRequest.GetByIdRequest +import zio.elasticsearch.ElasticRequest.getById +// this import is required for using `IndexName` and `DocumentId` +import zio.elasticsearch._ + +val request: ExistsRequest = getById(index = IndexName("index"), id = DocumentId("111")) +``` + +If you want to change the `refresh`, you can use `refresh`, `refreshFalse` or `refreshTrue` method: +```scala +val requestWithRefresh: GetByIdRequest = getById(index = IndexName("index"), id = DocumentId("111")).refresh(true) +val requestWithRefreshFalse: GetByIdRequest = getById(index = IndexName("index"), id = DocumentId("111")).refreshFalse +val requestWithRefreshTrue: GetByIdRequest = getById(index = IndexName("index"), id = DocumentId("111")).refreshTrue +``` + +If you want to change the `routing`, you can use the `routing` method: +```scala +// this import is required for `Routing` also +import zio.elasticsearch._ + +val requestWithRouting: GetByIdRequest = getById(index = IndexName("index"), id = DocumentId("111")).routing(Routing("routing")) +``` + +You can find more information about `GetById` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-get.html). diff --git a/docs/overview/requests/elastic_request_search.md b/docs/overview/requests/elastic_request_search.md index c498e465a..efc737c6a 100644 --- a/docs/overview/requests/elastic_request_search.md +++ b/docs/overview/requests/elastic_request_search.md @@ -3,4 +3,107 @@ id: elastic_request_search title: "Search Request" --- -TBD +The `Search` request allows you to execute a search query (and aggregation) and get back search hits that match the query. + +There are two ways of executing a search query: +1. By using `Search` request +2. By using `SearchAndAggregate` request + +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` +import zio.elasticsearch._ +import zio.elasticsearch.ElasticQuery._ + +val request: SearchRequest = search(index = IndexName("index"), query = matchAll) +``` + +To create a `SearchAndAggregate` request do the following: +```scala +import zio.elasticsearch.ElasticRequest.SearchAndAggregateRequest +import zio.elasticsearch.ElasticRequest.search +import zio.elasticsearch._ +import zio.elasticsearch.ElasticQuery._ +import zio.elasticsearch.ElasticAggregation._ + +val request: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")) +``` + +If you want to add aggregation to `SearchRequest`, you can use the `aggregate` method on it: +```scala +import zio.elasticsearch.ElasticAggregation._ + +val requestWithAggregation: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll).aggregate(aggregation = maxAggregation(name = "aggregation", field = "intField")) +``` + +If you want to change the `excludes`, you can use the `excludes` method on both requests: +```scala +val request1WithExcludes: SearchRequest = search(index = IndexName("index"), query = matchAll).excludes("longField") +val request2WithExcludes: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).excludes("longField", "intField") +// type-safe fields: +val request1TsWithExcludes: SearchRequest = search(index = IndexName("index"), query = matchAll).excludes(Document.longField) +val request2TsWithExcludes: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).excludes(Document.longField, Document.intField) +``` + +If you want to change the `from`, you can use the `from` method on both requests: +```scala +val request1WithFrom: SearchRequest = search(index = IndexName("index"), query = matchAll).from(2) +val request2WithFrom: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).from(2) +``` + +If you want to change the `highlight`, you can use the `highlights` method on both requests: +```scala +import zio.elasticsearch.ElasticHighlight.highlight + +val request1WithHighlights: SearchRequest = search(index = IndexName("index"), query = matchAll).highlights("intField") +val request2WithHighlights: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).highlights(Document.intField) +``` + +If you want to change the `includes`, you can use the `includes` method on both requests: +```scala +val request1WithIncludes: SearchRequest = search(index = IndexName("index"), query = matchAll).includes("longField") +val request2WithIncludes: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).includes("longField", "intField") +// type-safe fields: +val request1TsWithIncludes: SearchRequest = search(index = IndexName("index"), query = matchAll).includes(Document.longField) +val request2TsWithIncludes: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).includes(Document.longField, Document.intField) +// with schema +val request1WithIncludesSchema: SearchRequest = search(index = IndexName("index"), query = matchAll).includes[Document] +val request2WithIncludesSchema: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).includes[Document] +``` + +If you want to change the `routing`, you can use the `routing` method on both requests: +```scala +// this import is required for using `Routing` also +import zio.elasticsearch._ + +val request1WithRouting: SearchRequest = search(index = IndexName("index"), query = matchAll).routing(Routing("routing")) +val request2WithRouting: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).routing(Routing("routing")) +``` + +If you want to change the `search_after`, you can use the `searchAfter` method on both requests: +```scala +import zio.json.ast.Json.{Arr, Str} + +val request1WithSearchAfter: SearchRequest = search(index = IndexName("index"), query = matchAll).searchAfter(Arr(Str("12345"))) +val request2WithSearchAfter: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).searchAfter(Arr(Str("12345"))) +``` + +If you want to change the `size`, you can use the `size` method on both requests: +```scala +val request1WithSize: SearchRequest = search(index = IndexName("index"), query = matchAll).size(5) +val request2WithSize: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).size(5) +``` + +If you want to change the `sort`, you can use the `sort` method on both requests: +```scala +import zio.elasticsearch.ElasticSort.sortBy +import zio.elasticsearch.query.sort.SortOrder.Asc +import zio.elasticsearch.query.sort.Missing.First + +val request1WithSort: SearchRequest = search(index = IndexName("index"), query = matchAll).sort(sortBy(Document.intField).order(Asc)) +val request2WithSort: SearchAndAggregateRequest = search(index = IndexName("index"), query = matchAll, aggregation = maxAggregation(name = "aggregation", field = "intField")).sort(sortBy("intField").missing(First)) +``` + +You can find more information about `Search` and `SearchAndAggregate` requests [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-search.html). diff --git a/docs/overview/requests/elastic_request_update.md b/docs/overview/requests/elastic_request_update.md new file mode 100644 index 000000000..f74ac4237 --- /dev/null +++ b/docs/overview/requests/elastic_request_update.md @@ -0,0 +1,60 @@ +--- +id: elastic_request_update +title: "Update Request" +--- + +This request is used for updating a document either with script or with other document as parameter. + +In order to use the `Update` request import the following: +```scala +import zio.elasticsearch.ElasticRequest.UpdateRequest +import zio.elasticsearch.ElasticRequest._ +``` + +You can create a `Update` request using the `update` method with specified document this way: +```scala +// this import is required for using `IndexName` and `DocumentId` +import zio.elasticsearch._ +import zio.schema.Schema + +// example of document +final case class User(id: String, username: String) + +val user: User = User(id = "1", username = "johndoe") + +implicit val schema: Schema.CaseClass2[String, String, User] = DeriveSchema.gen[GitHubRepo] + +val request: UpdateRequest = update(index = IndexName("index"), id = DocumentId("documentId"), doc = user) +``` + +You can create a `Update` request using the `updateByScript` method with specified script this way: +```scala +import zio.elasticsearch._ +import zio.elasticsearch.script.Script + +val request: UpdateRequest = updateByScript(index = IndexName("index"), id = DocumentId("documentId"), script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)) +``` + +If you want to change the `upsert`, you can use the `orCreate` method: +```scala +val newUser: User = User(id = "2", username = "janedoe") + +val requestWithUpsert: UpdateRequest = update(index = IndexName("index"), id = DocumentId("documentId"), doc = user).orCreate(newUser) +``` + +If you want to change the `refresh`, you can use `refresh`, `refreshFalse` or `refreshTrue` method: +```scala +val requestWithRefresh: UpdateRequest = update(index = IndexName("index"), id = DocumentId("documentId"), doc = user).refresh(true) +val requestWithRefreshFalse: UpdateRequest = update(index = IndexName("index"), id = DocumentId("documentId"), doc = user).refreshFalse +val requestWithRefreshTrue: UpdateRequest = update(index = IndexName("index"), id = DocumentId("documentId"), doc = user).refreshTrue +``` + +If you want to change the `routing`, you can use the `routing` method: +```scala +// this import is required for using `Routing` also +import zio.elasticsearch._ + +val requestWithRouting: UpdateRequest = update(index = IndexName("index"), id = DocumentId("documentId"), doc = user).routing(Routing("routing")) +``` + +You can find more information about `Update` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-update.html). diff --git a/docs/overview/requests/elastic_request_update_by_query.md b/docs/overview/requests/elastic_request_update_by_query.md new file mode 100644 index 000000000..b831fa3ca --- /dev/null +++ b/docs/overview/requests/elastic_request_update_by_query.md @@ -0,0 +1,54 @@ +--- +id: elastic_request_update_by_query +title: "Update By Query Request" +--- + +The `UpdateByQuery` request updates documents that match the specified query. If no query is specified, performs an update on every document in the specified Elasticsearch index. + +In order to use the `UpdateByQuery` request import the following: +```scala +import zio.elasticsearch.ElasticRequest.UpdateByQueryRequest +import zio.elasticsearch.ElasticRequest._ +``` + +You can create a `UpdateByQuery` request using the `updateAllByQuery` method in the following manner: +```scala +// this import is required for using `IndexName` +import zio.elasticsearch._ +import zio.elasticsearch.script.Script + +val request: UpdateByQueryRequest = updateAllByQuery(index = IndexName("index"), script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)) +``` + +You can create a `UpdateByQuery` request using the `updateByQuery` method in the following manner: +```scala +import zio.elasticsearch._ +import zio.elasticsearch.script.Script +import zio.elasticsearch.ElasticQuery._ + +val request: UpdateByQueryRequest = updateByQuery(index = IndexName("index"), query = contains(field = Document.name, value = "test"), script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)) +``` + +If you want to change the `conflicts`, you can use the `conflicts` method: +```scala +import zio.elasticsearch.request.UpdateConflicts.Proceed + +val requestWithConflicts: UpdateByQueryRequest = updateAllByQuery(index = IndexName("index"), script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)).conflicts(Proceed) +``` + +If you want to change the `refresh`, you can use `refresh`, `refreshFalse` or `refreshTrue` method: +```scala +val requestWithRefresh: UpdateByQueryRequest = updateAllByQuery(index = IndexName("index"), script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)).refresh(true) +val requestWithRefreshFalse: UpdateByQueryRequest = updateAllByQuery(index = IndexName("index"), script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)).refreshFalse +val requestWithRefreshTrue: UpdateByQueryRequest = updateAllByQuery(index = IndexName("index"), script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)).refreshTrue +``` + +If you want to change the `routing`, you can use the `routing` method on any of previously mentioned methods: +```scala +// this import is required for using `Routing` also +import zio.elasticsearch._ + +val requestWithRouting: UpdateByQueryRequest = updateAllByQuery(index = IndexName("index"), script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)).routing(Routing("routing")) +``` + +You can find more information about `UpdateByQuery` request [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-update-by-query.html). diff --git a/docs/overview/requests/elastic_request_upsert.md b/docs/overview/requests/elastic_request_upsert.md deleted file mode 100644 index 179ecf3d4..000000000 --- a/docs/overview/requests/elastic_request_upsert.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -id: elastic_request_upsert -title: "Upsert Request" ---- - -TBD diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index 6ae862afb..25714141b 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -188,16 +188,16 @@ object ElasticRequest { DeleteIndex(name = name) /** - * Constructs an instance of [[ExistRequest]] used for checking whether document exists. + * Constructs an instance of [[ExistsRequest]] used for checking whether document exists. * * @param index * the name of the index where the document may be located * @param id * the ID of the document to check for existence * @return - * an instance of [[ExistRequest]] that represents exists operation to be performed. + * an instance of [[ExistsRequest]] that represents exists operation to be performed. */ - final def exists(index: IndexName, id: DocumentId): ExistRequest = + final def exists(index: IndexName, id: DocumentId): ExistsRequest = Exists(index = index, id = id, routing = None) /** @@ -541,14 +541,14 @@ object ElasticRequest { final case class DeleteIndex(name: IndexName) extends DeleteIndexRequest - sealed trait ExistRequest extends ElasticRequest[Boolean] with HasRouting[ExistRequest] + sealed trait ExistsRequest extends ElasticRequest[Boolean] with HasRouting[ExistsRequest] private[elasticsearch] final case class Exists( index: IndexName, id: DocumentId, routing: Option[Routing] - ) extends ExistRequest { self => - def routing(value: Routing): ExistRequest = + ) extends ExistsRequest { self => + def routing(value: Routing): ExistsRequest = self.copy(routing = Some(value)) } @@ -737,12 +737,12 @@ object ElasticRequest { def routing(value: Routing): SearchAndAggregateRequest = self.copy(routing = Some(value)) - def size(value: Int): SearchAndAggregateRequest = - self.copy(size = Some(value)) - def searchAfter(value: Json): SearchAndAggregateRequest = self.copy(searchAfter = Some(value)) + def size(value: Int): SearchAndAggregateRequest = + self.copy(size = Some(value)) + def sort(sort: Sort, sorts: Sort*): SearchAndAggregateRequest = self.copy(sortBy = sortBy ++ (sort +: sorts)) diff --git a/website/sidebars.js b/website/sidebars.js index a213a317d..0cbe8a573 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -53,7 +53,8 @@ module.exports = { 'overview/requests/elastic_request_exists', 'overview/requests/elastic_request_get_by_id', 'overview/requests/elastic_request_search', - 'overview/requests/elastic_request_upsert', + 'overview/requests/elastic_request_update', + 'overview/requests/elastic_request_update_by_query' ], }, 'overview/overview_zio_prelude_schema',