0

Per the latest Titan 0.4.1 docs, this code should work to order vertices in a result set:

graph.query().has("name",CONTAINS,"john").orderBy("age",Order.DESC).limit(10).vertices()

I would like to perform this type of query across a potentially large set of vertices, and thus would like to index it. The docs suggest:

Most external indexing backends support ordering natively and efficiently. However, the property key used in the orderBy method must be configured to be indexed in this indexing backend for native result ordering support. This is important in cases where the orderBy key is different from the query keys. If the property key is not indexed, then sorting requires loading all results into memory.

Specifically for the Elasticsearch backend, how can we create an index that will support this orderBy method?

Is there a simple 3rd parameter to pass to the KeyMaker's indexed method, possibly an extension to the below example from the docs?

graph.makeKey("age").dataType(Integer.class).indexed("search", Vertex.class).make();
bcm360
  • 1,437
  • 2
  • 17
  • 25

1 Answers1

2

You just need to index the property in ES (like any other property).

Indexed in Titan (wrong; orderBy will not be optimized):

graph.makeKey("age").dataType(Integer.class).indexed(Vertex.class).make();

Indexed in ES (correct; native result ordering in ES):

graph.makeKey("age").dataType(Integer.class).indexed("search", Vertex.class).make();

Cheeers, Daniel

Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • Thanks Daniel for your quick reply. So, I've tried running the following query after creating the ES index on `age` (using your second example above): `g.query().orderBy("age", Order.DESC).vertices()`. That results in the warning: `...Query requires iterating over all vertices...`. Should I be using a different query syntax? Or is there another step required to have titan use ES for the sort? – bcm360 Dec 21 '13 at 00:27
  • Oh, you really want to query ALL vertices? Depending on how large your graph is, this will be a super expensive operation. The first query (`graph.query().has("name",CONTAINS,"john").orderBy("age",Order.DESC).limit(10).vertices()`) should work just fine with the given type definition (but the filter condition is important, otherwise you wouldn't need ES to sort your result). – Daniel Kuppitz Dec 21 '13 at 00:42
  • To add: make sure both "age" and "name" are indexed in ES and that you have some sort of restrictive condition - otherwise you are going over the entire graph since sort orders aren't pre-computed. – Matthias Broecheler Dec 21 '13 at 07:49