20
votes

I have documents that represent users. They have fields name and surname.

Let's say I have two users indexed - Michael Jackson and Michael Starr. I want these sample searches to work:

  1. Michael => { Michael Jackson, Michael Starr }
  2. Jack Mich => { Michael Jackson } (incomplete words and reversed order)
  3. Michal Star => { Michael Starr } (fuzzy search)

I tried different queries and got the best results from multi_match query with cross_fields type. There are 2 problems though:

  1. It only finds something when at least one of the two words is complete. If I type Jackson Mich, it finds Michael Jackson but if I type Jack Mich, it doens't find anything (but I want it to find it).
  2. It cannot be set to fuzzy search. I really need the fuzzy search but keep the quality of multi_match with cross_fields type.

In other words, I want to implement Facebook-like people searching.

I'm pretty new to ElasticSearch so maybe I'm missing something obvious. Sorry if I do.

2

2 Answers

6
votes

Jack Mich type of searches

  • Make sure when you are querying use OR and not AND e.g. Jack OR Mich
  • Also essentially you want to do a partial matching on the fields. For this you need to enable nGrams on these fields (do this in mapping) so that index have matches for partial words

You are using the correct query type. These two should solve your problems.

PS: We all are learning here, doing that together is fun :)

2
votes

To answer your second problem:

It cannot be set to fuzzy search. I really need the fuzzy search but keep the quality of multi_match with cross_fields type.

The cross_fields query does not support fuzzy searching. See the issue on GitHub:

https://github.com/elasticsearch/elasticsearch/issues/6866