Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
861a986
Done: matches case for IntervalQuery
markok4 Jun 12, 2025
7bb2df6
Disable empty chunk in interval allof, Wildcard (with cointains, star…
markok4 Jun 13, 2025
907980b
Implement IntervalRange test
markok4 Jun 13, 2025
3b97832
Implement: export methods from Queries.scala to ElasticIntervalQuery.…
markok4 Jun 13, 2025
ee269c2
Implement: documentation in ElasticQuery.scala
markok4 Jun 13, 2025
441fe9f
Implement: move methods to another file, fix tests
markok4 Jun 16, 2025
af9b2fe
Fix formater bug, create ElasticIntervalQuerySpec
markok4 Jun 16, 2025
805f6bd
Fix IntervalRange, Change order of methods in ElasticQuery
markok4 Jun 16, 2025
f9a4a13
Add ElasticIntervalQuery.scala
markok4 Jun 16, 2025
84c908b
Add HasAnalyzer and HasUseField, correct some mistakes
markok4 Jun 16, 2025
d5b8bc8
Error: Scalafmt
markok4 Jun 16, 2025
7af2c13
Work on It tests
markok4 Jun 16, 2025
ba54dc1
Add integration tests for IntervalMatch
markok4 Jun 16, 2025
593f575
Refactor code in HttpExecutorSpec and create suite for IntervalMatch
markok4 Jun 17, 2025
5f42ed1
Add elastic_interval_query.md file
markok4 Jun 17, 2025
ff1916d
Complete changes on Boosting, refactor on type-safe useField
markok4 Jun 17, 2025
ca243e9
Use fieldPath
markok4 Jun 17, 2025
640500d
Implemented methods for gt,gte,lt,lte
markok4 Jun 17, 2025
6308187
Fix PR comments
markok4 Jun 18, 2025
41df198
Revert boost, add another type parameter into HasUseField
markok4 Jun 18, 2025
f7cb287
Fix some warnings about casting.
markok4 Jun 18, 2025
a136f4f
Try to remove strange chars.
markok4 Jun 18, 2025
7391cec
Done sbt prepared
markok4 Jun 18, 2025
45bc91e
Remove manually strange chars
markok4 Jun 18, 2025
cf31617
Resolve some comments.
markok4 Jun 19, 2025
3f44d44
Change elastic_interval_query.md file.
markok4 Jun 19, 2025
dfece62
Fix line endings and cleanup .gitattributes
markok4 Jun 19, 2025
500610f
Remove strange chars.
markok4 Jun 19, 2025
72715df
Remove space from ElasticQuerySpec.
markok4 Jun 19, 2025
7941fe3
Return copyright to HttpExecutorSpec.
markok4 Jun 19, 2025
26150f1
Add copyright in intervalSpec and some changes.
markok4 Jun 19, 2025
4d60d5f
Try to fix.
markok4 Jun 19, 2025
c7eab4e
Fix space in copyright
markok4 Jun 19, 2025
377bf8c
Fix space in copyright
markok4 Jun 19, 2025
b21a202
Fix space in copyright
markok4 Jun 19, 2025
b14e4d9
Add to sidebars.js
markok4 Jun 19, 2025
72e782b
Move ElasticInterval object in separated file, fix some problems.
markok4 Jun 20, 2025
626dfda
Add line in elastic_query_interval.md and do sbt prepare
markok4 Jun 20, 2025
61a4949
Done.
markok4 Jun 20, 2025
1aae089
Some fixes.
markok4 Jun 20, 2025
689ab2e
Add unit tests for query.
markok4 Jun 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package zio.elasticsearch

import zio.Chunk
import zio.{Chunk, NonEmptyChunk}
import zio.elasticsearch.ElasticPrimitive.ElasticPrimitive
import zio.elasticsearch.data.GeoPoint
import zio.elasticsearch.query._
import zio.elasticsearch.script.Script
import zio.json.ast.Json
import zio.schema.Schema

object ElasticQuery {
Expand Down Expand Up @@ -548,6 +549,48 @@ object ElasticQuery {
final def ids(value: String, values: String*): IdsQuery[Any] =
Ids(values = Chunk.fromIterable(value +: values))


/**
* Constructs an intervals query by combining a field and an interval query.
*
* The resulting query wraps the specified interval query under the given field in the intervals query structure.
*
* @param field
* the name of the field to be queried.
* @param rule
* an instance of [[zio.elasticsearch.query.IntervalRule]] representing the interval query rule.
* @return
* an [[zio.elasticsearch.ElasticQuery]] instance representing the intervals query.
*/

final def intervals(
field: String,
rule: IntervalRule
): ElasticQuery[Any] =
Intervals(field, rule)

/**
* Constructs a type-safe intervals query by combining a field and an interval query.
*
* The resulting query wraps the specified interval query under the given type-safe field in the intervals query
* structure.
*
* @param field
* the type-safe field on which the query is executed.
* @param rule
* an instance of [[zio.elasticsearch.query.IntervalRule]] representing the interval query rule.
* @tparam S
* the document type for which the query is defined.
* @return
* an [[zio.elasticsearch.ElasticQuery]] instance representing the intervals query.
*/

final def intervals[S](
field: Field[S, _],
rule: IntervalRule
): ElasticQuery[S] =
Intervals(field.toString, rule)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.KNNQuery]] using the specified parameters.
* [[zio.elasticsearch.query.KNNQuery]] is used to perform a k-nearest neighbor (kNN) search and returns the matching
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
package zio.elasticsearch.query

import zio.Chunk
import zio.elasticsearch.ElasticPrimitive.ElasticPrimitiveOps
import zio.json.ast.Json
import zio.json.ast.Json.{Arr, Obj, Str}

sealed trait IntervalRule {
private[elasticsearch] def toJson: Json
}

private[elasticsearch] final case class IntervalAllOf[S](
intervals: Chunk[IntervalRule],
maxGaps: Option[Int],
ordered: Option[Boolean],
filter: Option[IntervalFilter[S]]
) extends IntervalRule {
private[elasticsearch] def toJson: Json =
Obj(
"all_of" -> Obj(
Chunk(
Some("intervals" -> Arr(intervals.map(_.toJson): _*)),
maxGaps.map("max_gaps" -> _.toJson),
ordered.map("ordered" -> _.toJson),
filter.map("filter" -> _.toJson)
).flatten: _*
)
)
}

private[elasticsearch] final case class IntervalAnyOf[S](
intervals: Chunk[IntervalRule],
filter: Option[IntervalFilter[S]]
) extends IntervalRule { self =>

def withFilter(f: IntervalFilter[S]): IntervalAnyOf[S] = copy(filter = Some(f))

override private[elasticsearch] def toJson: Json =
Obj(
"any_of" -> Obj(
Chunk(
Some("intervals" -> Arr(intervals.map(_.toJson): _*)),
filter.map("filter" -> _.toJson)
).flatten: _*
)
)
}

private[elasticsearch] final case class IntervalFilter[S](
after: Option[IntervalRule] = None,
before: Option[IntervalRule] = None,
containedBy: Option[IntervalRule] = None,
containing: Option[IntervalRule] = None,
notContainedBy: Option[IntervalRule] = None,
notContaining: Option[IntervalRule] = None,
notOverlapping: Option[IntervalRule] = None,
overlapping: Option[IntervalRule] = None,
script: Option[Json] = None
) {
private[elasticsearch] def toJson: Json =
Obj(
Chunk(
after.map("after" -> _.toJson),
before.map("before" -> _.toJson),
containedBy.map("contained_by" -> _.toJson),
containing.map("containing" -> _.toJson),
notContainedBy.map("not_contained_by" -> _.toJson),
notContaining.map("not_containing" -> _.toJson),
notOverlapping.map("not_overlapping" -> _.toJson),
overlapping.map("overlapping" -> _.toJson),
script.map("script" -> _)
).flatten: _*
)
}

private[elasticsearch] final case class IntervalFuzzy[S](
term: String,
prefixLength: Option[Int],
transpositions: Option[Boolean],
fuzziness: Option[String],
analyzer: Option[String],
useField: Option[String]
) extends IntervalRule {
private[elasticsearch] def toJson: Json =
Obj(
"fuzzy" -> Obj(
Chunk(
Some("term" -> term.toJson),
prefixLength.map("prefix_length" -> _.toJson),
transpositions.map("transpositions" -> _.toJson),
fuzziness.map("fuzziness" -> _.toJson),
analyzer.map("analyzer" -> _.toJson),
useField.map("use_field" -> _.toJson)
).flatten: _*
)
)
}

private[elasticsearch] final case class IntervalMatch[S](
query: String,
analyzer: Option[String],
useField: Option[String],
maxGaps: Option[Int],
ordered: Option[Boolean],
filter: Option[IntervalFilter[S]]
) extends IntervalRule { self =>

def withAnalyzer(a: String) = copy(analyzer = Some(a))
def withUseField(f: String) = copy(useField = Some(f))
def withMaxGaps(g: Int) = copy(maxGaps = Some(g))
def withOrdered(o: Boolean) = copy(ordered = Some(o))
def withFilter(f: IntervalFilter[S]) = copy(filter = Some(f))

override private[elasticsearch] def toJson: Json =
Obj(
"match" -> Obj(
Chunk(
Some("query" -> Str(query)),
analyzer.map("analyzer" -> _.toJson),
useField.map("use_field" -> _.toJson),
maxGaps.map("max_gaps" -> _.toJson),
ordered.map("ordered" -> _.toJson),
filter.map("filter" -> _.toJson)
).flatten: _*
)
)
}

private[elasticsearch] final case class IntervalPrefix(
prefix: String,
analyzer: Option[String],
useField: Option[String]
) extends IntervalRule {

def withAnalyzer(a: String) = copy(analyzer = Some(a))
def withUseField(f: String) = copy(useField = Some(f))

override private[elasticsearch] def toJson: Json =
Obj(
"prefix" -> Obj(
Chunk(
Some("prefix" -> Str(prefix)),
analyzer.map(a => "analyzer" -> Str(a)),
useField.map(u => "use_field" -> Str(u))
).flatten: _*
)
)
}

private[elasticsearch] final case class IntervalRange[S](
lower: Option[Either[String, String]],
upper: Option[Either[String, String]],
analyzer: Option[String],
useField: Option[String]
) extends IntervalRule {
private[elasticsearch] def toJson: Json = {
val lowerJson = lower.map {
case Left(gt) => "gt" -> gt.toJson
case Right(gte) => "gte" -> gte.toJson
}
val upperJson = upper.map {
case Left(lt) => "lt" -> lt.toJson
case Right(lte) => "lte" -> lte.toJson
}

Obj(
"range" -> Obj(
Chunk(
lowerJson,
upperJson,
analyzer.map("analyzer" -> _.toJson),
useField.map("use_field" -> _.toJson)
).flatten: _*
)
)
}
}

private[elasticsearch] final case class IntervalRegexp[S](
pattern: Regexp[S],
analyzer: Option[String],
useField: Option[String]
) extends IntervalRule {
private[elasticsearch] def toJson: Json =
Obj(
"regexp" -> Obj(
Chunk(
Some("pattern" -> pattern.toJson(None)),
analyzer.map("analyzer" -> _.toJson),
useField.map("use_field" -> _.toJson)
).flatten: _*
)
)
}

private[elasticsearch] final case class IntervalWildcard[S](
pattern: String,
analyzer: Option[String],
useField: Option[String]
) extends IntervalRule {

def withAnalyzer(a: String): IntervalWildcard[S] =
copy(analyzer = Some(a))

def withUseField(f: String): IntervalWildcard[S] =
copy(useField = Some(f))

private[elasticsearch] def toJson: Json =
Obj(
"wildcard" -> Obj(
Chunk(
Some("pattern" -> pattern.toJson),
analyzer.map("analyzer" -> _.toJson),
useField.map("use_field" -> _.toJson)
).flatten: _*
)
)
}
object ElasticIntervalQuery {

def intervalAllOf[S](intervals: Chunk[IntervalRule]): IntervalAllOf[S] =
IntervalAllOf(intervals, maxGaps = None, ordered = None, filter = None)

def intervalAnyOf[S](intervals: Chunk[IntervalRule]): IntervalAnyOf[S] =
IntervalAnyOf(intervals, filter = None)

def intervalContains[S](pattern: String): IntervalWildcard[S] =
IntervalWildcard(s"*$pattern*", analyzer = None, useField = None)

def intervalEndsWith[S](pattern: String): IntervalWildcard[S] =
IntervalWildcard(s"*$pattern", analyzer = None, useField = None)

def intervalFilter[S](): IntervalFilter[S] =
IntervalFilter()

def intervalFuzzy[S](term: String): IntervalFuzzy[S] =
IntervalFuzzy(term, prefixLength = None, transpositions = None, fuzziness = None, analyzer = None, useField = None)

def intervalMatch[S](query: String): IntervalMatch[S] =
IntervalMatch(query, analyzer = None, useField = None, maxGaps = None, ordered = None, filter = None)

def intervalPrefix[S](prefix: String): IntervalPrefix =
IntervalPrefix(prefix, analyzer = None, useField = None)

def intervalRange[S](
lower: Option[Either[String, String]] = None,
upper: Option[Either[String, String]] = None,
analyzer: Option[String] = None,
useField: Option[String] = None
): IntervalRange[S] =
IntervalRange(lower, upper, analyzer, useField)

def intervalRegexp[S](pattern: Regexp[S]): IntervalRegexp[S] =
IntervalRegexp(pattern, analyzer = None, useField = None)

def intervalStartsWith[S](pattern: String): IntervalWildcard[S] =
IntervalWildcard(s"$pattern*", analyzer = None, useField = None)

def intervalWildcard[S](pattern: String): IntervalWildcard[S] =
IntervalWildcard(pattern, analyzer = None, useField = None)
}
Loading