Skip to content

Commit

Permalink
Energy consumption attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
goulven authored and goulven committed Dec 11, 2024
1 parent 3acd085 commit e774bc4
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,34 +144,47 @@ public void onProduct(Product data, VerticalConfig vConf) throws AggregationSkip
// Setting excluded state
//////////////////////////////////////////

data.setExcluded(shouldExclude(data,vConf));
updateExcludeStatus(data,vConf);

}

/**
* Set the product in excluded state (will not be exposed through indexation, searchservice,..)
* @param data
*/
private boolean shouldExclude(Product data, VerticalConfig vConf) {
private void updateExcludeStatus(Product data, VerticalConfig vConf) {
boolean ret = false;
// On brand
if (StringUtils.isEmpty(data.brand())) {
dedicatedLogger.info("Excluded because brand is missing : {}", data );
return true;
ret = true;
data.getExcludedCauses().add("missing_brand");
}

// On model
if (StringUtils.isEmpty(data.model())) {
dedicatedLogger.info("Excluded because model is missing : {}", data );
return true;
ret = true;
data.getExcludedCauses().add("missing_model");
}

Set<String> attrKeys = data.getAttributes().getattributesAsStringKeys();
if (vConf.getRequiredAttributes() != null && !attrKeys.containsAll(vConf.getRequiredAttributes())) {

Set<String> missing = new HashSet<>(vConf.getRequiredAttributes());
missing.retainAll(attrKeys);

missing.forEach(e-> {
data.getExcludedCauses().add("missing_attr_" + e);
});

dedicatedLogger.info("Excluded because attributes are missing : {}", data );
return true;
ret = true;

}

return false;
data.setExcluded(ret);

}


Expand Down Expand Up @@ -283,21 +296,21 @@ public Map<String, Object> onDataFragment(final DataFragment dataFragment, final
* @param unmatchedFeatures
* @return
*/
private void extractFeatures(ProductAttributes attributes) {

attributes.getFeatures().clear();


Map<String, ProductAttribute> features = attributes.getAll().entrySet().stream()
.filter(e -> e.getValue().isFeature())
.collect(Collectors.toMap(
Map.Entry::getKey, // key mapper: uses the key from each entry
Map.Entry::getValue // value mapper: uses the value from each entry
));

attributes.getFeatures().addAll(features.keySet());

}
// private void extractFeatures(ProductAttributes attributes) {
//
// attributes.getFeatures().clear();
//
//
// Map<String, ProductAttribute> features = attributes.getAll().entrySet().stream()
// .filter(e -> e.getValue().isFeature())
// .collect(Collectors.toMap(
// Map.Entry::getKey, // key mapper: uses the key from each entry
// Map.Entry::getValue // value mapper: uses the value from each entry
// ));
//
// attributes.getFeatures().addAll(features.values());
//
// }

/**
* Returns if an attribute is a feature, by comparing "yes" values from config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class VerticalSearchRequest {
Map<String, Set<String>> termsFilter = new HashMap<>();

boolean excluded = false;
Set<String> excludedFilters = new HashSet<String>();


private String sortField;
private String sortOrder;
Expand Down Expand Up @@ -147,6 +149,16 @@ public String toString() {
.append("sortOrder", sortOrder)
.toString();
}


public Set<String> getExcludedFilters() {
return excludedFilters;
}


public void setExcludedFilters(Set<String> excludedFilters) {
this.excludedFilters = excludedFilters;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.data.annotation.Transient;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductAttributes {

/**
Expand All @@ -23,15 +22,15 @@ public class ProductAttributes {

private Map<String, ProductAttribute> all = new HashMap<>();

@Transient
// Instance life cache for fastenning features
private Set<ProductAttribute> featuresCache;
// @Transient
// // Instance life cache for fastenning features
// private Set<ProductAttribute> featuresCache;

/**
* The features
* TODO(p3,i18n) : Internationalisation
*/
private Set<String> features = new HashSet<String>();
// private Set<ProductAttribute> features = new HashSet<ProductAttribute>();

/**
* Best effort method to return the string val of an attribute, looking up, by priority in :
Expand Down Expand Up @@ -76,13 +75,13 @@ public int count() {
return referentielAttributes.size() + indexed.size() + all.size();
}

public Set<ProductAttribute> features() {

if (this.featuresCache == null) {
featuresCache = all.values().stream().filter(e -> Boolean.TRUE.equals(IndexedAttribute.getBool(e.getValue())) ).collect(Collectors.toSet());
}
return featuresCache;
}
// public Set<ProductAttribute> features() {
//
// if (this.featuresCache == null) {
// featuresCache = all.values().stream().filter(e -> Boolean.TRUE.equals(IndexedAttribute.getBool(e.getValue())) ).collect(Collectors.toSet());
// }
// return featuresCache;
// }



Expand Down Expand Up @@ -148,22 +147,23 @@ public Map<String, ProductAttribute> getAll() {
}


//
// public Set<ProductAttribute> getFeaturesCache() {
// return featuresCache;
// }
//
// public void setFeaturesCache(Set<ProductAttribute> featuresCache) {
// this.featuresCache = featuresCache;
// }

public Set<ProductAttribute> getFeaturesCache() {
return featuresCache;
}

public void setFeaturesCache(Set<ProductAttribute> featuresCache) {
this.featuresCache = featuresCache;
}

public Set<String> getFeatures() {
return features;
}

public void setFeatures(Set<String> features) {
this.features = features;
}
// public Set<ProductAttribute> getFeatures() {
// return features;
// }
//
// public void setFeatures(Set<ProductAttribute> features) {
// this.features = features;
// }

public void setAll(Map<String, ProductAttribute> all) {
this.all = all;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public VerticalSearchResponse globalSearch(String initialQuery, Integer fromPric
* @param request
* @return
*/
@Cacheable(keyGenerator = CacheConstants.KEY_GENERATOR, cacheNames = CacheConstants.ONE_HOUR_LOCAL_CACHE_NAME)
// @Cacheable(keyGenerator = CacheConstants.KEY_GENERATOR, cacheNames = CacheConstants.ONE_HOUR_LOCAL_CACHE_NAME)
public VerticalSearchResponse verticalSearch(VerticalConfig vertical, VerticalSearchRequest request) {

VerticalSearchResponse vsr = new VerticalSearchResponse();
Expand All @@ -147,7 +147,9 @@ public VerticalSearchResponse verticalSearch(VerticalConfig vertical, VerticalSe
;

// Adding the filter on excluded if set
if (!request.isExcluded()) {
if (request.getExcludedFilters().size()>0) {
criterias.and(new Criteria("excludedCauses").in(request.getExcludedFilters()) );
} else if (!request.isExcluded()) {
criterias.and(new Criteria("excluded").is(false));
}

Expand Down Expand Up @@ -216,7 +218,7 @@ public VerticalSearchResponse verticalSearch(VerticalConfig vertical, VerticalSe
// .withAggregation("max_offers", Aggregation.of(a -> a.max(ta -> ta.field("offersCount"))))
.withAggregation("conditions", Aggregation.of(a -> a.terms(ta -> ta.field("price.conditions").missing(MISSING_BUCKET).size(3))) )
.withAggregation("trend", Aggregation.of(a -> a.terms(ta -> ta.field("price.trend").size(3))) )
.withAggregation("excluded", Aggregation.of(a -> a.terms(ta -> ta.field("excluded").size(2))) )
.withAggregation("excludedCauses", Aggregation.of(a -> a.terms(ta -> ta.field("excludedCauses").size(20))) )
.withAggregation("brands", Aggregation.of(a -> a.terms(ta -> ta.field("attributes.referentielAttributes.BRAND").missing(MISSING_BUCKET).size(AGGREGATION_BUCKET_SIZE) )) )
.withAggregation("country", Aggregation.of(a -> a.terms(ta -> ta.field("gtinInfos.country").missing(MISSING_BUCKET).size(AGGREGATION_BUCKET_SIZE) )) )

Expand Down Expand Up @@ -373,9 +375,9 @@ public VerticalSearchResponse verticalSearch(VerticalConfig vertical, VerticalSe
// excluded
///////

LongTermsAggregate excluded = aggregations.get("excluded").aggregation().getAggregate().lterms() ;
for (LongTermsBucket b : excluded.buckets().array()) {
vsr.getExcluded().add (new VerticalFilterTerm(b.keyAsString(), b.docCount()));
StringTermsAggregate excluded = aggregations.get("excludedCauses").aggregation().getAggregate().sterms() ;
for (StringTermsBucket b : excluded.buckets().array()) {
vsr.getExcluded().add (new VerticalFilterTerm(b.key().stringValue(), b.docCount()));
}
vsr.getExcluded().sort((o1, o2) -> o2.getCount().compareTo(o1.getCount()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public DataTableResults<Product> listUsersPaginated(@PathVariable(name = "vertic
case "brand" -> vRequest.addTermFilter("attributes.referentielAttributes.BRAND", attr[1]);
case "countries" -> vRequest.addTermFilter("gtinInfos.country", attr[1]);
case "trend" -> vRequest.addTermFilter("price.trend", attr[1]);
case "excluded" -> handleExcludedState(vRequest, attr);
case "excludedCauses" -> handleExcludedState(vRequest, attr);
default -> vRequest.addTermFilter(attr[0], attr[1]);
}
}
Expand Down Expand Up @@ -129,7 +129,7 @@ public DataTableResults<Product> listUsersPaginated(@PathVariable(name = "vertic
* @param attr
*/
private void handleExcludedState(VerticalSearchRequest vRequest, String[] attr) {
vRequest.addTermFilter("excluded", attr[1]);
vRequest.getExcludedFilters().add(attr[1]);
vRequest.setExcluded(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpSer
vRequest.getNumericFilters().add(new NumericRangeFilter("offersCount", 1.0, 10000.0, 1.0, false));
vRequest.getNumericFilters().add(new NumericRangeFilter("price.minPrice.price", 0.0001, 500000.0, 100.0, false));
vRequest.getNumericFilters().add(new NumericRangeFilter("scores.ECOSCORE.value", 0.0001, 500000.0, 0.1, true));

vRequest.getNumericFilters().add(new NumericRangeFilter("scores.BRAND_SUSTAINABILITY.value", 0.0001, 500000.0, 0.1, true));
List<AttributeConfig> numericFilters = config.getVerticalFilters().stream()
.map(e -> config.getAttributesConfig().getAttributeConfigByKey(e))
.filter(e-> e != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,20 @@
">
<th:block th:insert="~{inc/vertical-filter-item-terms.html}"></th:block>
</th:block>

<!--
<th:block th:with="title='Consommation en marche',
id='attributes.indexed.POWER_CONSUMPTION.numericValue',
labelType='alert-success',
icon='fa-cart-energy',
label='Consommation en marche (typique / max)',
min=0,
max=500,
interval=1,
includeUndefined=true,
showIncludeUndefinedButton=true,
includeUndefinedLabel='Afficher les non évalués'
">
-->

<!-- Custom eco filters -->
<th:block th:each="filterName : ${verticalConfig.ecoFilters}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</th:block>

<!-- The excluded -->
<th:block th:if="${user}" th:with="id='excluded',
<th:block th:if="${user}" th:with="id='excludedCauses',
title='Exclus',
icon='fa-excluded',
visibleSection=10,
Expand Down

0 comments on commit e774bc4

Please sign in to comment.