Java – elasticsearch – using filterbuilders

I'm new to elasticsearch and couchbase I'm building a sample Java application to learn more about elasticsearch and couchbase

Reading elasticsearch Java API, filters are better used when sorting and caching are not required I haven't thought about how to use filterbuilders and have the following problems:

Can I search with filterbuilders alone? > Or do they have to be used with queries? (if so, can anyone give an example?) > From the document, what should I do if I want to perform a search based on field values and want to use filter builders? (use andfilterbuilder or termfilterbuilder or infilterbuilder? I don't know the difference between them.)

For the third question, I actually test it with a query and use the filter shown below When I tried to search with filterbuilders, I got empty results (no rows) I don't know what I did wrong

Any example will help I went through a difficult time, I found that scarce documents and even search led to all kinds of 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 shown in the answer, the work is changed to lowercase "TX" The problem has been solved I have the following questions:

>Under what circumstances are filters used with queries? What will this serve? > The difference between infilter, termfilter and matchallfilter Any illustration will help

Solution

Yes, you should use filters to exclude even documents when executing queries Filters are faster because they do not involve any ratings and can also be cached

That is, obviously, you must use a filter with a search API that executes queries and accepts optional filters If you have only one filter, you can use match_ All query with your filter The filter can be a simple filter or a composite filter to combine multiple filters

With regard to the Java API, the name used is the name of the available filter, and there is no big difference Take a look at this search example, for example In your code, I can't see where you execute setfilter on the searchrequestbuilder object You also don't seem to need and filters because you use a single filter In addition, you may use the default mapping for indexing, so the term "TX" is low That's why when you search with the term filter, you can't find any matches Try searching for "TX" lowercased

If you want to maintain the "TX" term when indexing, you can change the mapping. If the field should only be a single token, you can set the field to not_ analyzed. Otherwise, you can change the filter. You may need to view a query being analyzed so that your query will be analyzed as a content index

For more information about queries and filters, see query DSL documentation:

>Matchallfilter: match all your documents, not as useful as I said > termfilter: filter documents containing term (not analyzed) fields > andfilter: composite filter for putting in and two or more filters

I don't know what infilterbuilder means. I can't find any filter with this name

Queries usually contain user input through a text search box Filters are more ways to optimize searches, such as clicking on facet entries That's why you still have queries plus one or more filters

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>