Search in sources :

Example 6 with HighlightBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class CustomHighlighterSearchIT method testThatCustomHighlighterCanBeConfiguredGlobally.

public void testThatCustomHighlighterCanBeConfiguredGlobally() throws Exception {
    Map<String, Object> options = new HashMap<>();
    options.put("myGlobalOption", "someValue");
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("test").setQuery(QueryBuilders.matchAllQuery()).highlighter(new HighlightBuilder().field("name").highlighterType("test-custom").options(options)).execute().actionGet();
    assertHighlight(searchResponse, 0, "name", 0, equalTo("standard response for name at position 1"));
    assertHighlight(searchResponse, 0, "name", 1, equalTo("field:myGlobalOption:someValue"));
}
Also used : HashMap(java.util.HashMap) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with HighlightBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class CustomHighlighterSearchIT method testThatCustomHighlightersAreSupported.

public void testThatCustomHighlightersAreSupported() throws IOException {
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("test").setQuery(QueryBuilders.matchAllQuery()).highlighter(new HighlightBuilder().field("name").highlighterType("test-custom")).execute().actionGet();
    assertHighlight(searchResponse, 0, "name", 0, equalTo("standard response for name at position 1"));
}
Also used : HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 8 with HighlightBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project xmall by Exrick.

the class SearchServiceImpl method search.

@Override
public SearchResult search(String key, int page, int size, String sort, int priceGt, int priceLte) {
    try {
        Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
        SearchResult searchResult = new SearchResult();
        // 设置查询条件
        QueryBuilder qb = matchQuery("productName", key);
        // 设置分页
        if (page <= 0) {
            page = 1;
        }
        int start = (page - 1) * size;
        // 设置高亮显示
        HighlightBuilder hiBuilder = new HighlightBuilder();
        hiBuilder.preTags("<a style=\"color: #e4393c\">");
        hiBuilder.postTags("</a>");
        hiBuilder.field("productName");
        // 执行搜索
        SearchResponse searchResponse = null;
        if (priceGt >= 0 && priceLte >= 0 && sort.isEmpty()) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).setPostFilter(// 过滤条件
            QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).get();
        } else if (priceGt >= 0 && priceLte >= 0 && sort.equals("1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).setPostFilter(// 过滤条件
            QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).addSort("salePrice", SortOrder.ASC).get();
        } else if (priceGt >= 0 && priceLte >= 0 && sort.equals("-1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).setPostFilter(// 过滤条件
            QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).addSort("salePrice", SortOrder.DESC).get();
        } else if ((priceGt < 0 || priceLte < 0) && sort.isEmpty()) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).get();
        } else if ((priceGt < 0 || priceLte < 0) && sort.equals("1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).addSort("salePrice", SortOrder.ASC).get();
        } else if ((priceGt < 0 || priceLte < 0) && sort.equals("-1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).addSort("salePrice", SortOrder.DESC).get();
        }
        SearchHits hits = searchResponse.getHits();
        // 返回总结果数
        searchResult.setRecordCount(hits.totalHits);
        List<SearchItem> list = new ArrayList<>();
        if (hits.totalHits > 0) {
            for (SearchHit hit : hits) {
                // 总页数
                int totalPage = (int) (hit.getScore() / size);
                if ((hit.getScore() % size) != 0) {
                    totalPage++;
                }
                // 返回结果总页数
                searchResult.setTotalPages(totalPage);
                // 设置高亮字段
                SearchItem searchItem = new Gson().fromJson(hit.getSourceAsString(), SearchItem.class);
                String productName = hit.getHighlightFields().get("productName").getFragments()[0].toString();
                searchItem.setProductName(productName);
                // 返回结果
                list.add(searchItem);
            }
        }
        searchResult.setItemList(list);
        return searchResult;
    } catch (Exception e) {
        e.printStackTrace();
        throw new XmallException("查询ES索引库出错");
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) SearchHit(org.elasticsearch.search.SearchHit) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) SearchResult(cn.exrick.manager.dto.front.SearchResult) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) XmallException(cn.exrick.common.exception.XmallException) XmallException(cn.exrick.common.exception.XmallException) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchItem(cn.exrick.manager.dto.front.SearchItem) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) SearchHits(org.elasticsearch.search.SearchHits) Settings(org.elasticsearch.common.settings.Settings) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 9 with HighlightBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project vertigo by KleeGroup.

the class ESSearchRequestBuilder method appendSearchQuery.

private static void appendSearchQuery(final SearchQuery searchQuery, final SearchRequestBuilder searchRequestBuilder, final boolean useHighlight) {
    final BoolQueryBuilder filterBoolQueryBuilder = QueryBuilders.boolQuery();
    final BoolQueryBuilder postFilterBoolQueryBuilder = QueryBuilders.boolQuery();
    // on ajoute les critères de la recherche AVEC impact sur le score
    final QueryBuilder queryBuilder = appendSearchQuery(searchQuery, filterBoolQueryBuilder);
    // on ajoute les filtres de sécurité SANS impact sur le score
    appendSecurityFilter(searchQuery.getSecurityListFilter(), filterBoolQueryBuilder);
    // on ajoute les filtres des facettes SANS impact sur le score
    appendSelectedFacetValues(searchQuery.getFacetedQuery(), filterBoolQueryBuilder, postFilterBoolQueryBuilder);
    final QueryBuilder requestQueryBuilder;
    if (searchQuery.isBoostMostRecent()) {
        requestQueryBuilder = appendBoostMostRecent(searchQuery, queryBuilder);
    } else {
        requestQueryBuilder = filterBoolQueryBuilder;
    }
    searchRequestBuilder.setQuery(requestQueryBuilder).setPostFilter(postFilterBoolQueryBuilder);
    if (useHighlight) {
        // .setHighlighterFilter(true) //We don't highlight the security filter
        // .addHighlightedField("*"); HOW TO ?
        searchRequestBuilder.highlighter(new HighlightBuilder().numOfFragments(3));
    }
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 10 with HighlightBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project graylog2-server by Graylog2.

the class ESMessageList method applyHighlightingIfActivated.

private void applyHighlightingIfActivated(SearchSourceBuilder searchSourceBuilder, SearchJob job, Query query) {
    if (!allowHighlighting) {
        return;
    }
    final QueryStringQueryBuilder highlightQuery = decoratedHighlightQuery(job, query);
    searchSourceBuilder.highlighter(new HighlightBuilder().requireFieldMatch(false).highlightQuery(highlightQuery).field("*").fragmentSize(0).numOfFragments(0));
}
Also used : QueryStringQueryBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryStringQueryBuilder) HighlightBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Aggregations

HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)21 SearchResponse (org.elasticsearch.action.search.SearchResponse)15 SearchHits (org.elasticsearch.search.SearchHits)6 ArrayList (java.util.ArrayList)5 Script (org.elasticsearch.script.Script)5 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)5 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)4 SearchHit (org.elasticsearch.search.SearchHit)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 SearchHitField (org.elasticsearch.search.SearchHitField)3 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)3 HighlightBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)3 IOException (java.io.IOException)2 Date (java.util.Date)2 ServiceResponse (net.yacy.grid.http.ServiceResponse)2 ElasticsearchClient (net.yacy.grid.io.index.ElasticsearchClient)2 Sort (net.yacy.grid.io.index.Sort)2