Lucene FullText Index

In addition to the standard FullText Index, which uses the SB-Tree index algorithm, you can also create FullText indexes using the Lucene Engine. Beginning from version 2.0, this plugin is packaged with OrientDB distribution.

Syntax:

  1. CREATE INDEX <name> ON <class-name> (prop-names) FULLTEXT ENGINE LUCENE

The following SQL statement will create a FullText index on the property name for the class City, using the Lucene Engine.

  1. orientdb> CREATE INDEX City.name ON City(name) FULLTEXT ENGINE LUCENE

Indexes can also be created on n-properties. For example, create an index on the properties name and description on the class City.

  1. orientdb> CREATE INDEX City.name_description ON City(name, description)
  2. FULLTEXT ENGINE LUCENE

Analyzer

This creates a basic FullText Index with the Lucene Engine on the specified properties. In the even that you do not specify the analyzer, OrientDB defaults to StandardAnalyzer.

In addition to the StandardAnalyzer, you can also create indexes that use different analyzer, using the METADATA operator through CREATE INDEX.

  1. orientdb> CREATE INDEX City.name ON City(name) FULLTEXT ENGINE LUCENE METADATA
  2. {"analyzer": "org.apache.lucene.analysis.en.EnglishAnalyzer"}

(from 2.2)

Starting from 2.2 it is possible to configure different analyzers for indexing and querying.

  1. orientdb> CREATE INDEX City.name ON City(name) FULLTEXT ENGINE LUCENE METADATA
  2. {
  3. "index_analyzer": "org.apache.lucene.analysis.en.EnglishAnalyzer",
  4. "query_analyzer": "org.apache.lucene.analysis.standard.StandardAnalyzer"
  5. }

EnglishAnalyzer will be used to analyze text while indexing and the StandardAnalyzer will be used to analyze query terms.

It is posssbile to configure analyzers at field level

  1. orientdb> CREATE INDEX City.name_description ON City(name, description) FULLTEXT ENGINE LUCENE METADATA
  2. {
  3. "index_analyzer": "org.apache.lucene.analysis.en.EnglishAnalyzer",
  4. "query_analyzer": "org.apache.lucene.analysis.standard.StandardAnalyzer",
  5. "name_index_analyzer": "org.apache.lucene.analysis.standard.StandardAnalyzer",
  6. "name_query_analyzer": "org.apache.lucene.analysis.core.KeywordAnalyzer"
  7. }

With this configuration name will be indexed with StandardAnalyzer and query will be analyzed with the KeywordAnalyzer: description hasn’t a custom configuration, so default analyzers for indexing an querying will be used.

You can also use the FullText Index with the Lucene Engine through the Java API.

  1. OSchema schema = databaseDocumentTx.getMetadata().getSchema();
  2. OClass oClass = schema.createClass("Foo");
  3. oClass.createProperty("name", OType.STRING);
  4. oClass.createIndex("City.name", "FULLTEXT", null, null, "LUCENE", new String[] { "name"});

Querying Lucene FullText Indexes

You can query the Lucene FullText Index using the custom operator LUCENE with the [Query Parser Synta]x(http://lucene.apache.org/core/5_4_1/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#package_description) from the Lucene Engine.

  1. orientdb> SELECT FROM V WHERE name LUCENE "test*"

This query searches for test, tests, tester, and so on from the property name of the class V.

Working with Multiple Fields

In addition to the standard Lucene query above, you can also query multiple fields. For example,

  1. orientdb> SELECT FROM Class WHERE [prop1, prop2] LUCENE "query"

In this case, if the word query is a plain string, the engine parses the query using MultiFieldQueryParser on each indexed field.

To execute a more complex query on each field, surround your query with parentheses, which causes the query to address specific fields.

  1. orientdb> SELECT FROM CLass WHERE [prop1, prop2] LUCENE "(prop1:foo AND prop2:bar)"

Here, hte engine parses the query using the QueryParser

Creating a Manual Lucene Index

Beginning with version 2.1, the Lucene Engine supports index creation without the need for a class.

Syntax:

  1. CREATE INDEX <name> FULLTEXT ENGINE LUCENE [<key-type>] [METADATA {<metadata>}]

For example, create a manual index using the CREATE INDEX command:

  1. orientdb> CREATE INDEX Manual FULLTEXT ENGINE LUCENE STRING, STRING

Once you have created the index Manual, you can insert values in index using the INSERT INTO INDEX:... command.

  1. orientdb> INSERT INTO INDEX:Manual (key, rid) VALUES(['Enrico', 'Rome'], #5:0)

You can then query the index through SELECT...FROM INDEX::

  1. orientdb> SELECT FROM INDEX:Manual WHERE key LUCENE "Enrico"