Skip to content

Sort elasticsearch documents given an order stored in another document

License

Notifications You must be signed in to change notification settings

datapublica/sort_by_doc

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sort Results using another Document

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.

  1. Build this plugin:

     mvn clean compile test package 
     PLUGIN_PATH=`pwd`/target/releases/sort-by-doc-0.0.22-SNAPSHOT.zip
    
  2. Install the PLUGIN

     cd $ELASTICSEARCH_HOME
     ./bin/plugin -url file:/$PLUGIN_PATH -install sort-by-doc
    
  3. Updating the plugin

     cd $ELASTICSEARCH_HOME
     ./bin/plugin -remove sort-by-doc
     ./bin/plugin -url file:/$PLUGIN_PATH -install sort-by-doc
    

Usage

Recommended Versions

ElasticSearch version 7.x: 0.0.23

ElasticSearch version 6.x: 0.0.22

ElasticSearch version 5.x: 0.0.21

ElasticSearch version 2.x: 0.0.14

ElasticSearch version 1.x: 0.0.8

Expected format of sort document

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 Parameters
  • 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
Example

See test.sh

About

Sort elasticsearch documents given an order stored in another document

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 93.6%
  • Shell 6.4%