Boosting by popularity in a nested field with a filter


#1

I am trying to create a searchable catalogue of products where products that the user has bought before will get a boost in scoring.

I have an index full of products, and these products may have been bought multiple times by different users which I have represented using nesting. Some products have entries for all users, some will have none.

I need to use ES scoring on text matching on the name and description fields and then need to use function_score field_value_factor on the nested field to pick out the particular users "count" field.

I have 2 queries, one that returns the count for the user I want, and one that does the full-text searching. The problem is I don't know how to combine these as when I use "should" both scores are normalised, when really I want to just add these _scores together.

Is there a way to do this?

Attempted query:

GET /nest_index_toy/_search
{ 
   "query": {
      "bool": {
         "should": [
            {
               "query": {
                  "multi_match": {
                      "use_dis_max": false, 
                     "query": "black super quality toner",
                     "fields": [
                        "name^3",
                        "description"
                     ],
                     "tie_breaker": 0.3
                  }
               }
            },
            {
               "query": {
                  "nested": {
                     "path": "user",
                     "query": {
                        "function_score": {
                           "filter": {
                              "term": {
                                 "user.userid": "MWUser1"
                              }
                           },
                           "functions": [
                              {
                                 "field_value_factor": {
                                    "field": "user.count",
                                    "modifier": "log1p",
                                    "missing": 0
                                 }
                              }
                           ]
                        }
                     }
                  }
               }
            }
         ]
      }
   }
}

My mapping is:

{
  "mappings": {
    "nest_type": {
      "properties": {
        "id" :             {"type":"string"},
        "company_code" :   {"type":"string"},
        "name" :           {"type":"string"},
        "description" :    {"type":"string"},
        "virtual_entity" : {"type":"boolean"},
        "created_at" :     {"type":"date"},
        "updated_at" :     {"type":"date"},
        "user": {
          "type": "nested",
          "properties": {
            "userid": {"type":"string"},
            "count": {"type":"short"},
            "last_bought": {"type":"date"}
          } 
        },
        "@timestamp" : {"type":"date"}
      }
    }
  }
}   

Some documents are:

{
  "id": "C8061X",
  "company_code": "MWCOMPCODE",
  "name": "Black LaserJet Toner Cartridge",
  "description": "- HP LaserJet C8061 Family Print Cartridges deliver extra sharp black text, smooth greyscales and fine detail in graphics.\n- HP LaserJet C8061 Family Print Cartridges with Smart Printing Technology with in-built reliability and rigorous quality testing ensure maximum printer uptime with minimum user intervention.\n- HP LaserJet C8061 Family Print Cartridges all-in-one design allow effortless installation and maintenance. Smart Printing Technology features monitoring of supplies status and usage information via the printers control panel or web browser.\n",
  "virtual_entity": false,
  "created_at": "2016-09-21T12:23:53.000Z",
  "updated_at": "2016-09-21T12:23:53.000Z",
  "user": [
    {
      "userid": "MWUser1",
      "count": 4,
      "last_bought": "2016-09-14T12:43:30.000Z"
    },
    {
      "userid": "MWUser2",
      "count": 2,
      "last_bought": "2016-09-14T10:00:00.000Z"
    }
  ],
  "@timestamp": "2016-09-21T13:38:30.077Z"
}
{
  "id": "C8061Y",
  "company_code": "MWCOMPCODE",
  "name": "Black LaserJet Toner Cartridge Super Quality",
  "description": "- HP LaserJet C8061 Family Print Cartridges deliver extra quality sharp black text, smooth greyscales and fine detail in graphics.",
  "virtual_entity": false,
  "created_at": "2016-09-21T12:23:53.000Z",
  "updated_at": "2016-09-21T12:23:53.000Z",
  "@timestamp": "2016-09-21T13:38:30.077Z"
}

#2

I think my issue was having extra "query" tags where I didn't need them. I ended up using the following:

GET /nest_index_toy/_search
{
   "query": {
      "bool": {
         "must": {
            "multi_match": {
               "use_dis_max": false,
               "query": "black toner super quality",
               "fields": [
                  "name^3",
                  "description"
               ],
               "tie_breaker": 0.3,
               "boost": 2
            }
         },
         "should": [
            {
               "multi_match": {
                  "use_dis_max": false,
                  "query": "black toner super quality",
                  "fields": [
                     "name^3",
                     "description"
                  ],
                  "tie_breaker": 0.3,
                  "boost": 2
               }
            },
            {
               "nested": {
                  "path": "user",
                  "query": {
                     "function_score": {
                        "filter": {
                           "term": {
                              "user.userid": "MWUser1"
                           }
                        },
                        "functions": [
                           {
                              "field_value_factor": {
                                 "field": "user.count",
                                 "modifier": "log1p",
                                 "missing": 0
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

(Mark Walkom) #3

Thanks for sharing your solution!


(system) #4