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);
}
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();
}
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");
}
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;
}
}
}
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;
}
Aggregations