# Custom score on the basis of a field value(colon separated) and boosting factor in ES 6.2.4

**URL:** https://discuss.elastic.co/t/custom-score-on-the-basis-of-a-field-value-colon-separated-and-boosting-factor-in-es-6-2-4/144888
**Category:** Elasticsearch
**Created:** [August 17, 2018, 11:15am UTC](https://discuss.elastic.co/t/custom-score-on-the-basis-of-a-field-value-colon-separated-and-boosting-factor-in-es-6-2-4/144888 "2018-08-17T11:15:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![amit.nema](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@amit.nema](https://discuss.elastic.co/u/amit.nema)
#### Post date: [August 17, 2018, 11:15am UTC](https://discuss.elastic.co/t/custom-score-on-the-basis-of-a-field-value-colon-separated-and-boosting-factor-in-es-6-2-4/144888/1 "2018-08-17T11:15:58Z")

</div>

custom score on the basis of a field value(colon separated) and booting factor

e.g I have a indexed a field as  
scorevector : "Lucene:1.3 Hadoop:4.3 Elastic:6.8 Lucene:7.2"

I would like to search the index as

> my\_index/\_search  
> get\_custom\_score : {  
> scorevector : "Lucene^5 Hadoop^2 Elastic^3 HDFS^1.5"  
> }

score must be returned as [(1.3+7.2)/2]\*5 + 4.3\*2 + 6.8\*3 = 50.25  
`term "Lucene" payload average [(1.3+7.2)/2]`

I tried few things as below

> POST my\_index  
> {  
> "settings": {  
> "analysis": {  
> "analyzer": {  
> "payloads": {  
> "type": "custom",  
> "tokenizer": "whitespace",  
> "filter": [  
> "my\_delimited\_payload"  
> ]  
> }  
> },  
> "filter": {  
> "my\_delimited\_payload": {  
> "type": "delimited\_payload",  
> "delimiter": ":",  
> "encoding": "float"  
> }  
> }  
> }  
> },  
> "mappings": {  
> "\_doc": {  
> "properties": {  
> "scorevector": {  
> "norms": true,  
> "index": true,  
> "store": false,  
> "type": "text",  
> "analyzer": "payloads"  
> }  
> }  
> }  
> }  
> }

> PUT my\_index  
> {scorevector : "Lucene:1.3 Hadoop:4.3 Elastic:6.8 HDFS:7.2"}

Now as per my understanding this payload (float value next to colon) should be analyzed.

I think I can make use of AveragePayloadFunction.

I need some solution to get the score as needed.

Could you please help me to with some example code?

Any help will be appreacciated.

Thanks in advance  
Amit

---

<div class="post-metadata">

### Author: ![amit.nema](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@amit.nema](https://discuss.elastic.co/u/amit.nema)
#### Post date: [August 31, 2018, 4:05pm UTC](https://discuss.elastic.co/t/custom-score-on-the-basis-of-a-field-value-colon-separated-and-boosting-factor-in-es-6-2-4/144888/2 "2018-08-31T16:05:37Z")

</div>

I could resolve it by making two plugins as below

- MySimilarityPlugin.java : extends Plugin  
@Override  
public void onIndexModule(IndexModule im) {  
&nbsp;&nbsp;&nbsp;&nbsp;im.addSimilarity(..., new MySimilarity(...))  
}  
MySimilarity extends ClassicSimilarity and all method returns as 1.

- MyQueryParserPlugin.java : extends Plugin implements SearchPlugin

1. Override getQueries(..MyQueryBuilder...)
2. MyQueryBuilder is same as QueryStringQueryBuilder use MyQueryParser instead QueryStringQueryParser.
3. MyQueryParser extends QueryStringQueryParser  
@Override  
public Query getFieldQuery(String f, String qt, boolean q) {  
&nbsp;&nbsp;&nbsp;&nbsp;return new PayloadScoreQuery(new SpanTermQuery(new Term(field, queryText)), new AveragePayloadFunction());  
}

- 'test' index as below  
curl -XPUT 'localhost:9200/test' -d ' {  
"settings": {  
"index": {  
"similarity": {  
"mysim": {  
"type": "mysimilarity"  
}  
},  
"analysis": {  
"analyzer": {  
"myanalyzer": {  
"filter": [  
"mytokenfilter"  
],  
"type": "custom",  
"tokenizer": "whitespace"  
}  
},  
"filter": {  
"mytokenfilter": {  
"type": "delimited\_payload",  
"delimiter": ":"  
}  
}  
}  
}  
},  
"mapping": {  
"docs": {  
"properties": {  
"mytext": {  
"type": "text",  
"similarity": "mysim",  
"analyzer": "myanalyzer"  
}  
}  
}  
}  
}'

Its working as expected. 😀

Please let me know, if you have any better approach.

---

<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: [September 28, 2018, 4:05pm UTC](https://discuss.elastic.co/t/custom-score-on-the-basis-of-a-field-value-colon-separated-and-boosting-factor-in-es-6-2-4/144888/3 "2018-09-28T16:05:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
