Lucene4 Plug-in

Skip to end of metadata
Go to start of metadata
This documentation is NOT for the latest version of GraphDB.

Latest version - GraphDB 6.6

OWLIM Documentation

Next versions

[OWLIM 5.6]
GraphDB 6.0 & 6.1
GraphDB 6.2
GraphDB 6.3
GraphDB 6.4
GraphDB 6.5

Previous versions



The Lucene4 plug-in for OWLIM provides extremely fast facet (aggregation) searches which are normally available through external Apache Solr services, but have the additional benefit to stay automatically up-to-date with the OWLIM repository data.


  • maintain a Lucene index that is always in sync with the data
  • multiple indexes per repository with independent settings. Index options supported:
    • whether to strip *ML tags in literals or not (default is to not strip markup)
    • whether the index is auto-updated or not (default is to keep the index up-to-date)
    • specifying Lucene analyzer to use (the default is Lucene's StandardAnalyzer)
    • a white list of predicates whose values to index
    • a white list rdf:type-s of entities to index (default is all)
    • a white list of languages to index (default is all)
    • a white list of predicates whose values to add to the facets index
  • full Lucene syntax for search
  • simple molecules - only literals reachable through zero hops (i.e. a single predicate of an entity)
  • snippet extraction - retrieve snippet with highlighted search terms from the search result
  • search flags
    • results paging through offset and limit parameters
    • specifying snippet size per query
    • specifying facets to aggregate

Differences with Lucene2


Lucene2 indexes each relevant statement (triple) as a single Lucene document. This feature is really beneficial for maintaining an index up-to-date, but leads to a few undesirable effects such as returning a single entity more than once for a given search (because more than one literals matched the query) and inability to sort by specific predicates. The Lucene4 plugin creates a single Lucene document per entire entity which has a field for each of the predicates listed in the index configuration. This leads to slower update times, but solves the two main problems posed above. These decisions have some other minor implications described below. Another significant feature of Lucene4 is the facets support (which is built in Apache Lucene 4.x.x).

Index options


The predicates index option is now mandatory and must not be empty. Indexing all predicates for a given entity is not supported.

Additional joins

The additionalJoins index options from Lucene2 have been removed in Lucene4. It can be emulated though. Consider the following Lucene2 index creation:

In order to do this in Lucene4, create the same index, but add urn:join to predicates instead of additionalJoins

The filtering itself is done through searching. Since Lucene4 creates Lucene document field for each predicate and the field name is exactly the predicate URI, we can utilize that through the Lucene query syntax

To dissect the

part (it's all part of the normal Lucene query syntax):

  • + means the following query is mandatory
  • is a query in the form field:value. However, since the field name contains :, we need to escape it with backslash. The backslash, however is also a escaping character in SPARQL and so we need to escape it again so that the plugin will see a single backslash. Furthermore, forward slashes are also special Lucene syntax and need to be escaped too. That means if the join predicate is _ the proper query would be "+(query) +http\\:\/\/\/slash:joinValue"
  • also note that whatever the original FTS query, it's best for it to be put in brackets, with plus in front. If you ask for "query +urn
    :join:joinValue" then Lucene will return all entities that match either query OR urn
    which is not the intent.
Optional joins

The optionalJoins parameter is still there in Lucene4 as in Lucene2: entities that have the specified predicate values OR are missing the predicates completely are indexed, the rest of the entities are not. The optional joins predicate values are indexed as other predicates, in a Lucene field per predicate, which allows to search for specific values as described above in the Additional joins section.

For example, if we create an index like this:

and then insert some entities:

Querying on urn:join is possible as in the additional joins example above:

Entities that don't have the optional join predicate get a default value indexed in that field name in order to be able to filter for those too. The default default value is OPTIONALJOINDEFAULT and can be set by using the optionalJoinDefaults index option. For example, with the above entities, searching for "words... +urn
:join:OPTIONALJOINDEFAULT" will return only <entity:will-be-indexed-too>.

We could create the index like this to specify different default value for <urn:join>:

Optional join predicate values are indexed, but not tokenized - that means queries for them should match exactly.

Search options

  • return.entities parameter is removed. Lucene4 always acts as Lucene2 with return.entities=dedup
  • parameter added to allow specifying predicates to sort on

User's Guide

Creating an index

To create an index, issue the following SPARQL update query

where <index-options> can be a combination of the following options, separated by a semicolon ';':

  • stripMarkup=true|false specifying whether to strip tags from HTML/XML literals (default is false)
  • autoUpdate=true|false specifying whether to keep this index automatically up-to-date (default is true)
  • enableSnippets=true|false specifying whether to enable snippets in this index. As of 2013-12-15, this is a dummy flag and snippets are always enabled. You should generally pass a meaningful value here in case we optimize our implementation later.
  • analyzer=<analyzer-class-name> Lucene analyzer to use when indexing literals in this index. Threre are two possibilities here:
    • specifying Lucene analyzer class name directly - in that case the analyzer should either have a default constructor or a constructor accepting a single org.apache.lucene.util.Version parameter. If you specify an analyzer that doesn't have one of those constructors the index won't be created
    • specifying a class derived from com.ontotext.trree.plugin.lucene4.AnalyzerFactory
  • predicates=<comma-separated-list-of-URIs> - only triples with those predicates will be indexed
  • languages=<comma-separated-languages> if specified, only literals tagged with the listed languages will be indexed
  • types=<comma-separated-list-of-URIs> if specified, a white list of types to index (i.e. will only index entities that have rdf:type equal to one of the specified URIs)
  • facets=<comma-separated-list-of-URIs> if specified, the listed predicates and their values will be indexed in the facets index
  • optionalJoins=<|-separated-predicate-object-pairs> if specified, a white list of additional optional joins to validate. Supports both URIs and Literals as objects. Literals can include spaces. Sample syntax: optionallJoins=urn:ontology:predicate,longer value|urn:ontology:predicate,another longer value. An entity is only indexed iff for each specified predicate it either has the predicate with one of the specified value or doesn't have the predicate at all. If an entity is indexed, a field for each optional join predicate is created with all values and in the case where the entity doesn't have the predicate - a default value for the optional join predicate specified with optionalJoinDefaults. The value is indexed, but not tokenized, so any searches within an optional joins field should match exactly
  • optionalJoinDefaults=<|-separated-predicate-object-pairs> if specified, provide different default value to index in the field of entities that don't have the optional join predicate at all. The default for all predicates is "OPTIONALJOINDEFAULT"
  • sortPredicates=<comma-separated-list-of-URIs> - predicate values that will be used for sorting at search time - only predicates specified here can be passed to _order.by_.


Drop an index


List all indices


Query indices health

Indices might become corrupt due to disk failure or other issues. The luc4:healthCheck predicate returns a list of indices along with their health status. In the example below, ?uri will bind to


  1. Simple - in the form
    Returns all matching entities' ids as the ?entity binding.
  2. Advanced - in the form
    Options is ;-separated list of option=value pairs. What will be bound as ?result depends on whether the options specify facet query or not. A list of all supported options:
    Parameter Value
    Returns the results starting from the specified value
    Lucene query limit - maximum number of results to return
    250 The size of the returned snippets
    none Hit highlights are represented with <span>. This optional parameter provides the class, i.e. <span class="xxx">
    Comma-separated list of facet predicates whose values to aggregate. All predicates specified here should also have been specified in the index options facets. Passing non-empty list of facet predicates changes the way results are returned, see facet examples below
    The number of top values to return per facet predicate. For example, specifying facets=urn:facet1,urn:facet2;facets.limit=5 will return ten facet results in total
    Comma-separated list of indexed predicates. If specified the results will be ordered the value of the specified predicates, in the specified order. Values are sorted in an ascending order, to sort certain predicate's values in descending order, prepend - (minus) to it. For example,-urn:predicate2 will sort the results by the value of urn:predicate1 ascending, then those results that have the same value in urn:predicate1 will be sorted by the value of urn:predicate2, descending. The special value score (lower-case) can be used instead of a predicate to order based on the normal Lucene score (descending by default). For example ",score" will sort on urn:predicate first (ascending) and then on score, while ",urn:predicate" will sort on score first and on urn:predicate second. Passing "-score" to reverse the order (from lowest to highest score) is also allowed. NOTE: Predicates on this list should be specified as sortPredicates at index creation. Ordering by predicate not specified in sortPredicates will lead to unpredictable order
  3. Faceted search results - for searches where facets search option is specified, the search predicate (e.g. luc4:indexName) binds a single dummy blank node to its object. You must then query for luc4:entities to get entities that matched the query and luc4:facets to get faceted results. Example, demonstrating this and related special predicates for extra result details:

    Note the use of UNION to avoid Cartesian product of the set of document and facet results, this is the recommended way to retrieve both documents and facets results.

Additional predicates allowed on bound search results

Additional predicates allowed on bound facets results


  • Make a simple query, return all results, ?entity will will to the resource URI
  • Return the top 10 resources, snippets are of size 20, ?entity will bind to the resource URI
  • Return the top 10 resources, starting from offset 10 (i.e. page size is 10, return second page), documents will be sorted by the value of rdfs:label instead of Lucene score

Real-world examples

Maybe to get started more quickly, these examples will help you understand how to create an index and then execute searches on it.

The following query creates an index on all entities that have an rdfs:labels with @en or no language tag, using an EnglishAnalyzer with snippets enabled.

Now that the index is created, you can run the following query to obtain the top 20 entries that start with "a" and snippet where the literal occurs, including the Lucene score.

Facets example

Consider, the following RDF data (in turtle format)

Now create an index, using rdf:type and test:facet as facet predicates, indexing rdfs:comment and having no type restriction:

Let's gather some results now:

The result bindings will look like in the table below, empty cell means this value is unbound:

score entity facetValue facetCount facetPredicate
1.0 urn:a      
1.0 urn:b      
1.0 urn:c      
1.0 urn:d      
    facet-value-1 2.0 test:facet
    facet-value-2 1.0 test:facet
    urn:Type1 3.0
    urn:Type2 1.0

Dependencies & Deployment Details


  • OWLIM Plug-in API (releases after build 5.4.6686)
  • Lucene 4.5.1
  • Apache commons-io 2.4


How to use UNION with Lucene4?

A: Just like in a normal query. Consider the following example:

The above query joins the union part (with bindings for ?c and ?s) with the lucene part on ?s. Provided that the lucene index contains things of the right classes, i.e. things of type Type1 AND Type2. There are a few noteworthy details though:

  1. The Lucene query limit applies before the SPARQL query limit.
  2. You can safely use DISTINCT in order to clean up duplicate results.

If I union up two lucene4 queries (on different lucene4 indices), will the snippets and scoring still work?

A: The short answer is no, because Lucene scores are generated per query, so basically one cannot execute 2 different Lucene query and expect adequate scoring when joining the results. Consider the following example:

While the query above is valid, it is not sane because of the reasons mentioned earlier. The results will be incorrect since different scoring is used for the two queries. Instead of using UNION, you should create a single index for Type1, Type2 and Predicate1 and execute just one query.

Sometimes the snippets are less than the number of snippet chars requested, why?

A: It is the way Lucene's FastVectorHighlighter generates snippets. For example if the "match" is on an indexed property that is relatively short (such as a report title), then the snippet tends to be less than the entire title even though the title length is less than the requested snippet length. Lucene tends to cut the snippet off chars before the first term match. The getBestFragment method's javadoc is not very helpful in explaining why.

The drop index SPARQL update request throws an error if the index does not exist. Is there any way to ask if the index exists and if so - drop it?

This is where the luc4:list comes in handy. If you have an index named "myIndex", then you can execute the following SPARQL update and get response code 200, even when the index does not exist:

How to search for terms/phrases containing stop words?

First of all, let's get the terminology right - a term represents a word of text. What you are actually trying to search for is a phrase. In addition, the Lucene4 plug-in supports the Lucene query syntax. This means that if you search for phrases such as "City of Manchester" like this (mind the quotes):

There is a caveat, in order to be able to use " in a literal, you need to use the additional quoting construct -

You will get appropriate results. Indeed, the Analyzers are filtering stop words, but this isn't an issue, since we are using the same analyzer for index and search - the one that was specified during index creation time. More info on how this actually works could be found here in the "Token Position Increments" section.

Are prefix wildcards supported? Can I use both prefix and suffix wildcards on the same term?

Yes, you can. Go for it!

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.