Direct Index Query - andrew-nguyen/titan GitHub Wiki
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:
for (Result<Vertex> result : graph.indexQuery("search","v.text:(farm uncle berry)").vertices()) {
System.out.println(result.getElement() + ": " + result.getScore());
}
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 and Lucence support the Lucene query syntax. Refer to the Lucene documentation or the Elasticsearch documentation for more information. The query used in the example above follows the Lucene query syntax.
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.
When you have property keys that contain non-alphabetic characters, you have to escape the entire property key name with quotation marks, as in:
graph.indexQuery("search","v.\"first_name\":john").vertices()