Search in sources :

Example 1 with SearchResult

use of com.netflix.conductor.common.run.SearchResult in project conductor by Netflix.

the class WorkflowClientTest method testSearch.

@Test
public void testSearch() {
    ClientResponse clientResponse = mock(ClientResponse.class);
    SearchResult<WorkflowSummary> workflowSearchResult = new SearchResult<>();
    workflowSearchResult.setTotalHits(1);
    WorkflowSummary workflowSummary = new WorkflowSummary(new Workflow());
    workflowSearchResult.setResults(Collections.singletonList(workflowSummary));
    when(clientResponse.getEntity(argThat((GenericType<SearchResult<WorkflowSummary>> type) -> ((ParameterizedTypeImpl) type.getType()).getRawType().equals(SearchResult.class) && ((ParameterizedTypeImpl) type.getType()).getActualTypeArguments()[0].equals(WorkflowSummary.class)))).thenReturn(workflowSearchResult);
    when(clientHandler.handle(argThat(argument -> argument.getURI().equals(URI.create("http://myuri:8080/workflow/search?query=my_complex_query"))))).thenReturn(clientResponse);
    SearchResult<WorkflowSummary> searchResult = workflowClient.search("my_complex_query");
    assertEquals(1, searchResult.getTotalHits());
    assertEquals(Collections.singletonList(workflowSummary), searchResult.getResults());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandler(com.sun.jersey.api.client.ClientHandler) SearchResult(com.netflix.conductor.common.run.SearchResult) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) ClientResponse(com.sun.jersey.api.client.ClientResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) WorkflowSummary(com.netflix.conductor.common.run.WorkflowSummary) Workflow(com.netflix.conductor.common.run.Workflow) GenericType(com.sun.jersey.api.client.GenericType) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) URI(java.net.URI) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Before(org.junit.Before) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Mockito.mock(org.mockito.Mockito.mock) WorkflowSummary(com.netflix.conductor.common.run.WorkflowSummary) Workflow(com.netflix.conductor.common.run.Workflow) SearchResult(com.netflix.conductor.common.run.SearchResult) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) Test(org.junit.Test)

Example 2 with SearchResult

use of com.netflix.conductor.common.run.SearchResult in project conductor by Netflix.

the class WorkflowServiceImplTest method searchV2Test.

@Test
public void searchV2Test() throws InterruptedException {
    CountDownLatch streamAlive = new CountDownLatch(1);
    AtomicReference<WorkflowServicePb.WorkflowSearchResult> result = new AtomicReference<>();
    SearchPb.Request req = SearchPb.Request.newBuilder().setStart(1).setSize(1).setSort("strings").setQuery("").setFreeText("").build();
    StreamObserver<WorkflowServicePb.WorkflowSearchResult> streamObserver = new StreamObserver<WorkflowServicePb.WorkflowSearchResult>() {

        @Override
        public void onNext(WorkflowServicePb.WorkflowSearchResult value) {
            result.set(value);
        }

        @Override
        public void onError(Throwable t) {
            streamAlive.countDown();
        }

        @Override
        public void onCompleted() {
            streamAlive.countDown();
        }
    };
    Workflow workflow = new Workflow();
    SearchResult<Workflow> searchResult = new SearchResult<>();
    searchResult.setTotalHits(1);
    searchResult.setResults(Collections.singletonList(workflow));
    when(workflowService.searchWorkflowsV2(1, 1, Collections.singletonList("strings"), "*", "")).thenReturn(searchResult);
    workflowServiceImpl.searchV2(req, streamObserver);
    streamAlive.await(10, TimeUnit.MILLISECONDS);
    WorkflowServicePb.WorkflowSearchResult workflowSearchResult = result.get();
    assertEquals(1, workflowSearchResult.getTotalHits());
    assertEquals(WorkflowPb.Workflow.newBuilder().build(), workflowSearchResult.getResultsList().get(0));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) SearchPb(com.netflix.conductor.grpc.SearchPb) Workflow(com.netflix.conductor.common.run.Workflow) AtomicReference(java.util.concurrent.atomic.AtomicReference) SearchResult(com.netflix.conductor.common.run.SearchResult) CountDownLatch(java.util.concurrent.CountDownLatch) WorkflowServicePb(com.netflix.conductor.grpc.WorkflowServicePb) Test(org.junit.Test)

Example 3 with SearchResult

use of com.netflix.conductor.common.run.SearchResult in project conductor by Netflix.

the class WorkflowServiceImplTest method searchTest.

@Test
public void searchTest() throws InterruptedException {
    CountDownLatch streamAlive = new CountDownLatch(1);
    AtomicReference<WorkflowServicePb.WorkflowSummarySearchResult> result = new AtomicReference<>();
    SearchPb.Request req = SearchPb.Request.newBuilder().setStart(1).setSize(1).setSort("strings").setQuery("").setFreeText("").build();
    StreamObserver<WorkflowServicePb.WorkflowSummarySearchResult> streamObserver = new StreamObserver<WorkflowServicePb.WorkflowSummarySearchResult>() {

        @Override
        public void onNext(WorkflowServicePb.WorkflowSummarySearchResult value) {
            result.set(value);
        }

        @Override
        public void onError(Throwable t) {
            streamAlive.countDown();
        }

        @Override
        public void onCompleted() {
            streamAlive.countDown();
        }
    };
    WorkflowSummary workflow = new WorkflowSummary();
    SearchResult<WorkflowSummary> searchResult = new SearchResult<>();
    searchResult.setTotalHits(1);
    searchResult.setResults(Collections.singletonList(workflow));
    when(workflowService.searchWorkflows(anyInt(), anyInt(), anyList(), anyString(), anyString())).thenReturn(searchResult);
    workflowServiceImpl.search(req, streamObserver);
    streamAlive.await(10, TimeUnit.MILLISECONDS);
    WorkflowServicePb.WorkflowSummarySearchResult workflowSearchResult = result.get();
    assertEquals(1, workflowSearchResult.getTotalHits());
    assertEquals(WorkflowSummaryPb.WorkflowSummary.newBuilder().build(), workflowSearchResult.getResultsList().get(0));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) WorkflowSummary(com.netflix.conductor.common.run.WorkflowSummary) SearchPb(com.netflix.conductor.grpc.SearchPb) AtomicReference(java.util.concurrent.atomic.AtomicReference) SearchResult(com.netflix.conductor.common.run.SearchResult) CountDownLatch(java.util.concurrent.CountDownLatch) WorkflowServicePb(com.netflix.conductor.grpc.WorkflowServicePb) Test(org.junit.Test)

Example 4 with SearchResult

use of com.netflix.conductor.common.run.SearchResult in project conductor by Netflix.

the class ElasticSearchRestDAOV5 method searchObjectIds.

/**
 * Tries to find object ids for a given query in an index.
 *
 * @param indexName The name of the index.
 * @param queryBuilder The query to use for searching.
 * @param start The start to use.
 * @param size The total return size.
 * @param sortOptions A list of string options to sort in the form VALUE:ORDER; where ORDER is optional and can be either ASC OR DESC.
 * @param docType The document type to searchObjectIdsViaExpression for.
 *
 * @return The SearchResults which includes the count and IDs that were found.
 * @throws IOException If we cannot communicate with ES.
 */
private SearchResult<String> searchObjectIds(String indexName, QueryBuilder queryBuilder, int start, int size, List<String> sortOptions, String docType) throws IOException {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(queryBuilder);
    searchSourceBuilder.from(start);
    searchSourceBuilder.size(size);
    if (sortOptions != null && !sortOptions.isEmpty()) {
        for (String sortOption : sortOptions) {
            SortOrder order = SortOrder.ASC;
            String field = sortOption;
            int index = sortOption.indexOf(":");
            if (index > 0) {
                field = sortOption.substring(0, index);
                order = SortOrder.valueOf(sortOption.substring(index + 1));
            }
            searchSourceBuilder.sort(new FieldSortBuilder(field).order(order));
        }
    }
    // Generate the actual request to send to ES.
    SearchRequest searchRequest = new SearchRequest(indexName);
    searchRequest.types(docType);
    searchRequest.source(searchSourceBuilder);
    SearchResponse response = elasticSearchClient.search(searchRequest);
    List<String> result = new LinkedList<>();
    response.getHits().forEach(hit -> result.add(hit.getId()));
    long count = response.getHits().getTotalHits();
    return new SearchResult<>(count, result);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SortOrder(org.elasticsearch.search.sort.SortOrder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchResult(com.netflix.conductor.common.run.SearchResult) LinkedList(java.util.LinkedList) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with SearchResult

use of com.netflix.conductor.common.run.SearchResult in project conductor by Netflix.

the class ElasticSearchDAOV5 method search.

private SearchResult<String> search(String indexName, String structuredQuery, int start, int size, List<String> sortOptions, String freeTextQuery, String docType) {
    try {
        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        if (StringUtils.isNotEmpty(structuredQuery)) {
            Expression expression = Expression.fromString(structuredQuery);
            queryBuilder = expression.getFilterBuilder();
        }
        BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder);
        QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery(freeTextQuery);
        BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery);
        final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(indexName).setQuery(fq).setTypes(docType).storedFields("_id").setFrom(start).setSize(size);
        if (sortOptions != null) {
            sortOptions.forEach(sortOption -> addSortOptionToSearchRequest(srb, sortOption));
        }
        SearchResponse response = srb.get();
        LinkedList<String> result = StreamSupport.stream(response.getHits().spliterator(), false).map(SearchHit::getId).collect(Collectors.toCollection(LinkedList::new));
        long count = response.getHits().getTotalHits();
        return new SearchResult<>(count, result);
    } catch (ParserException e) {
        String errorMsg = String.format("Error performing search on index:%s with docType:%s", indexName, docType);
        logger.error(errorMsg);
        throw new ApplicationException(Code.BACKEND_ERROR, errorMsg, e);
    }
}
Also used : ParserException(com.netflix.conductor.elasticsearch.query.parser.ParserException) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Expression(com.netflix.conductor.dao.es5.index.query.parser.Expression) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResult(com.netflix.conductor.common.run.SearchResult) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchResult (com.netflix.conductor.common.run.SearchResult)19 Test (org.junit.Test)14 Workflow (com.netflix.conductor.common.run.Workflow)12 WorkflowSummary (com.netflix.conductor.common.run.WorkflowSummary)9 Collections (java.util.Collections)5 SearchPb (com.netflix.conductor.grpc.SearchPb)4 WorkflowServicePb (com.netflix.conductor.grpc.WorkflowServicePb)4 ClientHandler (com.sun.jersey.api.client.ClientHandler)4 ClientResponse (com.sun.jersey.api.client.ClientResponse)4 GenericType (com.sun.jersey.api.client.GenericType)4 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)4 StreamObserver (io.grpc.stub.StreamObserver)4 URI (java.net.URI)4 LinkedList (java.util.LinkedList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 TestCase.assertEquals (junit.framework.TestCase.assertEquals)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 Before (org.junit.Before)4 RunWith (org.junit.runner.RunWith)4