Search in sources :

Example 11 with FacetedSearchResult

use of alien4cloud.dao.model.FacetedSearchResult in project alien4cloud by alien4cloud.

the class CrudCSARSStepDefinition method i_can_find_CSAR.

@Given("^I can find (\\d+) CSAR$")
public void i_can_find_CSAR(int expectedSize) throws Throwable {
    FilteredSearchRequest req = new FilteredSearchRequest(null, 0, 50, null);
    String jSon = jsonMapper.writeValueAsString(req);
    String response = Context.getRestClientInstance().postJSon("/rest/v1/csars/search", jSon);
    RestResponse<FacetedSearchResult> restResponse = JsonUtil.read(response, FacetedSearchResult.class);
    FacetedSearchResult searchResp = restResponse.getData();
    assertNotNull(searchResp);
    assertNotNull(searchResp.getData());
    assertEquals(expectedSize, searchResp.getData().length);
}
Also used : FilteredSearchRequest(alien4cloud.rest.model.FilteredSearchRequest) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult) Given(cucumber.api.java.en.Given)

Example 12 with FacetedSearchResult

use of alien4cloud.dao.model.FacetedSearchResult in project alien4cloud by alien4cloud.

the class TopologyTemplateStepDefinitions method getTopologyFromName.

private static Topology getTopologyFromName(String topologyTemplateName) throws IOException {
    FilteredSearchRequest request = new FilteredSearchRequest();
    request.setFilters(FilterUtil.singleKeyFilter("archiveName", topologyTemplateName));
    request.setFrom(0);
    request.setSize(1);
    String response = Context.getRestClientInstance().postJSon("/rest/v1/catalog/topologies/search", JsonUtil.toString(request));
    RestResponse<FacetedSearchResult> restResponse = JsonUtil.read(response, FacetedSearchResult.class);
    assertEquals(1, restResponse.getData().getData().length);
    return JsonUtil.readObject(JsonUtil.toString(restResponse.getData().getData()[0]), Topology.class);
}
Also used : FilteredSearchRequest(alien4cloud.rest.model.FilteredSearchRequest) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult)

Example 13 with FacetedSearchResult

use of alien4cloud.dao.model.FacetedSearchResult in project alien4cloud by alien4cloud.

the class AbstractToscaIndexSearchService method search.

public FacetedSearchResult search(Class<? extends T> clazz, String query, Integer size, Map<String, String[]> filters) {
    TopHitsBuilder topHitAggregation = AggregationBuilders.topHits("highest_version").setSize(1).addSort(new FieldSortBuilder("nestedVersion.majorVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.minorVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.incrementalVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.qualifier").order(SortOrder.DESC).missing("_first"));
    AggregationBuilder aggregation = AggregationBuilders.terms("query_aggregation").field(getAggregationField()).size(size).subAggregation(topHitAggregation);
    FacetedSearchResult<? extends T> searchResult = alienDAO.buildSearchQuery(clazz, query).setFilters(FilterUtil.singleKeyFilter(filters, "workspace", AlienConstants.GLOBAL_WORKSPACE_ID)).prepareSearch().setFetchContext(FetchContext.SUMMARY, topHitAggregation).facetedSearch(new IAggregationQueryManager() {

        @Override
        public AggregationBuilder getQueryAggregation() {
            return aggregation;
        }

        @Override
        @SneakyThrows({ IOException.class })
        public void setData(ObjectMapper objectMapper, Function getClassFromType, FacetedSearchResult result, Aggregation aggregation) {
            List<Object> resultData = Lists.newArrayList();
            List<String> resultTypes = Lists.newArrayList();
            if (aggregation == null) {
                result.setData(getArray(0));
                result.setTypes(new String[0]);
            }
            for (Terms.Bucket bucket : safe(((Terms) aggregation).getBuckets())) {
                TopHits topHits = bucket.getAggregations().get("highest_version");
                for (SearchHit hit : topHits.getHits()) {
                    resultTypes.add(hit.getType());
                    resultData.add(objectMapper.readValue(hit.getSourceAsString(), ((Function<String, Class>) getClassFromType).apply(hit.getType())));
                }
            }
            result.setData(resultData.toArray(getArray(resultData.size())));
            result.setTypes(resultTypes.toArray(new String[resultTypes.size()]));
            result.setFrom(0);
            result.setTo(resultData.size());
            if (size == Integer.MAX_VALUE || resultData.size() < size) {
                result.setTotalResults(resultData.size());
            } else {
                // just to show that there is more results to fetch but iteration is not possible through aggregations.
                result.setTotalResults(resultData.size() + ((Terms) aggregation).getSumOfOtherDocCounts());
            }
        }
    });
    return searchResult;
}
Also used : TopHitsBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) SearchHit(org.elasticsearch.search.SearchHit) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SneakyThrows(lombok.SneakyThrows) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Function(java.util.function.Function) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) IAggregationQueryManager(alien4cloud.dao.IAggregationQueryManager) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult)

Example 14 with FacetedSearchResult

use of alien4cloud.dao.model.FacetedSearchResult in project alien4cloud by alien4cloud.

the class EsDaoSearchTest method facetedSearchTest.

@Test
public void facetedSearchTest() throws IndexingServiceException, JsonParseException, JsonMappingException, IOException, InterruptedException {
    String searchText = "positive";
    FacetedSearchResult searchResp = dao.facetedSearch(NodeType.class, searchText, null, 10);
    assertNotNull(searchResp);
    assertEquals(2, searchResp.getTotalResults());
    assertEquals(2, searchResp.getTypes().length);
    assertEquals(2, searchResp.getData().length);
    String[] ids = new String[] { indexedNodeTypeTest.getId(), indexedNodeTypeTest4.getId() };
    for (int i = 0; i < searchResp.getData().length; i++) {
        NodeType idnt = (NodeType) searchResp.getData()[i];
        assertElementIn(idnt.getId(), ids);
    }
    // test facets
    Map<String, FacetedSearchFacet[]> mapp = searchResp.getFacets();
    FacetedSearchFacet[] capaFacets = mapp.get("capabilities.type");
    assertNotNull(capaFacets);
    boolean warExist = false;
    long warCount = 0;
    for (int i = 0; i < capaFacets.length; i++) {
        if (capaFacets[i].getFacetValue().equals("war")) {
            warExist = true;
            warCount = capaFacets[i].getCount();
        }
    }
    assertTrue(warExist);
    assertEquals(2, warCount);
    // faceted search with filters
    Map<String, String[]> filters = new HashMap<String, String[]>();
    filters.put("capabilities.type", new String[] { "container" });
    searchResp = dao.facetedSearch(NodeType.class, searchText, filters, 10);
    assertNotNull(searchResp);
    assertEquals(1, searchResp.getTotalResults());
    assertEquals(1, searchResp.getTypes().length);
    assertEquals(1, searchResp.getData().length);
    NodeType idnt = (NodeType) searchResp.getData()[0];
    assertElementIn(idnt.getElementId(), new String[] { "1" });
    // test nothing found
    searchText = "pacpac";
    searchResp = dao.facetedSearch(NodeType.class, searchText, null, 10);
    assertNotNull(searchResp);
    assertNotNull(searchResp.getData());
    assertNotNull(searchResp.getTypes());
    assertEquals(0, searchResp.getData().length);
    assertEquals(0, searchResp.getTypes().length);
}
Also used : FacetedSearchFacet(alien4cloud.dao.model.FacetedSearchFacet) HashMap(java.util.HashMap) NodeType(org.alien4cloud.tosca.model.types.NodeType) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult) Test(org.junit.Test)

Example 15 with FacetedSearchResult

use of alien4cloud.dao.model.FacetedSearchResult in project alien4cloud by alien4cloud.

the class ESGenericSearchDAO method parseAggregations.

/**
 * Parse aggregations and set facets to the given non null FacetedSearchResult instance.
 *
 * @param searchResponse The search response that contains aggregation results.
 * @param facetedSearchResult The instance in which to set facets.
 * @param aggregationQueryManager If not null the data of the FacetedSearchResult will be processed from an aggregation based on the given manager.
 */
private void parseAggregations(SearchResponse searchResponse, FacetedSearchResult facetedSearchResult, IAggregationQueryManager aggregationQueryManager) {
    if (searchResponse.getAggregations() == null) {
        return;
    }
    List<Aggregation> internalAggregationsList = searchResponse.getAggregations().asList();
    if (internalAggregationsList.size() == 0) {
        return;
    }
    Map<String, FacetedSearchFacet[]> facetMap = Maps.newHashMap();
    for (Aggregation aggregation : internalAggregationsList) {
        if (aggregationQueryManager != null && aggregation.getName().equals(aggregationQueryManager.getQueryAggregation().getName())) {
            aggregationQueryManager.setData(getJsonMapper(), getClassFromTypeFunc(), facetedSearchResult, aggregation);
        } else if (aggregation instanceof InternalTerms) {
            InternalTerms internalTerms = (InternalTerms) aggregation;
            List<FacetedSearchFacet> facets = new ArrayList<>();
            for (int i = 0; i < internalTerms.getBuckets().size(); i++) {
                Terms.Bucket bucket = internalTerms.getBuckets().get(i);
                facets.add(new FacetedSearchFacet(bucket.getKey(), bucket.getDocCount()));
            }
            // Find the missing aggregation
            internalAggregationsList.stream().filter(missingAggregation -> missingAggregation instanceof InternalMissing && missingAggregation.getName().equals("missing_" + internalTerms.getName())).findAny().ifPresent(missingAggregation -> {
                InternalMissing internalMissingAggregation = (InternalMissing) missingAggregation;
                if (internalMissingAggregation.getDocCount() > 0) {
                    facets.add(new FacetedSearchFacet(null, internalMissingAggregation.getDocCount()));
                }
            });
            facetMap.put(internalTerms.getName(), facets.toArray(new FacetedSearchFacet[facets.size()]));
        } else {
            log.debug("Aggregation is not a facet aggregation (terms) ignore. Name: {} ,Type: {}", aggregation.getName(), aggregation.getClass().getName());
        }
    }
    facetedSearchResult.setFacets(facetMap);
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SortBuilders(org.elasticsearch.search.sort.SortBuilders) Array(java.lang.reflect.Array) CountRequestBuilder(org.elasticsearch.action.count.CountRequestBuilder) SneakyThrows(lombok.SneakyThrows) MappingBuilder(org.elasticsearch.mapping.MappingBuilder) TopHitsBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder) HashMap(java.util.HashMap) MapUtil(alien4cloud.utils.MapUtil) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) FacetedSearchFacet(alien4cloud.dao.model.FacetedSearchFacet) Function(java.util.function.Function) ISearchBuilderAdapter(org.elasticsearch.mapping.ISearchBuilderAdapter) ScoreFunctionBuilders(org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) SortBuilder(org.elasticsearch.search.sort.SortBuilder) InternalMissing(org.elasticsearch.search.aggregations.bucket.missing.InternalMissing) SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchType(org.elasticsearch.action.search.SearchType) ElasticSearchClient(org.elasticsearch.mapping.ElasticSearchClient) ElasticSearchUtil(alien4cloud.utils.ElasticSearchUtil) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) InternalTerms(org.elasticsearch.search.aggregations.bucket.terms.InternalTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Resource(javax.annotation.Resource) IOException(java.io.IOException) JsonUtil(alien4cloud.rest.utils.JsonUtil) Field(java.lang.reflect.Field) Maps(com.google.common.collect.Maps) FilterValuesStrategy(org.elasticsearch.mapping.FilterValuesStrategy) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult) CombineFunction(org.elasticsearch.common.lucene.search.function.CombineFunction) QueryHelper(org.elasticsearch.mapping.QueryHelper) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryBuilderAdapter(org.elasticsearch.mapping.QueryBuilderAdapter) SortOrder(org.elasticsearch.search.sort.SortOrder) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) SourceFetchContext(org.elasticsearch.mapping.SourceFetchContext) InternalTerms(org.elasticsearch.search.aggregations.bucket.terms.InternalTerms) InternalMissing(org.elasticsearch.search.aggregations.bucket.missing.InternalMissing) FacetedSearchFacet(alien4cloud.dao.model.FacetedSearchFacet) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

FacetedSearchResult (alien4cloud.dao.model.FacetedSearchResult)22 Then (cucumber.api.java.en.Then)6 FilteredSearchRequest (alien4cloud.rest.model.FilteredSearchRequest)5 NodeType (org.alien4cloud.tosca.model.types.NodeType)5 Application (alien4cloud.model.application.Application)3 HashMap (java.util.HashMap)3 SneakyThrows (lombok.SneakyThrows)3 FacetedSearchFacet (alien4cloud.dao.model.FacetedSearchFacet)2 GetMultipleDataResult (alien4cloud.dao.model.GetMultipleDataResult)2 TestUtils.nullAsString (alien4cloud.it.utils.TestUtils.nullAsString)2 Deployment (alien4cloud.model.deployment.Deployment)2 Given (cucumber.api.java.en.Given)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Function (java.util.function.Function)2 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 FilterBuilder (org.elasticsearch.index.query.FilterBuilder)2