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:
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.
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
.
orientdb> CREATE INDEX City.name_description ON City(name, description)
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
.
orientdb> CREATE INDEX City.name ON City(name) FULLTEXT ENGINE LUCENE METADATA
{"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.
orientdb> CREATE INDEX City.name ON City(name) FULLTEXT ENGINE LUCENE METADATA
{
"index_analyzer": "org.apache.lucene.analysis.en.EnglishAnalyzer",
"query_analyzer": "org.apache.lucene.analysis.standard.StandardAnalyzer"
}
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
orientdb> CREATE INDEX City.name_description ON City(name, description) FULLTEXT ENGINE LUCENE METADATA
{
"index_analyzer": "org.apache.lucene.analysis.en.EnglishAnalyzer",
"query_analyzer": "org.apache.lucene.analysis.standard.StandardAnalyzer",
"name_index_analyzer": "org.apache.lucene.analysis.standard.StandardAnalyzer",
"name_query_analyzer": "org.apache.lucene.analysis.core.KeywordAnalyzer"
}
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.
OSchema schema = databaseDocumentTx.getMetadata().getSchema();
OClass oClass = schema.createClass("Foo");
oClass.createProperty("name", OType.STRING);
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.
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,
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.
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:
CREATE INDEX <name> FULLTEXT ENGINE LUCENE [<key-type>] [METADATA {<metadata>}]
For example, create a manual index using the CREATE INDEX
command:
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.
orientdb> INSERT INTO INDEX:Manual (key, rid) VALUES(['Enrico', 'Rome'], #5:0)
You can then query the index through SELECT...FROM INDEX:
:
orientdb> SELECT FROM INDEX:Manual WHERE key LUCENE "Enrico"