# Search by a weighted tag - deadend

**URL:** <https://discuss.elastic.co/t/search-by-a-weighted-tag-deadend/13504>\
**Category:** Elasticsearch\
**Created:** [September 7, 2013, 6:47am UTC](https://discuss.elastic.co/t/search-by-a-weighted-tag-deadend/13504 "2013-09-07T06:47:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sparky\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sparky_2/32/2113_2.png) [@sparky\_2](https://discuss.elastic.co/u/sparky_2)\
**Post date:** [September 7, 2013, 6:47am UTC](https://discuss.elastic.co/t/search-by-a-weighted-tag-deadend/13504/1 "2013-09-07T06:47:14Z")

</div>

Hi guys, I am struggling with a setup to be able to search using weighted  
tags. The score comes out wrong and some objects (that should match) are  
ignored completely.

My docs:

[  
"title":"1", "tags" : ["a", "b", "c"],  
"title":"2", "tags" : ["a", "b"],  
"title":"3", "tags" : ["c", "b"],  
"title":"4", "tags" : ["b"]  
]

My query:

{  
"query": {  
"custom\_filters\_score": {  
"query": {  
"terms": {  
"tags": ["a", "c"]  
}  
},  
"filters": [  
{"filter":{"term":{"tags":"a"}}, "script":"1.0"},  
{"filter":{"term":{"tags":"c"}}, "script":"1.5"}  
],  
"score\_mode": "total"  
}  
}  
}

Response:

{  
"\_shards": {  
"failed": 0,  
"successful": 5,  
"total": 5  
},  
"hits": {  
"hits": [  
{  
"\_id": "3",  
"\_index": "test",  
"\_score": 0.23837921,  
"\_source": {  
"tags": [  
"c",  
"b"  
],  
"title": "3"  
},  
"\_type": "bit"  
},  
{  
"\_id": "1",  
"\_index": "test",  
"\_score": 0.042195037,  
"\_source": {  
"tags": [  
"a",  
"b",  
"c"  
],  
"title": "1"  
},  
"\_type": "bit"  
}  
],  
"max\_score": 0.23837921,  
"total": 2  
},  
"timed\_out": false,  
"took": 3  
}

However, I expected the following result:

1. Document 1 (score: 2.5 because "a" and "c")
2. Document 3 (score: 1.5 because "c")
3. Document 2 (score: 1.0 because "a")

That is, the document #2 is missing completely, the order and the scores  
are wrong.  
Can you give me a hint of what I am doing wrong?

--  
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:** ![Britta\_Weber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/britta_weber/32/1113_2.png) [@Britta\_Weber](https://discuss.elastic.co/u/Britta_Weber)\
**Post date:** [September 7, 2013, 10:15am UTC](https://discuss.elastic.co/t/search-by-a-weighted-tag-deadend/13504/2 "2013-09-07T10:15:04Z")

</div>

Hi,

there are two issues here: First, "a" is a stopword and therfore when  
searching for ["a","c"] you only get results for ["c"]. Try a  
different mapping, for example with "index" set to "not\_analyzed".  
Second, the custom\_filters\_score multiplies the value from the script  
to the score of the query. If you want to avoid this, you can use a  
constant\_score like this:

```auto
{
    "query": {
        "custom_filters_score": {
            "filters": [
                {
                    "filter": {
                        "term": {
                            "tags": "a"
                        }
                    },
                    "script": "1.0"
                },
                {
                    "filter": {
                        "term": {
                            "tags": "c"
                        }
                    },
                    "script": "1.5"
                }
            ],
            "query": {
                "constant_score": {
                    "query": {
                        "terms": {
                            "tags": [
                                "a",
                                "c"
                            ]
                        }
                    }
                }
            },
            "score_mode": "total"
        }
    }
}

```

Cheers,  
Britta  
~  
~

On Sat, Sep 7, 2013 at 8:47 AM, sparky [roman.semko@gmail.com](mailto:roman.semko@gmail.com) wrote:

> Hi guys, I am struggling with a setup to be able to search using weighted  
> tags. The score comes out wrong and some objects (that should match) are  
> ignored completely.
> 
> My docs:
> 
> [  
> "title":"1", "tags" : ["a", "b", "c"],  
> "title":"2", "tags" : ["a", "b"],  
> "title":"3", "tags" : ["c", "b"],  
> "title":"4", "tags" : ["b"]  
> ]
> 
> My query:
> 
> {  
> "query": {  
> "custom\_filters\_score": {  
> "query": {  
> "terms": {  
> "tags": ["a", "c"]  
> }  
> },  
> "filters": [  
> {"filter":{"term":{"tags":"a"}}, "script":"1.0"},  
> {"filter":{"term":{"tags":"c"}}, "script":"1.5"}  
> ],  
> "score\_mode": "total"  
> }  
> }  
> }
> 
> Response:
> 
> {  
> "\_shards": {  
> "failed": 0,  
> "successful": 5,  
> "total": 5  
> },  
> "hits": {  
> "hits": [  
> {  
> "\_id": "3",  
> "\_index": "test",  
> "\_score": 0.23837921,  
> "\_source": {  
> "tags": [  
> "c",  
> "b"  
> ],  
> "title": "3"  
> },  
> "\_type": "bit"  
> },  
> {  
> "\_id": "1",  
> "\_index": "test",  
> "\_score": 0.042195037,  
> "\_source": {  
> "tags": [  
> "a",  
> "b",  
> "c"  
> ],  
> "title": "1"  
> },  
> "\_type": "bit"  
> }  
> ],  
> "max\_score": 0.23837921,  
> "total": 2  
> },  
> "timed\_out": false,  
> "took": 3  
> }
> 
> However, I expected the following result:
> 
> Document 1 (score: 2.5 because "a" and "c")  
> Document 3 (score: 1.5 because "c")  
> Document 2 (score: 1.0 because "a")
> 
> That is, the document #2 is missing completely, the order and the scores are  
> wrong.  
> Can you give me a hint of what I am doing wrong?
> 
> --  
> 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:** ![sparky\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sparky_2/32/2113_2.png) [@sparky\_2](https://discuss.elastic.co/u/sparky_2)\
**Post date:** [September 7, 2013, 11:23am UTC](https://discuss.elastic.co/t/search-by-a-weighted-tag-deadend/13504/3 "2013-09-07T11:23:30Z")

</div>

Didn't think about the stopwords - that was it! Thanks Britta and have a  
nice weekend! 🙂

Am Samstag, 7. September 2013 08:47:14 UTC+2 schrieb sparky:

> Hi guys, I am struggling with a setup to be able to search using weighted  
> tags. The score comes out wrong and some objects (that should match) are  
> ignored completely.
> 
> My docs:
> 
> [  
> "title":"1", "tags" : ["a", "b", "c"],  
> "title":"2", "tags" : ["a", "b"],  
> "title":"3", "tags" : ["c", "b"],  
> "title":"4", "tags" : ["b"]  
> ]
> 
> My query:
> 
> {  
> "query": {  
> "custom\_filters\_score": {  
> "query": {  
> "terms": {  
> "tags": ["a", "c"]  
> }  
> },  
> "filters": [  
> {"filter":{"term":{"tags":"a"}}, "script":"1.0"},  
> {"filter":{"term":{"tags":"c"}}, "script":"1.5"}  
> ],  
> "score\_mode": "total"  
> }  
> }  
> }
> 
> Response:
> 
> {  
> "\_shards": {  
> "failed": 0,  
> "successful": 5,  
> "total": 5  
> },  
> "hits": {  
> "hits": [  
> {  
> "\_id": "3",  
> "\_index": "test",  
> "\_score": 0.23837921,  
> "\_source": {  
> "tags": [  
> "c",  
> "b"  
> ],  
> "title": "3"  
> },  
> "\_type": "bit"  
> },  
> {  
> "\_id": "1",  
> "\_index": "test",  
> "\_score": 0.042195037,  
> "\_source": {  
> "tags": [  
> "a",  
> "b",  
> "c"  
> ],  
> "title": "1"  
> },  
> "\_type": "bit"  
> }  
> ],  
> "max\_score": 0.23837921,  
> "total": 2  
> },  
> "timed\_out": false,  
> "took": 3  
> }
> 
> However, I expected the following result:
> 
> 1. Document 1 (score: 2.5 because "a" and "c")
> 2. Document 3 (score: 1.5 because "c")
> 3. Document 2 (score: 1.0 because "a")
> 
> That is, the document #2 is missing completely, the order and the scores  
> are wrong.  
> Can you give me a hint of what I am doing wrong?

--  
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:17am UTC](https://discuss.elastic.co/t/search-by-a-weighted-tag-deadend/13504/4 "2017-07-06T02:17:37Z")

</div>


