Search in sources :

Example 51 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project fess by codelibs.

the class IndexingHelper method getChildDocumentList.

public List<Map<String, Object>> getChildDocumentList(final FessEsClient fessEsClient, final String id, final String[] fields) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final QueryBuilder queryBuilder = QueryBuilders.termQuery(fessConfig.getIndexFieldParentId(), id);
    return getDocumentListByQuery(fessEsClient, queryBuilder, fields);
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 52 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder 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 53 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project metron by apache.

the class ElasticsearchMetaAlertDao method getAllAlertsForMetaAlert.

@SuppressWarnings("unchecked")
protected List<Map<String, Object>> getAllAlertsForMetaAlert(Document update) throws IOException {
    Document latest = indexDao.getLatest(update.getGuid(), MetaAlertDao.METAALERT_TYPE);
    if (latest == null) {
        return new ArrayList<>();
    }
    List<String> guids = new ArrayList<>();
    List<Map<String, Object>> latestAlerts = (List<Map<String, Object>>) latest.getDocument().get(MetaAlertDao.ALERT_FIELD);
    for (Map<String, Object> alert : latestAlerts) {
        guids.add((String) alert.get(Constants.GUID));
    }
    List<Map<String, Object>> alerts = new ArrayList<>();
    QueryBuilder query = QueryBuilders.idsQuery().addIds(guids.toArray(new String[0]));
    SearchRequestBuilder request = elasticsearchDao.getClient().prepareSearch().setQuery(query);
    org.elasticsearch.action.search.SearchResponse response = request.get();
    for (SearchHit hit : response.getHits().getHits()) {
        alerts.add(hit.sourceAsMap());
    }
    return alerts;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.apache.metron.indexing.dao.update.Document)

Example 54 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project metacat by Netflix.

the class ElasticSearchUtilImpl method getIdsByQualifiedName.

/**
 * {@inheritDoc}
 */
@Override
public List<String> getIdsByQualifiedName(final String type, final QualifiedName qualifiedName) {
    List<String> result = Lists.newArrayList();
    // Run the query and get the response.
    final QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("name.qualifiedName.tree", qualifiedName)).must(QueryBuilders.termQuery("deleted_", false));
    final SearchRequestBuilder request = client.prepareSearch(esIndex).setTypes(type).setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(queryBuilder).setSize(Integer.MAX_VALUE).setFetchSource(false);
    final SearchResponse response = request.execute().actionGet();
    if (response.getHits().getHits().length != 0) {
        result = getIds(response);
    }
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 55 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project unipop by unipop-graph.

the class DocVertexSchema method getSearch.

@Override
public QueryBuilder getSearch(DeferredVertexQuery query) {
    PredicatesHolder predicatesHolder = this.toPredicates(query.getVertices());
    QueryBuilder queryBuilder = createQueryBuilder(predicatesHolder);
    return queryBuilder;
}
Also used : PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)357 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)245 Test (org.testng.annotations.Test)156 DynamicEntity (org.molgenis.data.support.DynamicEntity)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)55 QueryImpl (org.molgenis.data.support.QueryImpl)36 Map (java.util.Map)32 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)27 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 Test (org.junit.Test)25 SearchHit (org.elasticsearch.search.SearchHit)21 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)20 IOException (java.io.IOException)19 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)17 List (java.util.List)16 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)14