Search in sources :

Example 26 with SearchJob

use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.

the class SearchResource method search.

protected SearchResponse search(String query, int limit, String filter, boolean decorate, SearchUser searchUser, List<String> fieldList, Sort sorting, TimeRange timeRange) {
    final Search search = createSearch(query, limit, filter, fieldList, sorting, timeRange);
    final Optional<String> streamId = Searches.extractStreamId(filter);
    final SearchJob searchJob = searchExecutor.execute(search, searchUser, ExecutionState.empty());
    return extractSearchResponse(searchJob, query, decorate, fieldList, timeRange, streamId);
}
Also used : Search(org.graylog.plugins.views.search.Search) SearchJob(org.graylog.plugins.views.search.SearchJob) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString)

Example 27 with SearchJob

use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.

the class SearchResourceExecutionTest method executeSyncJobTriggersEvent.

@Test
public void executeSyncJobTriggersEvent() {
    mockCurrentUserName("peterchen");
    final SearchDTO search = mockSearchDTO();
    final SearchJob searchJob = new SearchJob("deadbeef", search.toSearch(), "peterchen");
    searchJob.addQueryResultFuture("query", CompletableFuture.completedFuture(QueryResult.emptyResult()));
    searchJob.seal();
    when(queryEngine.execute(any())).thenReturn(searchJob);
    final Response response = this.searchResource.executeSyncJob(search, 100, searchUser);
    final ArgumentCaptor<SearchJobExecutionEvent> eventCaptor = ArgumentCaptor.forClass(SearchJobExecutionEvent.class);
    verify(this.eventBus, times(1)).post(eventCaptor.capture());
    final SearchJobExecutionEvent searchJobExecutionEvent = eventCaptor.getValue();
    assertThat(searchJobExecutionEvent.user()).isEqualTo(currentUser);
    assertThat(searchJobExecutionEvent.searchJob().getId()).isEqualTo("deadbeef");
}
Also used : Response(javax.ws.rs.core.Response) SearchJobExecutionEvent(org.graylog.plugins.views.search.events.SearchJobExecutionEvent) SearchJob(org.graylog.plugins.views.search.SearchJob) Test(org.junit.Test)

Example 28 with SearchJob

use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.

the class SearchExecutor method execute.

public SearchJob execute(Search search, SearchUser searchUser, ExecutionState executionState) {
    final Search searchWithStreams = search.addStreamsToQueriesWithoutStreams(() -> searchUser.streams().loadAll());
    final Search searchWithExecutionState = searchWithStreams.applyExecutionState(objectMapper, firstNonNull(executionState, ExecutionState.empty()));
    authorize(searchWithExecutionState, searchUser);
    final SearchJob searchJob = queryEngine.execute(searchJobService.create(searchWithExecutionState, searchUser.username()));
    try {
        Uninterruptibles.getUninterruptibly(searchJob.getResultFuture(), 60000, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        LOG.error("Error executing search job <{}>", searchJob.getId(), e);
        throw new InternalServerErrorException("Error executing search job: " + e.getMessage(), e);
    } catch (TimeoutException e) {
        throw new InternalServerErrorException("Timeout while executing search job");
    } catch (Exception e) {
        LOG.error("Other error", e);
        throw e;
    }
    return searchJob;
}
Also used : Search(org.graylog.plugins.views.search.Search) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) SearchJob(org.graylog.plugins.views.search.SearchJob) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 29 with SearchJob

use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.

the class SearchResource method executeQuery.

@POST
@ApiOperation(value = "Execute the referenced search query asynchronously", notes = "Starts a new search, irrespective whether or not another is already running", response = SearchJobDTO.class)
@Path("{id}/execute")
@NoAuditEvent("Creating audit event manually in method body.")
@Produces({ MediaType.APPLICATION_JSON, SEARCH_FORMAT_V1 })
public Response executeQuery(@ApiParam(name = "id") @PathParam("id") String id, @ApiParam ExecutionState executionState, @Context SearchUser searchUser) {
    final SearchJob searchJob = searchExecutor.execute(id, searchUser, executionState);
    postAuditEvent(searchJob);
    final SearchJobDTO searchJobDTO = SearchJobDTO.fromSearchJob(searchJob);
    return Response.created(URI.create(BASE_PATH + "/status/" + searchJobDTO.id())).entity(searchJob).build();
}
Also used : SearchJob(org.graylog.plugins.views.search.SearchJob) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 30 with SearchJob

use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.

the class SearchExecutorTest method addsStreamsToSearchWithoutStreams.

@Test
public void addsStreamsToSearchWithoutStreams() {
    final Search search = mockSearch();
    final SearchUser searchUser = mock(SearchUser.class);
    when(searchUser.canReadView(any())).thenReturn(true);
    when(searchUser.canReadStream(any())).thenReturn(true);
    when(searchUser.username()).thenReturn("frank-drebin");
    final SearchJob searchJob = mock(SearchJob.class);
    when(searchJobService.create(search, "frank-drebin")).thenReturn(searchJob);
    when(searchJob.getResultFuture()).thenReturn(CompletableFuture.completedFuture(null));
    when(queryEngine.execute(searchJob)).thenReturn(searchJob);
    when(searchDomain.getForUser(eq("search1"), eq(searchUser))).thenReturn(Optional.of(search));
    this.searchExecutor.execute("search1", searchUser, ExecutionState.empty());
    verify(search, times(1)).addStreamsToQueriesWithoutStreams(any());
}
Also used : Search(org.graylog.plugins.views.search.Search) SearchUser(org.graylog.plugins.views.search.permissions.SearchUser) SearchJob(org.graylog.plugins.views.search.SearchJob) RestResourceBaseTest(org.graylog2.rest.resources.RestResourceBaseTest) Test(org.junit.Test)

Aggregations

SearchJob (org.graylog.plugins.views.search.SearchJob)49 Query (org.graylog.plugins.views.search.Query)24 Search (org.graylog.plugins.views.search.Search)24 Test (org.junit.Test)16 SearchType (org.graylog.plugins.views.search.SearchType)13 SearchConfig (org.graylog.plugins.views.search.engine.SearchConfig)12 QueryResult (org.graylog.plugins.views.search.QueryResult)10 Collections (java.util.Collections)9 Set (java.util.Set)9 Collectors (java.util.stream.Collectors)9 List (java.util.List)8 QueryStringDecorators (org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators)8 Optional (java.util.Optional)7 ElasticsearchQueryString (org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString)7 Message (org.graylog2.plugin.Message)7 IndexLookup (org.graylog.plugins.views.search.elasticsearch.IndexLookup)6 Named (com.google.inject.name.Named)5 MultiSearch (io.searchbox.core.MultiSearch)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5