Elastic search: use a filter and should bool query

I want to use elastic search to search a large database of addresses, and to make it look like some other applications, I start with a zip code that is great for the rest of the search query.

So Search :: Elasticsearch

I do

my $scroll = $e->scroll_helper(index => 'pdb', search_type => 'scan', size => 100, body => { query => { bool => { filter => [ {match => { pcode => $postcode }}, ], should => [ {match => { address => $keyword }}, {match => { name => $keyword }}, ], } } } ); 

However, it just spits out everything for $postcode and regardless of the fact that $keyword - this result is not reduced.

I need to have $postcode as a prerequisite, but also separately and, in addition, the other two fields should also be taken into account as a full-text search. How do I do this (I look at the docs and can interpret json-> perl hashrefs incorrectly, so any suggestions are welcome)

For a hypothetical example: The user enters NW1 4AQ . The above query will immediately return to, say, Albany Street and Portland Street, if the user requests Portland and this postcode, instead of receiving both of these results, I expect that the result will be only Portland Street. Right now with the above. He just keeps returning both records.

+8
perl elasticsearch
source share
2 answers

Following common sense, I found that the following is done for the bool segment:

 bool => { must => [ {match => { pcode => $postcode }}, ], should => [ {match => { address => $keyword }}, {match => { name => $keyword }}, ], minimum_should_match => 1, } 

Having minimum_should_match as 1 (which is a counter, not true / false), it seems like it inserts OR in those should

+2
source share

The elastic document says:

β€œ By default , none of the necessary proposals is required for compliance , with one exception: if there are no mandatory proposals, then there must be at least one mandatory condition must match. Just as we can control the accuracy of the match request, we can control how much should be consistent with sentences using the minimum_should_match parameter, either as an absolute number or as a percentage

So the way to do this is through minimum_should_match . Like you. What you did means that any address or name must be matched.

+1
source share

All Articles