Java Code Examples for org.elasticsearch.action.search.SearchAction#INSTANCE
The following examples show how to use
org.elasticsearch.action.search.SearchAction#INSTANCE .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Job.java From zentity with Apache License 2.0 | 6 votes |
/** * Submit a search query to Elasticsearch. * * @param indexName The name of the index to search. * @param query The query to search. * @return The search response returned by Elasticsearch. * @throws IOException */ private SearchResponse search(String indexName, String query) throws IOException { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList()); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new NamedXContentRegistry(searchModule .getNamedXContents()), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, query)) { searchSourceBuilder.parseXContent(parser); } SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE); searchRequestBuilder.setIndices(indexName).setSource(searchSourceBuilder); if (this.searchAllowPartialSearchResults != null) searchRequestBuilder.setAllowPartialSearchResults(this.searchAllowPartialSearchResults); if (this.searchBatchedReduceSize != null) searchRequestBuilder.setBatchedReduceSize(this.searchBatchedReduceSize); if (this.searchMaxConcurrentShardRequests != null) searchRequestBuilder.setMaxConcurrentShardRequests(this.searchMaxConcurrentShardRequests); if (this.searchPreFilterShardSize != null) searchRequestBuilder.setPreFilterShardSize(this.searchPreFilterShardSize); if (this.searchPreference != null) searchRequestBuilder.setPreference(this.searchPreference); if (this.searchRequestCache != null) searchRequestBuilder.setRequestCache(this.searchRequestCache); if (this.maxTimePerQuery != null) searchRequestBuilder.setTimeout(TimeValue.parseTimeValue(this.maxTimePerQuery, "timeout")); return searchRequestBuilder.execute().actionGet(); }
Example 2
Source File: TransportFeatureStoreAction.java From elasticsearch-learning-to-rank with Apache License 2.0 | 6 votes |
/** * Perform a test search request to validate the element prior to storing it. * * @param validation validation info * @param element the element stored * @param task the parent task * @param listener the action listener to write to * @param onSuccess action ro run when the validation is successfull */ private void validate(FeatureValidation validation, StorableElement element, Task task, ActionListener<FeatureStoreResponse> listener, Runnable onSuccess) { ValidatingLtrQueryBuilder ltrBuilder = new ValidatingLtrQueryBuilder(element, validation, factory); SearchRequestBuilder builder = new SearchRequestBuilder(client, SearchAction.INSTANCE); builder.setIndices(validation.getIndex()); builder.setQuery(ltrBuilder); builder.setFrom(0); builder.setSize(20); // Bail out early and don't score the whole index. builder.setTerminateAfter(1000); builder.request().setParentTask(clusterService.localNode().getId(), task.getId()); builder.execute(wrap((r) -> { if (r.getFailedShards() > 0) { ShardSearchFailure failure = r.getShardFailures()[0]; throw new IllegalArgumentException("Validating the element caused " + r.getFailedShards() + " shard failures, see root cause: " + failure.reason(), failure.getCause()); } onSuccess.run(); }, (e) -> listener.onFailure(new IllegalArgumentException("Cannot store element, validation failed.", e)))); }
Example 3
Source File: BaseClient.java From elasticsearch-helper with Apache License 2.0 | 6 votes |
public Long mostRecentDocument(String index) { if (client() == null) { return null; } SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE); SortBuilder sort = SortBuilders.fieldSort("_timestamp").order(SortOrder.DESC); SearchResponse searchResponse = searchRequestBuilder.setIndices(index).addField("_timestamp").setSize(1).addSort(sort).execute().actionGet(); if (searchResponse.getHits().getHits().length == 1) { SearchHit hit = searchResponse.getHits().getHits()[0]; if (hit.getFields().get("_timestamp") != null) { return hit.getFields().get("_timestamp").getValue(); } else { return 0L; } } return null; }
Example 4
Source File: IndexFunctionsDaoImpl.java From herd with Apache License 2.0 | 5 votes |
/** * The ids in index function will take as arguments the index name and the document type and will return a list of all the ids in the index. */ @Override public final List<String> getIdsInIndex(String indexName, String documentType) { // Create an array list for storing the ids List<String> idList = new ArrayList<>(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); // Create a search request and set the scroll time and scroll size final SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(new ElasticsearchClientImpl(), SearchAction.INSTANCE); searchRequestBuilder.setIndices(indexName).setTypes(documentType).setScroll(new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME)) .setSize(ELASTIC_SEARCH_SCROLL_PAGE_SIZE).setSource(searchSourceBuilder); // Retrieve the search response final Search.Builder searchBuilder = new Search.Builder(searchRequestBuilder.toString()).addIndex(indexName); searchBuilder.setParameter(Parameters.SIZE, ELASTIC_SEARCH_SCROLL_PAGE_SIZE); searchBuilder.setParameter(Parameters.SCROLL, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString()); JestResult jestResult = jestClientHelper.execute(searchBuilder.build()); // While there are hits available, page through the results and add them to the id list while (jestResult.getSourceAsStringList().size() != 0) { for (String jsonString : jestResult.getSourceAsStringList()) { JsonElement root = new JsonParser().parse(jsonString); idList.add(root.getAsJsonObject().get("id").getAsString()); } String scrollId = jestResult.getJsonObject().get("_scroll_id").getAsString(); SearchScroll scroll = new SearchScroll.Builder(scrollId, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString()).build(); jestResult = jestClientHelper.execute(scroll); } return idList; }
Example 5
Source File: CoordinateSearchRequestBuilder.java From siren-join with GNU Affero General Public License v3.0 | 4 votes |
public CoordinateSearchRequestBuilder(final ElasticsearchClient client) { // hack to be able to subclass SearchRequestBuilder: the action instance is only used in #execute which we overwrite super(client, SearchAction.INSTANCE); }
Example 6
Source File: TransportBasedClient.java From zeppelin with Apache License 2.0 | 4 votes |
@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)) { // The query can be either JSON-formatted, nor a Lucene 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; }
Example 7
Source File: DefaultQueryAction.java From elasticsearch-sql with Apache License 2.0 | 4 votes |
@Override public SqlElasticSearchRequestBuilder explain() throws SqlParseException { Hint scrollHint = null; for (Hint hint : select.getHints()) { if (hint.getType() == HintType.USE_SCROLL) { scrollHint = hint; break; } } if (scrollHint != null && scrollHint.getParams()[0] instanceof String) { return new SqlElasticSearchRequestBuilder(new SearchScrollRequestBuilder(client, SearchScrollAction.INSTANCE, (String) scrollHint.getParams()[0]).setScroll(new TimeValue((Integer) scrollHint.getParams()[1]))); } /* zhongshu-comment 6.1.1.5这个版本和elastic6.1.1这个分支用的是这一行代码 但是在本地调试时我的client没有实例化,并没有去连es,所以这行代码会报空指针 那就将这行注释掉吧,以后就用下面那行 */ // this.request = client.prepareSearch(); /* zhongshu-comment 6.2.4.1这个版本和master_zhongshu_dev_01用的是这一行代码,虽然client为null,但是下面这行代码并不会报空指针 为了在本地调试、执行下文的那些代码获得es的dsl,所以就使用这行代码,暂时将上面哪一行注释掉,上线的时候记得替换掉 变量request是es搜索请求对象,调用的是es的api,SearchRequestBuilder是es的原生api */ this.request = new SearchRequestBuilder(client, SearchAction.INSTANCE); setIndicesAndTypes(); //zhongshu-comment 将Select对象中封装的sql token信息转换并传到成员变量es搜索请求对象request中 setFields(select.getFields()); setWhere(select.getWhere()); setSorts(select.getOrderBys()); setLimit(select.getOffset(), select.getRowCount()); // if (scrollHint != null) { if (!select.isOrderdSelect()) request.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC); request.setSize((Integer) scrollHint.getParams()[0]).setScroll(new TimeValue((Integer) scrollHint.getParams()[1])); } else { request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); } updateRequestWithIndexAndRoutingOptions(select, request); updateRequestWithHighlight(select, request); updateRequestWithCollapse(select, request); updateRequestWithPostFilter(select, request); updateRequestWithStats(select, request); updateRequestWithPreference(select, request); updateRequestWithTrackTotalHits(select, request); updateRequestWithTimeout(select, request); updateRequestWithIndicesOptions(select, request); updateRequestWithMinScore(select, request); SqlElasticSearchRequestBuilder sqlElasticRequestBuilder = new SqlElasticSearchRequestBuilder(request); return sqlElasticRequestBuilder; }