Search code examples
node.jsmongodbelasticsearchmongoosemongoosastic

How to fuzzy query against multiple fields in elasticsearch?


Here's my query as it stands:

"query":{
    "fuzzy":{
        "author":{
            "value":query,
            "fuzziness":2
        },
        "career_title":{
            "value":query,
            "fuzziness":2
        }
    }
}

This is part of a callback in Node.js. Query (which is being plugged in as a value to compare against) is set earlier in the function.

What I need it to be able to do is to check both the author and the career_title of a document, fuzzily, and return any documents that match in either field. The above statement never returns anything, and whenever I try to access the object it should create, it says it's undefined. I understand that I could write two queries, one to check each field, then sort the results by score, but I feel like searching every object for one field twice will be slower than searching every object for two fields once.


Solution

  • https://www.elastic.co/guide/en/elasticsearch/guide/current/fuzzy-match-query.html

    If you see here, in a multi match query you can specify the fuzziness...

    {
      "query": {
        "multi_match": {
          "fields":  [ "text", "title" ],
          "query":     "SURPRIZE ME!",
          "fuzziness": "AUTO"
        }
      }
    }
    

    Somewhat like this.. Hope this helps.