Strict and fuzzy searching in ES


(Nikita Krasnov) #1

Here muy json:

{
	
	"office": "office 52",
    "header": "some test"
}

I have such Java code:

  BoolQueryBuilder bqb = QueryBuilders.boolQuery();
   if (esLogRequest.getHeader() != null) {
            bqb.filter(QueryBuilders.fuzzyQuery("header", esLogRequest.getHeader())
                    .fuzziness(Fuzziness.AUTO));
        }
            if (esLogRequest.getOffice() != null) {
                bqb.must(QueryBuilders.termQuery("office", esLogRequest.getOffice()));
            }

I want such result:

  1. Searching of header should work with only one part of header or full header. For example: "header" in ES is "some test" and I want search with part of it - "test" or full name "some test"
  2. I want to search due to strick meaning. For example:"office" in ES is "office 52" and I want to search only for that meaning "office 52"
    In my case first example works when I search for part of text but not full text.
    In second case, it works when in ES is record "52" or "office" and I search for"52" or "office" - it works fine. But when in ES is record "office 52" - I can't find such record (maybe because of space).
    What should I do in such cases?

(David Pilato) #2

If you want strict matching, then you need to use a keyword type instead of a text type for your field.


(Nikita Krasnov) #3

Ok, for strict fields I will change mapping on "keyword".
But what should I do for not strict(fuzzy) search? I have discribed problem upper.


(David Pilato) #4

Use a Match query I guess.

Better to provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing.


(Nikita Krasnov) #5

this helps:

if (esLogRequest.getHeader() != null) {
            bqb.filter(QueryBuilders.matchPhraseQuery("header", esLogRequest.getHeader()));
        }

(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.