Home
Categories
Dictionnary
Download
Project Details
Changes Log
FAQ
License

Searching


    1  Query configuration
       1.1  Query-time boosting
       1.2  Boolean model
       1.3  Token expandation
    2  See Also



Searching is simple:
      List<JSONObject> results = index.search("Oracle database profit");

The result array is sorted in descencing order, sorted by the score. Each result is a JSONObject with the following fields: For example, for the following index:

      List<JSONObject> results = index.search("Oracle database profit");

      JSONObject result = results.get(0);
      float score = result.getFloat("score");
      String ref = result.getString("ref");

ref will be equal to "1", so will refer to the document with the id "1".

Query configuration

It is possible to configure the query by using a JSON configuration rather than just the text to search.

Query-time boosting

uery-time boosting sets which fields to search in by passing in a JSON configuration, and setup boosting for each search field. If you setup this configuration, then the library will only search the query string in the specified fields with boosting weight.

The scoring mechanism used in elasticlunr is very complex, please goto Details for more information.

For example here the "title" field has more weight in the search than the "body" field:

      index.search("Oracle database profit", "{fields: {title: {boost: 2}, body: {boost: 1}}}");

Boolean model

By default, the library search for any of the search terms. But it is possible to ask for all the search terms to be present in a document.

For example, in the following search, the two search terms must be present in the same document:

      index.search("database profit", "{bool: AND}"):

The boolean model can be specified separately for each field. For example:

      index.search("Oracle database profit", "{fields: {title: {bool: OR}, body: {bool: AND}}}");

Token expandation

By default the library will try to find the complete words in the search (taking into account the pipeline, including stemming). This means that for example if you add the following document:

      JSONObject doc1 = new JSONObject();
      doc1.put("id", 1);
      doc1.put("title", "Oracle released its latest database Oracle 12g");
      doc1.put("body", "Yestaday Oracle has released its new database Oracle 12g, this would make more money for this company");

      index.addDoc(doc1);

By default you will not have any result if you perform the following search:

      index.search("Ora");

However it is possible to allow to expand the search terms. For example, the following search will return one result with the example document:

      index.search("Ora", "{expand: true}");

Note that the query results from expanded tokens are penalized because they are not exactly the same as the query token.

Token expandation can be configured per field. For example:

      index.search("Oracle database profit", "{fields: {title: {expand: false}, body: {expand: true}}}");

See Also


Categories: search

Copyright 2017 Wei Song. Copyright 2018 Herve Girod. All Rights Reserved. Documentation and source under the MIT licence