Search code examples
elasticsearchgrails

How to write a elasticsearchservice.search(Closure query, params, filter)?


I am trying to implement elastic search for my grails application. I am using grails 3.3.6 version, gradle wrapper 3.1, grails elasticsearch plugin is 2.4.0 and elastic search version is 5.4.1. I am struggling to pass a parameter of search method in elasticsearchservice class. "elasticsearchservice.search(Closure query, params, filter)"

I am passing parameter like this and getting error on

match

elasticSearchService.search({match(fields: ["title", "description"],
                query: q,fuzziness:'4',fuzzy_prefix_length:1)}, null,[indices: Book, types: Book, score: true])

Actually, here I am trying to use match with fuzziness, I tried with kibana and postman to _search using elasticsearch. It's working there.

POST /books/code/_search
{
  "query": {
    "match": {
      "author": {
        "query": "keeen",
        "fuzziness": 4,
        "prefix_length": 1
      }
    }
  }
}

I want a same structure for my application generated query in console. But my application generated query structure in console is

"query" : {
    "query_string" : {
      "query" : "grails",
      "fields" : [ ],
      "use_dis_max" : true,
      "tie_breaker" : 0.0,
      "default_operator" : "and",
      "auto_generate_phrase_queries" : false,
      "max_determinized_states" : 10000,
      "enable_position_increments" : true,
      "fuzziness" : "AUTO",
      "fuzzy_prefix_length" : 0,
      "fuzzy_max_expansions" : 50,
      "phrase_slop" : 0,
      "escape" : false,
      "split_on_whitespace" : true,
      "boost" : 1.0
    }
  }

How I will get my expected query over there? any idea - Can anyone please help me out ?

Thanks in advance.


Solution

  • Try this code:

    elasticSearchService.search([indices: Book, types: Book, score: true],
    {
       "match" {
         "author"(query: "keen", "fuzziness": 4, "prefix_length": 1)
       }
    })