Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder#setPostFilter()
The following examples show how to use
org.elasticsearch.action.search.SearchRequestBuilder#setPostFilter() .
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: EsQueryBuilder.java From es-service-parent with Apache License 2.0 | 6 votes |
/** * 获取过滤器 * * @return */ public BoolFilterBuilder makeFilterBuilder(SearchRequestBuilder srb) { // 过滤器 BoolFilterBuilder boolFilterBuilder = FilterBuilders.boolFilter(); if (request.getSafeFilterScript().size() > 0) { ScriptFilterBuilder filter; for (EsFilterScript filterScript : request.getSafeFilterScript()) { filter = FilterBuilders.scriptFilter(filterScript.getScript()); if (filterScript.getScriptParams() != null && filterScript.getScriptParams().size() > 0) { filter.params(filterScript.getScriptParams()); } boolFilterBuilder.should(filter); } srb.setPostFilter(boolFilterBuilder); } return boolFilterBuilder; }
Example 2
Source File: AnalyticsServiceElasticsearch.java From hawkular-apm with Apache License 2.0 | 6 votes |
private long getTraceCompletionCount(String tenantId, Criteria criteria, boolean onlyFaulty) { String index = client.getIndex(tenantId); if (!refresh(index)) { return 0; } BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, CompletionTime.class); SearchRequestBuilder request = getTraceCompletionRequest(index, criteria, query, 0); if (onlyFaulty) { FilterBuilder filter = FilterBuilders.queryFilter(QueryBuilders.boolQuery() .must(QueryBuilders.matchQuery(ElasticsearchUtil.PROPERTIES_NAME_FIELD, Constants.PROP_FAULT))); request.setPostFilter(FilterBuilders.nestedFilter("properties", filter)); } SearchResponse response = request.execute().actionGet(); if (response.isTimedOut()) { msgLog.warnQueryTimedOut(); return 0; } else { return response.getHits().getTotalHits(); } }
Example 3
Source File: SiteElasticSearchIndexBuilder.java From sakai with Educational Community License v2.0 | 6 votes |
@Override protected Pair<SearchRequestBuilder,QueryBuilder> addSearchSiteIds(Pair<SearchRequestBuilder,QueryBuilder> builders, List<String> siteIds) { SearchRequestBuilder searchRequestBuilder = builders.getLeft(); BoolQueryBuilder queryBuilder = (BoolQueryBuilder)builders.getRight(); // if we have sites filter results to include only the sites included if (siteIds.size() > 0) { searchRequestBuilder = searchRequestBuilder.setRouting(siteIds.toArray(new String[siteIds.size()])); // creating config whether or not to use filter, there are performance and caching differences that // maybe implementation decisions if (useSiteFilters) { OrFilterBuilder siteFilter = orFilter().add( termsFilter(SearchService.FIELD_SITEID, siteIds.toArray(new String[siteIds.size()])).execution("bool")); searchRequestBuilder = searchRequestBuilder.setPostFilter(siteFilter); } else { queryBuilder = queryBuilder.must(termsQuery(SearchService.FIELD_SITEID, siteIds.toArray(new String[siteIds.size()]))); } } return pairOf(searchRequestBuilder,queryBuilder); }
Example 4
Source File: SiteElasticSearchIndexBuilder.java From sakai with Educational Community License v2.0 | 6 votes |
@Override protected Pair<SearchRequestBuilder,QueryBuilder> addSearchSiteIds(Pair<SearchRequestBuilder,QueryBuilder> builders, List<String> siteIds) { SearchRequestBuilder searchRequestBuilder = builders.getLeft(); BoolQueryBuilder queryBuilder = (BoolQueryBuilder)builders.getRight(); // if we have sites filter results to include only the sites included if (siteIds.size() > 0) { searchRequestBuilder = searchRequestBuilder.setRouting(siteIds.toArray(new String[siteIds.size()])); // creating config whether or not to use filter, there are performance and caching differences that // maybe implementation decisions if (useSiteFilters) { OrFilterBuilder siteFilter = orFilter().add( termsFilter(SearchService.FIELD_SITEID, siteIds.toArray(new String[siteIds.size()])).execution("bool")); searchRequestBuilder = searchRequestBuilder.setPostFilter(siteFilter); } else { queryBuilder = queryBuilder.must(termsQuery(SearchService.FIELD_SITEID, siteIds.toArray(new String[siteIds.size()]))); } } return pairOf(searchRequestBuilder,queryBuilder); }
Example 5
Source File: ThreadAnalysisQueryHandler.java From uavstack with Apache License 2.0 | 5 votes |
@SuppressWarnings("rawtypes") private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter, List<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(ThreadAnalysisIndexMgr.JTA_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); } for (SortBuilder sb : sorts) { srb.addSort(sb); } SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout)); return sr; }
Example 6
Source File: SearchQueryServiceImpl.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
private SearchResponse executeSearch(final RepositoryQueryBuilder repoQuery, final String[] searchableIndexes, final int from, final int size, @Nullable final QueryBuilder postFilter) { SearchRequestBuilder searchRequestBuilder = client.get().prepareSearch(searchableIndexes) .setTypes(TYPE) .setQuery(repoQuery) .setFrom(from) .setSize(size) .setProfile(profile); if (repoQuery.sort != null) { for (SortBuilder entry : repoQuery.sort) { searchRequestBuilder.addSort(entry); } } if (postFilter != null) { searchRequestBuilder.setPostFilter(postFilter); } if (repoQuery.timeout != null) { searchRequestBuilder.setTimeout(repoQuery.timeout.getSeconds() + "s"); } SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); if (profile) { logProfileResults(searchResponse); } return searchResponse; }
Example 7
Source File: SearchQueryServiceImpl.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
private SearchResponse executeSearch(final RepositoryQueryBuilder repoQuery, final String[] searchableIndexes, final List<AggregationBuilder> aggregations, @Nullable final QueryBuilder postFilter) { SearchRequestBuilder searchRequestBuilder = client.get().prepareSearch(searchableIndexes) .setTypes(TYPE) .setQuery(repoQuery) .setFrom(0) .setSize(0) .setProfile(profile) .setTrackScores(true); for (AggregationBuilder aggregation : aggregations) { searchRequestBuilder.addAggregation(aggregation); } if (repoQuery.sort != null) { for (SortBuilder entry : repoQuery.sort) { searchRequestBuilder.addSort(entry); } } if (postFilter != null) { searchRequestBuilder.setPostFilter(postFilter); } if (repoQuery.timeout != null) { searchRequestBuilder.setTimeout(repoQuery.timeout.getSeconds() + "s"); } SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); if (profile) { logProfileResults(searchResponse); } return searchResponse; }
Example 8
Source File: SearchQueryServiceImpl.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
@Override public long count(final QueryBuilder query) { if (!validateQuery(query)) { return 0; } RepositoryQueryBuilder repoQuery = repositoryQuery(query); final String[] searchableIndexes = getSearchableIndexes(repoQuery); if (searchableIndexes.length == 0) { return 0; } SearchRequestBuilder searchRequestBuilder = client.get().prepareSearch(searchableIndexes) .setTypes(TYPE) .setQuery(repoQuery) .setFrom(0) .setSize(0); if (repoQuery.skipContentSelectors) { return searchRequestBuilder.execute().actionGet().getHits().totalHits(); } try (SubjectRegistration registration = searchSubjectHelper.register(securityHelper.subject())) { QueryBuilder selectorFilter = scriptQuery(ContentAuthPluginScriptFactory.newScript(registration.getId())); searchRequestBuilder.setPostFilter(selectorFilter); return searchRequestBuilder.execute().actionGet().getHits().totalHits(); } }
Example 9
Source File: QueryAction.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
protected void updateRequestWithPostFilter(Select select, SearchRequestBuilder request) { for (Hint hint : select.getHints()) { if (hint.getType() == HintType.POST_FILTER && hint.getParams() != null && 0 < hint.getParams().length) { request.setPostFilter(QueryBuilders.wrapperQuery(hint.getParams()[0].toString())); } } }
Example 10
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 11
Source File: SlowOperQueryHandler.java From uavstack with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter, SortBuilder[] sorts) { String indexDate = data.getRequest("indexdate"); String currentIndex; if (indexDate != null) { // 获得指定的index currentIndex = this.indexMgr.getIndexByDate(indexDate); } else { // get current index currentIndex = this.indexMgr.getCurrentIndex(); } SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex).setTypes(buildTypes(data)) .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 12
Source File: NewLogQueryHandler.java From uavstack with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter, SortBuilder[] sorts) { String indexDate = data.getRequest("indexdate"); String currentIndex; if (indexDate != null) { // 获得指定的index currentIndex = this.indexMgr.getIndexByDate(indexDate); } else { // get current index currentIndex = this.indexMgr.getCurrentIndex(); } // get logtype for search SearchRequestBuilder srb = null; if (StringHelper.isEmpty(data.getRequest("logtype"))) { srb = client.getClient().prepareSearch(currentIndex).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); } else { String logType = data.getRequest("logtype").replace('.', '_'); srb = client.getClient().prepareSearch(currentIndex).setTypes(logType) .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) { long startLine = DataConvertHelper.toLong(data.getRequest("sline"), -1); long endLine = DataConvertHelper.toLong(data.getRequest("eline"), -1); // 判断如果只有endline则需要将结果倒序,取其前100行,然后在返回响应时再逆序回来,从而取到endline前的100行 if (startLine == -1 && endLine > -1) { srb = srb.addSort("l_timestamp", SortOrder.DESC).addSort("l_num", SortOrder.DESC).setFrom(from) .setSize(size); } else { 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 13
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 14
Source File: SearchRequestBuilderStrategy.java From usergrid with Apache License 2.0 | 2 votes |
/** * Get the search request builder */ public SearchRequestBuilder getBuilder( final SearchEdge searchEdge, final SearchTypes searchTypes, final QueryVisitor visitor, final int limit, final int from, final List<SortPredicate> sortPredicates, final Map<String, Class> fieldsWithType ) { Preconditions .checkArgument( limit <= EntityIndex.MAX_LIMIT, "limit is greater than max " + EntityIndex.MAX_LIMIT ); Preconditions.checkNotNull( visitor, "query visitor cannot be null"); SearchRequestBuilder srb = esProvider.getClient().prepareSearch( alias.getReadAlias() ).setTypes( IndexingUtils.ES_ENTITY_TYPE ) .setSearchType( SearchType.QUERY_THEN_FETCH ); final Optional<QueryBuilder> queryBuilder = visitor.getQueryBuilder(); if ( queryBuilder.isPresent() ) { srb.setQuery( queryBuilder.get() ); } srb.setPostFilter( createFilterBuilder( searchEdge, visitor, searchTypes ) ); srb = srb.setFrom( from ).setSize( limit ); //if we have a geo field, sort by closest to farthest by default final GeoSortFields geoFields = visitor.getGeoSorts(); //no sort predicates, sort by edge time descending, entity id second if ( sortPredicates.size() == 0 ) { applyDefaultSortPredicates( srb, geoFields ); } else { applySortPredicates( srb, sortPredicates, geoFields, fieldsWithType ); } return srb; }