0

I have three fields name, name.ngram and model_number that I am trying to search. I have boosted name so any match on name ranks higher than name.ngram. However, I still have some cases where the name.ngram doesn't match with terms that contain weird punctuation so I want to complete my query with a fuzzy search on the name and model_number fields field.

Here is the query I ultimately want. If I remove the second multi_match, the query works but does not include the fuzzy search.

{
         explain: true,
         query:{
           function_score: { 
              "query": {
                "bool": {
                  "should": 
                    {
                      multi_match:{
                         fields: ["name^10", "name.ngram", "model_number"],
                         type: "most_fields",
                         query: "#{query}"
                       },
                       multi_match:{
                           fields: ["name", "model_number"],
                           type: "most_fields",
                           query: "#{query}",
                           fuzziness: "2"
                        }
                    },
                  "filter": {
                    "bool": { 
                      "must": filters
                    }
                  }
               }
             },field_value_factor:{
                    field: "popularity",
                    modifier: "log1p",
                    factor: 5

                 },
                 boost_mode: "sum"
             }
       },highlight: {
            fields: {
              :"*" => {}
            }
          },
        aggs: {categories: { terms: { field: "category.raw"} }} 

      } 

I want to ultimately treat the fuzzy search like I do the ngram so that non fuzzy matches rank first and fuzzy matches rank second. How do I add a second multi_match query that would allow for this?

1 Answer 1

0

Your bool/should clause is not properly formed, it should be an array with two elements, one for each multi_match constraint, like this:

  ...
  "should": [
    {
      "multi_match": {
        "fields": [
          "name^10",
          "name.ngram",
          "model_number"
        ],
        "type": "most_fields",
        "query": "#{query}"
      }
    },
    {
      "multi_match": {
        "fields": [
          "name",
          "model_number"
        ],
        "type": "most_fields",
        "query": "#{query}",
        "fuzziness": "2"
      }
    }
  ]
  ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.