private FilterBuilder getFilterBuilder(SearchQuery searchQuery) { Map<String, Object> filterMap = searchQuery.getFilter(); List<FilterBuilder> filterBuilders = new ArrayList<>(); if (filterMap != null && filterMap.size() > 0) { for (String key : filterMap.keySet()) { filterBuilders.add(FilterBuilders.termFilter(key, filterMap.get(key))); } } if (searchQuery.getPriceFrom() != null || searchQuery.getPriceTo() != null) filterBuilders.add( FilterBuilders.rangeFilter("price").from(searchQuery.getPriceFrom()).to(searchQuery.getPriceTo())); if (searchQuery.isShowEvent()) { filterBuilders.add(FilterBuilders.termFilter("is_special", true)); } if (searchQuery.isShowSale()) { filterBuilders.add(FilterBuilders.termFilter("is_sale", true)); } return FilterBuilders.andFilter(filterBuilders.toArray(new FilterBuilder[filterBuilders.size()])); }
private long getTraceCompletionCount(String tenantId, Criteria criteria, boolean onlyFaulty) { String index = client.getIndex(tenantId); if (!refresh(index)) { return 0; } BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, CompletionTime.class); SearchRequestBuilder request = getTraceCompletionRequest(index, criteria, query, 0); if (onlyFaulty) { FilterBuilder filter = FilterBuilders.queryFilter(QueryBuilders.boolQuery() .must(QueryBuilders.matchQuery(ElasticsearchUtil.PROPERTIES_NAME_FIELD, Constants.PROP_FAULT))); request.setPostFilter(FilterBuilders.nestedFilter("properties", filter)); } SearchResponse response = request.execute().actionGet(); if (response.isTimedOut()) { msgLog.warnQueryTimedOut(); return 0; } else { return response.getHits().getTotalHits(); } }
public void getContributionsByCandName(Client client, String candName, int i) { QueryBuilder matchQuery = QueryBuilders.matchQuery("candNm", candName); FilterBuilder contribRangeFilter = FilterBuilders.rangeFilter("contbReceiptAmt").gte(i); StatisticalFacetBuilder facet = FacetBuilders.statisticalFacet("stat1").field("contbReceiptAmt"); SearchRequestBuilder request = client.prepareSearch("contributions") .addSort(SortBuilders.fieldSort("contbReceiptAmt").order(SortOrder.DESC)) .setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(matchQuery).setPostFilter(contribRangeFilter) .addFacet(facet).setFrom(0).setSize(100) .addFields("contbrNm", "candNm", "contbrEmployer", "contbReceiptAmt"); System.out.println("SEARCH QUERY: " + request.toString()); SearchResponse response = request.execute().actionGet(); SearchHits searchHits = response.getHits(); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { Map<String, SearchHitField> fields = hit.getFields(); System.out.println(hit.getId() + ", contbrEmployer=" + fields.get("contbrEmployer").getValue().toString()); } }
private List<FilterBuilder> makeFilterList(BTSConfigItem configItem, BTSObject object) { Set<String> referenceTypes = configurationController.getReferenceTypesSet(object, configItem); List<FilterBuilder> filters = new ArrayList<FilterBuilder>(); for (String ref : referenceTypes) { if (ref.contains(BTSConstants.OWNER_REFERENCED_TYPES_PATH_SEPERATOR)) { String[] split = ref.split("\\."); if (split.length == 2) { filters.add(FilterBuilders.termFilter(new String("type"), ref)); } else if (split.length == 3) { filters.add(FilterBuilders.termFilter( new String("subtype"), ref)); } } } return filters; }
private Optional<OrFilterBuilder> itemTypesFilter() { if (!itemTypesPanes.getSelected().isEmpty()) { List<FilterBuilder> itemTypeFilters = itemTypesPanes.getSelected() .stream() .map(it -> { FilterBuilder itFilter = termFilter("attributes.itemType", it.itemType()); if (it.equipType() != null) { itFilter = andFilter(itFilter, termFilter("attributes.equipType", it.equipType())); } return itFilter; }) .collect(Collectors.toList()); return Optional.of(orFilter(toArray(itemTypeFilters, FilterBuilder.class))); } return Optional.empty(); }
@Test @Ignore public void testQuery() throws Exception { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); FilterBuilder filter = FilterBuilders.boolFilter() .must(FilterBuilders.termFilter("attributes.league", "Flashback Event HC (IC002)")) .must(FilterBuilders.termFilter("attributes.equipType", "Jewel")) .must(FilterBuilders.rangeFilter("modsTotal.#% increased maximum Life").gt(4)) .must(FilterBuilders.termFilter("shop.verified", "yes")) // .must(FilterBuilders.termFilter("attributes.rarity", "Magic")) ; searchSourceBuilder .query(QueryBuilders.filteredQuery(QueryBuilders.boolQuery().minimumNumberShouldMatch(2) .should(QueryBuilders.rangeQuery("modsTotal.#% increased Area Damage")) .should(QueryBuilders.rangeQuery("modsTotal.#% increased Projectile Damage")) .should(QueryBuilders.rangeQuery("modsTotal.#% increased Chaos Damage")), filter)) .sort("_score"); SearchResult result = client.execute(searchSourceBuilder.toString()).getSearchResult(); List<Hit<ExileToolsHit, Void>> hits = result.getHits(ExileToolsHit.class); hits.stream().map(hit -> hit.source).forEach(System.out::println); }
/** * As per ES documentation/tome, the best way to do our search is via Filters */ @Test @Ignore public void testExecuteMjolnerUsingFilters() throws Exception { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); List<FilterBuilder> filters = new LinkedList<>(); filters.add(FilterBuilders.termFilter("attributes.league", "Flashback Event (IC001)")); // filters.add(FilterBuilders.termFilter("info.name", "Mjolner")); filters.add(FilterBuilders.termFilter("info.name", "Hegemony's Era")); filters.add(FilterBuilders.rangeFilter("properties.Weapon.Physical DPS").from(400)); FilterBuilder filter = FilterBuilders.andFilter(filters.toArray(new FilterBuilder[filters.size()])); searchSourceBuilder.query(QueryBuilders.filteredQuery(null, filter)); searchSourceBuilder.size(100); SearchResult result = client.execute(searchSourceBuilder.toString()).getSearchResult(); List<Hit<ExileToolsHit, Void>> hits = result.getHits(ExileToolsHit.class); for (Hit<ExileToolsHit, Void> hit : hits) { // logger.info(hit.source.toString()); // hit.source.getQuality().ifPresent( q -> logger.info(q.toString()) ); hit.source.getPhysicalDPS().ifPresent( q -> logger.info(q.toString()) ); // logger.info(hit.source.toString()); // logger.info(hit.source.getRequirements().getLevel().toString()); // logger.info(hit.source.getExplicitMods().toString()); } }
final protected <T> IPage<T> findUniqueTerms(FilterBuilder filter, AggregationBuilder aggregation, String index, String documentType, Class<T> javaType) { SearchResponse response = client.prepareSearch(index) .setTypes(documentType) .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filter)) .addAggregation(aggregation) .setSize(0) .get(); Terms results = response.getAggregations().get(aggregation.getName()); IPage page = Page.<T>of(Iterables.transform(results.getBuckets(), new Function<Terms.Bucket, T>() { @Override public T apply(Terms.Bucket input) { return (T) input.getKey(); } })); page.setPageSize(page.size()); page.setPageNumber(1); page.setTotalElements(page.size()); return page; }
@Test @Ignore public void testQuery() throws Exception { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); FilterBuilder filter = FilterBuilders.boolFilter() .must(FilterBuilders.termFilter("attributes.league", "Flashback Event HC (IC002)")) .must(FilterBuilders.termFilter("attributes.equipType", "Jewel")) .must(FilterBuilders.rangeFilter("modsTotal.#% increased maximum Life").gt(4)) .must(FilterBuilders.termFilter("shop.verified", "yes")) // .must(FilterBuilders.termFilter("attributes.rarity", "Magic")) ; searchSourceBuilder .query(QueryBuilders.filteredQuery(QueryBuilders.boolQuery().minimumNumberShouldMatch(2) .should(QueryBuilders.rangeQuery("modsTotal.#% increased Area Damage")) .should(QueryBuilders.rangeQuery("modsTotal.#% increased Projectile Damage")) .should(QueryBuilders.rangeQuery("modsTotal.#% increased Chaos Damage")), filter)) .sort("_score"); // SearchResult result = client.execute(searchSourceBuilder.toString()).getSearchResult(); // List<Hit<ExileToolsHit, Void>> hits = result.getHits(ExileToolsHit.class); // hits.stream().map(hit -> hit.source).forEach(System.out::println); }
/** * As per ES documentation/tome, the best way to do our search is via Filters */ @Test @Ignore public void testExecuteMjolnerUsingFilters() throws Exception { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); List<FilterBuilder> filters = new LinkedList<>(); filters.add(FilterBuilders.termFilter("attributes.league", "Flashback Event (IC001)")); // filters.add(FilterBuilders.termFilter("info.name", "Mjolner")); filters.add(FilterBuilders.termFilter("info.name", "Hegemony's Era")); filters.add(FilterBuilders.rangeFilter("properties.Weapon.Physical DPS").from(400)); FilterBuilder filter = FilterBuilders.andFilter(filters.toArray(new FilterBuilder[filters.size()])); searchSourceBuilder.query(QueryBuilders.filteredQuery(null, filter)); searchSourceBuilder.size(100); // SearchResult result = client.execute(searchSourceBuilder.toString()).getSearchResult(); // List<Hit<ExileToolsHit, Void>> hits = result.getHits(ExileToolsHit.class); // for (Hit<ExileToolsHit, Void> hit : hits) { //// logger.info(hit.source.toString()); //// hit.source.getQuality().ifPresent( q -> logger.info(q.toString()) ); // hit.source.getPhysicalDPS().ifPresent( q -> logger.info(q.toString()) ); //// logger.info(hit.source.toString()); //// logger.info(hit.source.getRequirements().getLevel().toString()); //// logger.info(hit.source.getExplicitMods().toString()); // } }
private FilterBuilder getBaseFilterBuilder(String facetName, String fieldValue) { if(facetName.startsWith(SearchFacetName.SEARCH_FACET_TYPE_FACET_PREFIX)) { return getTermFilter(SearchFacetName.CATEGORIES_FIELD_PREFIX + facetName + "." + SearchDocumentFieldName.FACETFILTER.getFieldName(), fieldValue.toLowerCase()); } else if(facetName.startsWith(SearchFacetName.PRODUCT_PRICE_RANGE.getCode())) { return FilterBuilders.rangeFilter(SearchDocumentFieldName.PRICE.getFieldName()).includeLower(true).includeUpper(false).from(fieldValue.split("-")[0]).to(fieldValue.split("-")[1]); } else { return FilterBuilders.termFilter(facetName, fieldValue); } // return null; }
private FilterBuilder getBaseFilterBuilder(final String facetName, final String fieldValue) { if (facetName.startsWith(SearchFacetName.SEARCH_FACET_TYPE_FACET_PREFIX)) { return getTermFilter( SearchFacetName.CATEGORIES_FIELD_PREFIX + facetName + "." + SearchDocumentFieldName.FACETFILTER.getFieldName(), fieldValue.toLowerCase()); } else if (facetName.startsWith(SearchFacetName.PRODUCT_PRICE_RANGE.getCode())) { return FilterBuilders.rangeFilter(SearchDocumentFieldName.PRICE.getFieldName()).includeLower(true).includeUpper(false) .from(fieldValue.split("-")[0]).to(fieldValue.split("-")[1]); } else { return FilterBuilders.termFilter(facetName, fieldValue); } // return null; }
private static FilterBuilder fromValueType(String field, String value, int valueType){ if(value.contains("*") || value.contains("?")){ if( value.length() > 1 && value.indexOf('*') == (value.length()-1)) return FilterBuilders.prefixFilter(field, value.substring(0, value.length()-1)); else return FilterBuilders.queryFilter(QueryBuilders.wildcardQuery(field, value)); } switch(valueType){ case AST_TermExpression.TERM: return FilterBuilders.termFilter(field, value); case AST_TermExpression.PHRASE: /*for(byte b: value.getBytes()){ System.out.printf("0x%02X ", b); }*/ return FilterBuilders.queryFilter(QueryBuilders.matchPhraseQuery(field, value)); } return null; }
@Override public <T> List<T> search(Class<T> clazz, @Nullable QueryBuilder queryBuilder, @Nullable FilterBuilder filterBuilder) { SearchRequestBuilder builder = getSearchRequestBuilder(clazz); if (queryBuilder != null) { builder.setQuery(queryBuilder); } if (filterBuilder != null) { builder.setPostFilter(filterBuilder); } if (logger.isDebugEnabled()) { String queryStr = ""; String filterStr = ""; if (queryBuilder != null) { queryStr = queryBuilder.buildAsBytes().toUtf8(); } if (filterBuilder != null) { filterStr = filterBuilder.buildAsBytes().toUtf8(); } logger.debug("Search for class: {}, query: {}, filter: {}", clazz.getSimpleName(), queryStr, filterStr); } return search(clazz, builder); }
private FilterBuilder getBaseFilterBuilder(final String facetName, final String fieldValue) { if (facetName .startsWith(SearchFacetName.SEARCH_FACET_TYPE_FACET_PREFIX)) { return getTermFilter( SearchFacetName.CATEGORIES_FIELD_PREFIX + facetName + "." + SearchDocumentFieldName.FACETFILTER .getFieldName(), fieldValue.toLowerCase()); } else if (facetName.startsWith(SearchFacetName.PRODUCT_PRICE_RANGE .getCode())) { return FilterBuilders .rangeFilter(SearchDocumentFieldName.PRICE.getFieldName()) .includeLower(true).includeUpper(false) .from(fieldValue.split("-")[0]) .to(fieldValue.split("-")[1]); } else { return FilterBuilders.termFilter(facetName, fieldValue); } // return null; }
private static HistogramScriptFacetBuilder convertHistogramScriptFacet( String label, HistogramFacet facet, String filterJson, FilterBuilder visibilityFilter) { final HistogramScriptFacetBuilder hsf = FacetBuilders.histogramScriptFacet(label); hsf.keyScript(facet.getFacetValue().getKeyValueScript().getKeyScript()); hsf.valueScript(facet.getFacetValue().getKeyValueScript().getValueScript()); if (facet.getFacetValue().getKeyValueScript().isSetScriptParams()) { for (final Map.Entry<String, Object> entry : convertParamsList( facet.getFacetValue().getKeyValueScript().getScriptParams()).entrySet()) { hsf.param(entry.getKey(), entry.getValue()); } } addFacetFilter(hsf, filterJson, visibilityFilter); return hsf; }
protected void addFilters(BoolFilterBuilder boolFilter) { Map<String, Filter> filters = query.getFilters(); if (filters != null) { for (Filter filter : filters.values()) { FilterBuilder filterBuilder = converterFilter(filter); if (filterBuilder != null) { switch (filter.getClause()) { case SHOULD: boolFilter.should(filterBuilder); break; case MUST_NOT: boolFilter.mustNot(filterBuilder); break; case MUST: default: boolFilter.must(filterBuilder); } } } } }
@Override public FilterBuilder convert(Filter filter) { String field = filter.getString("field"); Geometry geometry = filter.get(Geometry.class, "geometry"); ShapeBuilder shapeBuilder = ShapeBuilderUtil.toShapeBuilder(geometry); GeoPolygonFilterBuilder result = null; if (geometry instanceof Polygon) { result = FilterBuilders.geoPolygonFilter(field); for (Coordinate coord : ((Polygon) geometry).getExteriorRing().getCoordinates()) { result.addPoint(coord.y, coord.x); } } else { // throw an error? } return result; }
@Test public void testAndFilter() { // basic steps: // 1. create FilterBuilder by FilterBuilders (optionally caching) FilterBuilder andFilter = FilterBuilders.andFilter(FilterBuilders.rangeFilter("speech_number").from(2).to(5), FilterBuilders.prefixFilter("text_entry", "against")).cache(true); // 2. create filteredQuery type query and then add filter QueryBuilder filteredQuery = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), andFilter); // 3. execute the search as usual SearchResponse response = srb.setQuery(filteredQuery).execute().actionGet(); // read response long totalMatched = response.getHits().getTotalHits(); System.out.println("Total matched: " + totalMatched); }
@Test public void testBoolFilter() { FilterBuilder boolFilter = FilterBuilders.boolFilter().must(FilterBuilders.termFilter("speaker", "WESTMORELAND")) .mustNot(FilterBuilders.termFilter("speech_number", 2)); QueryBuilder filteredQuery = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), boolFilter); // 3. execute the search as usual SearchResponse response = srb.setQuery(filteredQuery).execute().actionGet(); // read response long totalMatched = response.getHits().getTotalHits(); System.out.println("Total matched: " + totalMatched); }
@Test public void testBoolFilterWithMatchQuery() { FilterBuilder boolFilter = FilterBuilders.boolFilter().must(FilterBuilders.termFilter("speaker", "WESTMORELAND")) .mustNot(FilterBuilders.termFilter("speech_number", 2)); QueryBuilder filteredQuery = QueryBuilders.filteredQuery(QueryBuilders.matchQuery("text_entry", "day"), boolFilter); // 3. execute the search as usual SearchResponse response = srb.setQuery(filteredQuery).execute().actionGet(); // read response long totalMatched = response.getHits().getTotalHits(); System.out.println("Total matched: " + totalMatched); }
@Test public void testTermsFilter() { // execution: // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html#_execution_mode FilterBuilder termFilter = FilterBuilders.termsFilter("text_entry", "havoc", "gallant").execution("plain"); QueryBuilder filteredQuery = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), termFilter); SearchResponse response = srb.setQuery(filteredQuery).execute().actionGet(); // read response long totalMatched = response.getHits().getTotalHits(); System.out.println("Total matched: " + totalMatched); }
private QueryBuilder addFilters(QueryBuilder query, Class<?> clazz, Map<String, String[]> filters, Map<String, FilterValuesStrategy> filterStrategies, FilterBuilder... customFilters) { if (clazz == null) { return query; } final List<FilterBuilder> esFilters = buildFilters(clazz.getName(), filters, filterStrategies); if (customFilters != null) { for (FilterBuilder customFilter : customFilters) { if (customFilter != null) { esFilters.add(customFilter); } } } return addFilters(query, esFilters); }
private static FilterBuilder buildAvailabilityFilter() { NestedFilterBuilder rangeFilter = FilterBuilders.nestedFilter( EsContent.LOCATIONS, FilterBuilders.andFilter( FilterBuilders.rangeFilter(EsLocation.AVAILABILITY_TIME) .lte(ElasticsearchUtils.clampDateToFloorMinute(DateTime.now()) .toString()), FilterBuilders.rangeFilter(EsLocation.AVAILABILITY_END_TIME) .gte(ElasticsearchUtils.clampDateToFloorMinute(DateTime.now()) .toString()) ) ); return FilterBuilders.orFilter( rangeFilter, FilterBuilders.hasChildFilter(EsContent.CHILD_ITEM, rangeFilter) ); }
@SuppressWarnings("unchecked") private static FilterBuilder buildRegionFilter( Id regionId, ChannelGroupResolver channelGroupResolver ) { ChannelGroup region; try { Resolved<ChannelGroup<?>> resolved = Futures.get( channelGroupResolver.resolveIds(ImmutableList.of(regionId)), IOException.class ); region = resolved.getResources().first().get(); } catch (IOException e) { throw Throwables.propagate(e); } ImmutableList<ChannelNumbering> channels = ImmutableList.copyOf(region.<ChannelNumbering>getChannels()); ImmutableList<Long> channelsIdsForRegion = channels.stream() .map(c -> c.getChannel().getId()) .map(Id::longValue) .collect(MoreCollectors.toImmutableList()); return FilterBuilders.termsFilter( EsContent.BROADCASTS + "." + EsBroadcast.CHANNEL, channelsIdsForRegion ).cache(true); }
@Override public List<FilterBuilder> getVisibilityFilters() { List<FilterBuilder> filterBuilders = new ArrayList<>(); filterBuilders.add(FilterBuilders.termFilter(IS_VISIBLE, true)); filterBuilders.add(FilterBuilders.termFilter(IS_VISIBLE_IN_PL, true)); return filterBuilders; }
@Override public Set<Id> getProductIds(QueryNode queryNode, Map<String, Object> navFilterParts, Map<String, Set<Object>> uriFilterParts, SearchParams searchParams) { Client client = ElasticSearch.CLIENT.get(); ApplicationContext appCtx = app.context(); Merchant merchant = appCtx.getMerchant(); Store store = appCtx.getStore(); Map<String, Attribute> filterAttributes = attributeService .getAttributesForSearchFilter(TargetObjectCode.PRODUCT_LIST, TargetObjectCode.PRODUCT_FILTER); Map<String, FilterValue> searchQueryFilter = elasticsearchHelper.toQueryMap(navFilterParts, filterAttributes); List<FilterBuilder> builders = productListHelper.getVisibilityFilters(); builders.add(productListHelper.buildQuery(queryNode)); QueryBuilder searchQuery = elasticsearchHelper.buildQuery(builders, searchQueryFilter); SearchRequestBuilder searchRequestBuilder = client .prepareSearch(elasticsearchIndexHelper.indexName(merchant.getId(), store.getId(), Product.class)) .setTypes(Annotations.getIndexedCollectionName(Product.class)) .setSearchType(SearchType.QUERY_THEN_FETCH).setFrom(0).setSize(100000).addFields("product_id") .setQuery(searchQuery).setExplain(false); SearchResponse response = searchRequestBuilder.execute().actionGet(); SearchHits hits = response.getHits(); Set<Id> uniqueDocumentIds = new HashSet<>(); if (hits != null && hits.totalHits() > 0) { for (SearchHit searchHit : hits) { uniqueDocumentIds.add(Id.valueOf(searchHit.id())); } } return uniqueDocumentIds; }
@Override public Id[] getProductIds(ProductList productList, boolean checkVisibility, Integer limit) { SearchResult productListResult = null; try { if(limit == null) limit = 10000; List<FilterBuilder> builders = new ArrayList<>(); if(checkVisibility) productListHelper.getVisibilityFilters(); builders.add(productListHelper.buildQuery(productList.getQueryNode())); Map<String, Attribute> filterAttributes = attributeService .getAttributesForSearchFilter(TargetObjectCode.PRODUCT_LIST, TargetObjectCode.PRODUCT_FILTER); productListResult = elasticsearchService.findItems(Product.class, builders, filterAttributes, null, null, new SearchParams(), 0, limit, null); } catch (Exception ex) { System.out.println(ex.getMessage()); } if (productListResult != null && productListResult.getDocumentIds() != null && productListResult.getDocumentIds().size() > 0) { List<Id> ids = new ArrayList<>(); return elasticsearchHelper.toIds(productListResult.getDocumentIds().toArray()); } return null; }
@GET @Path("query") @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Response getProductsByQuery(@FilterParam Filter filter) { QueryOptions queryOptions = queryOptions(filter); if (storeHeaderExists()) queryOptions = QueryOptions.builder(queryOptions) .limitAttributeToStore("status_description", getStoreFromHeader()) .limitAttributeToStore("status_article", getStoreFromHeader()).build(); QueryNode query = filter.getQuery(); User user = getLoggedInUser(); if(user != null && user.getQueryNode() != null){ query = queryHelper.combine(user.getQueryNode(), filter.getQuery()); } FilterBuilder filterBuilder = queryHelper.buildQuery(query); List<FilterBuilder> builders = new ArrayList<>(); builders.add(filterBuilder); SearchParams searchParams = new SearchParams(); searchParams.setLimit(filter.getLimit()); searchParams.setOffset(filter.getOffset().intValue()); SearchResult productsResult = elasticsearchService.findItems(Product.class, builders, searchParams); Id[] ids = elasticsearchHelper.toIds(productsResult.getDocumentIds().toArray()); List<Product> products = new ArrayList<>(); if (ids != null && ids.length > 0) { products = productRepository.findByIds(Product.class, ids, queryOptions); } app.setQueryMetadata(QueryMetadata.builder().count(productsResult.getTotalNumResults()).build()); return ok(products); }
@Override public FilterBuilder getFilterBuilder() { List<FilterBuilder> filterBuilders = new ArrayList<>(); if (this.getPriceFrom() != null || this.getPriceTo() != null) { filterBuilders.add(FilterBuilders.rangeFilter("price").from(this.getPriceFrom()).to(this.getPriceTo())); } return filterBuilders.size() == 0 ? null : FilterBuilders.andFilter(filterBuilders.toArray(new FilterBuilder[filterBuilders.size()])); }
@Override public <T extends Model> SearchResult findItems(Class<T> modelClass, List<FilterBuilder> filterBuilders, Map<String, Attribute> filterAttributes, Map<String, Object> navFilterParts, Map<String, Set<Object>> uriFilterParts, SearchParams searchParams, Integer offset, Integer limit, String sort) { searchParams.setOffset(offset).setLimit(limit).setSort(sort); return findItems(modelClass, filterBuilders, filterAttributes, navFilterParts, uriFilterParts, searchParams); }
@Override public void processBatchUpdate(BatchData batchData) { if (Str.isEmpty(batchData.forType())) throw new NullPointerException("forType must be set when batch processing attributes"); AttributeTargetObject targetObj = getAttributeTargetObjectByCode(batchData.forType()); if (targetObj == null) throw new IllegalArgumentException("Invalid forType '" + batchData.forType() + "'. Check your attribute target objects mapping (MongoDB collection: attribute_target_objects)."); Map<String, ContextObject<?>> attributes = batchData.attributes(); Map<String, List<Id>> options = batchData.options(); Map<String, ContextObject<List<Id>>> xOptions = batchData.xOptions(); String searchKeyword = batchData.searchKeyword(); QueryNode query = batchData.query(); List<Id> ids = batchData.ids(); List<Id> idsToIgnore = batchData.idsToIgnore(); // No data to process. if ((attributes == null || attributes.isEmpty()) && (options == null || options.isEmpty()) && (xOptions == null || xOptions.isEmpty())) return; // No objects to process. if (Str.isEmpty(searchKeyword) && query == null && (ids == null || ids.isEmpty())) return; Class<? extends AttributeSupport> modelClass = targetObj.toModelType(); if (query != null) { FilterBuilder filterBuilder = queryHelper.buildQuery(query); SearchResult searchResult = elasticsearchService.findItems(modelClass, filterBuilder); } }
/** * This method returns a filter associated with the supplied criteria. * * @param criteria The criteria * @return The filter, or null if not relevant */ public static FilterBuilder buildFilter(Criteria criteria) { if (criteria.getTransaction() != null && criteria.getTransaction().trim().isEmpty()) { return FilterBuilders.missingFilter(TRANSACTION_FIELD); } return null; }
private FilterBuilder rarityOrFilter(List<Rarity> list) { List<TermFilterBuilder> filters = list.stream() .map(l -> termFilter("attributes.rarity", l.displayName())) .collect(Collectors.toList()); TermFilterBuilder[] array = new TermFilterBuilder[filters.size()]; array = filters.toArray(array); return orFilter(array); }
private Optional<FilterBuilder> qualityFilter(RangeOptional t) { if(!itemTypesPanes.getSelected().isEmpty()) { List<FilterBuilder> qualityFilters = itemTypesPanes.getSelected().stream() .map(it -> format("properties.%s.Quality", it.itemType())) .map(name -> t.rangeFilter(name)) .collect(Collectors.toList()); return Optional.of(orFilter(toArray(qualityFilters, FilterBuilder.class))); } return Optional.empty(); }
public FilterBuilder rangeFilter(String name) { FilterBuilder filter = null; if(max.isPresent() && max.get() == 0) { filter = FilterBuilders.notFilter(FilterBuilders.existsFilter(name)); } else { RangeFilterBuilder rfilter = FilterBuilders.rangeFilter(name); min.ifPresent(m -> rfilter.from(m)); max.ifPresent(m -> rfilter.to(m)); filter = rfilter; } return filter; }
final protected <T extends IDocument> T findDocument(FilterBuilder filter, String index, String documentType, Class<T> javaType) { SearchResponse response = client.prepareSearch(index) .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filter)) .setTypes(documentType) .setSize(2) .get(); if (response.getHits().getTotalHits() > 1) { throw new SearchServiceException("Unexpected multiple hits for query."); } else if (response.getHits().getTotalHits() == 1) { return (this.getDocumentFromSearchHit(response.getHits().getAt(0), javaType)); } else { return null; } }
/** * Runs the query asynchronously with a filter * * @param indexQueryPath * @param filter * @return */ public F.Promise<IndexResults<T>> fetchAsync(final IndexQueryPath indexQueryPath, final FilterBuilder filter) { final F.Promise<JestRichResult> jestResultPromise = getSearchRequestBuilder(indexQueryPath, filter).executeAsync(); return jestResultPromise.map(new F.Function<JestRichResult, IndexResults<T>>() { @Override public IndexResults<T> apply(JestRichResult jestResult) throws Throwable { return toSearchResults(jestResult); } }); }
@Test public void testAddPercolator() throws Exception { // Prepare: Setup Thrift Client and add some docs populateWithTestDocs(); final FilterBuilder geoDistanceFilter = new GeoDistanceFilterBuilder(TEST_TYPE + ".location").distance(10, DistanceUnit.KILOMETERS) .lat(whiteHouse.getLocation().getLat()).lon(whiteHouse.getLocation().getLon()); final QueryBuilder filteredQuery = new FilteredQueryBuilder(QueryBuilders.matchAllQuery(), geoDistanceFilter); final PercolateQuery percolator = new PercolateQuery(); percolator.setVisibility(new Visibility().setFormalVisibility("U")); percolator.setId(UUID.randomUUID().toString()); final String queryDoc = jsonBuilder().startObject().field("query", filteredQuery).endObject().string(); percolator.setQueryDocument(queryDoc); // Action: Create and execute an OR match query on title final Document doc = new Document( ElasticUtils.PERCOLATOR_TYPE, new Visibility().setFormalVisibility("U"), percolator.getQueryDocument()); final IndexResponse percolateResponse = client.put(Collections.singletonList(doc)).get(0); assertTrue(percolateResponse.isSuccess()); // Assert: There should be 2 matching documents oneaaNavyYardDoc.setPercolate(new PercolateRequest()); oneaaColumbiaDoc.setPercolate(new PercolateRequest()); whiteHouseDoc.setPercolate(new PercolateRequest()); washingtonMonumentDoc.setPercolate(new PercolateRequest()); final List<PercolateQuery> matches = client.percolate( Arrays.asList(oneaaNavyYardDoc, oneaaColumbiaDoc, whiteHouseDoc, washingtonMonumentDoc)); assertEquals(3, matches.size()); }
@Test public void testPercolator() throws Exception { // Prepare: Setup Thrift Client and add some docs client = pool.getClient(SERVICE_NAME, EzElastic.Client.class); client.setTypeMapping(TEST_TYPE, getMappingForTest(), fakeSecurity); client.bulkPut( Arrays.asList(washingtonMonumentDoc, whiteHouseDoc, oneaaColumbiaDoc, oneaaNavyYardDoc), fakeSecurity); final FilterBuilder geoDistanceFilter = new GeoDistanceFilterBuilder(TEST_TYPE + ".location").distance(10, DistanceUnit.KILOMETERS) .lat(whiteHouse.getLocation().getLat()).lon(whiteHouse.getLocation().getLon()); final QueryBuilder filteredQuery = new FilteredQueryBuilder(QueryBuilders.matchAllQuery(), geoDistanceFilter); final PercolateQuery percolator = new PercolateQuery(); percolator.setVisibility(new Visibility().setFormalVisibility("U")); percolator.setId(UUID.randomUUID().toString()); final String queryDoc = jsonBuilder().startObject().field("query", filteredQuery).endObject().string(); percolator.setQueryDocument(queryDoc); // Action: Create and execute an OR match query on title final IndexResponse percolateResponse = client.addPercolateQuery(percolator, fakeSecurity); assertTrue(percolateResponse.isSuccess()); // Assert: There should be 3 matching documents oneaaNavyYardDoc.setPercolate(new PercolateRequest()); oneaaColumbiaDoc.setPercolate(new PercolateRequest()); whiteHouseDoc.setPercolate(new PercolateRequest()); washingtonMonumentDoc.setPercolate(new PercolateRequest()); final List<PercolateQuery> matches = client.percolate( Arrays.asList( oneaaNavyYardDoc, oneaaColumbiaDoc, whiteHouseDoc, washingtonMonumentDoc), fakeSecurity); assertEquals(3, matches.size()); pool.returnToPool(client); }