📜 ⬆️ ⬇️

Grails full text search

Connecting full-text search in Grails is a fairly easy task. This is done using the Searchable plugin, which makes all Grails application entities indexable. Searchable allows you to abstract the whole process of indexing and searching. At the same time, the plugin itself uses the Compass library, which ensures that when an object changes (that is, when saved in the database), it is automatically reindexed. Compass itself is essentially a fairly powerful tool for “search ORM”:
  1. Compass at the lowest level uses Lucene , but with a special implementation of the index.
  2. Compass associates a Lucene document to each domain object. The object identifier thus becomes the document identifier.
  3. Fields of the object are converted into document fields.
  4. The search results are also objects. Their Compass creates (deserializes) based on data from the index.
  5. Compass can handle nested objects (this is called the searchable component) and object references (searchable reference). Nested objects will be saved inside the main document.
  6. Compass is able transactions.
Working with objects is immeasurably more convenient; in order to declare the Grails domain-class indexed, you need to add to it a configuration closure of the following type:

class Post { static searchable = { category index: 'not_analyzed', excludeFromAll: true title boost: 2.0 comments component: true } static hasMany = [comments: Comment] User author String title, post, category Date createdAt } 

Now all newly created Post instances will be indexed.

In general, nothing else needs to be done. You can already write search queries like:
 Post.search("   ") 
and the result will be the essence of your application! You can use Lucene query language.
')
In fact, I did not want to retell Searchable documentation now. There are some problems in it. They stem from the fact that Compass is a technically complex and universal product that can be used by both Lucene and other search engines. Therefore, to support all this code further, its author has no desire. And its author, Shay Banon, decided to do something less versatile, but more cool - the ElasticSearch search engine.

ElasticSearch:
  1. Based on Lucene 3
  2. Originally designed as highly available and scalable.
  3. Contains caching mechanisms (by the way, unlike Solr ).
Conceptually, ElasticSearch is quite different from Compass. In fact, ElasticSearch is an indexed repository of JSON objects. This storage can distinguish between different types of objects, handle nested objects, etc. Well, the most important thing - is able to search. The query language is also JSON and provides almost the same set of functions as the “naked” Lucene.

Compass is an embedded solution only. As for ElasticSearch, it can exist both as embedded (inside the JVM) and as an HTTP daemon. The ElasticSearch daemon can be accessed by any program that knows JSON, be it PHP code, Java, Ruby, or .NET.

For those who want to thoroughly understand what kind of software is involved here, I tried to depict this in the diagram:



Here you can see already three levels. The left shows the current stack based on Compass. Right - the stack is based on ElasticSearch. However, the distribution of functions for Grails plug-ins is not exactly the same (for example, Compass can convert Java objects into Lucene documents, but ElasticSearch does not).

What shines now to those unfortunate people who use Searchable (and these were us as well)?Now there is a transition period when:
However, by itself, such things are not done. What can I offer to interested? Now in the depths of the Internet began to develop a new Grails-plugin for ElasticSearch. The objectives for the development of the plugin are as follows:
The plugin is already successfully working in real applications. But more users are needed and feedback is needed! We need people who will actively use the plugin and point out problems that we may not see now.

Where can I get the plugin?My branch, of course, is better :-) - there is more functionality and it is constantly being run through our Grails applications in order to solve all the problems. Periodically, my changes are added to the main branch, but this process comes late.

Install the code from Github as follows:
  1. We assume that you already have a working Grails-application.
  2. If you don't want to mess around with git and build, you can install an already packaged plugin:
     grails install-plugin https://github.com/downloads/spn/elasticsearch-grails-plugin/grails-elasticsearch-0.14.2.2.zip 
  3. If you want to assemble yourself, it becomes a little more complicated:
     wget https://github.com/spn/elasticsearch-grails-plugin/tarball/master tar xvzf spn-elasticsearch-grails-plugin-3fde6c4.tar.gz cd spn-elasticsearch-grails-plugin-3fde6c4 grails package-plugin 
    - we will get the same packaged plugin (the file name from the Downloads link in github)
In general, I urge to try. For information - the plugin itself can work as a text Grails application, just type
 grails run-app 
in the folder with the plugin.

Source: https://habr.com/ru/post/114363/


All Articles