# Best approach for weighting tags on a document

**URL:** <https://discuss.elastic.co/t/best-approach-for-weighting-tags-on-a-document/12437>\
**Category:** Elasticsearch\
**Created:** [June 16, 2013, 10:22am UTC](https://discuss.elastic.co/t/best-approach-for-weighting-tags-on-a-document/12437 "2013-06-16T10:22:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Greening](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_greening/32/2285_2.png) [@Chris\_Greening](https://discuss.elastic.co/u/Chris_Greening)\
**Post date:** [June 16, 2013, 10:22am UTC](https://discuss.elastic.co/t/best-approach-for-weighting-tags-on-a-document/12437/1 "2013-06-16T10:22:42Z")

</div>

Hi All,

I've been thinking about how to index document tags - for example a user  
might add a tag to document to indicate what the document is about.

I'm assuming that the best way to index these tags would be to have an  
array of tags.

What I'd like to do though is have some way of weighting the tags - so if  
multiple people add the same tag it becomes more relevant than a tag that  
just a few people have added.

What's the best way of doing this?

Thanks  
Chris

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 16, 2013, 11:55am UTC](https://discuss.elastic.co/t/best-approach-for-weighting-tags-on-a-document/12437/2 "2013-06-16T11:55:38Z")

</div>

Do you want to display a Tag cloud?  
You can use a Terms facet to get the top10 tags.

Is it what you are looking for?

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 16 juin 2013 à 12:22, Chris Greening [cmgreening@gmail.com](mailto:cmgreening@gmail.com) a écrit :

> Hi All,
> 
> I've been thinking about how to index document tags - for example a user might add a tag to document to indicate what the document is about.
> 
> I'm assuming that the best way to index these tags would be to have an array of tags.
> 
> What I'd like to do though is have some way of weighting the tags - so if multiple people add the same tag it becomes more relevant than a tag that just a few people have added.
> 
> What's the best way of doing this?
> 
> ## Thanks Chris
> 
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Chris\_Greening](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_greening/32/2285_2.png) [@Chris\_Greening](https://discuss.elastic.co/u/Chris_Greening)\
**Post date:** [June 16, 2013, 2:38pm UTC](https://discuss.elastic.co/t/best-approach-for-weighting-tags-on-a-document/12437/3 "2013-06-16T14:38:49Z")

</div>

I was thinking more along the lines of something like this

doc1  
tags: shoes^10, green^5, mens^2

doc2  
tags: shoes^20, green^1, turquoise^6

So the tags would be given a higher weighting based on the number of people  
who had tagged the item with the same tag value.

If I did a search for "green shoes" then doc1 would come before doc2 in the  
search results.

Cheers  
Chris.

On Sunday, June 16, 2013 12:55:38 PM UTC+1, David Pilato wrote:

> Do you want to display a Tag cloud?  
> You can use a Terms facet to get the top10 tags.
> 
> Is it what you are looking for?
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 16 juin 2013 à 12:22, Chris Greening \<[cmgre...@gmail.com](mailto:cmgre...@gmail.com) \<javascript:\>\>  
> a écrit :
> 
> Hi All,
> 
> I've been thinking about how to index document tags - for example a user  
> might add a tag to document to indicate what the document is about.
> 
> I'm assuming that the best way to index these tags would be to have an  
> array of tags.
> 
> What I'd like to do though is have some way of weighting the tags - so if  
> multiple people add the same tag it becomes more relevant than a tag that  
> just a few people have added.
> 
> What's the best way of doing this?
> 
> Thanks  
> Chris
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![q42jaap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/q42jaap/32/2241_2.png) [@q42jaap](https://discuss.elastic.co/u/q42jaap)\
**Post date:** [June 17, 2013, 7:21pm UTC](https://discuss.elastic.co/t/best-approach-for-weighting-tags-on-a-document/12437/4 "2013-06-17T19:21:23Z")

</div>

I think you need a custom\_score query for every term in the search string.

The custom\_score can then take a script to extract the score from the  
document. You can use the mvel-scripting language to do this, but I've  
never done that before.

I'd store the values like this:

{  
tags: [  
{ key:"shoes", value: 20 },  
{ key:"green", value: 5 }  
]  
}

The custom\_score for 1 term could look like this:

{  
custom\_score : {  
"script" : ????, // should result in the value that belongs to shoes  
"query" : {  
"filtered" : {  
"filter" : {  
term : { "tags.key" : "shoes" }  
}  
}  
}  
}  
}

This query uses a filter to make use of caching. If you have more than 1  
term, you should combine them in a bool query, this will also combine the  
scoring for the individual custom\_queries.  
The mvel script will be a bit verbose probably, you have to foreach over  
the tags and check to see if the search term matches the key.

note: mvel scripting can be parameterized which you should always do. But  
since you'll have to write a foreach, it will be very slow, be warned.

An optimization could be the following format to store the boosting:

{  
tags : ["shoes", "green", "mens"]  
tags\_score: {  
"shoes" : 20,  
"green" : 5,  
"mens" : 1  
}  
}

that way you can filter on "tags", and fetch the scores with a map lookup:  
[http://mvel.codehaus.org/MVEL+2.0+Property+Navigation#MVEL20PropertyNavigation-MapAccess](http://mvel.codehaus.org/MVEL+2.0+Property+Navigation#MVEL20PropertyNavigation-MapAccess)  
Again, I haven't done anything with mvel myself, so I'm not sure this  
MapAccess is even supported...

Good luck,

Jaap

On Sunday, June 16, 2013 4:38:49 PM UTC+2, Chris Greening wrote:

> I was thinking more along the lines of something like this
> 
> doc1  
> tags: shoes^10, green^5, mens^2
> 
> doc2  
> tags: shoes^20, green^1, turquoise^6
> 
> So the tags would be given a higher weighting based on the number of  
> people who had tagged the item with the same tag value.
> 
> If I did a search for "green shoes" then doc1 would come before doc2 in  
> the search results.
> 
> Cheers  
> Chris.
> 
> On Sunday, June 16, 2013 12:55:38 PM UTC+1, David Pilato wrote:
> 
> > Do you want to display a Tag cloud?  
> > You can use a Terms facet to get the top10 tags.
> > 
> > Is it what you are looking for?
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 16 juin 2013 à 12:22, Chris Greening [cmgre...@gmail.com](mailto:cmgre...@gmail.com) a écrit :
> > 
> > Hi All,
> > 
> > I've been thinking about how to index document tags - for example a user  
> > might add a tag to document to indicate what the document is about.
> > 
> > I'm assuming that the best way to index these tags would be to have an  
> > array of tags.
> > 
> > What I'd like to do though is have some way of weighting the tags - so if  
> > multiple people add the same tag it becomes more relevant than a tag that  
> > just a few people have added.
> > 
> > What's the best way of doing this?
> > 
> > Thanks  
> > Chris
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_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 6, 2017, 2:30am UTC](https://discuss.elastic.co/t/best-approach-for-weighting-tags-on-a-document/12437/5 "2017-07-06T02:30:45Z")

</div>


