Whenever you have a large set of documents, and you want your user to be able to search on a subset, you can use the Term Filter. However, sometimes you also want to have a sort order dependant on your user, without having to duplicate your documents. This plugin enables you to define a mapping of document ids to scores, so that only documents present in the mapping are returned, and they are returned in the order given by the scores. This kind of query also enables you to add your own search criterias to furthermore restrict the number of documents returned.
-
Build this plugin:
mvn clean compile test package PLUGIN_PATH=`pwd`/target/releases/sort-by-doc-0.0.1-SNAPSHOT.zip
-
Install the PLUGIN
cd $ELASTICSEARCH_HOME ./bin/plugin -url file:/$PLUGIN_PATH -install sort-by-doc
-
Updating the plugin
cd $ELASTICSEARCH_HOME ./bin/plugin -remove sort-by-doc ./bin/plugin -url file:/$PLUGIN_PATH -install sort-by-doc
ElasticSearch version 1.7.1
The document must contain a list of objects. These objects must contains two fields: an id and a score.
{
"_id": "sort_doc_for_user_1",
"sort_object": [
{ "id": "doc_id_1", "score": 1 },
{ "id": "doc_id_2", "score": 2 },
...
]
"other_fields": ...
}
- query - A subquery that will be filtered and scored
- index - The index name where to find the sort document
- type - The type where to find the sort document
- doc_id - The id of the sort document
- root - The path to the list of objets
- id - the field name of document ids in the objects
- score - the field name of score values in the objects
See test.sh