Skip to content

Commit

Permalink
Added span field masking query (#2568)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxim D <[email protected]>
  • Loading branch information
hellhoundmipt and Maxim D authored Dec 8, 2021
1 parent bf26a9e commit f3e70d0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.sksamuel.elastic4s.requests.searches.queries.funcscorer.FunctionScore
import com.sksamuel.elastic4s.requests.searches.queries.geo.{GeoBoundingBoxQuery, GeoDistanceQuery, GeoPolygonQuery, GeoShapeQuery, Shape}
import com.sksamuel.elastic4s.requests.searches.queries.matches.{MatchAllQuery, MatchBoolPrefixQuery, MatchNoneQuery, MatchPhrasePrefixQuery, MatchPhraseQuery, MatchQuery, MultiMatchQuery}
import com.sksamuel.elastic4s.requests.searches.queries.{ArtificialDocument, BoostingQuery, CombinedFieldsQuery, CommonTermsQuery, ConstantScore, DisMaxQuery, DistanceFeatureQuery, ExistsQuery, FuzzyQuery, HasChildQuery, HasParentQuery, IdQuery, IntervalsQuery, IntervalsRule, MoreLikeThisItem, MoreLikeThisQuery, MultiTermQuery, NestedQuery, PercolateQuery, PinnedQuery, PrefixQuery, Query, QueryStringQuery, RangeQuery, RankFeatureQuery, RawQuery, RegexQuery, ScriptQuery, SimpleStringQuery}
import com.sksamuel.elastic4s.requests.searches.span.{SpanContainingQuery, SpanFirstQuery, SpanMultiTermQuery, SpanNearQuery, SpanNotQuery, SpanOrQuery, SpanQuery, SpanTermQuery, SpanWithinQuery}
import com.sksamuel.elastic4s.requests.searches.span.{SpanContainingQuery, SpanFieldMaskingQuery, SpanFirstQuery, SpanMultiTermQuery, SpanNearQuery, SpanNotQuery, SpanOrQuery, SpanQuery, SpanTermQuery, SpanWithinQuery}
import com.sksamuel.elastic4s.requests.searches.term.{TermQuery, TermsLookupQuery, TermsQuery, TermsSetQuery, WildcardQuery}
import com.sksamuel.elastic4s.requests.searches.{GeoPoint, ScoreMode, TermsLookup, span, term}

Expand Down Expand Up @@ -218,6 +218,7 @@ trait QueryApi {

def spanOrQuery(iterable: Iterable[SpanQuery]): SpanOrQuery = span.SpanOrQuery(iterable.toSeq)
def spanOrQuery(first: SpanQuery, rest: SpanQuery*): SpanOrQuery = spanOrQuery(first +: rest)
def spanFieldMaskingQuery(fieldToMask: String, query: SpanQuery): SpanFieldMaskingQuery = span.SpanFieldMaskingQuery(fieldToMask, query)

def spanContainingQuery(big: SpanQuery, little: SpanQuery): SpanContainingQuery =
SpanContainingQuery(big, little)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.sksamuel.elastic4s.requests.searches.queries.span

import com.sksamuel.elastic4s.handlers.searches.queries.span.SpanFieldMaskingQueryBodyFn
import com.sksamuel.elastic4s.requests.searches.span.{SpanFieldMaskingQuery, SpanTermQuery}
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

class SpanFieldMaskingQueryBodyFnTest extends AnyFunSuite with Matchers {
test("SpanFieldMaskingQueryBodyFn apply should return appropriate XContentBuilder") {
val builder = SpanFieldMaskingQueryBodyFn.apply(SpanFieldMaskingQuery(
field = "masked_field",
query = SpanTermQuery("field1", "value1", Some("name1"), Some(4.0)),
boost = Some(2.0),
queryName = Some("rootName")
))
builder.string() shouldBe """{"span_field_masking":{"field":"masked_field","boost":2.0,"_name":"rootName","query":{"span_term":{"field1":"value1","boost":4.0,"_name":"name1"}}}}"""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sksamuel.elastic4s.requests.searches.span

case class SpanFieldMaskingQuery(field: String, query: SpanQuery, boost: Option[Double] = None, queryName: Option[String] = None)
extends SpanQuery {

def boost(boost: Double): SpanFieldMaskingQuery = copy(boost = Option(boost))
def queryName(queryName: String): SpanFieldMaskingQuery = withQueryName(queryName)
def withQueryName(queryName: String): SpanFieldMaskingQuery = copy(queryName = Option(queryName))
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.sksamuel.elastic4s.handlers.searches.RawQueryBodyFn
import com.sksamuel.elastic4s.handlers.searches.queries.compound.{BoolQueryBuilderFn, BoostingQueryBodyFn, ConstantScoreBodyFn, DisMaxQueryBodyFn}
import com.sksamuel.elastic4s.handlers.searches.queries.geo.{GeoBoundingBoxQueryBodyFn, GeoDistanceQueryBodyFn, GeoPolyonQueryBodyFn, GeoShapeQueryBodyFn}
import com.sksamuel.elastic4s.handlers.searches.queries.nested.{HasChildBodyFn, HasParentBodyFn, NestedQueryBodyFn, ParentIdQueryBodyFn}
import com.sksamuel.elastic4s.handlers.searches.queries.span.{SpanContainingQueryBodyFn, SpanFirstQueryBodyFn, SpanMultiTermQueryBodyFn, SpanNearQueryBodyFn, SpanNotQueryBodyFn, SpanOrQueryBodyFn, SpanTermQueryBodyFn, SpanWithinQueryBodyFn}
import com.sksamuel.elastic4s.handlers.searches.queries.span.{SpanContainingQueryBodyFn, SpanFieldMaskingQueryBodyFn, SpanFirstQueryBodyFn, SpanMultiTermQueryBodyFn, SpanNearQueryBodyFn, SpanNotQueryBodyFn, SpanOrQueryBodyFn, SpanTermQueryBodyFn, SpanWithinQueryBodyFn}
import com.sksamuel.elastic4s.handlers.searches.queries.term.{ExistsQueryBodyFn, FuzzyQueryBodyFn, IdQueryBodyFn, PrefixQueryBodyFn, RangeQueryBodyFn, RegexQueryBodyFn, TermQueryBodyFn, TermsLookupQueryBodyFn, TermsQueryBodyFn, TermsSetQueryBodyFn, WildcardQueryBodyFn}
import com.sksamuel.elastic4s.handlers.searches.queries.text.{CombinedFieldsQueryBodyFn, CommonTermsQueryBodyFn, MatchAllBodyFn, MatchBoolPrefixBodyFn, MatchNoneBodyFn, MatchPhrasePrefixBodyFn, MatchPhraseQueryBodyFn, MatchQueryBuilderFn, MultiMatchBodyFn, QueryStringBodyFn, SimpleStringBodyFn}
import com.sksamuel.elastic4s.json.{XContentBuilder, XContentFactory}
Expand All @@ -13,7 +13,7 @@ import com.sksamuel.elastic4s.requests.searches.queries.funcscorer.{FunctionScor
import com.sksamuel.elastic4s.requests.searches.queries.geo.{GeoBoundingBoxQuery, GeoDistanceQuery, GeoPolygonQuery, GeoShapeQuery}
import com.sksamuel.elastic4s.requests.searches.queries.matches.{MatchAllQuery, MatchBoolPrefixQuery, MatchNoneQuery, MatchPhrasePrefixQuery, MatchPhraseQuery, MatchQuery, MultiMatchQuery}
import com.sksamuel.elastic4s.requests.searches.queries.{BoostingQuery, CombinedFieldsQuery, CommonTermsQuery, ConstantScore, DisMaxQuery, DistanceFeatureQuery, ExistsQuery, FuzzyQuery, HasChildQuery, HasParentQuery, IdQuery, IntervalsQuery, MoreLikeThisQuery, NestedQuery, NoopQuery, ParentIdQuery, PercolateQuery, PinnedQuery, PrefixQuery, Query, QueryStringQuery, RangeQuery, RankFeatureQuery, RawQuery, RegexQuery, ScriptQuery, SimpleStringQuery}
import com.sksamuel.elastic4s.requests.searches.span.{SpanContainingQuery, SpanFirstQuery, SpanMultiTermQuery, SpanNearQuery, SpanNotQuery, SpanOrQuery, SpanTermQuery, SpanWithinQuery}
import com.sksamuel.elastic4s.requests.searches.span.{SpanContainingQuery, SpanFieldMaskingQuery, SpanFirstQuery, SpanMultiTermQuery, SpanNearQuery, SpanNotQuery, SpanOrQuery, SpanTermQuery, SpanWithinQuery}
import com.sksamuel.elastic4s.requests.searches.term.{TermQuery, TermsLookupQuery, TermsQuery, TermsSetQuery, WildcardQuery}

object QueryBuilderFn {
Expand Down Expand Up @@ -71,6 +71,7 @@ object QueryBuilderFn {
case t: TermsLookupQuery => TermsLookupQueryBodyFn(t)
case t: TermsSetQuery => TermsSetQueryBodyFn(t)
case q: WildcardQuery => WildcardQueryBodyFn(q)
case q: SpanFieldMaskingQuery => SpanFieldMaskingQueryBodyFn(q)
// case c: CustomQuery => c.buildQueryBody()

// Not implemented
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sksamuel.elastic4s.handlers.searches.queries.span

import com.sksamuel.elastic4s.handlers.searches.queries
import com.sksamuel.elastic4s.json.{XContentBuilder, XContentFactory}
import com.sksamuel.elastic4s.requests.searches.span.SpanFieldMaskingQuery

object SpanFieldMaskingQueryBodyFn {
def apply(q: SpanFieldMaskingQuery): XContentBuilder = {
val builder = XContentFactory.jsonBuilder()
builder.startObject("span_field_masking")

builder.field("field", q.field)
q.boost.foreach(builder.field("boost", _))
q.queryName.foreach(builder.field("_name", _))

builder.rawField("query", queries.QueryBuilderFn(q.query))

builder.endObject()
builder.endObject()
}
}

0 comments on commit f3e70d0

Please sign in to comment.