Revision 380e94bbc6097ebb62adbdf1a3d590965c25e184 authored by Dan LaRocque on 21 July 2014, 06:20:05 UTC, committed by Dan LaRocque on 21 July 2014, 06:20:05 UTC
1 parent 8f81954
Raw File
directindex.txt
[[direct-index-query]]
Direct Index Query
~~~~~~~~~~~~~~~~~~

Titan's `Graph.query()` querying mechanism supports boolean queries for vertices or edges. In other words, an element either matches the query or it does not.

Some indexing backends additionally support fuzzy search queries. For those queries, a score is computed for each match to indicate the "goodness" of the match and results are returned in the order of their score. Fuzzy search is particularly useful when dealing with full-text search queries where matching more words is considered to be better.

Since fuzzy search implementations and scoring algorithms differ significantly between indexing backends, Titan does not support fuzzy search natively. However, Titan provides a _direct index query_ mechanism that allows search queries to be directly send to the indexing backend for evaluation (for those backends that support it).

Use `Graph.indexQuery()` to compose a query that is executed directly against an indexing backend. This query constructor expects two parameters:

. The name of the indexing backend to query. This must be the name configured in Titan's configuration and used in the property key indexing definitions
. The query string

The constructor allows to configure the maximal number of elements to be returned via its `limit(int)` method. To retrieve all vertex or edges matching the given query in the specified indexing backend, invoke `vertices()` or `edges()`, respectively. It is not possible to query for both vertices and edges at the same time.
These methods return an `Iterable` over `Result` objects. A result object contains the matched handle, retrievable via `getElement()`, and the associated score - `getScore()`.

Consider the following example:

[source,java]
for (Result<Vertex> result : graph.indexQuery("search","v.text:(farm uncle berry)").vertices()) {
   System.out.println(result.getElement() + ": " + result.getScore());
}

Query String
^^^^^^^^^^^^

The query string is handed directly to the indexing backend for processing and hence the query string syntax depends on what is supported by the indexing backend. For vertex queries, Titan will analyze the query string for property key references starting with "v." and replace those by a handle to the indexing field that corresponds to the property key. Likewise, for edge queries, Titan will replace property key references starting with "e.".
Hence, to refer to a property of a vertex, use "v.[KEY_NAME]" in the query string. Likewise, for edges write "e.[KEY_NAME]".

<<elasticsearch,Elasticsearch>> and <<lucene,Lucene>> support the http://lucene.apache.org/core/4_1_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html[Lucene query syntax]. Refer to the http://lucene.apache.org/core/4_1_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html[Lucene documentation] or the http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html[Elasticsearch documentation] for more information. The query used in the example above follows the Lucene query syntax.

[source,java]
graph.indexQuery("search","v.text:(farm uncle berry)").vertices()

This query matches all vertices where the text contains any of the three words (grouped by parentheses) and score matches higher the more words are matched in the text.

Gotchas
^^^^^^^

When you have property keys that contain non-alphabetic characters, you have to escape the entire property key name with quotation marks, as in:

[source,java]
graph.indexQuery("search","v.\"first_name\":john").vertices()
back to top