Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder#setQuery()
The following examples show how to use
org.elasticsearch.action.search.SearchRequestBuilder#setQuery() .
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: ElasticsearchExtendTransportFactory.java From database-transform-tool with Apache License 2.0 | 7 votes |
public String selectTermAll(String indexs,String types,String field,String value){ try { if(client==null){ init(); } SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(",")); request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); request.setQuery(QueryBuilders.termQuery(field, value)); request.highlighter(new HighlightBuilder().field(field)); request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword")); request.setExplain(false); SearchResponse response = request.get(); return response.toString(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
Example 2
Source File: BsFailureUrlCB.java From fess with Apache License 2.0 | 6 votes |
@Override public SearchRequestBuilder build(SearchRequestBuilder builder) { if (_conditionQuery != null) { QueryBuilder queryBuilder = _conditionQuery.getQuery(); if (queryBuilder != null) { builder.setQuery(queryBuilder); } _conditionQuery.getFieldSortBuilderList().forEach(sort -> { builder.addSort(sort); }); } if (_conditionAggregation != null) { _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation); } if (_specification != null) { builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null); } return builder; }
Example 3
Source File: BsWebAuthenticationCB.java From fess with Apache License 2.0 | 6 votes |
@Override public SearchRequestBuilder build(SearchRequestBuilder builder) { if (_conditionQuery != null) { QueryBuilder queryBuilder = _conditionQuery.getQuery(); if (queryBuilder != null) { builder.setQuery(queryBuilder); } _conditionQuery.getFieldSortBuilderList().forEach(sort -> { builder.addSort(sort); }); } if (_conditionAggregation != null) { _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation); } if (_specification != null) { builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null); } return builder; }
Example 4
Source File: BsDataConfigCB.java From fess with Apache License 2.0 | 6 votes |
@Override public SearchRequestBuilder build(SearchRequestBuilder builder) { if (_conditionQuery != null) { QueryBuilder queryBuilder = _conditionQuery.getQuery(); if (queryBuilder != null) { builder.setQuery(queryBuilder); } _conditionQuery.getFieldSortBuilderList().forEach(sort -> { builder.addSort(sort); }); } if (_conditionAggregation != null) { _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation); } if (_specification != null) { builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null); } return builder; }
Example 5
Source File: BsScheduledJobCB.java From fess with Apache License 2.0 | 6 votes |
@Override public SearchRequestBuilder build(SearchRequestBuilder builder) { if (_conditionQuery != null) { QueryBuilder queryBuilder = _conditionQuery.getQuery(); if (queryBuilder != null) { builder.setQuery(queryBuilder); } _conditionQuery.getFieldSortBuilderList().forEach(sort -> { builder.addSort(sort); }); } if (_conditionAggregation != null) { _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation); } if (_specification != null) { builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null); } return builder; }
Example 6
Source File: ScanBuilder.java From dremio-oss with Apache License 2.0 | 6 votes |
protected void applyFilter(SearchRequestBuilder searchRequest, ElasticIntermediateScanPrel scan, ElasticsearchFilter filter, ElasticTableXattr tableAttributes) throws ExpressionNotAnalyzableException { QueryBuilder b = null; if (tableAttributes.hasAliasFilter()) { b = new WrapperQueryBuilder(tableAttributes.getAliasFilter()); } if (filter != null) { QueryBuilder filterQuery = PredicateAnalyzer.analyze(scan, filter.getCondition(), tableAttributes.getVariationDetected()); if (b != null) { b = QueryBuilders.boolQuery().must(filterQuery).must(b); } else { b = filterQuery; } } if (b != null) { searchRequest.setQuery(b); } else { searchRequest.setQuery(QueryBuilders.matchAllQuery()); } }
Example 7
Source File: BsFavoriteLogCB.java From fess with Apache License 2.0 | 6 votes |
@Override public SearchRequestBuilder build(SearchRequestBuilder builder) { if (_conditionQuery != null) { QueryBuilder queryBuilder = _conditionQuery.getQuery(); if (queryBuilder != null) { builder.setQuery(queryBuilder); } _conditionQuery.getFieldSortBuilderList().forEach(sort -> { builder.addSort(sort); }); } if (_conditionAggregation != null) { _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation); } if (_specification != null) { builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null); } return builder; }
Example 8
Source File: BsRequestHeaderCB.java From fess with Apache License 2.0 | 6 votes |
@Override public SearchRequestBuilder build(SearchRequestBuilder builder) { if (_conditionQuery != null) { QueryBuilder queryBuilder = _conditionQuery.getQuery(); if (queryBuilder != null) { builder.setQuery(queryBuilder); } _conditionQuery.getFieldSortBuilderList().forEach(sort -> { builder.addSort(sort); }); } if (_conditionAggregation != null) { _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation); } if (_specification != null) { builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null); } return builder; }
Example 9
Source File: ClientFacade.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
private SearchRequestBuilder createSearchRequest( QueryBuilder query, Integer from, Integer size, Sort sort, List<AggregationBuilder> aggregations, List<Index> indexes) { String[] indexNames = toIndexNames(indexes); SearchRequestBuilder searchRequest = client.prepareSearch(indexNames); if (query != null) { searchRequest.setQuery(query); } if (from != null) { searchRequest.setFrom(from); } if (size != null) { searchRequest.setSize(size); } if (sort != null) { List<SortBuilder> sorts = sortContentBuilder.createSorts(sort); sorts.forEach(searchRequest::addSort); } if (aggregations != null) { aggregations.forEach(searchRequest::addAggregation); } return searchRequest; }
Example 10
Source File: QueryComponent.java From elasticsearch-reindex-tool with Apache License 2.0 | 5 votes |
public SearchResponse prepareSearchScrollRequest() { // find out how many indices and shards are affected by this query to not get huge result sets when there are very many indices affected by the name, e.g. when wildcards are used // otherwise we regularly run into OOMs when a query goes against a large number of indices // I did not find a better way to find out the number of shards than to query a list of indices and for each index query the number of shards via the settings GetSettingsResponse getSettingsResponse = client.admin().indices().getSettings(new GetSettingsRequest().indices(dataPointer.getIndexName())).actionGet(); int numShards = 0, numIndices = 0; for(ObjectCursor<Settings> settings : getSettingsResponse.getIndexToSettings().values()) { numShards += settings.value.getAsInt("index.number_of_shards", 0); numIndices++; } int sizePerShard = (int)Math.ceil((double)SCROLL_SHARD_LIMIT/numShards); logger.info("Found " + numIndices + " indices and " + numShards + " shards matching the index-pattern, thus setting the sizePerShard to " + sizePerShard); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(dataPointer.getIndexName()) .setTypes(dataPointer.getTypeName()) .setSearchType(SearchType.SCAN) .addFields("_ttl", "_source") .setScroll(new TimeValue(SCROLL_TIME_LIMIT)) .setSize(sizePerShard); if (!Strings.isNullOrEmpty(query.getQuery())) { searchRequestBuilder.setQuery(query.getQuery()); } if (!Strings.isNullOrEmpty(query.getSortField())) { searchRequestBuilder.addSort(new FieldSortBuilder(query.getSortField()).order(query.getSortOrder())); } bound.map(resolvedBound -> boundedFilterFactory.createBoundedFilter(segmentationField.get(), resolvedBound)) .ifPresent(searchRequestBuilder::setQuery); return searchRequestBuilder.execute().actionGet(); }
Example 11
Source File: ElasticsearchClient.java From yacy_grid_mcp with GNU Lesser General Public License v2.1 | 5 votes |
@SuppressWarnings("unused") private List<Map<String, Object>> queryWithConstraints(final String indexName, final String fieldName, final String fieldValue, final Map<String, String> constraints, boolean latest) throws IOException { SearchRequestBuilder request = this.elasticsearchClient.prepareSearch(indexName) .setSearchType(SearchType.QUERY_THEN_FETCH) .setFrom(0); BoolQueryBuilder bFilter = QueryBuilders.boolQuery(); bFilter.must(QueryBuilders.constantScoreQuery(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery(fieldName, fieldValue)))); for (Object o : constraints.entrySet()) { @SuppressWarnings("rawtypes") Map.Entry entry = (Map.Entry) o; bFilter.must(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery((String) entry.getKey(), ((String) entry.getValue()).toLowerCase()))); } request.setQuery(bFilter); // get response SearchResponse response = request.execute().actionGet(); // evaluate search result ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); SearchHit[] hits = response.getHits().getHits(); for (SearchHit hit: hits) { Map<String, Object> map = hit.getSourceAsMap(); result.add(map); } return result; }
Example 12
Source File: ElasticSearchFinder.java From attic-polygene-java with Apache License 2.0 | 5 votes |
@Override public EntityReference findEntity( Class<?> resultType, Predicate<Composite> whereClause, Map<String, Object> variables ) throws EntityFinderException { // Prepare request SearchRequestBuilder request = support.client().prepareSearch( support.index() ); BoolQueryBuilder baseQueryBuilder = baseQuery( resultType ); QueryBuilder whereQueryBuilder = processWhereSpecification( baseQueryBuilder, whereClause, variables ); request.setQuery( boolQuery().must( whereQueryBuilder ).filter( baseQueryBuilder ) ); request.setSize( 1 ); // Log LOGGER.debug( "Will search Entity: {}", request ); // Execute SearchResponse response = request.execute().actionGet(); if( response.getHits().getTotalHits() == 1 ) { return EntityReference.parseEntityReference( response.getHits().getAt( 0 ).getId() ); } return null; }
Example 13
Source File: ESUtil.java From ns4_gear_watchdog with Apache License 2.0 | 5 votes |
public <T> List<T> queryDocumentByParam(String indices, String type, MonitorEventSearchParam param, Class<T> clazz) { SearchRequestBuilder builder = buildRequest(indices,type); builder.addSort("timeHappend", SortOrder.ASC); builder.setQuery(convertParam(param)); builder.setFrom(0).setSize(10); SearchResponse resp = builder.get(); return convertResponse(resp,clazz); }
Example 14
Source File: ProductQueryServiceImpl.java From searchanalytics-bigdata with MIT License | 4 votes |
@Override public ProductSearchResult searchProducts( final SearchCriteria searchCriteria) { final QueryBuilder queryBuilder = getQueryBuilder(searchCriteria); final SearchRequestBuilder requestBuilder = getSearchRequestBuilder( searchCriteria.getIndexes(), searchCriteria.getDocumentTypes(), searchCriteria.getFrom(), searchCriteria.getSize()); requestBuilder.addFields(SearchDocumentFieldName.productQueryFields); if (searchCriteria.isRescoreOnSoldOut()) { final Rescorer rescorer = RescoreBuilder .queryRescorer( QueryBuilders.termQuery( SearchDocumentFieldName.SOLD_OUT .getFieldName(), false)) .setQueryWeight(1.0f) // default .setRescoreQueryWeight(1.5f); requestBuilder.setRescorer(rescorer); } if (searchCriteria.hasFilters()) { final AndFilterBuilder andFilterBuilder = getFilterBuilderForSearchCriteria(searchCriteria); requestBuilder.setQuery(QueryBuilders.filteredQuery(queryBuilder, andFilterBuilder)); } else { requestBuilder.setQuery(queryBuilder); } if (!searchCriteria.isNoFacets() && searchCriteria.getFacets().size() > 0) { addFacets(searchCriteria, requestBuilder); } // Add sorting if (searchCriteria.getSortOrder() != null) { // First on given field requestBuilder.addSort(SortBuilders .fieldSort( SearchDocumentFieldName.AVAILABLE_DATE .getFieldName()) .order(searchCriteria.getSortOrder()).missing("_last")); // then on score based requestBuilder.addSort(SortBuilders.scoreSort()); } logger.debug("Executing following search request:" + requestBuilder.internalBuilder().toString()); final SearchResponse searchResponse = requestBuilder.execute() .actionGet(); printSearchResponseForDebug(searchResponse); return getProductSearchResults(searchResponse); }
Example 15
Source File: InvokeChainQueryHandler.java From uavstack with Apache License 2.0 | 4 votes |
/** * * @param data * @param queryBuilder * @param postFilter * @return */ @SuppressWarnings("rawtypes") private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter, SortBuilder[] sorts) { String date = data.getRequest("indexdate"); String currentIndex; if (date != null) { // 指定index currentIndex = this.indexMgr.getIndexByDate(date); } else { // current index currentIndex = this.indexMgr.getCurrentIndex(); } SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex) .setTypes(InvokeChainIndexMgr.IVC_Table).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); int from = DataConvertHelper.toInt(data.getRequest("from"), -1); int size = DataConvertHelper.toInt(data.getRequest("size"), -1); if (from != -1 && size != -1) { srb = srb.setFrom(from).setSize(size); } srb.setQuery(queryBuilder); if (postFilter != null) { srb.setPostFilter(postFilter); } if (sorts != null && sorts.length > 0) { for (SortBuilder sb : sorts) { srb.addSort(sb); } } SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout)); return sr; }
Example 16
Source File: EsQuery.java From AsuraFramework with Apache License 2.0 | 4 votes |
private SearchRequestBuilder buildSearchRequest (EsQueryDo esQueryObj) throws EsException { if (Check.NuNStrStrict(esClientFactory.getIndexs(esQueryObj.getIndexName()))) { throw new EsException("没有指定要搜索的索引名称(indexName)"); } for (ThreadPoolStats.Stats stats : esClientFactory.getClient().threadPool().stats()) { logger.info(JSON.toJSONString(stats)); } //加载要搜索索引 SearchRequestBuilder searchRequestBuilder = esClientFactory.getClient().prepareSearch(esClientFactory.getIndexs(esQueryObj.getIndexName())); //由spring从配置加载要搜索的index的类型 searchRequestBuilder.setTypes(esQueryObj.getTypeName()); //由spring从配置加载要搜索的类型 searchRequestBuilder.setSearchType(SearchType.fromId(esQueryObj.getSearchType())); //查询可以为null searchRequestBuilder.setQuery(esQueryObj.getQueryBuilder()); if (!Check.NuNCollection(esQueryObj.getSortBuilders())) { for (SortBuilder sortBuilder : esQueryObj.getSortBuilders()) { searchRequestBuilder.addSort(sortBuilder); } } if (!Check.NuNCollection(esQueryObj.getAggregationBuilders())) { for (AbstractAggregationBuilder aggregationBuilder : esQueryObj.getAggregationBuilders()) { searchRequestBuilder.addAggregation(aggregationBuilder); } } //设置高亮域 if (esQueryObj.isHighLigth()) { if (!Check.NuNObject(esQueryObj.highLigthFields())) { for (String hlFieldName : esQueryObj.highLigthFields()) { searchRequestBuilder.addHighlightedField(hlFieldName).setHighlighterPreTags(esQueryObj.getHighLigthPreTag()) .setHighlighterPostTags(esQueryObj.getHighLigthPostTag()); } } } //分页 searchRequestBuilder.setFrom(esQueryObj.getFromIndex()).setSize(esQueryObj.getSize()); searchRequestBuilder.setExplain(esQueryObj.isExplain()); return searchRequestBuilder; }
Example 17
Source File: ElasticSearchFinder.java From attic-polygene-java with Apache License 2.0 | 4 votes |
@Override public Stream<EntityReference> findEntities( Class<?> resultType, Predicate<Composite> whereClause, List<OrderBy> orderBySegments, Integer firstResult, Integer maxResults, Map<String, Object> variables ) throws EntityFinderException { // Prepare request SearchRequestBuilder request = support.client().prepareSearch( support.index() ); BoolQueryBuilder baseQueryBuilder = baseQuery( resultType ); QueryBuilder whereQueryBuilder = processWhereSpecification( baseQueryBuilder, whereClause, variables ); request.setQuery( boolQuery().must( whereQueryBuilder ).filter( baseQueryBuilder ) ); if( firstResult != null ) { request.setFrom( firstResult ); } if( maxResults != null ) { request.setSize( maxResults ); } else { //request.setSize( Integer.MAX_VALUE ); // TODO Use scrolls? } if( orderBySegments != null ) { for( OrderBy order : orderBySegments ) { request.addSort( order.property().toString(), order.order() == OrderBy.Order.ASCENDING ? SortOrder.ASC : SortOrder.DESC ); } } // Log LOGGER.debug( "Will search Entities: {}", request ); // Execute SearchResponse response = request.execute().actionGet(); return StreamSupport.stream( response.getHits().spliterator(), false ) .map( hit -> EntityReference.parseEntityReference( hit.getId() ) ); }
Example 18
Source File: ESQueryState.java From sql4es with Apache License 2.0 | 4 votes |
/** * Builds the Elasticsearch query object based on the parsed information from the SQL query * @param searchReq * @param info */ private void buildQuery(SearchRequestBuilder searchReq, ParseResult info) { String[] types = new String[info.getSources().size()]; for(int i=0; i<info.getSources().size(); i++) types[i] = info.getSources().get(i).getSource(); SearchRequestBuilder req = searchReq.setTypes(types); // add filters and aggregations if(info.getAggregation() != null){ // when aggregating the query must be a query and not a filter if(info.getQuery() != null) req.setQuery(info.getQuery()); req.addAggregation(info.getAggregation()); // ordering does not work on aggregations (has to be done in client) }else if(info.getQuery() != null){ if(info.getRequestScore()) req.setQuery(info.getQuery()); // use query instead of filter to get a score else req.setPostFilter(info.getQuery()); // add order for(OrderBy ob : info.getSorts()){ req.addSort(ob.getField(), ob.getOrder()); } } else req.setQuery(QueryBuilders.matchAllQuery()); this.limit = info.getLimit(); if(splitRS) maxRowsRS = fetchSize; //System.out.println("fetch: "+fetchSize+" limit: "+limit+" split: "+splitRS); // add limit and determine to use scroll if(info.getAggregation() != null) { req = req.setSize(0); } else{ if(limit > 0 && limit < fetchSize){ // no scroll needed req.setSize(limit); } else{ // use scrolling req.setSize(fetchSize); req.setScroll(new TimeValue(Utils.getIntProp(props, Utils.PROP_SCROLL_TIMEOUT_SEC, 60)*1000)); if (info.getSorts().isEmpty()) req.addSort("_doc", SortOrder.ASC); // scroll works fast with sort on _doc } } // use query cache when this was indicated in FROM clause if(info.getUseCache()) req.setRequestCache(true); req.setTimeout(TimeValue.timeValueMillis(Utils.getIntProp(props, Utils.PROP_QUERY_TIMEOUT_MS, 10000))); }
Example 19
Source File: ESSearch.java From common-project with Apache License 2.0 | 4 votes |
public List<Map<String, Object>> execute() throws Exception { if (StringUtils.isEmpty(indexName)&& ArrayUtils.isEmpty(types)){ throw new Exception("indexName or types is null"); } SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch(this.indexName).setTypes(this.types); if (page>0&&pageSize>0){ searchRequestBuilder.setFrom(this.page).setSize(this.pageSize); } if (!ArrayUtils.isEmpty(showField)){ searchRequestBuilder.setFetchSource(showField,null); } if (size>0){ searchRequestBuilder.setSize(size); } searchRequestBuilder.setFetchSource(true); //按字段排序 if (!StringUtils.isEmpty(sortField)){ String[] split = sortField.split(" "); if (split[1].equalsIgnoreCase("asc")){ searchRequestBuilder.addSort(split[0], SortOrder.ASC); } if (split[1].equalsIgnoreCase("desc")){ searchRequestBuilder.addSort(split[0], SortOrder.DESC); } } BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); if (bean!=null){ Map<String, Object> fieldNameAndVauleMapping = ClassUtil.getNotNullFieldNameAndVauleMapping(this.bean); if (!fieldNameAndVauleMapping.isEmpty()){ fieldNameAndVauleMapping.forEach((k,v)->{ if (matchPhrase==true){ boolQuery.must(QueryBuilders.matchPhraseQuery(k,v)); }else{ boolQuery.must(QueryBuilders.matchQuery(k,v)); } }); } } searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery()); searchRequestBuilder.setQuery(boolQuery); // 设置是否按查询匹配度排序 searchRequestBuilder.setExplain(true); SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); long totalHits = searchResponse.getHits().totalHits; long length = searchResponse.getHits().getHits().length; logger.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length); return searchResponse(searchResponse,null); }
Example 20
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; }