public static QueryBuilder urlParamsToQueryBuilder(RestRequest request) { String queryString = request.param("q"); if (queryString == null) { return null; } QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(queryString); queryBuilder.defaultField(request.param("df")); queryBuilder.analyzer(request.param("analyzer")); queryBuilder.analyzeWildcard(request.paramAsBoolean("analyze_wildcard", false)); queryBuilder.lenient(request.paramAsBoolean("lenient", null)); String defaultOperator = request.param("default_operator"); if (defaultOperator != null) { queryBuilder.defaultOperator(Operator.fromString(defaultOperator)); } return queryBuilder; }
public static QuerySourceBuilder parseQuerySource(RestRequest request) { String queryString = request.param("q"); if (queryString == null) { return null; } QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(queryString); queryBuilder.defaultField(request.param("df")); queryBuilder.analyzer(request.param("analyzer")); queryBuilder.analyzeWildcard(request.paramAsBoolean("analyze_wildcard", false)); queryBuilder.lowercaseExpandedTerms(request.paramAsBoolean("lowercase_expanded_terms", true)); queryBuilder.lenient(request.paramAsBoolean("lenient", null)); String defaultOperator = request.param("default_operator"); if (defaultOperator != null) { if ("OR".equals(defaultOperator)) { queryBuilder.defaultOperator(QueryStringQueryBuilder.Operator.OR); } else if ("AND".equals(defaultOperator)) { queryBuilder.defaultOperator(QueryStringQueryBuilder.Operator.AND); } else { throw new IllegalArgumentException("Unsupported defaultOperator [" + defaultOperator + "], can either be [OR] or [AND]"); } } return new QuerySourceBuilder().setQuery(queryBuilder); }
@Override public FacetedPage<TopicComment> findByCon(NativeSearchQueryBuilder searchQueryBuilder , String field , String aggname, String q , final int p , final int ps) { FacetedPage<TopicComment> pages = null ; if(!StringUtils.isBlank(q)){ searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ; } SearchQuery searchQuery = searchQueryBuilder.build(); if(elasticsearchTemplate.indexExists(TopicComment.class)){ if(!StringUtils.isBlank(q)){ pages = elasticsearchTemplate.queryForPage(searchQuery, TopicComment.class , new UKResultMapper()); }else{ pages = elasticsearchTemplate.queryForPage(searchQuery, TopicComment.class , new UKAggTopResultExtractor(field , aggname)); } } return pages ; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public FacetedPage<Serializable> findByCon(Class clazz,NativeSearchQueryBuilder searchQueryBuilder, String q , final int p , final int ps) { FacetedPage<Serializable> pages = null ; if(!StringUtils.isBlank(q)){ searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ; } SearchQuery searchQuery = searchQueryBuilder.build(); if(elasticsearchTemplate.indexExists(clazz)){ if(!StringUtils.isBlank(q)){ pages = elasticsearchTemplate.queryForPage(searchQuery, clazz, new UKResultMapper()); }else{ pages = elasticsearchTemplate.queryForPage(searchQuery, clazz , new UKAggTopResultExtractor("userid" , "top")); } } return pages ; }
@Override public FacetedPage<Topic> getTopicByCate(String cate , String q, final int p , final int ps) { FacetedPage<Topic> pages = null ; NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(termQuery("cate" , cate)).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC)); if(!StringUtils.isBlank(q)){ searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ; } searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ; SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ; if(elasticsearchTemplate.indexExists(Topic.class)){ if(!StringUtils.isBlank(q)){ pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class , new UKResultMapper()); }else{ pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class); } } return pages ; }
@Override public FacetedPage<Topic> getTopicByCateAndUser(String cate , String q , String user ,final int p , final int ps) { FacetedPage<Topic> pages = null ; NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(termQuery("cate" , cate)).withQuery(termQuery("creater" , user)).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC)); if(!StringUtils.isBlank(q)){ searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ; } SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)); if(elasticsearchTemplate.indexExists(Topic.class)){ if(!StringUtils.isBlank(q)){ pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, new UKResultMapper()); }else{ pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class); } } return pages ; }
@Override public FacetedPage<Topic> findByCon(NativeSearchQueryBuilder searchQueryBuilder, String q , final int p , final int ps) { FacetedPage<Topic> pages = null ; if(!StringUtils.isBlank(q)){ searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ; } SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)); if(elasticsearchTemplate.indexExists(Topic.class)){ if(!StringUtils.isBlank(q)){ pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class , new UKResultMapper()); }else{ pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class); } } return pages ; }
private static void elasticSearch(JestClient client, String indexName, String type, String query) { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery(query); searchSourceBuilder.query(queryStringQueryBuilder); Search searchElastic = (Search) new Search.Builder(searchSourceBuilder.toString()) // multiple index or types can be added. .addIndex(indexName).addType(type).build(); try { SearchResult elasticRs = client.execute(searchElastic); List<Hit<Article, Void>> hits = elasticRs.getHits(Article.class); for (Hit<Article, Void> hit : hits) { Article talk = hit.source; System.out.println("Elastic hits: " + talk.toString()); } } catch (IOException e) { e.printStackTrace(); } }
/** * query_string-分词搜索 * * @param boolQ * @param conditions * @param conditionType */ private void doQueryString(BoolQueryBuilder boolQ, List<Condition> conditions, ConditionType conditionType) { // 构造查询语法串 StringBuilder build = new StringBuilder(); String filed; List<String> words; for (Condition condition : conditions) { filed = condition.getFiled(); filed = StringUtils.isBlank(filed) ? "_all" : filed; words = KeyWordUtil.processKeyWord(condition.getValue()); for (String word : words) { build.append("(").append(filed).append(":").append(word).append(") ") .append(conditionType.name()); } } if (build.length() < 1) { return; } String querystring = build.substring(0, build.lastIndexOf(conditionType.name())); QueryStringQueryBuilder queryString = QueryBuilders.queryString(querystring) .defaultOperator(default_operator); mergeBuilder(boolQ, queryString, conditionType); }
@Override public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException { // Wrap the query to also get any meta-alerts. QueryBuilder qb = constantScoreQuery(boolQuery() .must(boolQuery() .should(new QueryStringQueryBuilder(searchRequest.getQuery())) .should(nestedQuery( ALERT_FIELD, new QueryStringQueryBuilder(searchRequest.getQuery()), ScoreMode.None ) ) ) // Ensures that it's a meta alert with active status or that it's an alert (signified by // having no status field) .must(boolQuery() .should(termQuery(MetaAlertDao.STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString())) .should(boolQuery().mustNot(existsQuery(MetaAlertDao.STATUS_FIELD))) ) .mustNot(existsQuery(MetaAlertDao.METAALERT_FIELD)) ); return elasticsearchDao.search(searchRequest, qb); }
/** * @param querySettings * @return builder for query, newer null */ protected QueryBuilder handleFulltextSearchSettings(QuerySettings querySettings) { if (querySettings.getQuery() != null) { QueryStringQueryBuilder qb = QueryBuilders.queryString(querySettings.getQuery()); Map<String, Object> fields = configService.get(ConfigService.CFGNAME_SEARCH_FULLTEXT_QUERY_FIELDS); if (fields != null) { for (String fieldName : fields.keySet()) { String value = (String) fields.get(fieldName); if (value != null && !value.trim().isEmpty()) { try { qb.field(fieldName, Float.parseFloat(value)); } catch (NumberFormatException e) { log.warning("Boost value has not valid float format for fulltext field " + fieldName + " in DCP configuration document " + ConfigService.CFGNAME_SEARCH_FULLTEXT_QUERY_FIELDS); qb.field(fieldName); } } else { qb.field(fieldName); } } } return qb; } else { return QueryBuilders.matchAllQuery(); } }
private void addQuery(BoolQueryBuilder boolQuery, boolean positive, String query, String modifier) { if (Strings.isNullOrEmpty(query)) { return; } QueryStringQueryBuilder builder = QueryBuilders.queryStringQuery(query) .field("title." + modifier) .field("text." + modifier) .defaultOperator(Operator.AND); if (positive) { boolQuery.must(builder); } else { boolQuery.mustNot(builder); } }
@Override public FacetedPage<TopicComment> findByCon( NativeSearchQueryBuilder searchQueryBuilder, String q, int p, int ps) { searchQueryBuilder.withPageable(new PageRequest(p, ps)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC)) ; searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("content").fragmentSize(200)) ; if(!StringUtils.isBlank(q)){ searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ; } return elasticsearchTemplate.queryForPage(searchQueryBuilder.build(), TopicComment.class , new UKResultMapper()) ; }
public static ToXContent fullParamer(QueryStringQueryBuilder query, Paramer paramer) { if (paramer.analysis != null) { query.analyzer(paramer.analysis); } if (paramer.boost != null) { query.boost(paramer.boost); } return query; }
/** * construct the only query used by all * * @param query * @return */ private static QueryBuilder queryBuilder(String query) { //return QueryBuilders.queryStringQuery(query).defaultOperator(QueryStringQueryBuilder.Operator.AND); if (StringUtils.isEmpty(query)) { return QueryBuilders.matchAllQuery(); } // return QueryBuilders.multiMatchQuery(query) // .operator(MatchQueryBuilder.Operator.AND) // .field("name^3") // .field("content").field("path").field("version").field("project") // ; return QueryBuilders.queryStringQuery(query) .allowLeadingWildcard(true) .defaultOperator(QueryStringQueryBuilder.Operator.AND) .field("name^3") .field("content").field("path").field("version").field("project").field("extension") ; //QueryBuilders.queryStringQuery(query) //.defaultOperator(QueryStringQueryBuilder.Operator.AND) //TODO : attention, si on souhaite que la recherche s'effectue bien sur l'ensemble des champs //il faut veuillez à ce qu'il soit tous présent ici //en cas d'ajout, penser à les ajouter ici //.field("content") /* return QueryBuilders.boolQuery() .should(QueryBuilders.termQuery("content", query)) .should( QueryBuilders.queryStringQuery(query) .defaultOperator(QueryStringQueryBuilder.Operator.AND) );*/ }
@Override public List<TaskExecLog> getTaskLogs(String taskId) { try { QueryBuilder qf = QueryBuilders.matchAllQuery(); Expression expression = Expression.fromString("taskId='" + taskId + "'"); qf = expression.getFilterBuilder(); BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(qf); QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery("*"); BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery); final SearchRequestBuilder srb = client.prepareSearch(indexName).setQuery(fq).setTypes(TASK_DOC_TYPE); SearchResponse response = srb.execute().actionGet(); SearchHit[] hits = response.getHits().getHits(); List<TaskExecLog> logs = new ArrayList<>(hits.length); for(SearchHit hit : hits) { String source = hit.getSourceAsString(); TaskExecLog tel = om.readValue(source, TaskExecLog.class); logs.add(tel); } return logs; }catch(Exception e) { log.error(e.getMessage(), e); } return null; }
private SearchResult<String> search(String structuredQuery, int start, int size, List<String> sortOptions, String freeTextQuery, String docType) throws ParserException { QueryBuilder qf = QueryBuilders.matchAllQuery(); if(StringUtils.isNotEmpty(structuredQuery)) { Expression expression = Expression.fromString(structuredQuery); qf = expression.getFilterBuilder(); } BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(qf); QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery(freeTextQuery); BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery); final SearchRequestBuilder srb = client.prepareSearch(indexName).setQuery(fq).setTypes(docType).storedFields("_id").setFrom(start).setSize(size); if(sortOptions != null){ sortOptions.forEach(sortOption -> { SortOrder order = SortOrder.ASC; String field = sortOption; int indx = sortOption.indexOf(':'); if(indx > 0){ //Can't be 0, need the field name at-least field = sortOption.substring(0, indx); order = SortOrder.valueOf(sortOption.substring(indx+1)); } srb.addSort(field, order); }); } List<String> result = new LinkedList<String>(); SearchResponse response = srb.get(); response.getHits().forEach(hit -> { result.add(hit.getId()); }); long count = response.getHits().getTotalHits(); return new SearchResult<String>(count, result); }
public List<TaskExecLog> getTaskLogs(String taskId) { try { QueryBuilder qf = QueryBuilders.matchAllQuery(); Expression expression = Expression.fromString("taskId='" + taskId + "'"); qf = expression.getFilterBuilder(); BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(qf); QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery("*"); BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery); final SearchRequestBuilder srb = client.prepareSearch(logIndexPrefix + "*").setQuery(fq).setTypes(TASK_DOC_TYPE).addSort(SortBuilders.fieldSort("createdTime").order(SortOrder.ASC)); SearchResponse response = srb.execute().actionGet(); SearchHit[] hits = response.getHits().getHits(); List<TaskExecLog> logs = new ArrayList<>(hits.length); for(SearchHit hit : hits) { String source = hit.getSourceAsString(); TaskExecLog tel = om.readValue(source, TaskExecLog.class); logs.add(tel); } return logs; }catch(Exception e) { log.error(e.getMessage(), e); } return null; }
private SearchResult<String> search(String structuredQuery, int start, int size, List<String> sortOptions, String freeTextQuery, String docType) throws ParserException { QueryBuilder qf = QueryBuilders.matchAllQuery(); if(StringUtils.isNotEmpty(structuredQuery)) { Expression expression = Expression.fromString(structuredQuery); qf = expression.getFilterBuilder(); } BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(qf); QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery(freeTextQuery); BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery); final SearchRequestBuilder srb = client.prepareSearch(indexName).setQuery(fq).setTypes(docType).setNoFields().setFrom(start).setSize(size); if(sortOptions != null){ sortOptions.forEach(sortOption -> { SortOrder order = SortOrder.ASC; String field = sortOption; int indx = sortOption.indexOf(':'); if(indx > 0){ //Can't be 0, need the field name at-least field = sortOption.substring(0, indx); order = SortOrder.valueOf(sortOption.substring(indx+1)); } srb.addSort(field, order); }); } List<String> result = new LinkedList<String>(); SearchResponse response = srb.execute().actionGet(); response.getHits().forEach(hit -> { result.add(hit.getId()); }); long count = response.getHits().getTotalHits(); return new SearchResult<String>(count, result); }
@Override public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException { if(searchRequest.getQuery() == null) { throw new InvalidSearchException("Search query is invalid: null"); } return search(searchRequest, new QueryStringQueryBuilder(searchRequest.getQuery())); }
@Override public GroupResponse group(GroupRequest groupRequest) throws InvalidSearchException { // Wrap the query to hide any alerts already contained in meta alerts QueryBuilder qb = QueryBuilders.boolQuery() .must(new QueryStringQueryBuilder(groupRequest.getQuery())) .mustNot(existsQuery(MetaAlertDao.METAALERT_FIELD)); return elasticsearchDao.group(groupRequest, qb); }
private Optional<SearchResponse> findByQueryRaw(List<String> indices, Query query, Sort sort, int limit) { try { List<String> indexes = indices; if (indices.isEmpty()) { indexes = this.getIndices(); } QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(escapeSpecialCharacters(query.toString())); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(queryBuilder); if (sort != null) { Arrays.stream(sort.getSort()).forEach(sortField -> { String field = sortField.getField(); searchSourceBuilder.sort(field); }); } if (limit > 0 && limit <= ELASTICSEARCH_MAX_SIZE) { searchSourceBuilder.size(limit); } else { searchSourceBuilder.size(ELASTICSEARCH_MAX_SIZE); } return Optional.of(this.getClient() .prepareSearch(sanitizeIndexes(indexes).toArray(new String[indexes.size()])) .setSource(searchSourceBuilder).get()); } catch (ElasticsearchException e) { logger.debug(MessageFormat.format("Can't perform search: {0}", e.getMessage())); } return Optional.empty(); }
protected QueryBuilder getQueryBuilder(SearchCriteria searchCriteria) { QueryBuilder matchQueryBuilder = null; String queryString = searchCriteria.getQuery(); if (StringUtils.isBlank(queryString)) { matchQueryBuilder = QueryBuilders.matchAllQuery(); } else { final String filterSpecialCharsQueryString = escapeQueryChars(queryString); final QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryString(filterSpecialCharsQueryString); // Add fields queryStringQueryBuilder.field(SearchDocumentFieldName.TITLE.getFieldName(), (float) 0.5) .field(SearchDocumentFieldName.DESCRIPTION.getFieldName(), (float) 0.15) ; for (final String contentCategoryFieldName : SearchFacetName.categoryFacetFields) { queryStringQueryBuilder.field(SearchDocumentFieldName.CATEGORIES_ARRAY.getFieldName() + "." + contentCategoryFieldName, 1); } matchQueryBuilder = queryStringQueryBuilder; } if(searchCriteria.isUseBoostingFactor()) { FunctionScoreQueryBuilder queryBuilder = new FunctionScoreQueryBuilder(matchQueryBuilder); ScoreFunctionBuilder scoreFunctionBuilder = new ScriptScoreFunctionBuilder().script(SearchDocumentFieldName .getCalculatedScoreScriptForBostFactor()); queryBuilder.add(scoreFunctionBuilder); return queryBuilder; } return matchQueryBuilder; }
protected QueryBuilder getQueryBuilder(final SearchCriteria searchCriteria) { QueryBuilder matchQueryBuilder = null; final String queryString = searchCriteria.getQuery(); if (StringUtils.isBlank(queryString)) { matchQueryBuilder = QueryBuilders.matchAllQuery(); } else { final String filterSpecialCharsQueryString = escapeQueryChars(queryString); final QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryString(filterSpecialCharsQueryString); // Add fields queryStringQueryBuilder.field(SearchDocumentFieldName.TITLE.getFieldName(), (float) 0.5).field( SearchDocumentFieldName.DESCRIPTION.getFieldName(), (float) 0.15); for (final String contentCategoryFieldName : SearchFacetName.categoryFacetFields) { queryStringQueryBuilder.field(SearchDocumentFieldName.CATEGORIES_ARRAY.getFieldName() + "." + contentCategoryFieldName, 1); } matchQueryBuilder = queryStringQueryBuilder; } if (searchCriteria.isUseBoostingFactor()) { final FunctionScoreQueryBuilder queryBuilder = new FunctionScoreQueryBuilder(matchQueryBuilder); final ScoreFunctionBuilder scoreFunctionBuilder = new ScriptScoreFunctionBuilder().script(SearchDocumentFieldName .getCalculatedScoreScriptForBostFactor()); queryBuilder.add(scoreFunctionBuilder); return queryBuilder; } return matchQueryBuilder; }
public static ToXContent fullParamer(QueryStringQueryBuilder query, Paramer paramer) { if (paramer.analysis != null) { query.analyzer(paramer.analysis); } if (paramer.boost != null) { query.boost(paramer.boost); } if (paramer.slop != null) { query.phraseSlop(paramer.slop); } return query; }
/** * A method to create filtered query builder with appropriate filters to fetch auto suggestions. * * @param resourceRequest the resource request * @param resourceResponse the resource response * @throws IOException Signals that an I/O exception has occurred. * @throws ElasticsearchAutocompleteException the elasticsearch autocomplete exception */ public void fetchAutoSuggestions(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, ElasticsearchAutocompleteException { if (Validator.isNull(client)) { throw new ElasticsearchAutocompleteException("Client object cannot be null......."); } if (_log.isDebugEnabled()) { _log.debug("About to get suggestions from elasticsearch server......."); } String searchTerm = ParamUtil.getString(resourceRequest, ElasticsearchPortletConstants.SEARCH_TEXT); QueryStringQueryBuilder stringQueryBuilder = QueryBuilders.queryString(searchTerm); appendQueryFieldsToBuilder(stringQueryBuilder); FilteredQueryBuilder filteredQueryBuilder = QueryBuilders.filteredQuery(stringQueryBuilder, prepareBoolFilterBuilder(resourceRequest)); requestBuilder.setQuery(filteredQueryBuilder); SearchResponse response = requestBuilder.execute().actionGet(); PrintWriter out = resourceResponse.getWriter(); out.println(parseResponse(response)); if (_log.isDebugEnabled()) { _log.debug("Suggestions retreived from elasticsearch server...."); } }
/** * A utility method to append query fields to ES Request builder. * * @param builder the builder */ private void appendQueryFieldsToBuilder(QueryStringQueryBuilder builder) { /** Iterate over the fields and add to Querybuilder */ for(Map.Entry<String, Float> entry : propsValues.getSuggestionQueryFields().entrySet()) { if(entry.getValue() > ElasticsearchPortletConstants.FLOAT_ZERO_VALUE) { builder = builder.field(entry.getKey(), entry.getValue()); } else { builder = builder.field(entry.getKey()); } } }
/** * Extremely beta reindexer! * * @param args */ public static void main(final String[] args) { ClientFactory clientFactory = new ClientFactory("localhost", 9300); final Client client = clientFactory.buildStandardClientConnectedToOneNode(); QueryStringQueryBuilder qb = QueryBuilders.queryString("*"); SearchResponse scrollResp = client.prepareSearch("asite.no").setTypes("snapshot") .setScroll(new TimeValue(60000)) .setQuery(qb) .setSize(100).execute().actionGet(); while (true) { if (scrollResp.getHits().getHits().length == 0) { break; } final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); for (SearchHit hit : scrollResp.getHits()) { bulkRequestBuilder.add(client.prepareIndex("test_1", "snapshot").setId(hit.getId()).setSource(hit.getSource())); } final BulkResponse bulkItemResponses = bulkRequestBuilder.execute().actionGet(); if (bulkItemResponses.hasFailures()) { System.out.print(bulkItemResponses.buildFailureMessage()); } scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet(); } }
protected QueryBuilder getQueryBuilder(final SearchCriteria searchCriteria) { QueryBuilder matchQueryBuilder = null; final String queryString = searchCriteria.getQuery(); if (StringUtils.isBlank(queryString)) { matchQueryBuilder = QueryBuilders.matchAllQuery(); } else { final String filterSpecialCharsQueryString = escapeQueryChars(queryString); final QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders .queryString(filterSpecialCharsQueryString); // Add fields queryStringQueryBuilder.field( SearchDocumentFieldName.TITLE.getFieldName(), (float) 0.5) .field(SearchDocumentFieldName.DESCRIPTION.getFieldName(), (float) 0.15); for (final String contentCategoryFieldName : SearchFacetName.categoryFacetFields) { queryStringQueryBuilder.field( SearchDocumentFieldName.CATEGORIES_ARRAY.getFieldName() + "." + contentCategoryFieldName, 1); } matchQueryBuilder = queryStringQueryBuilder; } if (searchCriteria.isUseBoostingFactor()) { final FunctionScoreQueryBuilder queryBuilder = new FunctionScoreQueryBuilder( matchQueryBuilder); final ScoreFunctionBuilder scoreFunctionBuilder = new ScriptScoreFunctionBuilder() .script(SearchDocumentFieldName .getCalculatedScoreScriptForBostFactor()); queryBuilder.add(scoreFunctionBuilder); return queryBuilder; } return matchQueryBuilder; }
@Override public org.elasticsearch.index.query.QueryBuilder query() { org.elasticsearch.index.query.QueryBuilder result = null; String q = query.getQ(); if (q != null && !q.isEmpty()) { result = queryStringQuery(q).defaultOperator(QueryStringQueryBuilder.Operator.OR); } return result; }
@Override public org.elasticsearch.index.query.QueryBuilder query() { org.elasticsearch.index.query.QueryBuilder result = null; String q = query.getQ(); if (q != null && !q.isEmpty()) { result = queryString(q).defaultOperator(QueryStringQueryBuilder.Operator.OR); } return result; }
@Override public FilterBuilder convert(Filter filter) { // TODO: add options for operator and caching; QueryStringQueryBuilder.Operator operator = QueryStringQueryBuilder.Operator.AND; boolean cache = filter.getBoolean("cache", false); String q = filter.getString("query"); FilterBuilder result = queryFilter(queryString(q).defaultOperator(operator)).cache(cache); return result; }
@Override protected boolean doEquals(QueryStringQueryBuilder other) { throw new RuntimeException("not implemented"); }
public static Optional<QueryStringQueryBuilder> fromXContent(QueryParseContext parseContext) { throw new RuntimeException("not implemented"); }
@Override protected boolean doEquals(QueryStringQueryBuilder other) { throw new MemgraphException("not implemented"); }
private void registerQueryParsers(List<SearchPlugin> plugins) { registerQuery(new QuerySpec<>(MatchQueryBuilder.NAME, MatchQueryBuilder::new, MatchQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchPhraseQueryBuilder.NAME, MatchPhraseQueryBuilder::new, MatchPhraseQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchPhrasePrefixQueryBuilder.NAME, MatchPhrasePrefixQueryBuilder::new, MatchPhrasePrefixQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MultiMatchQueryBuilder.NAME, MultiMatchQueryBuilder::new, MultiMatchQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(NestedQueryBuilder.NAME, NestedQueryBuilder::new, NestedQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(HasChildQueryBuilder.NAME, HasChildQueryBuilder::new, HasChildQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(HasParentQueryBuilder.NAME, HasParentQueryBuilder::new, HasParentQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(DisMaxQueryBuilder.NAME, DisMaxQueryBuilder::new, DisMaxQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(IdsQueryBuilder.NAME, IdsQueryBuilder::new, IdsQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchAllQueryBuilder.NAME, MatchAllQueryBuilder::new, MatchAllQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(QueryStringQueryBuilder.NAME, QueryStringQueryBuilder::new, QueryStringQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(BoostingQueryBuilder.NAME, BoostingQueryBuilder::new, BoostingQueryBuilder::fromXContent)); BooleanQuery.setMaxClauseCount(INDICES_MAX_CLAUSE_COUNT_SETTING.get(settings)); registerQuery(new QuerySpec<>(BoolQueryBuilder.NAME, BoolQueryBuilder::new, BoolQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(TermQueryBuilder.NAME, TermQueryBuilder::new, TermQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(TermsQueryBuilder.NAME, TermsQueryBuilder::new, TermsQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(FuzzyQueryBuilder.NAME, FuzzyQueryBuilder::new, FuzzyQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(RegexpQueryBuilder.NAME, RegexpQueryBuilder::new, RegexpQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(RangeQueryBuilder.NAME, RangeQueryBuilder::new, RangeQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(PrefixQueryBuilder.NAME, PrefixQueryBuilder::new, PrefixQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(WildcardQueryBuilder.NAME, WildcardQueryBuilder::new, WildcardQueryBuilder::fromXContent)); registerQuery( new QuerySpec<>(ConstantScoreQueryBuilder.NAME, ConstantScoreQueryBuilder::new, ConstantScoreQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanTermQueryBuilder.NAME, SpanTermQueryBuilder::new, SpanTermQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanNotQueryBuilder.NAME, SpanNotQueryBuilder::new, SpanNotQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanWithinQueryBuilder.NAME, SpanWithinQueryBuilder::new, SpanWithinQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanContainingQueryBuilder.NAME, SpanContainingQueryBuilder::new, SpanContainingQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(FieldMaskingSpanQueryBuilder.NAME, FieldMaskingSpanQueryBuilder::new, FieldMaskingSpanQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanFirstQueryBuilder.NAME, SpanFirstQueryBuilder::new, SpanFirstQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanNearQueryBuilder.NAME, SpanNearQueryBuilder::new, SpanNearQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanOrQueryBuilder.NAME, SpanOrQueryBuilder::new, SpanOrQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MoreLikeThisQueryBuilder.NAME, MoreLikeThisQueryBuilder::new, MoreLikeThisQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(WrapperQueryBuilder.NAME, WrapperQueryBuilder::new, WrapperQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(CommonTermsQueryBuilder.NAME, CommonTermsQueryBuilder::new, CommonTermsQueryBuilder::fromXContent)); registerQuery( new QuerySpec<>(SpanMultiTermQueryBuilder.NAME, SpanMultiTermQueryBuilder::new, SpanMultiTermQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(FunctionScoreQueryBuilder.NAME, FunctionScoreQueryBuilder::new, FunctionScoreQueryBuilder::fromXContent)); registerQuery( new QuerySpec<>(SimpleQueryStringBuilder.NAME, SimpleQueryStringBuilder::new, SimpleQueryStringBuilder::fromXContent)); registerQuery(new QuerySpec<>(TypeQueryBuilder.NAME, TypeQueryBuilder::new, TypeQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(ScriptQueryBuilder.NAME, ScriptQueryBuilder::new, ScriptQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(GeoDistanceQueryBuilder.NAME, GeoDistanceQueryBuilder::new, GeoDistanceQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(GeoBoundingBoxQueryBuilder.NAME, GeoBoundingBoxQueryBuilder::new, GeoBoundingBoxQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(GeoPolygonQueryBuilder.NAME, GeoPolygonQueryBuilder::new, GeoPolygonQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(ExistsQueryBuilder.NAME, ExistsQueryBuilder::new, ExistsQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchNoneQueryBuilder.NAME, MatchNoneQueryBuilder::new, MatchNoneQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(ParentIdQueryBuilder.NAME, ParentIdQueryBuilder::new, ParentIdQueryBuilder::fromXContent)); if (ShapesAvailability.JTS_AVAILABLE && ShapesAvailability.SPATIAL4J_AVAILABLE) { registerQuery(new QuerySpec<>(GeoShapeQueryBuilder.NAME, GeoShapeQueryBuilder::new, GeoShapeQueryBuilder::fromXContent)); } registerFromPlugin(plugins, SearchPlugin::getQueries, this::registerQuery); }
private QueryStringQueryBuilder lenientQuery(String queryText) { return queryStringQuery(queryText).lenient(true); }
/** * 查询返回一个Map对象 * * @param esIndexName * @param type * @param fields * @param content * @param sortField * @param order * @param from * @param size * @return */ public List<Map<String, Object>> queryForObject(String esIndexName, String type, String[] fields, String content, String sortField, SortOrder order, List<String> heightFields, int from, int size) { SearchRequestBuilder reqBuilder = esClient.prepareSearch(esIndexName) .setTypes(type).setSearchType(SearchType.DEFAULT) .setExplain(true); QueryStringQueryBuilder queryString = QueryBuilders.queryStringQuery("\"" + content + "\""); for (String k : fields) { queryString.field(k); } queryString.minimumShouldMatch("10"); reqBuilder.setExplain(true); reqBuilder.setQuery(QueryBuilders.boolQuery().should(queryString)) .setExplain(true); if (StringUtils.isNotEmpty(sortField) && order != null) { reqBuilder.addSort(sortField, order); } if (from >= 0 && size > 0) { reqBuilder.setFrom(from).setSize(size); } //设置高亮显示 if (heightFields != null) { for (String hs : heightFields) reqBuilder.addHighlightedField(hs); } reqBuilder.setHighlighterPreTags("<span style=\"color:red\">"); reqBuilder.setHighlighterPostTags("</span>"); SearchResponse resp = reqBuilder.execute().actionGet(); SearchHit[] hits = resp.getHits().getHits(); List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); for (SearchHit hit : hits) { Map<String, Object> map = new HashMap<String, Object>(); for (String key : hit.getSource().keySet()) { if (heightFields != null && heightFields.contains(key)) { map.put(key, hit.getHighlightFields().get(key).fragments()[0]); } else map.put(key, hit.getSource().get(key)); } results.add(map); } return results; }