Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I include spaces in fuzzy query fields?

I have this data:

name:
  first: 'John'
  last: 'Smith'

When I store it in ES, AFAICT it's better to make it one field. However, should this one field be:

name: 'John Smith'

or

name: 'JohnSmith'

?

I'm thinking that the query should be:

query: 
  match: 
    name: 
      query: searchTerm
      fuzziness: 'AUTO'
      operator: 'and'

Example search terms are what people might type in a search box, like

John
Jhon Smi
J Smith
Smith

etc.

like image 614
Loren Avatar asked Apr 18 '15 04:04

Loren


1 Answers

You will probably want a combination of ngrams and a fuzzy match query. I wrote a blog post about ngrams for Qbox if you need a primer: https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch. I'll swipe the starter code at the end of the post to illustrate what I mean here.

Also, I don't think it matters much whether you use two fields for name, or just one. If you have some other reason you want two fields, you may want to use the _all field in your query. For simplicity I'll just use a single field here.

Here is a mapping that will get you the partial-word matching you want, assuming you only care about tokens that start at the beginning of words (otherwise use ngrams instead of edge ngrams). There are lots of nuances to using ngrams, so I'll refer to you the documentation and my primer if you want more info.

PUT /test_index
{
   "settings": {
      "number_of_shards": 1,
      "analysis": {
         "filter": {
            "edge_ngram_filter": {
               "type": "edge_ngram",
               "min_gram": 1,
               "max_gram": 10
            }
         },
         "analyzer": {
            "edge_ngram_analyzer": {
               "type": "custom",
               "tokenizer": "standard",
               "filter": [
                  "lowercase",
                  "edge_ngram_filter"
               ]
            }
         }
      }
   },
   "mappings": {
      "doc": {
         "properties": {
            "name": {
               "type": "string",
               "index_analyzer": "edge_ngram_analyzer",
               "search_analyzer": "standard"
            }
         }
      }
   }
}

One thing to note here, in particular: "min_gram": 1. This means that single-character tokens will be generated from indexed values. This will cast a pretty wide net when you query (lots of words begin with "j", for example), so you may get some unexpected results, especially when combined with fuzziness. But this is needed to get your "J Smith" query to work right. So there are some trade-offs to consider.

For illustration, I indexed four documents:

PUT /test_index/doc/_bulk
{"index":{"_id":1}}
{"name":"John Hancock"}
{"index":{"_id":2}}
{"name":"John Smith"}
{"index":{"_id":3}}
{"name":"Bob Smith"}
{"index":{"_id":4}}
{"name":"Bob Jones"}

Your query mostly works, with a couple of caveats.

POST /test_index/_search
{
    "query": {
        "match": {
           "name": {
               "query": "John",
               "fuzziness": "AUTO",
               "operator": "and"
           }
        }
    }
}

this query returns three documents, because of ngrams plus fuzziness:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0.90169895,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.90169895,
            "_source": {
               "name": "John Hancock"
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 0.90169895,
            "_source": {
               "name": "John Smith"
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "4",
            "_score": 0.6235822,
            "_source": {
               "name": "Bob Jones"
            }
         }
      ]
   }
}

That may not be what you want. Also, "AUTO" doesn't work with the "Jhon Smi" query, because "Jhon" is an edit distance of 2 from "John", and "AUTO" uses an edit distance of 1 for strings of 3-5 characters (see the docs for more info). So I have to use this query instead:

POST /test_index/_search
{
    "query": {
        "match": {
           "name": {
               "query": "Jhon Smi",
               "fuzziness": 2,
               "operator": "and"
           }
        }
    }
}
...
{
   "took": 17,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1.4219328,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 1.4219328,
            "_source": {
               "name": "John Smith"
            }
         }
      ]
   }
}

The other queries work as expected. So this solution isn't perfect, but it will get you close.

Here's all the code I used:

http://sense.qbox.io/gist/ba5a6741090fd40c1bb20f5d36f3513b4b55ac77

like image 96
Sloan Ahrens Avatar answered Nov 15 '22 11:11

Sloan Ahrens