Vasiliy Pumpkin
Vasiliy Pumpkin

Reputation: 17

How to make search in opensearch more strict?

  1. In OpenSearch, there are documents that contain the following field:

     "address": {
        "city": "London",
        "street": "Baker street",
        "building": "221b",
        "floor": "1",
        "room": "4"
    }
    

When I send a request with request body through the API like this:

{
   "filters": {
       "address": "london baker street 221bsdfsdf"
   }

}

We receive all documents containing "london baker street 221b". How can I make it so that a request of this kind does not return documents, since "london baker street 221bsdfsdf" is an invalid address? Here is a block of code from the API:

if (requestFilters.address() != null) {
   boolQueryBuilder.must(
       new Query.Builder().nested(
           new NestedQuery.Builder()
               .path("address")
               .query(
                   queryForMatchField(
                       List.of(
                           "address.city",
                           "address.street",
                           "address.building",
                           "address.room"),
                       requestFilters.address()
                   )
               )
               .build()
       ).build()
   );

   private Query queryForMatchField(List<String> fieldNames, String value) {
       return new Query.Builder().multiMatch(
           new MultiMatchQuery.Builder()
               .fields(fieldNames)
               .query(value)
               .fuzziness("AUTO")
               .zeroTermsQuery(ZeroTermsQuery.All)
               .build()
       ).build();
   }
  1. How can I ensure that when requesting "address": "london baker street 221b", documents with "london baker street 221" or "london baker street 221a" are not returned?

Upvotes: 0

Views: 54

Answers (1)

Shakirov Ramil
Shakirov Ramil

Reputation: 1543

may be Minimum_should_match could help

https://opster.com/guides/opensearch/opensearch-search-apis/opensearch-match-multi-match-and-match-phrase-queries/

or you can use keyword mapping with exact match

Upvotes: 1

Related Questions