Skip to content

Commit

Permalink
Fix calculating selected range filter for Elasticsearch 6
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-social committed Apr 18, 2023
1 parent 635789d commit c160676
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ class PreparedAttrRangeFacetFilter(

internal val SINGLE_ATTR_INIT_SCRIPT = """
state.count = 0L;
state.min = Float.POSITIVE_INFINITY;
state.max = Float.NEGATIVE_INFINITY;
state.min = null;
state.max = null;
""".trimIndent()
internal val SINGLE_ATTR_MAP_SCRIPT = """
if (doc[params.attrsField].size() == 0) {
Expand All @@ -350,9 +350,9 @@ class PreparedAttrRangeFacetFilter(
foundAttr = true;
int valueBits = (int) (v & 0xffffffffL);
float value = Float.intBitsToFloat(valueBits);
if (value < state.min) {
if (state.min == null || value < state.min) {
state.min = value;
} else if (value > state.max) {
} else if (state.max == null || value > state.max) {
state.max = value;
}
}
Expand All @@ -361,24 +361,27 @@ class PreparedAttrRangeFacetFilter(
}
""".trimIndent()
internal val SINGLE_ATTR_COMBINE_SCRIPT = """
state.attrId = params.attrId;
return state;
""".trimIndent()
internal val SINGLE_ATTR_REDUCE_SCRIPT = """
Integer attrId = null;
long count = 0L;
Float min = null;
Float max = null;
for (state in states) {
attrId = state.attrId;
count += state.count;
if (min == null || state.min < min) {
if (state.min != null && (min == null || state.min < min)) {
min = state.min;
}
if (max == null || state.max > max) {
if (state.min != null && (max == null || state.max > max)) {
max = state.max;
}
}
def result = new HashMap();
result.put("attr_id", params.attrId);
result.put("attr_id", attrId);
result.put("count", count);
result.put("min", min);
result.put("max", max);
Expand Down Expand Up @@ -495,8 +498,8 @@ object AttrRangeFacetType : SimpleFieldType<AttrRangeFacet>() {
return AttrRangeFacet(
attrId = v.int("attr_id"),
count = v.long("count"),
min = v.float("min"),
max = v.float("max"),
min = v.floatOrNull("min"),
max = v.floatOrNull("max"),
)
}
}
Expand All @@ -510,6 +513,6 @@ data class AttrRangeFacetFilterResult(
data class AttrRangeFacet(
val attrId: Int,
val count: Long,
val min: Float,
val max: Float,
val min: Float?,
val max: Float?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.evo.elasticmagic.doc.list
import io.kotest.matchers.shouldBe
import io.kotest.matchers.booleans.shouldBeFalse
import io.kotest.matchers.booleans.shouldBeTrue
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.nulls.shouldNotBeNull

import kotlin.test.Test
Expand Down Expand Up @@ -286,7 +287,7 @@ class AttrFacetFilterTests : ElasticsearchTestBase() {
@Test
@Suppress("CyclomaticComplexMethod")
fun facet() = runTestWithSerdes {
withFixtures(ItemDoc, FIXTURES, cleanup = false) {
withFixtures(ItemDoc, FIXTURES) {
var searchQuery = SearchQuery()
searchQuery.execute(index).totalHits shouldBe 8

Expand Down Expand Up @@ -553,6 +554,51 @@ class AttrFacetFilterTests : ElasticsearchTestBase() {
}
}

searchQuery = SearchQuery()
ItemQueryFilters.apply(
searchQuery,
mapOf(
listOf("attr", "2") to listOf("5"),
listOf("attr", "5", "gte") to listOf("6.5"),
)
).let { appliedFilters ->
val searchResult = searchQuery.execute(index)
searchResult.totalHits shouldBe 0

val qfResult = appliedFilters.processResult(searchResult)

val selectAttrsFilter = qfResult[ItemQueryFilters.selectAttrs]
selectAttrsFilter.name shouldBe "selectAttrs"
selectAttrsFilter.paramName shouldBe "attr"
selectAttrsFilter.facets.size shouldBe 1

val processorFacet = selectAttrsFilter
.facets[Processor.ATTR_ID]
.shouldNotBeNull()
processorFacet.values.size shouldBe 4
processorFacet.iterator().let { values ->
values.get() shouldBe AttrFacetValue(Processor.Snapdragon.valueId, 1)
values.get() shouldBe AttrFacetValue(Processor.Mediatek.valueId, 1)
values.get() shouldBe AttrFacetValue(Processor.Exinos.valueId, 1)
values.get() shouldBe AttrFacetValue(Processor.Tensor.valueId, 1)
}

val rangeAttrsFilter = qfResult[ItemQueryFilters.rangeAttrs]
rangeAttrsFilter.name shouldBe "rangeAttrs"
rangeAttrsFilter.paramName shouldBe "attr"
rangeAttrsFilter.facets.size shouldBe 1

rangeAttrsFilter
.facets[DisplaySize.ATTR_ID]
.shouldNotBeNull()
.let { facet ->
facet.attrId shouldBe DisplaySize.ATTR_ID
facet.count shouldBe 0
facet.min.shouldBeNull()
facet.max.shouldBeNull()
}
}

searchQuery = SearchQuery()
ItemQueryFilters.apply(
searchQuery,
Expand Down

0 comments on commit c160676

Please sign in to comment.