/** * Gets the creation data of the single newest entry. * * @return ISO8601 formatted time of most recent entry, or null on empty or error. */ private String getMostRecentEntry() { long totalEntries = client.prepareCount(index).setQuery(matchAllQuery()).execute().actionGet().getCount(); if (totalEntries > 0) { FilteredQueryBuilder updatedAtQuery = QueryBuilders .filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.existsFilter("created_at")); FieldSortBuilder updatedAtSort = SortBuilders.fieldSort("created_at").order(SortOrder.DESC); SearchResponse response = client.prepareSearch(index) .setQuery(updatedAtQuery) .addSort(updatedAtSort) .setSize(1) .execute() .actionGet(); String createdAt = (String) response.getHits().getAt(0).getSource().get("created_at"); logger.debug("Most recent event was created at {}", createdAt); return createdAt; } else { // getData will get all data on a null. logger.info("No existing entries, assuming first run"); return null; } }
@Test public void deleteUnsupportedProjectEntries() { String projectId="spring-framework"; List<String> supportedVersions = Arrays.asList("4.1.0.RELEASE","4.0.0.RELEASE"); String expected = "{" + "\"query\":{" + "\"filtered\":{" + "\"query\":{\"match_all\":{}}," + "\"filter\":{" + "\"and\":{\"filters\":[" + "{\"term\":{\"projectId\":\"spring-framework\"}}," + "{\"not\":{\"filter\":{" + "\"or\":{\"filters\":[" + "{\"term\":{\"version\":\"4.1.0.RELEASE\"}}," + "{\"term\":{\"version\":\"4.0.0.RELEASE\"}}" + "]}" + "}}}" + "]}" + "}" + "}" + "}}"; FilteredQueryBuilder builder = SaganQueryBuilders.matchUnsupportedProjectEntries(projectId, supportedVersions); String result = SaganQueryBuilders.wrapQuery(builder.toString()); assertThat(result.replaceAll("[\\s|\\r|\\n]",""), equalTo(expected)); }
/** * A method to create filtered query builder with appropriate filters to fetch auto suggestions. * * @param resourceRequest the resource request * @param resourceResponse the resource response * @throws IOException Signals that an I/O exception has occurred. * @throws ElasticsearchAutocompleteException the elasticsearch autocomplete exception */ public void fetchAutoSuggestions(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, ElasticsearchAutocompleteException { if (Validator.isNull(client)) { throw new ElasticsearchAutocompleteException("Client object cannot be null......."); } if (_log.isDebugEnabled()) { _log.debug("About to get suggestions from elasticsearch server......."); } String searchTerm = ParamUtil.getString(resourceRequest, ElasticsearchPortletConstants.SEARCH_TEXT); QueryStringQueryBuilder stringQueryBuilder = QueryBuilders.queryString(searchTerm); appendQueryFieldsToBuilder(stringQueryBuilder); FilteredQueryBuilder filteredQueryBuilder = QueryBuilders.filteredQuery(stringQueryBuilder, prepareBoolFilterBuilder(resourceRequest)); requestBuilder.setQuery(filteredQueryBuilder); SearchResponse response = requestBuilder.execute().actionGet(); PrintWriter out = resourceResponse.getWriter(); out.println(parseResponse(response)); if (_log.isDebugEnabled()) { _log.debug("Suggestions retreived from elasticsearch server...."); } }
@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); }
/** * 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; } }
/** * Apply "document level security" filtering to the query filter. See <a * href="https://github.com/searchisko/searchisko/issues/143">issue #134</a> * * @param qb to apply additional filter to * @return new query filter with applied security filtering */ protected QueryBuilder applyContentLevelSecurityFilter(QueryBuilder qb) { FilterBuilder securityFilter = getContentLevelSecurityFilterInternal(); if (securityFilter == null) { return qb; } else { return new FilteredQueryBuilder(qb, securityFilter); } }
private SettableFuture<SearchResponse> queryInternal( AttributeQuerySet query, Iterable<Publisher> publishers, Selection selection ) { SettableFuture<SearchResponse> response = SettableFuture.create(); EsQueryParser.EsQuery esQuery = esQueryParser.parse(query); QueryBuilder queryBuilder = queryBuilderFactory.buildQuery(esQuery.getAttributeQuerySet()); /* matchAllFilter as a bool filter with less than 1 clause is invalid */ BoolFilterBuilder filterBuilder = FilterBuilders.boolFilter() .must(FilterBuilders.matchAllFilter()); SearchRequestBuilder reqBuilder = esClient .prepareSearch(index) .setTypes( EsContent.CHILD_ITEM, EsContent.TOP_LEVEL_CONTAINER, EsContent.TOP_LEVEL_ITEM ) .addField(EsContent.CANONICAL_ID) .addField(EsContent.ID) .addField(EsContent.SOURCE) .setPostFilter(FiltersBuilder.buildForPublishers(EsContent.SOURCE, publishers)) .setFrom(selection.getOffset()) .setSize(Objects.firstNonNull(selection.getLimit(), DEFAULT_LIMIT)); addOrdering(esQuery.getIndexQueryParams(), reqBuilder); queryBuilder = addFuzzyQuery(esQuery.getIndexQueryParams(), queryBuilder, reqBuilder); addBrandId(esQuery.getIndexQueryParams(), filterBuilder); addSeriesId(esQuery.getIndexQueryParams(), filterBuilder); addTopicFilter(esQuery.getIndexQueryParams(), filterBuilder); addActionableFilter(esQuery.getIndexQueryParams(), filterBuilder); reqBuilder.addSort(EsContent.ID, SortOrder.ASC); FilteredQueryBuilder finalQuery = QueryBuilders.filteredQuery(queryBuilder, filterBuilder); reqBuilder.setQuery(finalQuery); log.debug(reqBuilder.internalBuilder().toString()); reqBuilder.execute(FutureSettingActionListener.setting(response)); return response; }
@Test // @ElasticsearchIndex( // indexName = ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM, // cleanAfter = true, // mappings = @ElasticsearchMapping(typeName = ES_DEFAULT_TYPE_NAME_CAMUNDA_BPM, properties = { // @Elasticsearch // })) public void testIndexingSingleInvoice() throws IOException { HashMap<String,ProcessDataContainer> processesById = TestDataGenerator.startInvoiceProcess(processEngineRule.getProcessEngine(), 1); String[] pids = processesById.keySet().toArray(new String[0]); // elasticsearch ////////////////////////////// flushAndRefresh(); showMappings(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM); FilteredQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter("processInstanceId", pids[0])); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM) .setQuery(query); IoUtil.writeToFile(searchRequestBuilder.toString(), this.getClass().getSimpleName() + ".json", true); SearchResponse searchResponse = searchRequestBuilder.get(); SearchHits hits = searchResponse.getHits(); assertEquals(1, hits.totalHits()); SearchHit hit = hits.getAt(0); assertEquals(pids[0], hit.getId()); assertEquals(ES_DEFAULT_TYPE_NAME_CAMUNDA_BPM, hit.getType()); Map<String,Object> source = hit.getSource(); assertNotNull(source.get("startTime")); assertNotNull(source.get("endTime")); ArrayList variables = (ArrayList) source.get("variables"); assertEquals(5, variables.size()); ArrayList tasks = (ArrayList) source.get("tasks"); assertEquals(9, tasks.size()); ArrayList activities = (ArrayList) source.get("activities"); assertEquals(19, activities.size()); logger.info(searchResponse.toString()); // for (SearchHit searchHit : searchResponse.getHits()) { // logger.info(searchHit.sourceAsString()); // } showMappings(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM); assertMappings(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM); // TODO: write assertions for mapping }
@Test public void testIndexingSingleInvoice() throws IOException { HashMap<String,ProcessDataContainer> processesById = TestDataGenerator.startInvoiceProcess(processEngineRule.getProcessEngine(), 1); String[] pids = processesById.keySet().toArray(new String[0]); // elasticsearch ////////////////////////////// flushAndRefresh(); FilteredQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter("processInstanceId", pids[0])); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM) .setQuery(query); IoUtil.writeToFile(searchRequestBuilder.toString(), this.getClass().getSimpleName() + ".json", true); SearchResponse searchResponse = searchRequestBuilder.get(); SearchHits hits = searchResponse.getHits(); assertEquals(1, hits.totalHits()); SearchHit hit = hits.getAt(0); assertEquals(pids[0], hit.getId()); assertEquals(ES_DEFAULT_TYPE_NAME_CAMUNDA_BPM, hit.getType()); Map<String,Object> source = hit.getSource(); assertNotNull(source.get("startTime")); assertNotNull(source.get("endTime")); ArrayList variables = (ArrayList) source.get("variables"); assertEquals(5, variables.size()); ArrayList tasks = (ArrayList) source.get("tasks"); assertEquals(9, tasks.size()); ArrayList activities = (ArrayList) source.get("activities"); assertEquals(19, activities.size()); logger.info(searchResponse.toString()); // for (SearchHit searchHit : searchResponse.getHits()) { // logger.info(searchHit.sourceAsString()); // } }
public void removeOldProjectEntriesFromIndex(String projectId, List<String> supportedVersions) { FilteredQueryBuilder builder = SaganQueryBuilders.matchUnsupportedProjectEntries(projectId, supportedVersions); String query = SaganQueryBuilders.wrapQuery(builder.toString()); execute(new DeleteByQuery.Builder(query).build()); }