Search in sources :

Example 1 with SuggestBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilder in project elasticsearch by elastic.

the class RestSearchAction method parseSearchSource.

/**
     * Parses the rest request on top of the SearchSourceBuilder, preserving
     * values that are not overridden by the rest request.
     */
private static void parseSearchSource(final SearchSourceBuilder searchSourceBuilder, RestRequest request) {
    QueryBuilder queryBuilder = RestActions.urlParamsToQueryBuilder(request);
    if (queryBuilder != null) {
        searchSourceBuilder.query(queryBuilder);
    }
    int from = request.paramAsInt("from", -1);
    if (from != -1) {
        searchSourceBuilder.from(from);
    }
    int size = request.paramAsInt("size", -1);
    if (size != -1) {
        searchSourceBuilder.size(size);
    }
    if (request.hasParam("explain")) {
        searchSourceBuilder.explain(request.paramAsBoolean("explain", null));
    }
    if (request.hasParam("version")) {
        searchSourceBuilder.version(request.paramAsBoolean("version", null));
    }
    if (request.hasParam("timeout")) {
        searchSourceBuilder.timeout(request.paramAsTime("timeout", null));
    }
    if (request.hasParam("terminate_after")) {
        int terminateAfter = request.paramAsInt("terminate_after", SearchContext.DEFAULT_TERMINATE_AFTER);
        if (terminateAfter < 0) {
            throw new IllegalArgumentException("terminateAfter must be > 0");
        } else if (terminateAfter > 0) {
            searchSourceBuilder.terminateAfter(terminateAfter);
        }
    }
    if (request.param("fields") != null) {
        throw new IllegalArgumentException("The parameter [" + SearchSourceBuilder.FIELDS_FIELD + "] is no longer supported, please use [" + SearchSourceBuilder.STORED_FIELDS_FIELD + "] to retrieve stored fields or _source filtering " + "if the field is not stored");
    }
    StoredFieldsContext storedFieldsContext = StoredFieldsContext.fromRestRequest(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), request);
    if (storedFieldsContext != null) {
        searchSourceBuilder.storedFields(storedFieldsContext);
    }
    String sDocValueFields = request.param("docvalue_fields");
    if (sDocValueFields == null) {
        sDocValueFields = request.param("fielddata_fields");
    }
    if (sDocValueFields != null) {
        if (Strings.hasText(sDocValueFields)) {
            String[] sFields = Strings.splitStringByCommaToArray(sDocValueFields);
            for (String field : sFields) {
                searchSourceBuilder.docValueField(field);
            }
        }
    }
    FetchSourceContext fetchSourceContext = FetchSourceContext.parseFromRestRequest(request);
    if (fetchSourceContext != null) {
        searchSourceBuilder.fetchSource(fetchSourceContext);
    }
    if (request.hasParam("track_scores")) {
        searchSourceBuilder.trackScores(request.paramAsBoolean("track_scores", false));
    }
    String sSorts = request.param("sort");
    if (sSorts != null) {
        String[] sorts = Strings.splitStringByCommaToArray(sSorts);
        for (String sort : sorts) {
            int delimiter = sort.lastIndexOf(":");
            if (delimiter != -1) {
                String sortField = sort.substring(0, delimiter);
                String reverse = sort.substring(delimiter + 1);
                if ("asc".equals(reverse)) {
                    searchSourceBuilder.sort(sortField, SortOrder.ASC);
                } else if ("desc".equals(reverse)) {
                    searchSourceBuilder.sort(sortField, SortOrder.DESC);
                }
            } else {
                searchSourceBuilder.sort(sort);
            }
        }
    }
    String sStats = request.param("stats");
    if (sStats != null) {
        searchSourceBuilder.stats(Arrays.asList(Strings.splitStringByCommaToArray(sStats)));
    }
    String suggestField = request.param("suggest_field");
    if (suggestField != null) {
        String suggestText = request.param("suggest_text", request.param("q"));
        int suggestSize = request.paramAsInt("suggest_size", 5);
        String suggestMode = request.param("suggest_mode");
        searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion(suggestField, termSuggestion(suggestField).text(suggestText).size(suggestSize).suggestMode(SuggestMode.resolve(suggestMode))));
    }
}
Also used : StoredFieldsContext(org.elasticsearch.search.fetch.StoredFieldsContext) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 2 with SuggestBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilder in project elasticsearch by elastic.

the class TermSuggestionBuilderTests method testMalformedJson.

public void testMalformedJson() {
    final String field = RandomStrings.randomAsciiOfLength(random(), 10).toLowerCase(Locale.ROOT);
    String suggest = "{\n" + "  \"bad-payload\" : {\n" + "    \"text\" : \"the amsterdma meetpu\",\n" + "    \"term\" : {\n" + "      \"field\" : { \"" + field + "\" : \"bad-object\" }\n" + "    }\n" + "  }\n" + "}";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, suggest)) {
        final SuggestBuilder suggestBuilder = SuggestBuilder.fromXContent(parser);
        fail("Should not have been able to create SuggestBuilder from malformed JSON: " + suggestBuilder);
    } catch (Exception e) {
        assertThat(e.getMessage(), containsString("parsing failed"));
    }
}
Also used : SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IOException(java.io.IOException)

Example 3 with SuggestBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilder in project pancm_project by xuwujing.

the class EsHighLevelRestSearchTest method search.

/**
 * search查询使用示例
 *
 * @throws IOException
 */
private static void search() throws IOException {
    /*
         * 全文查询使用示例
         */
    // 查询指定的索引库
    SearchRequest searchRequest = new SearchRequest("user");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // 搜索字段user为pancm的数据
    MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "pancm");
    // 设置模糊查询
    matchQueryBuilder.fuzziness(Fuzziness.AUTO);
    // 设置前缀长度
    matchQueryBuilder.prefixLength(3);
    // 设置最大扩展选项来控制查询的模糊过程
    matchQueryBuilder.maxExpansions(10);
    /*
         * QueryBuilder也可以
         */
    // QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
    // .fuzziness(Fuzziness.AUTO)
    // .prefixLength(3)
    // .maxExpansions(10);
    SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
    searchSourceBuilder2.query(matchQueryBuilder);
    SearchRequest searchRequest2 = new SearchRequest();
    searchRequest2.source(searchSourceBuilder2);
    // 同步查询
    SearchResponse searchResponse2 = client.search(searchRequest, RequestOptions.DEFAULT);
    SearchHits hits = searchResponse2.getHits();
    // 总条数和分值
    long totalHits = hits.getTotalHits();
    float maxScore = hits.getMaxScore();
    hits.forEach(hit -> {
        String index = hit.getIndex();
        String type = hit.getType();
        String id = hit.getId();
        float score = hit.getScore();
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        String string = hit.getSourceAsString();
        System.out.println("Match查询的Map结果:" + sourceAsMap);
        System.out.println("Match查询的String结果:" + string);
        String documentTitle = (String) sourceAsMap.get("title");
        // List<Object> users = (List<Object>) sourceAsMap.get("user");
        Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject");
    });
    System.out.println("\n=================\n");
    /*
         * 高亮查询
         */
    SearchSourceBuilder searchSourceBuilder3 = new SearchSourceBuilder();
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title");
    // 设置字段高亮字体
    highlightTitle.highlighterType("user");
    highlightBuilder.field(highlightTitle);
    HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
    highlightBuilder.field(highlightUser);
    searchSourceBuilder3.highlighter(highlightBuilder);
    SearchRequest searchRequest3 = new SearchRequest();
    searchRequest3.source(searchSourceBuilder3);
    // 同步查询
    SearchResponse searchResponse3 = client.search(searchRequest3, RequestOptions.DEFAULT);
    searchResponse3.getHits().forEach(hit -> {
        Map<String, Object> map = hit.getSourceAsMap();
        String string = hit.getSourceAsString();
        System.out.println("Highlight查询的Map结果:" + map);
        System.out.println("Highlight查询的String结果:" + string);
    });
    System.out.println("\n=================\n");
    /**
     * 聚合查询
     */
    SearchSourceBuilder searchSourceBuilder4 = new SearchSourceBuilder();
    // terms 就是分组统计 根据user进行分组并创建一个新的聚合user_
    TermsAggregationBuilder aggregation = AggregationBuilders.terms("user_").field("user");
    aggregation.subAggregation(AggregationBuilders.avg("average_age").field("age"));
    searchSourceBuilder4.aggregation(aggregation);
    SearchRequest searchRequest4 = new SearchRequest();
    searchRequest4.source(searchSourceBuilder4);
    // 同步查询
    SearchResponse searchResponse4 = client.search(searchRequest4, RequestOptions.DEFAULT);
    // 聚合查询返回条件
    Aggregations aggregations = searchResponse4.getAggregations();
    System.out.println("聚合查询");
    for (Aggregation agg : aggregations) {
        String type = agg.getType();
        String name = agg.getName();
        Terms terms = (Terms) aggregations.get(name);
        for (Terms.Bucket bucket : terms.getBuckets()) {
            System.out.println("条数:" + bucket.getDocCount());
            System.out.println("key:" + bucket.getKey());
            System.out.println("num:" + bucket.getKeyAsNumber());
            Avg avg = bucket.getAggregations().get("average_age");
            System.out.println("value:" + avg.getValue());
        }
        if (type.equals(TermsAggregationBuilder.NAME)) {
            Bucket elasticBucket = ((Terms) agg).getBucketByKey("average_age");
            long numberOfDocs = elasticBucket.getDocCount();
            System.out.println("条数:" + numberOfDocs);
        }
    }
    /*
         * 建议查询
         */
    SearchSourceBuilder searchSourceBuilder5 = new SearchSourceBuilder();
    SuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("pancm");
    SuggestBuilder suggestBuilder = new SuggestBuilder();
    suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
    searchSourceBuilder5.suggest(suggestBuilder);
    SearchRequest searchRequest5 = new SearchRequest();
    searchRequest5.source(searchSourceBuilder5);
    // 同步查询
    SearchResponse searchResponse5 = client.search(searchRequest5, RequestOptions.DEFAULT);
    Suggest suggest = searchResponse5.getSuggest();
    TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
    for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
        for (TermSuggestion.Entry.Option option : entry) {
            String suggestText = option.getText().string();
            System.out.println("返回结果:" + suggestText);
        }
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Aggregations(org.elasticsearch.search.aggregations.Aggregations) Suggest(org.elasticsearch.search.suggest.Suggest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) SearchHits(org.elasticsearch.search.SearchHits) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Map(java.util.Map) SuggestionBuilder(org.elasticsearch.search.suggest.SuggestionBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 4 with SuggestBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilder in project incubator-sdap-mudrod by apache.

the class ESDriver method autoComplete.

public List<String> autoComplete(String index, String term) {
    boolean exists = this.getClient().admin().indices().prepareExists(index).execute().actionGet().isExists();
    if (!exists) {
        return new ArrayList<>();
    }
    Set<String> suggestHS = new HashSet<String>();
    List<String> suggestList = new ArrayList<>();
    // please make sure that the completion field is configured in the ES mapping
    CompletionSuggestionBuilder suggestionsBuilder = SuggestBuilders.completionSuggestion("Dataset-Metadata").prefix(term, Fuzziness.fromEdits(2)).size(100);
    SearchRequestBuilder suggestRequestBuilder = getClient().prepareSearch(index).suggest(new SuggestBuilder().addSuggestion("completeMe", suggestionsBuilder));
    SearchResponse sr = suggestRequestBuilder.setFetchSource(false).execute().actionGet();
    Iterator<? extends Suggest.Suggestion.Entry.Option> iterator = sr.getSuggest().getSuggestion("completeMe").iterator().next().getOptions().iterator();
    while (iterator.hasNext()) {
        Suggest.Suggestion.Entry.Option next = iterator.next();
        String suggest = next.getText().string().toLowerCase();
        suggestList.add(suggest);
    }
    suggestHS.addAll(suggestList);
    suggestList.clear();
    suggestList.addAll(suggestHS);
    return suggestList;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Suggest(org.elasticsearch.search.suggest.Suggest) SearchResponse(org.elasticsearch.action.search.SearchResponse) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) Entry(java.util.Map.Entry) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder)

Example 5 with SuggestBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilder in project graylog2-server by Graylog2.

the class QuerySuggestionsES7 method suggest.

@Override
public SuggestionResponse suggest(SuggestionRequest req) {
    final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(req.streams(), req.timerange());
    final TermSuggestionBuilder suggestionBuilder = SuggestBuilders.termSuggestion(req.field()).text(req.input()).size(req.size());
    final SearchSourceBuilder search = new SearchSourceBuilder().query(QueryBuilders.prefixQuery(req.field(), req.input())).size(0).aggregation(AggregationBuilders.terms("fieldvalues").field(req.field()).size(req.size())).suggest(new SuggestBuilder().addSuggestion("corrections", suggestionBuilder));
    try {
        final SearchResponse result = client.singleSearch(new SearchRequest(affectedIndices.toArray(new String[] {})).source(search), "Failed to execute aggregation");
        final ParsedStringTerms fieldValues = result.getAggregations().get("fieldvalues");
        final List<SuggestionEntry> entries = fieldValues.getBuckets().stream().map(b -> new SuggestionEntry(b.getKeyAsString(), b.getDocCount())).collect(Collectors.toList());
        if (!entries.isEmpty()) {
            return SuggestionResponse.forSuggestions(req.field(), req.input(), entries, fieldValues.getSumOfOtherDocCounts());
        } else {
            TermSuggestion suggestion = result.getSuggest().getSuggestion("corrections");
            final List<SuggestionEntry> corrections = suggestion.getEntries().stream().flatMap(e -> e.getOptions().stream()).map(o -> new SuggestionEntry(o.getText().string(), o.getFreq())).collect(Collectors.toList());
            return SuggestionResponse.forSuggestions(req.field(), req.input(), corrections, null);
        }
    } catch (org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException exception) {
        final SuggestionError err = tryResponseException(exception).orElseGet(() -> parseException(exception));
        return SuggestionResponse.forError(req.field(), req.input(), err);
    }
}
Also used : TermSuggestionBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestionBuilder) SearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder) ParsedStringTerms(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) SuggestBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilder) SuggestionEntry(org.graylog.plugins.views.search.engine.suggestions.SuggestionEntry) SearchRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest) Set(java.util.Set) TermSuggestion(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestion) SuggestionRequest(org.graylog.plugins.views.search.engine.suggestions.SuggestionRequest) Collectors(java.util.stream.Collectors) AggregationBuilders(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.AggregationBuilders) Inject(javax.inject.Inject) List(java.util.List) QuerySuggestionsService(org.graylog.plugins.views.search.engine.QuerySuggestionsService) ResponseError(org.graylog.storage.elasticsearch7.errors.ResponseError) Optional(java.util.Optional) SuggestBuilders(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilders) SuggestionResponse(org.graylog.plugins.views.search.engine.suggestions.SuggestionResponse) IndexLookup(org.graylog.plugins.views.search.elasticsearch.IndexLookup) QueryBuilders(org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilders) SuggestionError(org.graylog.plugins.views.search.engine.suggestions.SuggestionError) SearchRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest) ParsedStringTerms(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) SearchSourceBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse) SuggestBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.SuggestBuilder) SuggestionError(org.graylog.plugins.views.search.engine.suggestions.SuggestionError) TermSuggestion(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestion) TermSuggestionBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.suggest.term.TermSuggestionBuilder) SuggestionEntry(org.graylog.plugins.views.search.engine.suggestions.SuggestionEntry)

Aggregations

SuggestBuilder (org.elasticsearch.search.suggest.SuggestBuilder)4 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 Suggest (org.elasticsearch.search.suggest.Suggest)2 IndexLookup (org.graylog.plugins.views.search.elasticsearch.IndexLookup)2 QuerySuggestionsService (org.graylog.plugins.views.search.engine.QuerySuggestionsService)2 SuggestionEntry (org.graylog.plugins.views.search.engine.suggestions.SuggestionEntry)2 SuggestionError (org.graylog.plugins.views.search.engine.suggestions.SuggestionError)2 SuggestionRequest (org.graylog.plugins.views.search.engine.suggestions.SuggestionRequest)2 SuggestionResponse (org.graylog.plugins.views.search.engine.suggestions.SuggestionResponse)2 JestClient (io.searchbox.client.JestClient)1 Search (io.searchbox.core.Search)1 SearchResult (io.searchbox.core.SearchResult)1 TermsAggregation (io.searchbox.core.search.aggregation.TermsAggregation)1 IOException (java.io.IOException)1 Collections (java.util.Collections)1