0

I have a query that works decently well on a product catalog. My query currently utilizes fuzziness in the multi_match, but I would prefer that the search utilized the fuzziness option if the same query (without fuzziness) didn't return any results. Is this possible to do within the query? (Using Rails 5)

Here is my current query:

@products = Product.search(
       query:{
         function_score:{
           query:{
             bool:{
               must:{
                 multi_match:{
                   fields: ['brand^10', '_all'],
                   query: "#{query}",
                   fuzziness: "AUTO"
                 }
               },

                 filter:{
                   bool:{
                     must:filters
                   }
                 }

             }
           },
           field_value_factor:{
              field: "popularity",
              modifier: "log1p",
              factor: 0.5

           },
           boost_mode: "sum"
         }
       }).page(page).per(25)
Cannon Moyer
  • 3,014
  • 3
  • 31
  • 75

1 Answers1

0

What I often do is using a bool query with 2 match clauses, one without fuzzy which I’m boosting the score by a factor 2 or 3, one another with the fuzzy match which will also try with fuzzy but with a lesser score so it will be at the end of the list.

I demonstrated something similar with this gist.

Basically do something like:

DELETE user
POST user/doc
{
  "name": "David"
}
GET user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": {
              "query": "david",
              "boost": 3.0
            }
          }
        },
        {
          "match": {
            "name": {
              "query": "david",
              "fuzziness": 2
            }
          }
        }
      ]
    }
  }
}
GET user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": {
              "query": "dovad",
              "boost": 3.0
            }
          }
        },
        {
          "match": {
            "name": {
              "query": "dovad",
              "fuzziness": 2
            }
          }
        }
      ]
    }
  }
}
dadoonet
  • 14,109
  • 3
  • 42
  • 49