# Elasticsearch for product database search - weighting

**URL:** <https://discuss.elastic.co/t/elasticsearch-for-product-database-search-weighting/53046>\
**Category:** Elasticsearch\
**Created:** [June 16, 2016, 7:09pm UTC](https://discuss.elastic.co/t/elasticsearch-for-product-database-search-weighting/53046 "2016-06-16T19:09:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![midget2000x](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@midget2000x](https://discuss.elastic.co/u/midget2000x)\
**Post date:** [June 16, 2016, 7:09pm UTC](https://discuss.elastic.co/t/elasticsearch-for-product-database-search-weighting/53046/1 "2016-06-16T19:09:14Z")

</div>

Absolute newb here, so go easy! 😏

I am wanting to build a better search for my shopping site, and I'm tinkering with elasticsearch right now. I'm trying to come up with a somewhat smarter "default" product search query that searches four fields, each weighted differently:

name (product name), weighted highest  
search\_words (list of keywords), medium weight  
description (item copy), low weight  
skuid (item number), lowest weight - needed in case the customer plugs in a SKU to the search field

But I also want to consider two other fields: sort\_priority and creation\_date

sort\_priority is a value we use for "important" items that we want to show higher when browsing category. Since it denotes an item's importance, I want to factor it in to search somehow. 9999999 is the default, and anything lower is considered more important, with "1" being the most important.

creation\_date is just that. It would be awesome to be able to weight newer items a bit higher.

My index has the following mappings:

`mappings => { "product" => { properties => { "price" => {type => 'double'}, "origprice" => {type => 'double'}, "sort_priority" => {type => 'integer'}, "creation_date" => { type => "date", format => "YYYY-mm-dd" } } } }`  
Which I hope will give me the ability to use sort\_priority and creation\_date in the fashion I outlined.

This query seems to work for the matching:

`"query" : { "multi_match" : { "query": "awesome product", "type": "best_fields", "fields": ["name^10", "search_words^5", "description^2", "skuid^1"] } }`

But I can't figure out the scoring manipulation. I'd think something in this realm?

`"query" : { "function_score": { "query" : { "multi_match" : { "query": "awesome product", "type": "best_fields", "fields": ["name^10", "search_words^5", "description^2", "skuid^1"] } } }, "functions" : ["script_score" : { "script" : "_score * (1000000 - doc['sort_priority'].value)" } ] }`

(though that doesn't factor in creation\_date at all)

Any help or hints appreciated!

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [June 16, 2016, 7:38pm UTC](https://discuss.elastic.co/t/elasticsearch-for-product-database-search-weighting/53046/2 "2016-06-16T19:38:42Z")

</div>

> [@midget2000x](#):
>
> I'd think something in this realm?

Yeah, something like it. I think you have all the right tools but are maybe missing a few points.

1. Have a look at [decay functions](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-decay). You probably don't want to use dynamic scripting because it is a security hole, and, unless you are on 5.0's alphas, you only have groovy to work with, which isn't properly sandboxed. In 5.0 you'll have painless which **is** sandboxed, but it still might be simpler to use the decay functions.

2. You might want cross\_fields instead of best\_fields.

3. When you post a code snippet, wrap it in ``` above and below and the site will preserve the formatting.

4. You should be able to apply creation\_date in the same way as sort priority. Personally I like gaussian decay for creation date kinds of things.

---

<div class="post-metadata">

**Author:** ![midget2000x](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@midget2000x](https://discuss.elastic.co/u/midget2000x)\
**Post date:** [June 16, 2016, 8:17pm UTC](https://discuss.elastic.co/t/elasticsearch-for-product-database-search-weighting/53046/3 "2016-06-16T20:17:02Z")

</div>

Thanks so much for the guidance, Nik. I'll check it out.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:43pm UTC](https://discuss.elastic.co/t/elasticsearch-for-product-database-search-weighting/53046/4 "2017-07-05T22:43:07Z")

</div>


