From 53dc256d78d5c5b802f07a1156c53bed9b8d9f10 Mon Sep 17 00:00:00 2001 From: Mouaad Aallam Date: Tue, 20 Jul 2021 10:02:51 +0200 Subject: [PATCH] feat(dynamic facets): facet ordering support (#747) --- .../search/models/indexing/SearchResult.java | 14 +++++ .../models/rules/ConsequenceParams.java | 10 +++ .../search/models/rules/FacetOrdering.java | 56 +++++++++++++++++ .../search/models/rules/FacetValuesOrder.java | 63 +++++++++++++++++++ .../search/models/rules/FacetsOrder.java | 45 +++++++++++++ .../search/models/rules/RenderingContent.java | 47 ++++++++++++++ .../search/models/settings/IndexSettings.java | 24 ++++++- 7 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetOrdering.java create mode 100644 algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetValuesOrder.java create mode 100644 algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetsOrder.java create mode 100644 algoliasearch-core/src/main/java/com/algolia/search/models/rules/RenderingContent.java diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/SearchResult.java b/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/SearchResult.java index f3e0c850d..307bd251b 100644 --- a/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/SearchResult.java +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/indexing/SearchResult.java @@ -1,5 +1,6 @@ package com.algolia.search.models.indexing; +import com.algolia.search.models.rules.RenderingContent; import com.algolia.search.util.AlgoliaUtils; import com.fasterxml.jackson.annotation.JsonSetter; import java.io.Serializable; @@ -31,6 +32,7 @@ public class SearchResult implements Serializable { private Boolean exhaustiveNbHits; private Integer appliedRelevancyStrictness; private Integer nbSortedHits; + private RenderingContent renderingContent; public Explain getExplain() { return explain; @@ -131,6 +133,15 @@ public SearchResult setQueryAfterRemoval(String queryAfterRemoval) { return this; } + public RenderingContent getRenderingContent() { + return renderingContent; + } + + public SearchResult setRenderingContent(RenderingContent renderingContent) { + this.renderingContent = renderingContent; + return this; + } + private Long offset; private Long length; private String parsedQuery; @@ -384,6 +395,9 @@ public String toString() { + '\'' + ", nbSortedHits=" + nbSortedHits + + '\'' + + ", renderingContent=" + + renderingContent + '}'; } } diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/rules/ConsequenceParams.java b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/ConsequenceParams.java index d0e1c1d0b..e321c9670 100644 --- a/algoliasearch-core/src/main/java/com/algolia/search/models/rules/ConsequenceParams.java +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/ConsequenceParams.java @@ -84,6 +84,16 @@ public ConsequenceParams setAutomaticOptionalFacetFilters( return this; } + public RenderingContent getRenderingContent() { + return renderingContent; + } + + public ConsequenceParams setRenderingContent(RenderingContent renderingContent) { + this.renderingContent = renderingContent; + return this; + } + + private RenderingContent renderingContent; private ConsequenceQuery query; @JsonDeserialize(using = AutomaticFacetFilterDeserializer.class) diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetOrdering.java b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetOrdering.java new file mode 100644 index 000000000..0f3768653 --- /dev/null +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetOrdering.java @@ -0,0 +1,56 @@ +package com.algolia.search.models.rules; + +import com.fasterxml.jackson.annotation.JsonInclude; +import java.io.Serializable; +import java.util.Map; +import java.util.Objects; + +/** Facets and facets values ordering rules container. */ +@JsonInclude(JsonInclude.Include.NON_NULL) +public class FacetOrdering implements Serializable { + private FacetsOrder facets; + private Map values; + + public FacetOrdering() {} + + public FacetOrdering(FacetsOrder facets, Map values) { + this.facets = facets; + this.values = values; + } + + public FacetsOrder getFacets() { + return facets; + } + + public FacetOrdering setFacets(FacetsOrder facets) { + this.facets = facets; + return this; + } + + public Map getValues() { + return values; + } + + public FacetOrdering setValues(Map values) { + this.values = values; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof FacetOrdering)) return false; + FacetOrdering that = (FacetOrdering) o; + return Objects.equals(facets, that.facets) && Objects.equals(values, that.values); + } + + @Override + public int hashCode() { + return Objects.hash(facets, values); + } + + @Override + public String toString() { + return "FacetOrdering{" + "facets=" + facets + ", values=" + values + '}'; + } +} diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetValuesOrder.java b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetValuesOrder.java new file mode 100644 index 000000000..04e80aebe --- /dev/null +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetValuesOrder.java @@ -0,0 +1,63 @@ +package com.algolia.search.models.rules; + +import com.fasterxml.jackson.annotation.JsonInclude; +import java.io.Serializable; +import java.util.List; +import java.util.Objects; + +/** Facet values ordering rule container. */ +@JsonInclude(JsonInclude.Include.NON_NULL) +public class FacetValuesOrder implements Serializable { + private List order; + private String sortRemainingBy; + + public FacetValuesOrder() {} + + public FacetValuesOrder(List order, String sortRemainingBy) { + this.order = order; + this.sortRemainingBy = sortRemainingBy; + } + + public List getOrder() { + return order; + } + + public FacetValuesOrder setOrder(List order) { + this.order = order; + return this; + } + + public String getSortRemainingBy() { + return sortRemainingBy; + } + + public FacetValuesOrder setSortRemainingBy(String sortRemainingBy) { + this.sortRemainingBy = sortRemainingBy; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof FacetValuesOrder)) return false; + FacetValuesOrder that = (FacetValuesOrder) o; + return Objects.equals(order, that.order) + && Objects.equals(sortRemainingBy, that.sortRemainingBy); + } + + @Override + public int hashCode() { + return Objects.hash(order, sortRemainingBy); + } + + @Override + public String toString() { + return "FacetValuesOrder{" + + "order=" + + order + + ", sortRemainingBy='" + + sortRemainingBy + + '\'' + + '}'; + } +} diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetsOrder.java b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetsOrder.java new file mode 100644 index 000000000..08b4bbdeb --- /dev/null +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/FacetsOrder.java @@ -0,0 +1,45 @@ +package com.algolia.search.models.rules; + +import com.fasterxml.jackson.annotation.JsonInclude; +import java.io.Serializable; +import java.util.List; +import java.util.Objects; + +/** Define or override the way facet attributes are displayed. */ +@JsonInclude(JsonInclude.Include.NON_NULL) +public class FacetsOrder implements Serializable { + private List order; + + public FacetsOrder() {} + + public FacetsOrder(List order) { + this.order = order; + } + + public List getOrder() { + return order; + } + + public FacetsOrder setOrder(List order) { + this.order = order; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof FacetsOrder)) return false; + FacetsOrder that = (FacetsOrder) o; + return Objects.equals(order, that.order); + } + + @Override + public int hashCode() { + return Objects.hash(order); + } + + @Override + public String toString() { + return "FacetsOrder{" + "order=" + order + '}'; + } +} diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/rules/RenderingContent.java b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/RenderingContent.java new file mode 100644 index 000000000..3dcb3452a --- /dev/null +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/rules/RenderingContent.java @@ -0,0 +1,47 @@ +package com.algolia.search.models.rules; + +import com.fasterxml.jackson.annotation.JsonInclude; +import java.io.Serializable; +import java.util.Objects; + +/** + * Content defining how the search interface should be rendered. This is set via the settings for a + * default value and can be overridden via rules. + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +public class RenderingContent implements Serializable { + private FacetOrdering facetOrdering; + + public RenderingContent() {} + + public RenderingContent(FacetOrdering facetOrdering) { + this.facetOrdering = facetOrdering; + } + + public FacetOrdering getFacetOrdering() { + return facetOrdering; + } + + public RenderingContent setFacetOrdering(FacetOrdering facetOrdering) { + this.facetOrdering = facetOrdering; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof RenderingContent)) return false; + RenderingContent that = (RenderingContent) o; + return Objects.equals(facetOrdering, that.facetOrdering); + } + + @Override + public int hashCode() { + return Objects.hash(facetOrdering); + } + + @Override + public String toString() { + return "RenderingContent{" + "facetOrdering=" + facetOrdering + '}'; + } +} diff --git a/algoliasearch-core/src/main/java/com/algolia/search/models/settings/IndexSettings.java b/algoliasearch-core/src/main/java/com/algolia/search/models/settings/IndexSettings.java index 0582bdf1f..feccc0c87 100644 --- a/algoliasearch-core/src/main/java/com/algolia/search/models/settings/IndexSettings.java +++ b/algoliasearch-core/src/main/java/com/algolia/search/models/settings/IndexSettings.java @@ -1,6 +1,13 @@ package com.algolia.search.models.settings; -import com.fasterxml.jackson.annotation.*; +import com.algolia.search.models.rules.RenderingContent; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; import java.io.Serializable; import java.util.HashMap; import java.util.List; @@ -104,6 +111,9 @@ public class IndexSettings implements Serializable { /* Virtual Indices */ protected Integer relevancyStrictness; + /* Facets Ordering */ + private RenderingContent renderingContent; + public List getAttributesForFaceting() { return attributesForFaceting; } @@ -712,6 +722,15 @@ public IndexSettings setRelevancyStrictness(Integer relevancyStrictness) { return this; } + public RenderingContent getRenderingContent() { + return renderingContent; + } + + public IndexSettings setRenderingContent(RenderingContent renderingContent) { + this.renderingContent = renderingContent; + return this; + } + @Override public String toString() { return "IndexSettings{" @@ -826,6 +845,9 @@ public String toString() { + '\'' + ", decompoundQuery=" + decompoundQuery + + '\'' + + ", renderingContent=" + + renderingContent + '}'; } }