ElasticSearch - 使用FilterBuilders [英] ElasticSearch - Using FilterBuilders

查看:107
本文介绍了ElasticSearch - 使用FilterBuilders的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ElasticSearch和Couchbase的新手。我正在构建一个示例Java应用程序来了解有关ElasticSearch和Couchbase的更多信息。



阅读 ElasticSearch Java API ,过滤器在不需要排序和缓存的情况下更好地使用。
我还没有想出如何使用FilterBuilders并有以下问题:




  • 可以 FilterBuilders 单独用于搜索?或者他们总是需要使用查询? (如果是真的,有人可以列举一个例子吗?)

  • 如果要根据字段值执行搜索并想要使用FilterBuilders,请执行文档,如何完成? (使用 AndFilterBuilder TermFilterBuilder InFilterBuilder ?我不是清楚他们之间的差异。)



对于第三个问题,我实际上使用查询进行搜索并使用如下所示的过滤器。
当我尝试使用 FilterBuilders 搜索时,我得到空结果(没有行)。我不知道我做错了什么。



任何示例都将有所帮助。我经历了艰难的时间,我发现稀疏,甚至搜索导致各种不可靠的用户论坛。

  private void processQuery (){
SearchRequestBuilder srb = getSearchRequestBuilder(BUCKET);
QueryBuilder qb = QueryBuilders.fieldQuery(doc.address.state,TX);
srb.setQuery(qb);

SearchResponse resp = srb.execute()。actionGet();
System.out.println(response:+ resp);
}

private void searchWithFilters(){
SearchRequestBuilder srb = getSearchRequestBuilder(BUCKET);
srb.setFilter(FilterBuilders.termFilter(doc.address.state,tx));
// AndFilterBuilder andFb = FilterBuilders.andFilter();
//andFb.add(FilterBuilders.termFilter(\"doc.address.state,TX));
//srb.setFilter(andFb);
SearchResponse resp = srb.execute()。actionGet();
System.out.println(response:+ resp);
}

- -UPDATE -



如答案所示,改为小写tx的作品。这个问题解决了。我仍然有以下问题:




  • 在什么情况下,是查询使用的过滤器?这个服务的目的是什么?

  • InFilter TermFilter MatchAllFilter


解决方案

对了,你应该使用过滤器来排除文档不均匀在执行查询时考虑。过滤器更快,因为它们不涉及任何评分,并且可以缓存。



表示,很明显,您必须使用带有搜索api ,它执行查询并接受可选过滤器。如果您只有一个过滤器,您可以使用 match_all 查询以及你的过滤器过滤器可以是一个简单的过滤器,也可以是一个复合的过滤器,以便将多个过滤器组合在一起。



关于 Java API ,使用的名称是可用的过滤器的名称,没有什么大的区别。请参阅此搜索示例。在你的代码中,我没有看到你在 SearchRequestBuilder 对象中执行 setFilter 的位置。你似乎也不需要和过滤器,因为你使用一个单一的过滤器。此外,您可能会使用默认映射进行索引,因此术语TX较低。这就是为什么当您使用术语过滤器进行搜索时,找不到任何匹配项。尝试搜索tx低级。



如果要在索引时保持TX术语,可以更改映射,可能将字段设置为 not_analyzed 如果它只应该是一个令牌。否则,您可以更改过滤器,您可能需要查看已分析的查询,以便您的查询将以内容索引的方式进行分析。



有关查询和过滤器的更多信息,请查看查询DSL文档





不知道你是什么意思是 InFilterBuilder ,找不到任何具有此名称的过滤器。



查询通常包含用户通过文本搜索框输入的内容。过滤器是更多的方法来优化搜索,例如点击facet条目。这就是为什么您仍然可以查询一个或多个过滤器。


I am new to ElasticSearch and Couchbase. I am building a sample Java application to learn more about ElasticSearch and Couchbase.

Reading the ElasticSearch Java API, Filters are better used in cases where sort on score is not necessary and for caching. I still haven't figured out how to use FilterBuilders and have following questions:

  • Can FilterBuilders be used alone to search?
  • Or Do they always have to be used with a Query? ( If true, can someone please list an example? )
  • Going through a documentation, if I want to perform a search based on field values and want to use FilterBuilders, how can I accomplish that? (using AndFilterBuilder or TermFilterBuilder or InFilterBuilder? I am not clear about the differences between them.)

For the 3rd question, I actually tested it with search using queries and using filters as shown below. I got empty result (no rows) when I tried search using FilterBuilders. I am not sure what am I doing wrong.

Any examples will be helpful. I have had a tough time going through documentation which I found sparse and even searching led to various unreliable user forums.

private void processQuery() {
        SearchRequestBuilder srb = getSearchRequestBuilder(BUCKET);
        QueryBuilder qb = QueryBuilders.fieldQuery("doc.address.state", "TX");
        srb.setQuery(qb);

        SearchResponse resp = srb.execute().actionGet();
        System.out.println("response :" + resp);
    }

private void searchWithFilters(){
        SearchRequestBuilder srb = getSearchRequestBuilder(BUCKET);
        srb.setFilter(FilterBuilders.termFilter("doc.address.state", "tx"));
        //AndFilterBuilder andFb = FilterBuilders.andFilter();
        //andFb.add(FilterBuilders.termFilter("doc.address.state", "TX")); 
        //srb.setFilter(andFb);
        SearchResponse resp = srb.execute().actionGet();
        System.out.println("response :" + resp);
    }

--UPDATE--

As suggested in the answer, changing to lowercase "tx" works. With this question resolved. I still have following questions:

  • In what scenario(s), are filters used with query? What purpose will this serve?
  • Difference between InFilter, TermFilter and MatchAllFilter. Any illustration will help.

解决方案

Right, you should use filters to exclude documents from being even considered when executing the query. Filters are faster since they don't involve any scoring, and cacheable as well.

That said, it's pretty obvious that you have to use a filter with the search api, which does execute a query and accepts an optional filter. If you only have a filter you can just use the match_all query together with your filter. A filter can be a simple one, or a compund one in order to combine multiple filters together.

Regarding the Java API, the names used are the names of the filters available, no big difference. Have a look at this search example for instance. In your code I don't see where you do setFilter on your SearchRequestBuilder object. You don't seem to need the and filter either, since you are using a single filter. Furthermore, it might be that you are indexing using the default mappings, thus the term "TX" is lowercased. That's why when you search using the term filter you don't find any match. Try searching for "tx" lowercased.

You can either change your mapping if you want to keep the "TX" term as it is while indexing, probably setting the field as not_analyzed if it should only be a single token. Otherwise you can change filter, you might want to have a look at a query that is analyzed, so that your query wil be analyzed the same way the content was indexed.

Have a look at the query DSL documentation for more information regarding queries and filters:

  • MatchAllFilter: matches all your document, not that useful I'd say
  • TermFilter: Filters documents that have fields that contain a term (not analyzed)
  • AndFilter: compound filter used to put in and two or more filters

Don't know what you mean by InFilterBuilder, couldn't find any filter with this name.

The query usually contains what the user types in through the text search box. Filters are more way to refine the search, for example clicking on facet entries. That's why you would still have the query plus one or more filters.

这篇关于ElasticSearch - 使用FilterBuilders的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆