Disable fuzzy match ion matchquery on search and return only desired set of fields in response

1- In elastic java rest client, How I can disable the fuzzy match?

with below source builder, I need only properly matching records. if I give 456-ABC, it should not return anything.
if I give just ABC or 123-ABC, it should return.


 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.boolQuery()
        .must(QueryBuilders.rangeQuery("insertTime").gte(todayStartTimestamp).lte(todayEndTimestamp))
        .must(QueryBuilders.matchQuery("id", "123-ABC"))
    );

2- Instead of returning entire payload, Can I just get specific matching field

The problem here is related to your mapping. I believe that id is a text field and not a keyword field.

If you are using the default mapping, you can search in id.keyword instead. And use a term query instead of match although it will do the same thing.

thanks, whats suggested/recommended data type for ID field?

"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},

this is my settings

That's what I said. So if you don't want to change the mapping, try:

 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.boolQuery()
        .must(QueryBuilders.rangeQuery("insertTime").gte(todayStartTimestamp).lte(todayEndTimestamp))
        .must(QueryBuilders.termQuery("id.keyword", "123-ABC"))
    );

It depends on your use case, but if you want to search for exact content, I'd use a keyword type:

"id": {
  "type": "keyword"
}

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