use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project hazelcast by hazelcast.
the class CommonElasticSourcesTest method given_indexWithOneDocument_whenReadFromElasticSource_thenFinishWithOneResult.
@Test
public void given_indexWithOneDocument_whenReadFromElasticSource_thenFinishWithOneResult() {
indexDocument("my-index", of("name", "Frantisek"));
Pipeline p = Pipeline.create();
BatchSource<String> source = new ElasticSourceBuilder<>().clientFn(elasticClientSupplier()).searchRequestFn(() -> new SearchRequest("my-index")).mapToItemFn(hit -> (String) hit.getSourceAsMap().get("name")).build();
p.readFrom(source).writeTo(Sinks.list(results));
submitJob(p);
assertThat(results).containsExactly("Frantisek");
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project hazelcast by hazelcast.
the class CommonElasticSourcesTest method given_emptyIndex_when_readFromElasticSource_then_finishWithNoResults.
@Test
public void given_emptyIndex_when_readFromElasticSource_then_finishWithNoResults() throws IOException {
// Ideally we would just create the index but it gives "field _id not found" when there are no documents
// in the index, not sure if it is an Elastic bug or wrong setup
//
// elasticClient.indices().create(new CreateIndexRequest("my-index"), DEFAULT);
// Instead we index a document and delete it, ending up with index with correct settings applied
indexDocument("my-index", of("name", "Frantisek"));
deleteDocuments();
Pipeline p = Pipeline.create();
BatchSource<String> source = new ElasticSourceBuilder<>().clientFn(elasticClientSupplier()).searchRequestFn(() -> new SearchRequest("my-index")).mapToItemFn(SearchHit::getSourceAsString).build();
p.readFrom(source).writeTo(Sinks.list(results));
submitJob(p);
assertThat(results).isEmpty();
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project Anserini by castorini.
the class SearchElastic method searchTweets.
public <K> ScoredDocuments searchTweets(String queryString, long t) {
SearchHits results = null;
String specials = "+-=&|><!(){}[]^\"~*?:\\/";
for (int i = 0; i < specials.length(); i++) {
char c = specials.charAt(i);
queryString = queryString.replace(String.valueOf(c), " ");
}
// Do not consider the tweets with tweet ids that are beyond the queryTweetTime
// <querytweettime> tag contains the timestamp of the query in terms of the
// chronologically nearest tweet id within the corpus
RangeQueryBuilder queryTweetTime = QueryBuilders.rangeQuery(TweetGenerator.TweetField.ID_LONG.name).from(0L).to(t);
QueryStringQueryBuilder queryTerms = QueryBuilders.queryStringQuery(queryString).defaultField("contents").analyzer("english");
BoolQueryBuilder query = QueryBuilders.boolQuery().filter(queryTweetTime).should(queryTerms);
SearchRequest searchRequest = new SearchRequest(args.esIndex);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(query);
sourceBuilder.size(args.hits);
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
sourceBuilder.sort(new FieldSortBuilder(TweetGenerator.TweetField.ID_LONG.name).order(SortOrder.DESC));
searchRequest.source(sourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, COMMON_OPTIONS);
results = searchResponse.getHits();
} catch (Exception e) {
LOG.error("Exception during ES query: ", e);
}
ScoreTiesAdjusterReranker reranker = new ScoreTiesAdjusterReranker();
return reranker.rerank(ScoredDocuments.fromESDocs(results), null);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project engine by craftercms.
the class ContentTypeBasedDataFetcher method doGet.
/**
* {@inheritDoc}
*/
@Override
public Object doGet(final DataFetchingEnvironment env) {
Field field = env.getMergedField().getSingleField();
String fieldName = field.getName();
// Get arguments for pagination & sorting
int offset = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_OFFSET)).orElse(0);
int limit = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_LIMIT)).orElse(defaultLimit);
String sortBy = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_BY)).orElse(defaultSortField);
String sortOrder = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_ORDER)).orElse(defaultSortOrder);
List<String> queryFieldIncludes = new LinkedList<>();
// Add content-type to includes, we might need it for a GraphQL TypeResolver
queryFieldIncludes.add(QUERY_FIELD_NAME_CONTENT_TYPE);
List<Map<String, Object>> items = new LinkedList<>();
Map<String, Object> result = new HashMap<>(2);
result.put(FIELD_NAME_ITEMS, items);
// Setup the ES query
SearchSourceBuilder source = new SearchSourceBuilder();
BoolQueryBuilder query = boolQuery();
source.query(query).from(offset).size(limit).sort(sortBy, SortOrder.fromString(sortOrder));
StopWatch watch = new StopWatch(field.getName() + " - " + field.getAlias());
watch.start("build filters");
// Filter by the content-type
switch(fieldName) {
case FIELD_NAME_CONTENT_ITEMS:
query.filter(existsQuery(QUERY_FIELD_NAME_CONTENT_TYPE));
break;
case FIELD_NAME_PAGES:
query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_PAGE));
break;
case FIELD_NAME_COMPONENTS:
query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_COMPONENT));
break;
default:
// Get the content-type name from the field name
query.filter(termQuery(QUERY_FIELD_NAME_CONTENT_TYPE, getOriginalName(fieldName)));
break;
}
// Check the selected fields to build the ES query
Optional<Field> itemsField = field.getSelectionSet().getSelections().stream().map(f -> (Field) f).filter(f -> f.getName().equals(FIELD_NAME_ITEMS)).findFirst();
if (itemsField.isPresent()) {
List<Selection> selections = itemsField.get().getSelectionSet().getSelections();
selections.forEach(selection -> processSelection(StringUtils.EMPTY, selection, query, queryFieldIncludes, env));
}
// Only fetch the selected fields for better performance
source.fetchSource(queryFieldIncludes.toArray(new String[0]), new String[0]);
watch.stop();
logger.debug("Executing query: {}", source);
watch.start("searching items");
SearchResponse response = elasticsearch.search(new SearchRequest().source(source));
watch.stop();
watch.start("processing items");
result.put(FIELD_NAME_TOTAL, response.getHits().totalHits);
if (response.getHits().totalHits > 0) {
for (SearchHit hit : response.getHits().getHits()) {
items.add(fixItems(hit.getSourceAsMap()));
}
}
watch.stop();
if (logger.isTraceEnabled()) {
logger.trace(watch.prettyPrint());
}
return result;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project engine by craftercms.
the class SiteAwareElasticsearchService method updateFilters.
@Override
protected void updateFilters(final SearchRequest request) {
super.updateFilters(request);
BoolQueryBuilder mainQuery = (BoolQueryBuilder) request.source().query();
Authentication auth = null;
SecurityContext context = SecurityContextHolder.getContext();
if (context != null) {
auth = context.getAuthentication();
}
// Include all public items
BoolQueryBuilder securityQuery = boolQuery().should(boolQuery().mustNot(existsQuery(roleFieldName))).should(matchQuery(roleFieldName, "anonymous"));
if (auth != null && !(auth instanceof AnonymousAuthenticationToken) && isNotEmpty(auth.getAuthorities())) {
logger.debug("Filtering search results for roles: {}", auth.getAuthorities());
securityQuery.should(matchQuery(roleFieldName, auth.getAuthorities().stream().map(GrantedAuthority::getAuthority).map(role -> role + " " + (startsWith(role, ROLE_PREFIX) ? removeStart(role, ROLE_PREFIX) : appendIfMissing(role, ROLE_PREFIX))).collect(joining(" "))));
} else {
logger.debug("Filtering search to show only public items");
}
mainQuery.filter(boolQuery().must(securityQuery));
}
Aggregations