带有filteredQuery的QueryBuilder和elasticsearch中的userquery

时间:2015-07-03 07:04:03

标签: elasticsearch elasticsearch-plugin

我正在使用这段代码从elasticSearch获取数据: -

1

这很好用。但现在我希望使用此查询过滤结果。就像我们在filteredQuery中做的那样。

        QueryBuilder qb;
        if (query == null || query.trim().length() <= 0) {
            qb = QueryBuilders.matchAllQuery();
        } else {
            qb = QueryBuilders.queryString(query.replaceAll(" ", " OR ").replaceAll(",", " AND ").replaceAll("!", " NOT "));

        }
        org.elasticsearch.action.search.SearchResponse searchHits =  node.client()
                .prepareSearch(name)
                .setQuery(qb)
                .addHighlightedField("file.filename")
                .addHighlightedField("content")
                .addHighlightedField("meta.title")
                .setHighlighterPreTags("<span class='badge badge-info'>")
                .setHighlighterPostTags("</span>")
                .addFields("*", "_source")
                .execute().actionGet();

如何使用queryBuilder添加FilterBuilder?

1 个答案:

答案 0 :(得分:2)

    QueryBuilder qb;
    if (query == null || query.trim().length() <= 0) {
        qb = QueryBuilders.matchAllQuery();
    } else {
        qb = QueryBuilders.queryString(query.replaceAll(" ", " OR ")
                .replaceAll(",", " AND ").replaceAll("!", " NOT "));
    }

    FilterBuilder fb = FilterBuilders.andFilter(.....);
    FilteredQueryBuilder fqBuilder = QueryBuilders.filteredQuery(qb, fb);

    org.elasticsearch.action.search.SearchResponse searchHits = node
            .client()
            .prepareSearch(name)
            .setQuery(fqBuilder)
            .addHighlightedField("file.filename")
            .addHighlightedField("content")
            .addHighlightedField("meta.title")
            .setHighlighterPreTags("<span class='badge badge-info'>")
            .setHighlighterPostTags("</span>").addFields("*", "_source")
            .execute().actionGet();