# Writing a custom scoring plugin

**URL:** <https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576>\
**Category:** Elasticsearch\
**Created:** [September 2, 2014, 4:48pm UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576 "2014-09-02T16:48:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)\
**Post date:** [September 2, 2014, 4:48pm UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576/1 "2014-09-02T16:48:11Z")

</div>

I am trying to write a custom scoring plugin that will let me provide some  
extra data that is not in the index (through an external service) and do  
customized ranking. I would ideally like to custom score all the documents  
in the index that match the query.

Currently I am planning to implement it as a service by extending  
AbstractLifecycleComponent which will periodically pull that extra data  
from my external service. Is there a better way ? When and how the plugin  
services will be called. I would like to know in detail about the entry  
points of plugins and how i should write this plugin. Any help or pointers  
would be appreciated.

Thanks  
Srini

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [September 2, 2014, 5:10pm UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576/2 "2014-09-02T17:10:31Z")

</div>

Hello Srinivasan ,

You cna easily create this as a plugin.  
There are many examples here -

> <https://github.com/imotov/elasticsearch-native-script-example/tree/master/src/main/java/org/elasticsearch/examples/nativescript/script>
>
> //github.com/imotov/elasticsearch-native-script-example/tree/master/src/main/java/org/elasticsearch/examples/nativescript/script

Thanks  
Vineeth

On Tue, Sep 2, 2014 at 10:18 PM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
wrote:

> I am trying to write a custom scoring plugin that will let me provide some  
> extra data that is not in the index (through an external service) and do  
> customized ranking. I would ideally like to custom score all the documents  
> in the index that match the query.
> 
> Currently I am planning to implement it as a service by extending  
> AbstractLifecycleComponent which will periodically pull that extra data  
> from my external service. Is there a better way ? When and how the plugin  
> services will be called. I would like to know in detail about the entry  
> points of plugins and how i should write this plugin. Any help or pointers  
> would be appreciated.
> 
> Thanks  
> Srini
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5kxLE1fkk7qhyP0WPzA7\_BmDaMDrqvcW\_%2BULKf%3DfD0Ehw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5kxLE1fkk7qhyP0WPzA7_BmDaMDrqvcW_%2BULKf%3DfD0Ehw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [September 2, 2014, 5:16pm UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576/3 "2014-09-02T17:16:56Z")

</div>

You can write a function score plugin.

Here is an example:

> **[GitHub - jprante/elasticsearch-functionscore-conditionalboost: Boost...](https://github.com/jprante/elasticsearch-functionscore-conditionalboost)**
>
> Boost documents in Elasticsearch when they match dynamic conditions - GitHub - jprante/elasticsearch-functionscore-conditionalboost: Boost documents in Elasticsearch when they match dynamic conditions

Jörg

On Tue, Sep 2, 2014 at 6:48 PM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
wrote:

> I am trying to write a custom scoring plugin that will let me provide some  
> extra data that is not in the index (through an external service) and do  
> customized ranking. I would ideally like to custom score all the documents  
> in the index that match the query.
> 
> Currently I am planning to implement it as a service by extending  
> AbstractLifecycleComponent which will periodically pull that extra data  
> from my external service. Is there a better way ? When and how the plugin  
> services will be called. I would like to know in detail about the entry  
> points of plugins and how i should write this plugin. Any help or pointers  
> would be appreciated.
> 
> Thanks  
> Srini
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)\
**Post date:** [September 2, 2014, 6:09pm UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576/4 "2014-09-02T18:09:50Z")

</div>

Thanks Vineeth. I stumbled upon that example too, thats one of the best  
resources I have found so far.

Thanks Joerg, this example looks very interesting to me. I see that you are  
sending in the extra parameters with the query, but i would like the plugin  
to read the data from external source and not during scoring time (in a  
seperate background thread periodically). I have data for every document  
but organized in a multilayered structure. Thats one of the reason I  
couldnt put it in the document index and retrieve it efficiently during  
scoring. Now I am planning to put it in a in-memory data structure which is  
available for the plugin to use. Do you have any thoughts/recommendation on  
that ?

Thanks  
Srini

On Tue, Sep 2, 2014 at 10:16 AM, [joergprante@gmail.com](mailto:joergprante@gmail.com) \<  
[joergprante@gmail.com](mailto:joergprante@gmail.com)\> wrote:

> You can write a function score plugin.
> 
> Here is an example:
> 
> [GitHub - jprante/elasticsearch-functionscore-conditionalboost: Boost documents in Elasticsearch when they match dynamic conditions](https://github.com/jprante/elasticsearch-functionscore-conditionalboost)
> 
> Jörg
> 
> On Tue, Sep 2, 2014 at 6:48 PM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
> wrote:
> 
> > I am trying to write a custom scoring plugin that will let me provide  
> > some extra data that is not in the index (through an external service) and  
> > do customized ranking. I would ideally like to custom score all the  
> > documents in the index that match the query.
> > 
> > Currently I am planning to implement it as a service by extending  
> > AbstractLifecycleComponent which will periodically pull that extra data  
> > from my external service. Is there a better way ? When and how the plugin  
> > services will be called. I would like to know in detail about the entry  
> > points of plugins and how i should write this plugin. Any help or pointers  
> > would be appreciated.
> > 
> > Thanks  
> > Srini
> > 
> > --  
> > 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).
> > 
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe](https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [September 2, 2014, 9:01pm UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576/5 "2014-09-02T21:01:32Z")

</div>

Not sure if you looked at the source code of the function score weighted  
boost plugin. The plugin uses query-time parameterization but it is of  
course very easy to modify this and use settings at ES node startup time  
for function score boosting, or a refresh mechanism that fetches parameters  
from wherever you like.

Jörg

On Tue, Sep 2, 2014 at 8:09 PM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
wrote:

> Thanks Vineeth. I stumbled upon that example too, thats one of the best  
> resources I have found so far.
> 
> Thanks Joerg, this example looks very interesting to me. I see that you  
> are sending in the extra parameters with the query, but i would like the  
> plugin to read the data from external source and not during scoring time  
> (in a seperate background thread periodically). I have data for every  
> document but organized in a multilayered structure. Thats one of the reason  
> I couldnt put it in the document index and retrieve it efficiently during  
> scoring. Now I am planning to put it in a in-memory data structure which is  
> available for the plugin to use. Do you have any thoughts/recommendation on  
> that ?
> 
> Thanks  
> Srini
> 
> On Tue, Sep 2, 2014 at 10:16 AM, [joergprante@gmail.com](mailto:joergprante@gmail.com) \<  
> [joergprante@gmail.com](mailto:joergprante@gmail.com)\> wrote:
> 
> > You can write a function score plugin.
> > 
> > Here is an example:
> > 
> > [GitHub - jprante/elasticsearch-functionscore-conditionalboost: Boost documents in Elasticsearch when they match dynamic conditions](https://github.com/jprante/elasticsearch-functionscore-conditionalboost)
> > 
> > Jörg
> > 
> > On Tue, Sep 2, 2014 at 6:48 PM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
> > wrote:
> > 
> > > I am trying to write a custom scoring plugin that will let me provide  
> > > some extra data that is not in the index (through an external service) and  
> > > do customized ranking. I would ideally like to custom score all the  
> > > documents in the index that match the query.
> > > 
> > > Currently I am planning to implement it as a service by extending  
> > > AbstractLifecycleComponent which will periodically pull that extra data  
> > > from my external service. Is there a better way ? When and how the plugin  
> > > services will be called. I would like to know in detail about the entry  
> > > points of plugins and how i should write this plugin. Any help or pointers  
> > > would be appreciated.
> > > 
> > > Thanks  
> > > Srini
> > > 
> > > --  
> > > 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).
> > > 
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe](https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)\
**Post date:** [September 5, 2014, 1:14am UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576/6 "2014-09-05T01:14:30Z")

</div>

Hi Joerg,

I tried the data loading part as a separate module and it works, but i have  
a small issue with it. When there is an exception the constructor gets  
called multiple times.

public class CustomDataModule extends AbstractModule {  
private final Settings settings;

```
public SalesDataModule(Settings settings){
    this.settings = settings;
}

@Override
protected void configure() {
    bind(CustomData.class).asEagerSingleton();
}

```

}

public final class CustomData {  
private static Logger logger = LoggerFactory.getLogger(CustomData.class);  
@Inject  
public CustomData(Settings settings) {  
logger.info("CustomData constructor called");  
WebResource webResource = getDataServiceResource(settings);  
response = getSalesDataFromDataService(webResource);  
loadCustomDataIntoMemory(response);  
}  
}

when the external service (which is the source of data) is down or there is  
some other issue, the CustomData constructor gets called repeatedly (approx  
400 times). When there is no exception it gets called only once. I am  
trying to understand why its called multiple times when there is an  
exception.

I took a look at your code sample and I am trying to understand the purpose  
of the Build class and who calls it ?

Can you also elaborate on your thoughts about reloading this customData  
periodically. Currently I am thinking of using LoadingCache to store the  
data and refresh it by using the refresh mechanism.

Thanks  
Srini

On Tue, Sep 2, 2014 at 2:01 PM, [joergprante@gmail.com](mailto:joergprante@gmail.com) \<[joergprante@gmail.com](mailto:joergprante@gmail.com)

> wrote:

> Not sure if you looked at the source code of the function score weighted  
> boost plugin. The plugin uses query-time parameterization but it is of  
> course very easy to modify this and use settings at ES node startup time  
> for function score boosting, or a refresh mechanism that fetches parameters  
> from wherever you like.
> 
> Jörg
> 
> On Tue, Sep 2, 2014 at 8:09 PM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
> wrote:
> 
> > Thanks Vineeth. I stumbled upon that example too, thats one of the best  
> > resources I have found so far.
> > 
> > Thanks Joerg, this example looks very interesting to me. I see that you  
> > are sending in the extra parameters with the query, but i would like the  
> > plugin to read the data from external source and not during scoring time  
> > (in a seperate background thread periodically). I have data for every  
> > document but organized in a multilayered structure. Thats one of the reason  
> > I couldnt put it in the document index and retrieve it efficiently during  
> > scoring. Now I am planning to put it in a in-memory data structure which is  
> > available for the plugin to use. Do you have any thoughts/recommendation on  
> > that ?
> > 
> > Thanks  
> > Srini
> > 
> > On Tue, Sep 2, 2014 at 10:16 AM, [joergprante@gmail.com](mailto:joergprante@gmail.com) \<  
> > [joergprante@gmail.com](mailto:joergprante@gmail.com)\> wrote:
> > 
> > > You can write a function score plugin.
> > > 
> > > Here is an example:
> > > 
> > > [GitHub - jprante/elasticsearch-functionscore-conditionalboost: Boost documents in Elasticsearch when they match dynamic conditions](https://github.com/jprante/elasticsearch-functionscore-conditionalboost)
> > > 
> > > Jörg
> > > 
> > > On Tue, Sep 2, 2014 at 6:48 PM, Srinivasan Ramaswamy \<[ursvasan@gmail.com](mailto:ursvasan@gmail.com)
> > > 
> > > > wrote:
> > > 
> > > > I am trying to write a custom scoring plugin that will let me provide  
> > > > some extra data that is not in the index (through an external service) and  
> > > > do customized ranking. I would ideally like to custom score all the  
> > > > documents in the index that match the query.
> > > > 
> > > > Currently I am planning to implement it as a service by extending  
> > > > AbstractLifecycleComponent which will periodically pull that extra  
> > > > data from my external service. Is there a better way ? When and how the  
> > > > plugin services will be called. I would like to know in detail about the  
> > > > entry points of plugins and how i should write this plugin. Any help or  
> > > > pointers would be appreciated.
> > > > 
> > > > Thanks  
> > > > Srini
> > > > 
> > > > --  
> > > > 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).
> > > > 
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com)  
> > > > [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .  
> > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe](https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe).  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > 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).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe](https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVybOxcL68vfZ\_Lji8bqB20d%3DJYoV9D\_DDr4u\_qEYQMJ1A%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVybOxcL68vfZ_Lji8bqB20d%3DJYoV9D_DDr4u_qEYQMJ1A%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [September 5, 2014, 9:01am UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576/7 "2014-09-05T09:01:34Z")

</div>

You must handle exceptions very carefully in plugins. You should log errors  
to the log, skip/disable the plugin operation, and that's it.

Jörg

On Fri, Sep 5, 2014 at 3:14 AM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
wrote:

> Hi Joerg,
> 
> I tried the data loading part as a separate module and it works, but i  
> have a small issue with it. When there is an exception the constructor gets  
> called multiple times.
> 
> public class CustomDataModule extends AbstractModule {  
> private final Settings settings;
> 
> ```
> public SalesDataModule(Settings settings){
> this.settings = settings;
> }
> 
> @Override
> protected void configure() {
> bind(CustomData.class).asEagerSingleton();
> }
> 
> ```
> 
> }
> 
> public final class CustomData {  
> private static Logger logger = LoggerFactory.getLogger(CustomData.class);  
> @Inject  
> public CustomData(Settings settings) {  
> logger.info("CustomData constructor called");  
> WebResource webResource = getDataServiceResource(settings);  
> response = getSalesDataFromDataService(webResource);  
> loadCustomDataIntoMemory(response);  
> }  
> }
> 
> when the external service (which is the source of data) is down or there  
> is some other issue, the CustomData constructor gets called repeatedly  
> (approx 400 times). When there is no exception it gets called only once. I  
> am trying to understand why its called multiple times when there is an  
> exception.
> 
> I took a look at your code sample and I am trying to understand the  
> purpose of the Build class and who calls it ?
> 
> Can you also elaborate on your thoughts about reloading this customData  
> periodically. Currently I am thinking of using LoadingCache to store the  
> data and refresh it by using the refresh mechanism.
> 
> Thanks  
> Srini
> 
> On Tue, Sep 2, 2014 at 2:01 PM, [joergprante@gmail.com](mailto:joergprante@gmail.com) \<  
> [joergprante@gmail.com](mailto:joergprante@gmail.com)\> wrote:
> 
> > Not sure if you looked at the source code of the function score weighted  
> > boost plugin. The plugin uses query-time parameterization but it is of  
> > course very easy to modify this and use settings at ES node startup time  
> > for function score boosting, or a refresh mechanism that fetches parameters  
> > from wherever you like.
> > 
> > Jörg
> > 
> > On Tue, Sep 2, 2014 at 8:09 PM, Srinivasan Ramaswamy [ursvasan@gmail.com](mailto:ursvasan@gmail.com)  
> > wrote:
> > 
> > > Thanks Vineeth. I stumbled upon that example too, thats one of the best  
> > > resources I have found so far.
> > > 
> > > Thanks Joerg, this example looks very interesting to me. I see that you  
> > > are sending in the extra parameters with the query, but i would like the  
> > > plugin to read the data from external source and not during scoring time  
> > > (in a seperate background thread periodically). I have data for every  
> > > document but organized in a multilayered structure. Thats one of the reason  
> > > I couldnt put it in the document index and retrieve it efficiently during  
> > > scoring. Now I am planning to put it in a in-memory data structure which is  
> > > available for the plugin to use. Do you have any thoughts/recommendation on  
> > > that ?
> > > 
> > > Thanks  
> > > Srini
> > > 
> > > On Tue, Sep 2, 2014 at 10:16 AM, [joergprante@gmail.com](mailto:joergprante@gmail.com) \<  
> > > [joergprante@gmail.com](mailto:joergprante@gmail.com)\> wrote:
> > > 
> > > > You can write a function score plugin.
> > > > 
> > > > Here is an example:
> > > > 
> > > > [GitHub - jprante/elasticsearch-functionscore-conditionalboost: Boost documents in Elasticsearch when they match dynamic conditions](https://github.com/jprante/elasticsearch-functionscore-conditionalboost)
> > > > 
> > > > Jörg
> > > > 
> > > > On Tue, Sep 2, 2014 at 6:48 PM, Srinivasan Ramaswamy \<  
> > > > [ursvasan@gmail.com](mailto:ursvasan@gmail.com)\> wrote:
> > > > 
> > > > > I am trying to write a custom scoring plugin that will let me provide  
> > > > > some extra data that is not in the index (through an external service) and  
> > > > > do customized ranking. I would ideally like to custom score all the  
> > > > > documents in the index that match the query.
> > > > > 
> > > > > Currently I am planning to implement it as a service by extending  
> > > > > AbstractLifecycleComponent which will periodically pull that extra  
> > > > > data from my external service. Is there a better way ? When and how the  
> > > > > plugin services will be called. I would like to know in detail about the  
> > > > > entry points of plugins and how i should write this plugin. Any help or  
> > > > > pointers would be appreciated.
> > > > > 
> > > > > Thanks  
> > > > > Srini
> > > > > 
> > > > > --  
> > > > > 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).
> > > > > 
> > > > > To view this discussion on the web visit  
> > > > > [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com)  
> > > > > [https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/dd21c829-867c-47f4-99f0-29465ae1b33d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > .  
> > > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to a topic in the  
> > > > Google Groups "elasticsearch" group.  
> > > > To unsubscribe from this topic, visit  
> > > > [https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe](https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe)  
> > > > .  
> > > > To unsubscribe from this group and all its topics, send an email to  
> > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com)  
> > > > [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEJMueVnRAquoiCMEHBBWGTS1D091v0ArmWK4f6GE2aCA%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > > > .
> > > > 
> > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > 
> > > --  
> > > 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).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVw2ZReG-aMUDKzDZrPzHfb6hHMqJOJ4VS3qSsH6DfGFTw%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe](https://groups.google.com/d/topic/elasticsearch/qJn1yUQToP0/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoGmo5MXGVH%2B-NQSEMqzNN0A-GLz7C67C%2BhZNSRHQSFxew%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVybOxcL68vfZ\_Lji8bqB20d%3DJYoV9D\_DDr4u\_qEYQMJ1A%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVybOxcL68vfZ_Lji8bqB20d%3DJYoV9D_DDr4u_qEYQMJ1A%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAL1MvVybOxcL68vfZ\_Lji8bqB20d%3DJYoV9D\_DDr4u\_qEYQMJ1A%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL1MvVybOxcL68vfZ_Lji8bqB20d%3DJYoV9D_DDr4u_qEYQMJ1A%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEmH9wA-tKUoR-2sibNzGbGOhDPchA5gdO1EKygi\_WjfA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEmH9wA-tKUoR-2sibNzGbGOhDPchA5gdO1EKygi_WjfA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![moises\_acuna](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@moises\_acuna](https://discuss.elastic.co/u/moises_acuna)\
**Post date:** [May 12, 2017, 4:08pm UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576/8 "2017-05-12T16:08:55Z")

</div>

Hi, i know it's been a few years since the last reply. I'm building something similar as this, a custom scoring function that relies on external data ( doc\_id , score map) coming from a rest endpoint. I've setup the function score and the service but i found now way for the function score code to access the data structre from the service module.  
Thanks for any help.

---

<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:00pm UTC](https://discuss.elastic.co/t/writing-a-custom-scoring-plugin/19576/9 "2017-07-05T22:00:25Z")

</div>


