From 552a274f55422f51f1196c83427fca0eeb1c09b6 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Tue, 30 May 2023 16:41:32 +0200 Subject: [PATCH 1/2] Provide aggregations website documentation --- .../elastic_aggregation_bucket_selector.md | 22 ++ .../elastic_aggregation_bucket_sort.md | 38 ++++ .../elastic_aggregation_cardinality.md | 34 ++++ .../aggregations/elastic_aggregation_max.md | 35 ++++ .../aggregations/elastic_aggregation_terms.md | 48 ++++- docs/overview/elastic_aggregation.md | 87 +++++++- docs/overview/elastic_query.md | 2 + docs/overview/queries/elastic_query_bool.md | 2 +- .../queries/elastic_query_function_score.md | 189 ------------------ docs/overview/queries/elastic_query_range.md | 2 +- docs/overview/queries/elastic_query_term.md | 4 +- docs/overview/queries/elastic_query_terms.md | 6 +- .../queries/elastic_query_wildcard.md | 2 +- .../requests/elastic_request_aggregate.md | 15 +- .../overview/requests/elastic_request_bulk.md | 38 +--- .../requests/elastic_request_count.md | 33 +-- .../requests/elastic_request_create.md | 80 +------- .../requests/elastic_request_create_index.md | 23 +-- .../requests/elastic_request_delete_by_id.md | 29 +-- .../elastic_request_delete_by_query.md | 30 +-- .../requests/elastic_request_delete_index.md | 14 +- .../requests/elastic_request_exists.md | 22 +- .../requests/elastic_request_get_by_id.md | 29 +-- .../requests/elastic_request_search.md | 105 +--------- .../requests/elastic_request_update.md | 60 ------ .../elastic_request_update_by_query.md | 54 ----- .../requests/elastic_request_upsert.md | 6 + .../elasticsearch/ElasticAggregation.scala | 80 ++++---- website/sidebars.js | 4 + 29 files changed, 333 insertions(+), 760 deletions(-) create mode 100644 docs/overview/aggregations/elastic_aggregation_bucket_selector.md create mode 100644 docs/overview/aggregations/elastic_aggregation_bucket_sort.md create mode 100644 docs/overview/aggregations/elastic_aggregation_cardinality.md create mode 100644 docs/overview/aggregations/elastic_aggregation_max.md delete mode 100644 docs/overview/queries/elastic_query_function_score.md delete mode 100644 docs/overview/requests/elastic_request_update.md delete mode 100644 docs/overview/requests/elastic_request_update_by_query.md create mode 100644 docs/overview/requests/elastic_request_upsert.md diff --git a/docs/overview/aggregations/elastic_aggregation_bucket_selector.md b/docs/overview/aggregations/elastic_aggregation_bucket_selector.md new file mode 100644 index 000000000..7e1a05e9b --- /dev/null +++ b/docs/overview/aggregations/elastic_aggregation_bucket_selector.md @@ -0,0 +1,22 @@ +--- +id: elastic_aggregation_bucket_selector +title: "Bucket Selector Aggregation" +--- + +This aggregation is a parent pipeline aggregation which executes a script which determines whether the current bucket will be retained in the parent multi-bucket aggregation. + +To create a `BucketSelector` aggregation do the following: +```scala +import zio.elasticsearch.aggregation.BucketSelectorAggregation +import zio.elasticsearch.ElasticAggregation.bucketSelectorAggregation +import zio.elasticsearch.script.Script + +val aggregation: BucketSelectorAggregation = bucketSelectorAggregation(name = "aggregationSelector", script = Script("params.value > 10"), bucketsPath = Map("value" -> "otherAggregation")) +``` + +If you want to add aggregation (on the same level), you can use `withAgg` method: +```scala +val multipleAggregations: MultipleAggregations = bucketSelectorAggregation(name = "aggregationSelector", script = Script("params.value > 10"), bucketsPath = Map("value" -> "otherAggregation")).withAgg(maxAggregation(name = "maxAggregation", field = Document.doubleField)) +``` + +You can find more information about `BucketSelector` aggregation [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-pipeline-bucket-selector-aggregation.html). diff --git a/docs/overview/aggregations/elastic_aggregation_bucket_sort.md b/docs/overview/aggregations/elastic_aggregation_bucket_sort.md new file mode 100644 index 000000000..2e6f72a74 --- /dev/null +++ b/docs/overview/aggregations/elastic_aggregation_bucket_sort.md @@ -0,0 +1,38 @@ +--- +id: elastic_aggregation_bucket_sort +title: "Bucket Sort Aggregation" +--- + +The `BucketSort` aggregation is a parent pipeline aggregation which sorts the buckets of its parent multi-bucket aggregation. Zero or more sort fields may be specified together with the corresponding sort order. + +To create a `BucketSort` aggregation do the following: +```scala +import zio.elasticsearch.aggregation.BucketSortAggregation +import zio.elasticsearch.ElasticAggregation.bucketSortAggregation + +val aggregation: BucketSortAggregation = bucketSortAggregation(name = "aggregationSort") +``` + +If you want to change the `from`, you can use `from` method: +```scala +val aggregationWithFrom: BucketSortAggregation = bucketSortAggregation(name = "aggregationSort").from(5) +``` + +If you want to change the `size`, you can use `size` method: +```scala +val aggregationWithSize: BucketSortAggregation = bucketSortAggregation(name = "aggregationSort").size(5) +``` + +If you want to change the `sort`, you can use `sort` method: +```scala +import zio.elasticsearch.query.sort.SortByField.{byCount, byKey} + +val aggregationWithSort: BucketSortAggregation = bucketSortAggregation(name = "aggregationSort").sort(byCount, byKey) +``` + +If you want to add aggregation (on the same level), you can use `withAgg` method: +```scala +val multipleAggregations: MultipleAggregations = bucketSortAggregation(name = "aggregationSort").withAgg(maxAggregation(name = "maxAggregation", field = Document.doubleField)) +``` + +You can find more information about `BucketSort` aggregation [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-pipeline-bucket-sort-aggregation.html). diff --git a/docs/overview/aggregations/elastic_aggregation_cardinality.md b/docs/overview/aggregations/elastic_aggregation_cardinality.md new file mode 100644 index 000000000..f807ffc02 --- /dev/null +++ b/docs/overview/aggregations/elastic_aggregation_cardinality.md @@ -0,0 +1,34 @@ +--- +id: elastic_aggregation_cardinality +title: "Cardinality Aggregation" +--- + +The `Cardinality` aggregation is a single-value metrics aggregation that calculates an approximate count of distinct values. + +In order to use the `Cardinality` aggregation import the following: +```scala +import zio.elasticsearch.aggregation.CardinalityAggregation +import zio.elasticsearch.ElasticAggregation.cardinalityAggregation +``` + +You can create a `Cardinality` aggregation using the `cardinalityAggregation` method in the following manner: +```scala +val aggregation: CardinalityAggregation = cardinalityAggregation(name = "cardinalityAggregation", field = "intField") +``` + +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `Cardinality` aggregation using the `cardinalityAggregation` method in the following manner: +```scala +val aggregation: CardinalityAggregation = cardinalityAggregation(name = "cardinalityAggregation", field = Document.intField) +``` + +If you want to change the `missing`, you can use `missing` method: +```scala +val aggregationWithMissing: CardinalityAggregation = cardinalityAggregation(name = "cardinalityAggregation", field = Document.intField).missing(10.0) +``` + +If you want to add aggregation (on the same level), you can use `withAgg` method: +```scala +val multipleAggregations: MultipleAggregations = cardinalityAggregation(name = "cardinalityAggregation", field = Document.intField).withAgg(maxAggregation(name = "maxAggregation", field = Document.doubleField)) +``` + +You can find more information about `Cardinality` aggregation [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-metrics-cardinality-aggregation.html). diff --git a/docs/overview/aggregations/elastic_aggregation_max.md b/docs/overview/aggregations/elastic_aggregation_max.md new file mode 100644 index 000000000..fde2e41a2 --- /dev/null +++ b/docs/overview/aggregations/elastic_aggregation_max.md @@ -0,0 +1,35 @@ +--- +id: elastic_aggregation_max +title: "Max Aggregation" +--- + +The `Max` aggregation is a single-value metrics aggregation that keeps track and returns the maximum value among the numeric values extracted from the aggregated documents. + +In order to use the `Max` aggregation import the following: +```scala +import zio.elasticsearch.aggregation.MaxAggregation +import zio.elasticsearch.ElasticAggregation.maxAggregation +``` + +You can create a `Max` aggregation using the `maxAggregation` method this way: +```scala +val aggregation: MaxAggregation = maxAggregation(name = "maxAggregation", field = "intField") +``` + +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `Max` aggregation using the `maxAggregation` method this way: +```scala +// Document.intField must be number value, because of Max aggregation +val aggregation: MaxAggregation = maxAggregation(name = "maxAggregation", field = Document.intField) +``` + +If you want to change the `missing`, you can use `missing` method: +```scala +val aggregationWithMissing: MaxAggregation = maxAggregation(name = "maxAggregation", field = Document.intField).missing(10.0) +``` + +If you want to add aggregation (on the same level), you can use `withAgg` method: +```scala +val multipleAggregations: MultipleAggregations = maxAggregation(name = "maxAggregation1", field = Document.intField).withAgg(maxAggregation(name = "maxAggregation2", field = Document.doubleField)) +``` + +You can find more information about `Max` aggregation [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-metrics-max-aggregation.html). diff --git a/docs/overview/aggregations/elastic_aggregation_terms.md b/docs/overview/aggregations/elastic_aggregation_terms.md index 98c1b5158..a42b6d9f8 100644 --- a/docs/overview/aggregations/elastic_aggregation_terms.md +++ b/docs/overview/aggregations/elastic_aggregation_terms.md @@ -3,4 +3,50 @@ id: elastic_aggregation_terms title: "Terms Aggregation" --- -TBD +This aggregation is a multi-bucket value source based aggregation where buckets are dynamically built - one per unique value. + +In order to use the `Terms` aggregation import the following: +```scala +import zio.elasticsearch.aggregation.TermsAggregation +import zio.elasticsearch.ElasticAggregation.termsAggregation +``` + +You can create a `Terms` aggregation using the `termsAggregation` method this way: +```scala +val aggregation: TermsAggregation = termsAggregation(name = "termsAggregation", field = "stringField.keyword") +``` + +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `Terms` aggregation using the `termsAggregation` method this way: +```scala +// Document.stringField must be string value, because of Terms aggregation +val aggregation: TermsAggregation = termsAggregation(name = "termsAggregation", field = Document.stringField.keyword) +``` + +If you want to change the `order`, you can use `orderBy`, `orderByCountAsc`, `orderByCountDesc`, `orderByKeyAsc` or `orderByKeyDesc` method: +```scala +import zio.elasticsearch.aggregation.AggregationOrder +import zio.elasticsearch.query.sort.SortOrder.Asc + +val aggregationWithOrder1: TermsAggregation = termsAggregation(name = "termsAggregation", field = Document.stringField).orderBy(AggregationOrder("otherAggregation", Asc)) +val aggregationWithOrder2: TermsAggregation = termsAggregation(name = "termsAggregation", field = Document.stringField).orderByCountAsc +val aggregationWithOrder3: TermsAggregation = termsAggregation(name = "termsAggregation", field = Document.stringField).orderByCountDesc +val aggregationWithOrder4: TermsAggregation = termsAggregation(name = "termsAggregation", field = Document.stringField).orderByKeyAsc +val aggregationWithOrder5: TermsAggregation = termsAggregation(name = "termsAggregation", field = Document.stringField).orderByKeyDesc +``` + +If you want to change the `size`, you can use `size` method: +```scala +val aggregationWithSize: TermsAggregation = termsAggregation(name = "termsAggregation", field = Document.stringField).size(5) +``` + +If you want to add aggregation (on the same level), you can use `withAgg` method: +```scala +val multipleAggregations: MultipleAggregations = termsAggregation(name = "termsAggregation", field = Document.stringField).withAgg(maxAggregation(name = "maxAggregation", field = Document.intField)) +``` + +If you want to add another sub-aggregation, you can use `withSubAgg` method: +```scala +val aggregationWithSubAgg: TermsAggregation = termsAggregation(name = "termsAggregation", field = Document.stringField).withSubAgg(maxAggregation(name = "maxAggregation", field = Document.intField)) +``` + +You can find more information about `Terms` aggregation [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation). diff --git a/docs/overview/elastic_aggregation.md b/docs/overview/elastic_aggregation.md index 80fa018d5..48ca535fc 100644 --- a/docs/overview/elastic_aggregation.md +++ b/docs/overview/elastic_aggregation.md @@ -3,11 +3,94 @@ id: elastic_aggregation title: "Overview" --- -In order to execute Elasticsearch aggregation requests... +In order to execute Elasticsearch aggregation requests, you first must specify the type of the aggregation along with the corresponding parameters for that type. Aggregations are described with the `ElasticAggregation` data type, which can be constructed from the DSL methods found under the following import: ```scala import zio.elasticsearch.ElasticAggregation._ ``` -TBD +Aggregation DSL methods that require a field solely accept field types that are defined as Elasticsearch primitives. +You can pass field names simply as strings, or you can use the type-safe aggregation methods that make use of ZIO Schema's accessors. +An example with a `max` aggregation is shown below: + +```scala +import zio.elasticsearch.ElasticAggregation._ + +final case class User(id: Int, name: String, age: Int) + +object User { + implicit val schema: Schema.CaseClass3[Int, String, Int, User] = + DeriveSchema.gen[User] + + val (id, name, age) = schema.makeAccessors(FieldAccessorBuilder) +} + +maxAggregation(name = "maxAggregation", field = "age") + +// type-safe method +maxAggregation(name = "maxAggregation", field = User.age) +``` + +You can also represent a field from nested structures with type-safe aggregation methods, using the `/` operator on accessors: + +```scala +import zio._ +import zio.elasticsearch._ +import zio.elasticsearch.ElasticAggregation._ +import zio.schema.annotation.fieldName +import zio.schema.{DeriveSchema, Schema} + +final case class Name( + @fieldName("first_name") + firstName: String, + @fieldName("last_name") + lastName: String +) + +object Name { + implicit val schema: Schema.CaseClass2[String, String, Name] = DeriveSchema.gen[Name] + + val (firstName, lastName) = schema.makeAccessors(FieldAccessorBuilder) +} + +final case class User(id: String, name: Name, email: String, age: Int) + +object User { + implicit val schema: Schema.CaseClass4[String, Name, String, Int, User] = + DeriveSchema.gen[User] + + val (id, name, email, age) = schema.makeAccessors(FieldAccessorBuilder) +} + +termsAggregation(name = "termsAggregation", field = "name.first_name") + +// type-safe method +termsAggregation(name = "termsAggregation", field = User.name / Name.firstName) +``` + +Accessors also have a `suffix` method, in case you want to use one in aggregations: + +```scala +ElasticAggregation.cardinality(name = "cardinalityAggregation", field = "email.keyword") + +// type-safe method +ElasticAggregation.cardinality(name = "cardinalityAggregation", field = User.email.suffix("keyword")) +``` + +In case the suffix is `"keyword"` or `"raw"` you can use `keyword` and `raw` methods respectively. + +Now, after describing an aggregation, you can pass it to the `aggregate`/`search` method to obtain the `ElasticRequest` corresponding to that aggregation: + +```scala +import zio.elasticsearch.ElasticAggregation._ +import zio.elasticsearch.ElasticQuery._ + +ElasticRequest.aggregate(index = IndexName("index"), aggregation = termsAggregation(name = "termsAggregation", field = "name.first_name.keyword")) +ElasticRequest.search(IndexName("index"), query = matchAll, aggregation = termsAggregation(name = "termsAggregation", field = "name.first_name.keyword")) + +// type-safe methods +ElasticRequest.aggregate(index = IndexName("index"), aggregation = termsAggregation(name = "termsAggregation", field = User.name / Name.firstName.keyword)) +ElasticRequest.search(index = IndexName("index"), query = matchAll, aggregation = termsAggregation(name = "termsAggregation", field = User.name / Name.firstName.keyword)) + +``` diff --git a/docs/overview/elastic_query.md b/docs/overview/elastic_query.md index 644f10f6a..8f6899501 100644 --- a/docs/overview/elastic_query.md +++ b/docs/overview/elastic_query.md @@ -16,6 +16,8 @@ You can pass field names simply as strings, or you can use the type-safe query m An example with a `term` query is shown below: ```scala +import zio.elasticsearch.ElasticQuery._ + final case class User(id: Int, name: String) object User { diff --git a/docs/overview/queries/elastic_query_bool.md b/docs/overview/queries/elastic_query_bool.md index 7f4577a30..8200ed14c 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_function_score.md b/docs/overview/queries/elastic_query_function_score.md deleted file mode 100644 index 9a0b54b09..000000000 --- a/docs/overview/queries/elastic_query_function_score.md +++ /dev/null @@ -1,189 +0,0 @@ ---- -id: elastic_query_function_score -title: "Function Score Query" ---- - -The `FunctionScore` allows you to modify the score of documents that are retrieved by a query. - -In order to use the `FunctionScore` query and create needed `FunctionScoreFunction` import the following: -```scala -import zio.elasticsearch.query.FunctionScoreQuery -import zio.elasticsearch.query.FunctionScoreFunction._ -import zio.elasticsearch.ElasticQuery._ -``` - -For creating `FunctionScore` query you require `FunctionScoreFunction` or multiple of them. -You can create these functions in following way. - -
- -You can create `DecayFunction` with `DecayFunctionType.Exp` using the `expDecayFunction` method with origin and scale in the following manner: -```scala -val function: DecayFunction[Any] = expDecayFunction("field", origin = "11, 12", scale = "2km") -``` -You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) -`DecayFunction` with `DecayFunctionType.Exp` using the `expDecayFunction` method with origin and scale in the following manner: -```scala -val function: DecayFunction[Document] = expDecayFunction(field = Document.field, origin = "11, 12", scale = "2km") -``` - -
- -You can create `DecayFunction` with `DecayFunctionType.Gauss` using the `gaussDecayFunction` method with origin and scale in the following manner: -```scala -val function: DecayFunction[Any] = gaussDecayFunction(field = "field", origin = "11, 12", scale = "2km") -``` -You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) -`DecayFunction` with `DecayFunctionType.Gauss` using the `gaussDecayFunction` method with origin and scale in the following manner: -```scala -val function: DecayFunction[Document] = gaussDecayFunction(field = Document.field, origin = "11, 12", scale = "2km") -``` - -
- -You can create `DecayFunction` with `DecayFunctionType.Linear` using the `linearDecayFunction` method with origin and scale in the following manner: -```scala -val function: DecayFunction[Any] = linearDecayFunction(field = "field", origin = "11, 12", scale = "2km") -``` - -You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) -`DecayFunction` with `DecayFunctionType.Linear` using the `expDecayFunction` method with origin and scale in the following manner: -```scala -val function: DecayFunction[Document] = linearDecayFunction(field = Document.field, origin = "11, 12", scale = "2km") -``` - -
- -You can create `FieldValueFactor` using the `fieldValueFactor` method: - -```scala -val function: FieldValueFactor[Any] = fieldValueFactor(field = "field") -``` - -You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) -`FieldValueFactor` using the `fieldValueFactor` method: - -```scala -val function: FieldValueFactor[Document] = fieldValueFactor(field = Document.field) -``` - -
- -You can create `RandomScoreFunction` using the `randomScoreFunction` in three different ways depending on amount of parameters -you want to use: - -```scala -val function: RandomScoreFunction[Any] = randomScoreFunction() - -val function: RandomScoreFunction[Any] = randomScoreFunction(seed = 10) - -val function: RandomScoreFunction[Any] = randomScoreFunction(seed = 10, field = "field") -``` - -You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) -`RandomScoreFunction` using the `randomScoreFunction` method: -```scala -val function: RandomScoreFunction = randomScoreFunction(seed = 10, field = Document.field) -``` - -
- -You can create `ScriptScoreFunction` using the `scriptScoreFunction` method with script in following manner: - -```scala -val function: ScriptScoreFunction[Any] = scriptScoreFunction(script = Script("params.agg1 > 10")) -val function: ScriptScoreFunction[Any] = scriptScoreFunction(scriptSource = "params.agg1 > 10") -``` - -
- -You can create `WeightFunction` using the `weightFunction` method(you must provide `Any` type parameter when using): - -```scala -val function: WeightFunction[Any] = scriptScoreFunction(weight = 10) -``` - -

- -You can use these functions to create `FunctionScore` query using the `functionScore` method in the following manner: - -```scala -val randomScoreFunction: RandomScoreFunction[Any] = randomScoreFunction() -val weightFunction: WeightFunction[Any] = scriptScoreFunction(weight = 10) -val query: FunctionScoreQuery[Any] = functionScore(randomScoreFunction, weightFunction) -``` - -You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `FunctionScore` query -using the `functionScore`, if all functions are created type-safe, in the following manner: - -```scala -val decayFunction: DecayFunction[Document] = expDecayFunction(field = Document.field, origin = "11, 12", scale = "2km") -val randomScoreFunction: RandomScoreFunction[Document] = randomScoreFunction(seed = 10, field = Document.field) -val weightFunction: WeightFunction[Any] = scriptScoreFunction(weight = 10) -val query: FunctionScoreQuery[Document] = functionScore(decayFunction, randomScoreFunction, weightFunction) -``` - -
- -If you want to change the `boost`, you can use `boost` method: -```scala -import zio.elasticsearch.query.DistanceUnit - -val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).boost(5.0) -``` - -
- -If you want to change the `boostMode`, you can use `boostMode` method: -```scala -import zio.elasticsearch.query.DistanceUnit - -val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).boostMode(FunctionScoreBoostMode.Max) -``` - -
- -If you want to change the `maxBoost`, you can use `maxBoost` method: -```scala -import zio.elasticsearch.query.DistanceUnit - -val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).maxBoost(5.0) -``` - -
- -If you want to change the `minScore`, you can use `minScore` method: -```scala -import zio.elasticsearch.query.DistanceUnit - -val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).minScore(5.0) -``` - -
- -If you want to change the `query`, you can use `query` method: -```scala -import zio.elasticsearch.query.DistanceUnit - -val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).query(matches(Document.field, "value")) -``` - -
- -If you want to change the `scoreMode`, you can use `scoreMode` method: -```scala -import zio.elasticsearch.query.DistanceUnit - -val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).scoreMode(FunctionScoreScoreMode.Max) -``` - -
- -If you want to add a one or multiple new `FunctionScoreFunction` you can use `withFunctions` method: -```scala -import zio.elasticsearch.query.DistanceUnit - -val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).withFunctions(scriptScoreFunction(weight = 10)) -``` - -You can find more information about `FunctionScore` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-function-score-query.html). diff --git a/docs/overview/queries/elastic_query_range.md b/docs/overview/queries/elastic_query_range.md index ee679beb2..6f0b612c0 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 63ade9c25..7b49e5442 100644 --- a/docs/overview/queries/elastic_query_term.md +++ b/docs/overview/queries/elastic_query_term.md @@ -5,7 +5,7 @@ title: "Term Query" The `Term` query returns documents that contain an exact term in a provided field. -In order to use the `TermQuery` query import the following: +In order to use the `Term` query import the following: ```scala import zio.elasticsearch.query.TermQuery import zio.elasticsearch.ElasticQuery._ @@ -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 e8115d5f0..79e02490a 100644 --- a/docs/overview/queries/elastic_query_terms.md +++ b/docs/overview/queries/elastic_query_terms.md @@ -6,10 +6,10 @@ title: "Terms Query" The `Terms` query returns documents that contain one or more exact terms in a provided field. This query is the same as the Term query, except you can search for multiple values. -In order to use the `TermsQuery` query import the following: +In order to use the `Terms` query import the following: ```scala import zio.elasticsearch.query.TermsQuery -import zio.elasticsearch.ElasticQuery._ +import zio.elasticsearch.ElasticQuery.terms ``` You can create a `Terms` query using the `terms` method this way: @@ -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 c96189ccb..c32d4d1de 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 a24fb8f90..517d26dc4 100644 --- a/docs/overview/requests/elastic_request_aggregate.md +++ b/docs/overview/requests/elastic_request_aggregate.md @@ -3,17 +3,4 @@ id: elastic_request_aggregate title: "Aggregation Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_bulk.md b/docs/overview/requests/elastic_request_bulk.md index 4a8f9fcb1..2bd075a60 100644 --- a/docs/overview/requests/elastic_request_bulk.md +++ b/docs/overview/requests/elastic_request_bulk.md @@ -3,40 +3,4 @@ id: elastic_request_bulk title: "Bulk Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_count.md b/docs/overview/requests/elastic_request_count.md index f56c13512..e147667f5 100644 --- a/docs/overview/requests/elastic_request_count.md +++ b/docs/overview/requests/elastic_request_count.md @@ -3,35 +3,4 @@ id: elastic_request_count title: "Count Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_create.md b/docs/overview/requests/elastic_request_create.md index 05c616b86..c67ca33aa 100644 --- a/docs/overview/requests/elastic_request_create.md +++ b/docs/overview/requests/elastic_request_create.md @@ -1,82 +1,6 @@ --- id: elastic_request_create -title: "Create Request, CreateWithId Request and CreateOrUpdate Request" +title: "Create Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_create_index.md b/docs/overview/requests/elastic_request_create_index.md index 41fc0808d..b96b8632e 100644 --- a/docs/overview/requests/elastic_request_create_index.md +++ b/docs/overview/requests/elastic_request_create_index.md @@ -3,25 +3,4 @@ id: elastic_request_create_index title: "Create Index Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_delete_by_id.md b/docs/overview/requests/elastic_request_delete_by_id.md index 559222839..b38f6fd30 100644 --- a/docs/overview/requests/elastic_request_delete_by_id.md +++ b/docs/overview/requests/elastic_request_delete_by_id.md @@ -3,31 +3,4 @@ id: elastic_request_delete_by_id title: "Delete By ID Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_delete_by_query.md b/docs/overview/requests/elastic_request_delete_by_query.md index 7fa56085a..d5741e2a8 100644 --- a/docs/overview/requests/elastic_request_delete_by_query.md +++ b/docs/overview/requests/elastic_request_delete_by_query.md @@ -3,32 +3,4 @@ id: elastic_request_delete_by_query title: "Delete By Query Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_delete_index.md b/docs/overview/requests/elastic_request_delete_index.md index cabeca6cb..fec4fb18b 100644 --- a/docs/overview/requests/elastic_request_delete_index.md +++ b/docs/overview/requests/elastic_request_delete_index.md @@ -3,16 +3,4 @@ id: elastic_request_delete_index title: "Delete Index Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_exists.md b/docs/overview/requests/elastic_request_exists.md index cc7701263..7e3de64c2 100644 --- a/docs/overview/requests/elastic_request_exists.md +++ b/docs/overview/requests/elastic_request_exists.md @@ -3,24 +3,4 @@ id: elastic_request_exists title: "Exists Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_get_by_id.md b/docs/overview/requests/elastic_request_get_by_id.md index 5325ff588..294f51ab7 100644 --- a/docs/overview/requests/elastic_request_get_by_id.md +++ b/docs/overview/requests/elastic_request_get_by_id.md @@ -3,31 +3,4 @@ id: elastic_request_get_by_id title: "Get By ID Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_search.md b/docs/overview/requests/elastic_request_search.md index efc737c6a..c498e465a 100644 --- a/docs/overview/requests/elastic_request_search.md +++ b/docs/overview/requests/elastic_request_search.md @@ -3,107 +3,4 @@ id: elastic_request_search title: "Search Request" --- -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). +TBD diff --git a/docs/overview/requests/elastic_request_update.md b/docs/overview/requests/elastic_request_update.md deleted file mode 100644 index f74ac4237..000000000 --- a/docs/overview/requests/elastic_request_update.md +++ /dev/null @@ -1,60 +0,0 @@ ---- -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 deleted file mode 100644 index b831fa3ca..000000000 --- a/docs/overview/requests/elastic_request_update_by_query.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -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 new file mode 100644 index 000000000..179ecf3d4 --- /dev/null +++ b/docs/overview/requests/elastic_request_upsert.md @@ -0,0 +1,6 @@ +--- +id: elastic_request_upsert +title: "Upsert Request" +--- + +TBD diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticAggregation.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticAggregation.scala index 222417d99..e50bd4692 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticAggregation.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticAggregation.scala @@ -43,6 +43,18 @@ object ElasticAggregation { ): BucketSelectorAggregation = BucketSelector(name = name, script = script, bucketsPath = bucketsPath) + /** + * Constructs an instance of [[zio.elasticsearch.aggregation.BucketSortAggregation]] using the specified parameters. + * + * @param name + * aggregation name + * @return + * an instance of [[zio.elasticsearch.aggregation.BucketSortAggregation]] that represents bucket sort aggregation to + * be performed. + */ + final def bucketSortAggregation(name: String): BucketSortAggregation = + BucketSort(name = name, sortBy = Chunk.empty, from = None, size = None) + /** * Constructs a type-safe instance of [[zio.elasticsearch.aggregation.CardinalityAggregation]] using the specified * parameters. It calculates an approximate count of distinct values. @@ -73,6 +85,34 @@ object ElasticAggregation { final def cardinalityAggregation(name: String, field: String): CardinalityAggregation = Cardinality(name = name, field = field, missing = None) + /** + * Constructs a type-safe instance of [[zio.elasticsearch.aggregation.MaxAggregation]] using the specified parameters. + * + * @param name + * aggregation name + * @param field + * the type-safe field for which max aggregation will be executed + * @tparam A + * expected number type + * @return + * an instance of [[zio.elasticsearch.aggregation.MaxAggregation]] that represents max aggregation to be performed. + */ + final def maxAggregation[A: Numeric](name: String, field: Field[_, A]): MaxAggregation = + Max(name = name, field = field.toString, missing = None) + + /** + * Constructs an instance of [[zio.elasticsearch.aggregation.MaxAggregation]] using the specified parameters. + * + * @param name + * aggregation name + * @param field + * the field for which max aggregation will be executed + * @return + * an instance of [[zio.elasticsearch.aggregation.MaxAggregation]] that represents max aggregation to be performed. + */ + final def maxAggregation(name: String, field: String): MaxAggregation = + Max(name = name, field = field, missing = None) + /** * Constructs an empty instance of the [[zio.elasticsearch.aggregation.MultipleAggregations]]. * @@ -110,44 +150,4 @@ object ElasticAggregation { */ final def termsAggregation(name: String, field: String): TermsAggregation = Terms(name = name, field = field, order = Chunk.empty, subAggregations = Chunk.empty, size = None) - - /** - * Constructs a type-safe instance of [[zio.elasticsearch.aggregation.MaxAggregation]] using the specified parameters. - * - * @param name - * aggregation name - * @param field - * the type-safe field for which max aggregation will be executed - * @tparam A - * expected number type - * @return - * an instance of [[zio.elasticsearch.aggregation.MaxAggregation]] that represents max aggregation to be performed. - */ - final def maxAggregation[A: Numeric](name: String, field: Field[_, A]): MaxAggregation = - Max(name = name, field = field.toString, missing = None) - - /** - * Constructs an instance of [[zio.elasticsearch.aggregation.MaxAggregation]] using the specified parameters. - * - * @param name - * aggregation name - * @param field - * the field for which max aggregation will be executed - * @return - * an instance of [[zio.elasticsearch.aggregation.MaxAggregation]] that represents max aggregation to be performed. - */ - final def maxAggregation(name: String, field: String): MaxAggregation = - Max(name = name, field = field, missing = None) - - /** - * Constructs an instance of [[zio.elasticsearch.aggregation.BucketSortAggregation]] using the specified parameters. - * - * @param name - * aggregation name - * @return - * an instance of [[zio.elasticsearch.aggregation.BucketSortAggregation]] that represents bucket sort aggregation to - * be performed. - */ - final def bucketSortAggregation(name: String): BucketSortAggregation = - BucketSort(name = name, sortBy = Chunk.empty, from = None, size = None) } diff --git a/website/sidebars.js b/website/sidebars.js index 7c25fccce..64376675a 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -35,6 +35,10 @@ module.exports = { label: 'Elastic Aggregation', items: [ 'overview/elastic_aggregation', + 'overview/aggregations/elastic_aggregation_bucket_selector', + 'overview/aggregations/elastic_aggregation_bucket_sort', + 'overview/aggregations/elastic_aggregation_cardinality', + 'overview/aggregations/elastic_aggregation_max', 'overview/aggregations/elastic_aggregation_terms', ], }, From e1d3f80770dea7c4c646d071a8c5f3fec8c24907 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Tue, 30 May 2023 16:50:01 +0200 Subject: [PATCH 2/2] Dummy commit --- docs/overview/queries/elastic_query_bool.md | 2 +- docs/overview/queries/elastic_query_exists.md | 2 +- .../queries/elastic_query_function_score.md | 189 ++++++++++++++++++ docs/overview/queries/elastic_query_term.md | 2 +- docs/overview/queries/elastic_query_terms.md | 2 +- .../queries/elastic_query_wildcard.md | 2 +- .../requests/elastic_request_aggregate.md | 15 +- .../overview/requests/elastic_request_bulk.md | 38 +++- .../requests/elastic_request_count.md | 33 ++- .../requests/elastic_request_create.md | 80 +++++++- .../requests/elastic_request_create_index.md | 23 ++- .../requests/elastic_request_delete_by_id.md | 29 ++- .../elastic_request_delete_by_query.md | 30 ++- .../requests/elastic_request_delete_index.md | 14 +- .../requests/elastic_request_exists.md | 22 +- .../requests/elastic_request_get_by_id.md | 29 ++- .../requests/elastic_request_search.md | 105 +++++++++- .../requests/elastic_request_update.md | 60 ++++++ .../elastic_request_update_by_query.md | 54 +++++ .../requests/elastic_request_upsert.md | 6 - 20 files changed, 714 insertions(+), 23 deletions(-) create mode 100644 docs/overview/queries/elastic_query_function_score.md create mode 100644 docs/overview/requests/elastic_request_update.md create mode 100644 docs/overview/requests/elastic_request_update_by_query.md delete mode 100644 docs/overview/requests/elastic_request_upsert.md diff --git a/docs/overview/queries/elastic_query_bool.md b/docs/overview/queries/elastic_query_bool.md index 8200ed14c..ceef50a1c 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 `Bool` 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_exists.md b/docs/overview/queries/elastic_query_exists.md index fbcd2009e..37f7d1f1d 100644 --- a/docs/overview/queries/elastic_query_exists.md +++ b/docs/overview/queries/elastic_query_exists.md @@ -26,4 +26,4 @@ If you want to change the `boost`, you can use `boost` method: val queryWithBoost: ExistsQuery = exists(field = "name").boost(2.0) ``` -You can find more information about Exists query [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html#query-dsl-exists-query). +You can find more information about `Exists` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html#query-dsl-exists-query). diff --git a/docs/overview/queries/elastic_query_function_score.md b/docs/overview/queries/elastic_query_function_score.md new file mode 100644 index 000000000..9a0b54b09 --- /dev/null +++ b/docs/overview/queries/elastic_query_function_score.md @@ -0,0 +1,189 @@ +--- +id: elastic_query_function_score +title: "Function Score Query" +--- + +The `FunctionScore` allows you to modify the score of documents that are retrieved by a query. + +In order to use the `FunctionScore` query and create needed `FunctionScoreFunction` import the following: +```scala +import zio.elasticsearch.query.FunctionScoreQuery +import zio.elasticsearch.query.FunctionScoreFunction._ +import zio.elasticsearch.ElasticQuery._ +``` + +For creating `FunctionScore` query you require `FunctionScoreFunction` or multiple of them. +You can create these functions in following way. + +
+ +You can create `DecayFunction` with `DecayFunctionType.Exp` using the `expDecayFunction` method with origin and scale in the following manner: +```scala +val function: DecayFunction[Any] = expDecayFunction("field", origin = "11, 12", scale = "2km") +``` +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) +`DecayFunction` with `DecayFunctionType.Exp` using the `expDecayFunction` method with origin and scale in the following manner: +```scala +val function: DecayFunction[Document] = expDecayFunction(field = Document.field, origin = "11, 12", scale = "2km") +``` + +
+ +You can create `DecayFunction` with `DecayFunctionType.Gauss` using the `gaussDecayFunction` method with origin and scale in the following manner: +```scala +val function: DecayFunction[Any] = gaussDecayFunction(field = "field", origin = "11, 12", scale = "2km") +``` +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) +`DecayFunction` with `DecayFunctionType.Gauss` using the `gaussDecayFunction` method with origin and scale in the following manner: +```scala +val function: DecayFunction[Document] = gaussDecayFunction(field = Document.field, origin = "11, 12", scale = "2km") +``` + +
+ +You can create `DecayFunction` with `DecayFunctionType.Linear` using the `linearDecayFunction` method with origin and scale in the following manner: +```scala +val function: DecayFunction[Any] = linearDecayFunction(field = "field", origin = "11, 12", scale = "2km") +``` + +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) +`DecayFunction` with `DecayFunctionType.Linear` using the `expDecayFunction` method with origin and scale in the following manner: +```scala +val function: DecayFunction[Document] = linearDecayFunction(field = Document.field, origin = "11, 12", scale = "2km") +``` + +
+ +You can create `FieldValueFactor` using the `fieldValueFactor` method: + +```scala +val function: FieldValueFactor[Any] = fieldValueFactor(field = "field") +``` + +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) +`FieldValueFactor` using the `fieldValueFactor` method: + +```scala +val function: FieldValueFactor[Document] = fieldValueFactor(field = Document.field) +``` + +
+ +You can create `RandomScoreFunction` using the `randomScoreFunction` in three different ways depending on amount of parameters +you want to use: + +```scala +val function: RandomScoreFunction[Any] = randomScoreFunction() + +val function: RandomScoreFunction[Any] = randomScoreFunction(seed = 10) + +val function: RandomScoreFunction[Any] = randomScoreFunction(seed = 10, field = "field") +``` + +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) +`RandomScoreFunction` using the `randomScoreFunction` method: +```scala +val function: RandomScoreFunction = randomScoreFunction(seed = 10, field = Document.field) +``` + +
+ +You can create `ScriptScoreFunction` using the `scriptScoreFunction` method with script in following manner: + +```scala +val function: ScriptScoreFunction[Any] = scriptScoreFunction(script = Script("params.agg1 > 10")) +val function: ScriptScoreFunction[Any] = scriptScoreFunction(scriptSource = "params.agg1 > 10") +``` + +
+ +You can create `WeightFunction` using the `weightFunction` method(you must provide `Any` type parameter when using): + +```scala +val function: WeightFunction[Any] = scriptScoreFunction(weight = 10) +``` + +

+ +You can use these functions to create `FunctionScore` query using the `functionScore` method in the following manner: + +```scala +val randomScoreFunction: RandomScoreFunction[Any] = randomScoreFunction() +val weightFunction: WeightFunction[Any] = scriptScoreFunction(weight = 10) +val query: FunctionScoreQuery[Any] = functionScore(randomScoreFunction, weightFunction) +``` + +You can create a [type-safe](https://lambdaworks.github.io/zio-elasticsearch/overview/overview_zio_prelude_schema) `FunctionScore` query +using the `functionScore`, if all functions are created type-safe, in the following manner: + +```scala +val decayFunction: DecayFunction[Document] = expDecayFunction(field = Document.field, origin = "11, 12", scale = "2km") +val randomScoreFunction: RandomScoreFunction[Document] = randomScoreFunction(seed = 10, field = Document.field) +val weightFunction: WeightFunction[Any] = scriptScoreFunction(weight = 10) +val query: FunctionScoreQuery[Document] = functionScore(decayFunction, randomScoreFunction, weightFunction) +``` + +
+ +If you want to change the `boost`, you can use `boost` method: +```scala +import zio.elasticsearch.query.DistanceUnit + +val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).boost(5.0) +``` + +
+ +If you want to change the `boostMode`, you can use `boostMode` method: +```scala +import zio.elasticsearch.query.DistanceUnit + +val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).boostMode(FunctionScoreBoostMode.Max) +``` + +
+ +If you want to change the `maxBoost`, you can use `maxBoost` method: +```scala +import zio.elasticsearch.query.DistanceUnit + +val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).maxBoost(5.0) +``` + +
+ +If you want to change the `minScore`, you can use `minScore` method: +```scala +import zio.elasticsearch.query.DistanceUnit + +val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).minScore(5.0) +``` + +
+ +If you want to change the `query`, you can use `query` method: +```scala +import zio.elasticsearch.query.DistanceUnit + +val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).query(matches(Document.field, "value")) +``` + +
+ +If you want to change the `scoreMode`, you can use `scoreMode` method: +```scala +import zio.elasticsearch.query.DistanceUnit + +val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).scoreMode(FunctionScoreScoreMode.Max) +``` + +
+ +If you want to add a one or multiple new `FunctionScoreFunction` you can use `withFunctions` method: +```scala +import zio.elasticsearch.query.DistanceUnit + +val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).withFunctions(scriptScoreFunction(weight = 10)) +``` + +You can find more information about `FunctionScore` query [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-function-score-query.html). diff --git a/docs/overview/queries/elastic_query_term.md b/docs/overview/queries/elastic_query_term.md index 7b49e5442..e0812188b 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 79e02490a..f8cddd250 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