use of org.elasticsearch.action.search.SearchRequestBuilder in project fess by codelibs.
the class FessProp method processSearchPreference.
public default default void processSearchPreference(final SearchRequestBuilder searchRequestBuilder, final OptionalThing<FessUserBean> userBean) {
userBean.map(user -> {
if (user.hasRoles(getAuthenticationAdminRolesAsArray())) {
return Constants.SEARCH_PREFERENCE_PRIMARY;
}
return user.getUserId();
}).ifPresent(p -> searchRequestBuilder.setPreference(p)).orElse(() -> LaRequestUtil.getOptionalRequest().map(r -> {
final HttpSession session = r.getSession(false);
if (session != null) {
return session.getId();
}
final String preference = r.getParameter("preference");
if (preference != null) {
return Integer.toString(preference.hashCode());
}
return null;
}).ifPresent(p -> searchRequestBuilder.setPreference(p)));
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project zeppelin by apache.
the class TransportBasedClient method search.
@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
final SearchRequestBuilder reqBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE);
reqBuilder.setIndices();
if (indices != null) {
reqBuilder.setIndices(indices);
}
if (types != null) {
reqBuilder.setTypes(types);
}
if (!StringUtils.isEmpty(query)) {
// So, try to parse as a JSON => if there is an error, consider the query a Lucene one
try {
@SuppressWarnings("rawtypes") final Map source = gson.fromJson(query, Map.class);
reqBuilder.setExtraSource(source);
} catch (final JsonSyntaxException e) {
// This is not a JSON (or maybe not well formatted...)
reqBuilder.setQuery(QueryBuilders.queryStringQuery(query).analyzeWildcard(true));
}
}
reqBuilder.setSize(size);
final SearchResponse searchResp = reqBuilder.get();
final ActionResponse actionResp = new ActionResponse().succeeded(true).totalHits(searchResp.getHits().getTotalHits());
if (searchResp.getAggregations() != null) {
setAggregations(searchResp.getAggregations(), actionResp);
} else {
for (final SearchHit hit : searchResp.getHits()) {
// Fields can be found either in _source, or in fields (it depends on the query)
// => specific for elasticsearch's version < 5
//
String src = hit.getSourceAsString();
if (src == null) {
final Map<String, Object> hitFields = new HashMap<>();
for (final SearchHitField hitField : hit.getFields().values()) {
hitFields.put(hitField.getName(), hitField.getValues());
}
src = gson.toJson(hitFields);
}
actionResp.addHit(new HitWrapper(hit.getIndex(), hit.getType(), hit.getId(), src));
}
}
return actionResp;
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.
the class SearchSliceIT method testNumericSort.
public void testNumericSort() throws Exception {
int numShards = setupIndex(true);
SearchResponse sr = client().prepareSearch("test").setQuery(matchAllQuery()).setSize(0).get();
int numDocs = (int) sr.getHits().getTotalHits();
assertThat(numDocs, equalTo(NUM_DOCS));
int max = randomIntBetween(2, numShards * 3);
for (String field : new String[] { "_uid", "random_int", "static_int" }) {
int fetchSize = randomIntBetween(10, 100);
SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).addSort(SortBuilders.fieldSort("random_int")).setSize(fetchSize);
assertSearchSlicesWithScroll(request, field, max);
}
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.
the class SearchSliceIT method testDocIdSort.
public void testDocIdSort() throws Exception {
int numShards = setupIndex(true);
SearchResponse sr = client().prepareSearch("test").setQuery(matchAllQuery()).setSize(0).get();
int numDocs = (int) sr.getHits().getTotalHits();
assertThat(numDocs, equalTo(NUM_DOCS));
int max = randomIntBetween(2, numShards * 3);
for (String field : new String[] { "_uid", "random_int", "static_int" }) {
int fetchSize = randomIntBetween(10, 100);
SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).addSort(SortBuilders.fieldSort("_doc"));
assertSearchSlicesWithScroll(request, field, max);
}
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.
the class SearchScrollIT method testDeepScrollingDoesNotBlowUp.
/**
* Tests that we use an optimization shrinking the batch to the size of the shard. Thus the Integer.MAX_VALUE window doesn't OOM us.
*/
public void testDeepScrollingDoesNotBlowUp() throws Exception {
client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).execute().get();
/*
* Disable the max result window setting for this test because it'll reject the search's unreasonable batch size. We want
* unreasonable batch sizes to just OOM.
*/
client().admin().indices().prepareUpdateSettings("index").setSettings(Settings.builder().put(IndexSettings.MAX_RESULT_WINDOW_SETTING.getKey(), Integer.MAX_VALUE)).get();
for (SearchType searchType : SearchType.values()) {
SearchRequestBuilder builder = client().prepareSearch("index").setSearchType(searchType).setQuery(QueryBuilders.matchAllQuery()).setSize(Integer.MAX_VALUE).setScroll("1m");
SearchResponse response = builder.execute().actionGet();
try {
ElasticsearchAssertions.assertHitCount(response, 1L);
} finally {
String scrollId = response.getScrollId();
if (scrollId != null) {
clearScroll(scrollId);
}
}
}
}
Aggregations