/** * Check if some statistics record exists for specified conditions. * * @param type of record we are looking for * @param conditions for lookup. Key is a name of field to filter over, Value is a value to filter for using term * condition. * @return true if at least one record matching conditions exits */ public boolean checkStatisticsRecordExists(StatsRecordType type, Map<String, Object> conditions) { if (!statsConfiguration.enabled()) { return false; } SearchRequestBuilder srb = new SearchRequestBuilder(client); srb.setIndices(type.getSearchIndexName()); srb.setTypes(type.getSearchIndexType()); AndFilterBuilder fb = new AndFilterBuilder(); fb.add(new TermsFilterBuilder(FIELD_TYPE, type.getSearchIndexedValue())); if (conditions != null) { for (String fieldName : conditions.keySet()) { fb.add(new TermsFilterBuilder(fieldName, conditions.get(fieldName))); } } srb.setQuery(new FilteredQueryBuilder(QueryBuilders.matchAllQuery(), fb)); srb.addField("_id"); try { SearchResponse searchResponse = srb.execute().actionGet(); return searchResponse.getHits().getTotalHits() > 0; } catch (org.elasticsearch.indices.IndexMissingException e) { return false; } }
protected QueryBuilder applyCommonFilters(Map<String, FilterBuilder> searchFilters, QueryBuilder qb) { if (!searchFilters.isEmpty()) { return new FilteredQueryBuilder(qb, new AndFilterBuilder(searchFilters.values().toArray( new FilterBuilder[searchFilters.size()]))); } else { return qb; } }
/** * @param querySettings * @param srb */ protected void handleFacetSettings(QuerySettings querySettings, Map<String, FilterBuilder> searchFilters, SearchRequestBuilder srb) { Set<FacetValue> facets = querySettings.getFacets(); if (facets != null) { if (facets.contains(FacetValue.PER_PROJECT_COUNTS)) { srb.addFacet(createTermsFacetBuilder(FacetValue.PER_PROJECT_COUNTS, DcpContentObjectFields.DCP_PROJECT, 500, searchFilters)); } if (facets.contains(FacetValue.PER_DCP_TYPE_COUNTS)) { srb.addFacet(createTermsFacetBuilder(FacetValue.PER_DCP_TYPE_COUNTS, DcpContentObjectFields.DCP_TYPE, 20, searchFilters)); } if (facets.contains(FacetValue.TOP_CONTRIBUTORS)) { srb.addFacet(createTermsFacetBuilder(FacetValue.TOP_CONTRIBUTORS, DcpContentObjectFields.DCP_CONTRIBUTORS, 100, searchFilters)); if (searchFilters != null && searchFilters.containsKey(DcpContentObjectFields.DCP_CONTRIBUTORS)) { // we filter over contributors so we have to add second facet which provide numbers for these contributors // because they can be out of normal facet due count limitation TermsFacetBuilder tb = new TermsFacetBuilder(FacetValue.TOP_CONTRIBUTORS + "_filter") .field(DcpContentObjectFields.DCP_CONTRIBUTORS).size(30).global(true) .facetFilter(new AndFilterBuilder(getFilters(searchFilters, null))); srb.addFacet(tb); } } if (facets.contains(FacetValue.TAG_CLOUD)) { srb.addFacet(createTermsFacetBuilder(FacetValue.TAG_CLOUD, DcpContentObjectFields.DCP_TAGS, 50, searchFilters)); } if (facets.contains(FacetValue.ACTIVITY_DATES_HISTOGRAM)) { srb.addFacet(new DateHistogramFacetBuilder(FacetValue.ACTIVITY_DATES_HISTOGRAM.toString()).field( DcpContentObjectFields.DCP_ACTIVITY_DATES).interval(selectActivityDatesHistogramInterval(querySettings))); } } }
protected TermsFacetBuilder createTermsFacetBuilder(FacetValue facetName, String facetField, int size, Map<String, FilterBuilder> searchFilters) { TermsFacetBuilder tb = new TermsFacetBuilder(facetName.toString()).field(facetField).size(size).global(true); if (searchFilters != null && !searchFilters.isEmpty()) { FilterBuilder[] fb = getFilters(searchFilters, facetField); if (fb != null && fb.length > 0) tb.facetFilter(new AndFilterBuilder(fb)); } return tb; }
@Override public ProductSearchResult searchProducts(SearchCriteria searchCriteria) { QueryBuilder queryBuilder = getQueryBuilder(searchCriteria); SearchRequestBuilder requestBuilder = getSearchRequestBuilder(searchCriteria.getIndexes(), searchCriteria.getDocumentTypes(), searchCriteria.getFrom(), searchCriteria.getSize()); requestBuilder.addFields(SearchDocumentFieldName.productQueryFields); if(searchCriteria.isRescoreOnSoldOut()) { Rescorer rescorer = RescoreBuilder.queryRescorer(QueryBuilders.termQuery(SearchDocumentFieldName.SOLD_OUT.getFieldName(), false)) .setQueryWeight(1.0f) //default .setRescoreQueryWeight(1.5f) ; requestBuilder.setRescorer(rescorer); } if (searchCriteria.hasFilters()) { AndFilterBuilder andFilterBuilder = getFilterBuilderForSearchCriteria(searchCriteria); requestBuilder.setQuery(QueryBuilders.filteredQuery(queryBuilder, andFilterBuilder)); } else { requestBuilder.setQuery(queryBuilder); } if (!searchCriteria.isNoFacets() && searchCriteria.getFacets().size() > 0) { addFacets(searchCriteria, requestBuilder); } //Add sorting if(searchCriteria.getSortOrder() !=null) { //First on given field requestBuilder.addSort(SortBuilders.fieldSort(SearchDocumentFieldName.AVAILABLE_DATE.getFieldName()).order(searchCriteria.getSortOrder()).missing("_last")); //then on score based requestBuilder.addSort(SortBuilders.scoreSort()); } logger.debug("Executing following search request:" + requestBuilder.internalBuilder().toString()); SearchResponse searchResponse = requestBuilder.execute().actionGet(); printSearchResponseForDebug(searchResponse); return getProductSearchResults(searchResponse); }
private FilterBuilder getSpecificationsFacetFilterBuilder(SearchCriteria searchCriteria) { //This is used in case you want to search based on separate resolution/memory facets Map<String, List<String>> multiSelectFilters = searchCriteria.getMultiSelectFilters(); List<String> resolutionFilters = new ArrayList<String>(); List<String> memoryFilters = new ArrayList<String>(); for (Entry<String, List<String>> entry : multiSelectFilters.entrySet()) { if(entry.getKey().equals(SearchFacetName.SPECIFICATION_RESOLUTION.getCode())) { resolutionFilters.addAll(entry.getValue()); } else if(entry.getKey().equals(SearchFacetName.SPECIFICATION_MEMORY.getCode())) { memoryFilters.addAll(entry.getValue()); } } if(resolutionFilters.size() == 0 && memoryFilters.size() == 0 ) { return FilterBuilders.queryFilter(QueryBuilders.matchAllQuery()); } AndFilterBuilder andResolutionAndMemoryFilterBuilder = FilterBuilders.andFilter(); if(resolutionFilters.size() > 0) { OrFilterBuilder OrResolutionFilterBuilder = FilterBuilders.orFilter(); for (String resolution : resolutionFilters) { OrResolutionFilterBuilder.add(FilterBuilders.termFilter(SearchDocumentFieldName.RESOLUTION.getFieldName(), resolution)); } andResolutionAndMemoryFilterBuilder.add(OrResolutionFilterBuilder); } if(memoryFilters.size() > 0) { OrFilterBuilder OrMemoryFilterBuilder = FilterBuilders.orFilter(); for (String memory : memoryFilters) { OrMemoryFilterBuilder.add(FilterBuilders.termFilter(SearchDocumentFieldName.MEMORY.getFieldName(), memory)); } andResolutionAndMemoryFilterBuilder.add(OrMemoryFilterBuilder); } // else if(specifications.size() == 1) // { // Specification specification = searchCriteria.getSpecifications().get(0); // // FilterBuilder filterBuilder = FilterBuilders.andFilter(FilterBuilders // .termFilter(SearchDocumentFieldName.RESOLUTION.getFieldName(), specification.getResolution()), // FilterBuilders.termFilter(SearchDocumentFieldName.MEMORY.getFieldName(), specification.getMemory())); // // specificationFilterBuilder = filterBuilder; // } // else // { // specificationFilterBuilder = FilterBuilders.matchAllFilter(); // } return andResolutionAndMemoryFilterBuilder; }
@Override public ProductSearchResult searchProducts(final SearchCriteria searchCriteria) { final QueryBuilder queryBuilder = getQueryBuilder(searchCriteria); final SearchRequestBuilder requestBuilder = getSearchRequestBuilder(searchCriteria.getIndexes(), searchCriteria.getDocumentTypes(), searchCriteria.getFrom(), searchCriteria.getSize()); requestBuilder.addFields(SearchDocumentFieldName.productQueryFields); if (searchCriteria.isRescoreOnSoldOut()) { final Rescorer rescorer = RescoreBuilder .queryRescorer(QueryBuilders.termQuery(SearchDocumentFieldName.SOLD_OUT.getFieldName(), false)).setQueryWeight(1.0f) // default .setRescoreQueryWeight(1.5f); requestBuilder.setRescorer(rescorer); } if (searchCriteria.hasFilters()) { final AndFilterBuilder andFilterBuilder = getFilterBuilderForSearchCriteria(searchCriteria); requestBuilder.setQuery(QueryBuilders.filteredQuery(queryBuilder, andFilterBuilder)); } else { requestBuilder.setQuery(queryBuilder); } if (!searchCriteria.isNoFacets() && searchCriteria.getFacets().size() > 0) { addFacets(searchCriteria, requestBuilder); } // Add sorting if (searchCriteria.getSortOrder() != null) { // First on given field requestBuilder.addSort(SortBuilders.fieldSort(SearchDocumentFieldName.AVAILABLE_DATE.getFieldName()) .order(searchCriteria.getSortOrder()).missing("_last")); // then on score based requestBuilder.addSort(SortBuilders.scoreSort()); } logger.debug("Executing following search request:" + requestBuilder.internalBuilder().toString()); final SearchResponse searchResponse = requestBuilder.execute().actionGet(); printSearchResponseForDebug(searchResponse); return getProductSearchResults(searchResponse); }
private FilterBuilder getSpecificationsFacetFilterBuilder(final SearchCriteria searchCriteria) { // This is used in case you want to search based on separate resolution/memory facets final Map<String, List<String>> multiSelectFilters = searchCriteria.getMultiSelectFilters(); final List<String> resolutionFilters = new ArrayList<String>(); final List<String> memoryFilters = new ArrayList<String>(); for (final Entry<String, List<String>> entry : multiSelectFilters.entrySet()) { if (entry.getKey().equals(SearchFacetName.SPECIFICATION_RESOLUTION.getCode())) { resolutionFilters.addAll(entry.getValue()); } else if (entry.getKey().equals(SearchFacetName.SPECIFICATION_MEMORY.getCode())) { memoryFilters.addAll(entry.getValue()); } } if (resolutionFilters.size() == 0 && memoryFilters.size() == 0) { return FilterBuilders.queryFilter(QueryBuilders.matchAllQuery()); } final AndFilterBuilder andResolutionAndMemoryFilterBuilder = FilterBuilders.andFilter(); if (resolutionFilters.size() > 0) { final OrFilterBuilder OrResolutionFilterBuilder = FilterBuilders.orFilter(); for (final String resolution : resolutionFilters) { OrResolutionFilterBuilder.add(FilterBuilders.termFilter(SearchDocumentFieldName.RESOLUTION.getFieldName(), resolution)); } andResolutionAndMemoryFilterBuilder.add(OrResolutionFilterBuilder); } if (memoryFilters.size() > 0) { final OrFilterBuilder OrMemoryFilterBuilder = FilterBuilders.orFilter(); for (final String memory : memoryFilters) { OrMemoryFilterBuilder.add(FilterBuilders.termFilter(SearchDocumentFieldName.MEMORY.getFieldName(), memory)); } andResolutionAndMemoryFilterBuilder.add(OrMemoryFilterBuilder); } // else if(specifications.size() == 1) // { // Specification specification = searchCriteria.getSpecifications().get(0); // // FilterBuilder filterBuilder = FilterBuilders.andFilter(FilterBuilders // .termFilter(SearchDocumentFieldName.RESOLUTION.getFieldName(), specification.getResolution()), // FilterBuilders.termFilter(SearchDocumentFieldName.MEMORY.getFieldName(), specification.getMemory())); // // specificationFilterBuilder = filterBuilder; // } // else // { // specificationFilterBuilder = FilterBuilders.matchAllFilter(); // } return andResolutionAndMemoryFilterBuilder; }
private FilterBuilder getSpecificationsFacetFilterBuilder( final SearchCriteria searchCriteria) { // This is used in case you want to search based on separate // resolution/memory facets final Map<String, List<String>> multiSelectFilters = searchCriteria .getMultiSelectFilters(); final List<String> resolutionFilters = new ArrayList<String>(); final List<String> memoryFilters = new ArrayList<String>(); for (final Entry<String, List<String>> entry : multiSelectFilters .entrySet()) { if (entry.getKey().equals( SearchFacetName.SPECIFICATION_RESOLUTION.getCode())) { resolutionFilters.addAll(entry.getValue()); } else if (entry.getKey().equals( SearchFacetName.SPECIFICATION_MEMORY.getCode())) { memoryFilters.addAll(entry.getValue()); } } if (resolutionFilters.size() == 0 && memoryFilters.size() == 0) { return FilterBuilders.queryFilter(QueryBuilders.matchAllQuery()); } final AndFilterBuilder andResolutionAndMemoryFilterBuilder = FilterBuilders .andFilter(); if (resolutionFilters.size() > 0) { final OrFilterBuilder OrResolutionFilterBuilder = FilterBuilders .orFilter(); for (final String resolution : resolutionFilters) { OrResolutionFilterBuilder.add(FilterBuilders.termFilter( SearchDocumentFieldName.RESOLUTION.getFieldName(), resolution)); } andResolutionAndMemoryFilterBuilder.add(OrResolutionFilterBuilder); } if (memoryFilters.size() > 0) { final OrFilterBuilder OrMemoryFilterBuilder = FilterBuilders .orFilter(); for (final String memory : memoryFilters) { OrMemoryFilterBuilder.add(FilterBuilders.termFilter( SearchDocumentFieldName.MEMORY.getFieldName(), memory)); } andResolutionAndMemoryFilterBuilder.add(OrMemoryFilterBuilder); } // else if(specifications.size() == 1) // { // Specification specification = // searchCriteria.getSpecifications().get(0); // // FilterBuilder filterBuilder = FilterBuilders.andFilter(FilterBuilders // .termFilter(SearchDocumentFieldName.RESOLUTION.getFieldName(), // specification.getResolution()), // FilterBuilders.termFilter(SearchDocumentFieldName.MEMORY.getFieldName(), // specification.getMemory())); // // specificationFilterBuilder = filterBuilder; // } // else // { // specificationFilterBuilder = FilterBuilders.matchAllFilter(); // } return andResolutionAndMemoryFilterBuilder; }
/** * Create "terms aggregation" which can be [optionally] nested in "filtered aggregation" if any filters * are used and always nested in "global filter". * * Nesting of aggregations. First comes the global aggregation, first-level nested * is filter aggregation and second-level nested is terms aggregation. * <pre> * { * "aggs" : { * "aggregationName" : { * "global" : {}, * // if any filters from searchFilters apply * "aggs" : { * "aggregationName_filter" : { * "filter" : { _filters_ }, * // buckets * "aggs" : { * "aggregationName_buckets" : { * "terms" : { * "field" : ... , * "size" : ... * } * } * } * } * } * } * } * } * </pre> * * @param aggregationName top level name of the aggregation * @param aggregationField index field the aggregation buckets are calculated for * @param size terms field size * @param searchFilters used filters * @param excluding if true then filters on top of aggregationField are excluded from searchFilters * @return GlobalBuilder */ protected GlobalBuilder createTermsBuilder(String aggregationName, String aggregationField, int size, Map<String, FilterBuilder> searchFilters, boolean excluding) { FilterAggregationBuilder fab = null; if (searchFilters != null && !searchFilters.isEmpty()) { FilterBuilder[] fb = excluding ? filtersMapToArrayExcluding(searchFilters, aggregationField) : filtersMapToArray(searchFilters); if (fb != null && fb.length > 0) { fab = AggregationBuilders.filter(aggregationName + "_filter"); fab.filter(new AndFilterBuilder(fb)); } } TermsBuilder tb = AggregationBuilders.terms(aggregationName+"_buckets").field(aggregationField).size(size); GlobalBuilder gb = AggregationBuilders.global(aggregationName); if (fab != null) { fab.subAggregation(tb); gb.subAggregation(fab); } else { gb.subAggregation(tb); } return gb; }
@Override public ProductSearchResult searchProducts( final SearchCriteria searchCriteria) { final QueryBuilder queryBuilder = getQueryBuilder(searchCriteria); final SearchRequestBuilder requestBuilder = getSearchRequestBuilder( searchCriteria.getIndexes(), searchCriteria.getDocumentTypes(), searchCriteria.getFrom(), searchCriteria.getSize()); requestBuilder.addFields(SearchDocumentFieldName.productQueryFields); if (searchCriteria.isRescoreOnSoldOut()) { final Rescorer rescorer = RescoreBuilder .queryRescorer( QueryBuilders.termQuery( SearchDocumentFieldName.SOLD_OUT .getFieldName(), false)) .setQueryWeight(1.0f) // default .setRescoreQueryWeight(1.5f); requestBuilder.setRescorer(rescorer); } if (searchCriteria.hasFilters()) { final AndFilterBuilder andFilterBuilder = getFilterBuilderForSearchCriteria(searchCriteria); requestBuilder.setQuery(QueryBuilders.filteredQuery(queryBuilder, andFilterBuilder)); } else { requestBuilder.setQuery(queryBuilder); } if (!searchCriteria.isNoFacets() && searchCriteria.getFacets().size() > 0) { addFacets(searchCriteria, requestBuilder); } // Add sorting if (searchCriteria.getSortOrder() != null) { // First on given field requestBuilder.addSort(SortBuilders .fieldSort( SearchDocumentFieldName.AVAILABLE_DATE .getFieldName()) .order(searchCriteria.getSortOrder()).missing("_last")); // then on score based requestBuilder.addSort(SortBuilders.scoreSort()); } logger.debug("Executing following search request:" + requestBuilder.internalBuilder().toString()); final SearchResponse searchResponse = requestBuilder.execute() .actionGet(); printSearchResponseForDebug(searchResponse); return getProductSearchResults(searchResponse); }