Dam Forums
Dam Forums

Reputation: 240

Like query in ElasticSearch

How to use a like query in a elasticsearch query?

this is what i have been trying so far.

array:3 [
"index" => "users"
"type" => "user"
"body" => array:1 [
"query" => array:1 [
  "bool" => array:1 [
    "should" => array:1 [
      0 => array:3 [
        0 => array:1 [
          "match" => array:1 [
            "fullname" => "Mag*"
          ]
        ]
        1 => array:1 [
          "match" => array:1 [
            "industry_name" => "other"
          ]
        ]
        2 => array:1 [
          "match" => array:1 [
            "active" => "yes"
           ]
         ]
       ]
     ]
     ]
     ]
   ]
   ]

My Search json is something like this,

{"1": "Mag","industry_name":"other", "active": "yes"}

In my indexes, there is a record for the full name "Maggie Wilfred". when i run this query nothing gets returned.

any reason?.i feel the problem is in the wild card? "fullname" => "Mag*"

Upvotes: 2

Views: 2053

Answers (3)

epipko
epipko

Reputation: 397

{
    "prefix" : { "fullname" : "Mag" }
}

Upvotes: 0

Christophe Schutz
Christophe Schutz

Reputation: 613

The match query doesn't support wildcards as stated on https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

You should try with the Wildcardquery.

Upvotes: 0

Amitsh
Amitsh

Reputation: 355

It should work, try it in this way:

    {
        "wildcard" : { "fullname" : "Mag*" }
    }

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html

Upvotes: 1

Related Questions