Search in sources :

Example 86 with SearchHits

use of org.elasticsearch.search.SearchHits in project graylog2-server by Graylog2.

the class FieldContentValueAlertConditionTest method testRunMatchingMessagesInStream.

@Test
public void testRunMatchingMessagesInStream() throws Exception {
    final SearchHits searchHits = mock(SearchHits.class);
    final SearchHit searchHit = mock(SearchHit.class);
    final HashMap<String, Object> source = Maps.newHashMap();
    source.put("message", "something is in here");
    when(searchHit.getId()).thenReturn("some id");
    when(searchHit.getSource()).thenReturn(source);
    when(searchHit.getIndex()).thenReturn("graylog_test");
    when(searchHits.iterator()).thenReturn(Iterators.singletonIterator(searchHit));
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final IndexRange indexRange = MongoIndexRange.create("graylog_test", now.minusDays(1), now, now, 0);
    final Set<IndexRange> indexRanges = Sets.newHashSet(indexRange);
    final SearchResult searchResult = spy(new SearchResult(searchHits, indexRanges, "message:something", null, new TimeValue(100, TimeUnit.MILLISECONDS)));
    when(searchResult.getTotalResults()).thenReturn(1L);
    when(searches.search(anyString(), anyString(), any(RelativeRange.class), anyInt(), anyInt(), any(Sorting.class))).thenReturn(searchResult);
    final FieldContentValueAlertCondition condition = getCondition(getParametersMap(0, "message", "something"), "Alert Condition for testing");
    final AlertCondition.CheckResult result = condition.runCheck();
    assertTriggered(condition, result);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResult(org.graylog2.indexer.results.SearchResult) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DateTime(org.joda.time.DateTime) Sorting(org.graylog2.indexer.searches.Sorting) IndexRange(org.graylog2.indexer.ranges.IndexRange) MongoIndexRange(org.graylog2.indexer.ranges.MongoIndexRange) RelativeRange(org.graylog2.plugin.indexer.searches.timeranges.RelativeRange) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) SearchHits(org.elasticsearch.search.SearchHits) TimeValue(org.elasticsearch.common.unit.TimeValue) Test(org.junit.Test) AlertConditionTest(org.graylog2.alerts.AlertConditionTest)

Example 87 with SearchHits

use of org.elasticsearch.search.SearchHits in project sonarqube by SonarSource.

the class EsUtilsTest method convertToDocs_empty.

@Test
public void convertToDocs_empty() {
    SearchHits hits = mock(SearchHits.class, Mockito.RETURNS_MOCKS);
    List<BaseDoc> docs = EsUtils.convertToDocs(hits, new Function<Map<String, Object>, BaseDoc>() {

        @Override
        public BaseDoc apply(Map<String, Object> input) {
            return new IssueDoc(input);
        }
    });
    assertThat(docs).isEmpty();
}
Also used : IssueDoc(org.sonar.server.issue.index.IssueDoc) SearchHits(org.elasticsearch.search.SearchHits) Map(java.util.Map) Test(org.junit.Test)

Example 88 with SearchHits

use of org.elasticsearch.search.SearchHits in project sonarqube by SonarSource.

the class FooIndex method hasAccessToProject.

public boolean hasAccessToProject(String projectUuid) {
    SearchHits hits = esClient.prepareSearch(FOO_INDEX).setTypes(FOO_TYPE).setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery(FooIndexDefinition.FIELD_PROJECT_UUID, projectUuid)).filter(authorizationTypeSupport.createQueryFilter())).get().getHits();
    List<String> names = Arrays.stream(hits.hits()).map(h -> h.getSource().get(FooIndexDefinition.FIELD_NAME).toString()).collect(Collectors.toList());
    return names.size() == 2 && names.contains("bar") && names.contains("baz");
}
Also used : Arrays(java.util.Arrays) List(java.util.List) SearchHits(org.elasticsearch.search.SearchHits) FOO_INDEX(org.sonar.server.permission.index.FooIndexDefinition.FOO_INDEX) Collectors(org.sonar.core.util.stream.Collectors) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) FOO_TYPE(org.sonar.server.permission.index.FooIndexDefinition.FOO_TYPE) EsClient(org.sonar.server.es.EsClient) SearchHits(org.elasticsearch.search.SearchHits)

Example 89 with SearchHits

use of org.elasticsearch.search.SearchHits in project fess by codelibs.

the class EsAbstractBehavior method delegateBulkRequest.

protected void delegateBulkRequest(final ConditionBean cb, Function<SearchHits, Boolean> handler) {
    SearchResponse response = null;
    while (true) {
        if (response == null) {
            final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setTypes(asEsIndexType()).setScroll(scrollForCursor).setSize(sizeForCursor);
            final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
            if (esCb.getPreference() != null) {
                builder.setPreference(esCb.getPreference());
            }
            esCb.request().build(builder);
            response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
        } else {
            final String scrollId = response.getScrollId();
            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
        }
        final SearchHits searchHits = response.getHits();
        final SearchHit[] hits = searchHits.getHits();
        if (hits.length == 0) {
            break;
        }
        if (!handler.apply(searchHits)) {
            break;
        }
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 90 with SearchHits

use of org.elasticsearch.search.SearchHits in project fess by codelibs.

the class EsAbstractBehavior method delegateSelectList.

@Override
protected <RESULT extends ENTITY> List<RESULT> delegateSelectList(final ConditionBean cb, final Class<? extends RESULT> entityType) {
    // #pending check response
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setTypes(asEsSearchType());
    final int from;
    final int size;
    if (cb.isFetchScopeEffective()) {
        from = cb.getPageStartIndex();
        size = cb.getFetchSize();
    } else {
        from = 0;
        size = 10;
    }
    builder.setFrom(from);
    builder.setSize(size);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        builder.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    final SearchResponse response = esCb.build(builder).execute().actionGet(searchTimeout);
    final EsPagingResultBean<RESULT> list = new EsPagingResultBean<>(builder);
    final SearchHits searchHits = response.getHits();
    searchHits.forEach(hit -> {
        final Map<String, Object> source = hit.getSource();
        final RESULT entity = createEntity(source, entityType);
        final DocMeta docMeta = ((EsAbstractEntity) entity).asDocMeta();
        docMeta.id(hit.getId());
        docMeta.version(hit.getVersion());
        list.add(entity);
    });
    list.setPageSize(size);
    list.setAllRecordCount((int) searchHits.getTotalHits());
    list.setCurrentPageNumber(cb.getFetchPageNumber());
    list.setTook(response.getTookInMillis());
    list.setTotalShards(response.getTotalShards());
    list.setSuccessfulShards(response.getSuccessfulShards());
    list.setFailedShards(response.getFailedShards());
    list.setAggregation(response.getAggregations());
    return list;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) DocMeta(org.codelibs.fess.es.user.allcommon.EsAbstractEntity.DocMeta) SearchHits(org.elasticsearch.search.SearchHits)

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)95 SearchResponse (org.elasticsearch.action.search.SearchResponse)61 SearchHit (org.elasticsearch.search.SearchHit)52 ArrayList (java.util.ArrayList)24 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)16 IOException (java.io.IOException)15 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)13 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)13 ElasticsearchAssertions.assertSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits)13 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)10 TopDocs (org.apache.lucene.search.TopDocs)9 SearchHitField (org.elasticsearch.search.SearchHitField)9 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)9 Text (org.elasticsearch.common.text.Text)7 ElasticsearchAssertions.assertOrderedSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits)7