Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch how to use multi_match with wildcard

I have a User object with properties Name and Surname. I want to search these fields using one query, and I found multi_match in the documentation, but I don't know how to properly use that with a wildcard. Is it possible?

I tried with a multi_match query but it didn't work:

{
    "query": {
        "multi_match": {
            "query": "*mar*",
            "fields": [
                "user.name",
                "user.surname"
            ]
        }
    }
}
like image 758
Martin Kvasňa Kvasnica Avatar asked Jun 05 '13 07:06

Martin Kvasňa Kvasnica


3 Answers

Alternatively you could use a query_string query with wildcards.

"query": {
    "query_string": {
        "query": "*mar*",
        "fields": ["user.name", "user.surname"]
    }
}

This will be slower than using an nGram filter at index-time (see my other answer), but if you are looking for a quick and dirty solution...

Also I am not sure about your mapping, but if you are using user.name instead of name your mapping needs to look like this:

"your_type_name_here": {
    "properties": {
        "user": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "surname": {
                    "type": "string"
                }
            }
        }
    }
}
like image 94
ramseykhalaf Avatar answered Sep 20 '22 14:09

ramseykhalaf


Such a query worked for me:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "should": [
            {"query": {"wildcard": {"user.name": {"value": "*mar*"}}}},
            {"query": {"wildcard": {"user.surname": {"value": "*mar*"}}}}
          ]
        }
      }
    }
  }
}

Similar to what you are doing, except that in my case there could be different masks for different fields.

like image 20
Catherine Tsokur Avatar answered Sep 21 '22 14:09

Catherine Tsokur


I just did this now:

GET _search {
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "theDate": {
                            "gte": "2014-01-01",
                            "lte": "2014-12-31"
                        }
                    }
                },
                {
                    "match" : {
                        "Country": "USA"
                    }
                }
            ],
            "should": [
                {
                    "wildcard" : { "Id_A" : "0*" }
                },
                {
                    "wildcard" : { "Id_B" : "0*" }
                }
            ],"minimum_number_should_match": 1
        }
    }
}
like image 12
David Johnson Avatar answered Sep 21 '22 14:09

David Johnson