Search using two search indexes for a single traversal query

Using two search indexes in one traversal

Create a second search index like an example search index from Creating indexes for vertex label person.

schema.vertexLabel('person').index('search').search().
            by('name').asString().
            by('nick_name').ifNotExists().add()

This search index will use DSE Search to index nickname as full text using tokenization, and name as a string.

This traversal query demonstrates a mid-traversal V() that allows a search index for person as well as a search index for recipe to be used to execute the query. The first index uses a tokenRegex() to find recipe instructions that start with the word Braise; this part of the query is labeled as r for use later in the query. Then the search index for person is searched for an person name that starts with the letter J, and traversed through an outgoing edge to a vertex where the search found in the first part of the query is found with where(eq('r')).

g.V().has('recipe', 'instructions', tokenRegex('Braise.*')).as('r').
  V().has('person', 'name', prefix('J')).out().where(eq('r')).values('name')

results in:

==>Beef Bourguignon
==>Beef Bourguignon
==>Beef Bourguignon
==>Beef Bourguignon

This query traversal finds the recipe Beef Bourguignon four times, and illustrates some of the complexity that can be successfully used with search indexes. A modified query that gets the path from recipe ->person->recipe finds that Julia CHILD created the recipe Beef Bourguignon, but also finds the three reviews written about Beef Bourguignon by John DOE, John SMITH, and Jane DOE:

 g.V().has('recipe', 'instructions', tokenRegex('Braise.*')).as('r').
   V().has('person', 'name', prefix('J')).out().where(eq('r')).path().unfold().values('name')

with results:

==>Beef Bourguignon
==>John DOE
==>Beef Bourguignon
==>Beef Bourguignon
==>Julia CHILD
==>Beef Bourguignon
==>Beef Bourguignon
==>John SMITH
==>Beef Bourguignon
==>Beef Bourguignon
==>Jane DOE
==>Beef Bourguignon

Each three lines in the results represents the recipe ->person->recipe path.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com