Entry Points Since OrientDB v 1.7

The entry points for creating a new Index Engine are two:

  • OIndexFactory
  • OIndexEngine

Implementing OIndexFactory

Create your own facory that implements OIndexFactory.

In your factory you have to declare:

  1. Which types of index you support
  2. Which types of algorithms you support

and you have to implements the createIndex method

Example of custom factory for Lucene Indexing

  1. package com.orientechnologies.lucene;
  2. import java.util.Collections;
  3. import java.util.HashSet;
  4. import java.util.Set;
  5. import com.orientechnologies.lucene.index.OLuceneFullTextIndex;
  6. import com.orientechnologies.lucene.index.OLuceneSpatialIndex;
  7. import com.orientechnologies.lucene.manager.*;
  8. import com.orientechnologies.lucene.shape.OShapeFactoryImpl;
  9. import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
  10. import com.orientechnologies.orient.core.db.record.OIdentifiable;
  11. import com.orientechnologies.orient.core.exception.OConfigurationException;
  12. import com.orientechnologies.orient.core.index.OIndexFactory;
  13. import com.orientechnologies.orient.core.index.OIndexInternal;
  14. import com.orientechnologies.orient.core.metadata.schema.OClass;
  15. import com.orientechnologies.orient.core.record.impl.ODocument;
  16. /**
  17. * Created by enricorisa on 21/03/14.
  18. */
  19. public class OLuceneIndexFactory implements OIndexFactory {
  20. private static final Set<String> TYPES;
  21. private static final Set<String> ALGORITHMS;
  22. public static final String LUCENE_ALGORITHM = "LUCENE";
  23. static {
  24. final Set<String> types = new HashSet<String>();
  25. types.add(OClass.INDEX_TYPE.UNIQUE.toString());
  26. types.add(OClass.INDEX_TYPE.NOTUNIQUE.toString());
  27. types.add(OClass.INDEX_TYPE.FULLTEXT.toString());
  28. types.add(OClass.INDEX_TYPE.DICTIONARY.toString());
  29. types.add(OClass.INDEX_TYPE.SPATIAL.toString());
  30. TYPES = Collections.unmodifiableSet(types);
  31. }
  32. static {
  33. final Set<String> algorithms = new HashSet<String>();
  34. algorithms.add(LUCENE_ALGORITHM);
  35. ALGORITHMS = Collections.unmodifiableSet(algorithms);
  36. }
  37. public OLuceneIndexFactory() {
  38. }
  39. @Override
  40. public Set<String> getTypes() {
  41. return TYPES;
  42. }
  43. @Override
  44. public Set<String> getAlgorithms() {
  45. return ALGORITHMS;
  46. }
  47. @Override
  48. public OIndexInternal<?> createIndex(ODatabaseRecord oDatabaseRecord, String indexType, String algorithm,
  49. String valueContainerAlgorithm, ODocument metadata) throws OConfigurationException {
  50. return createLuceneIndex(oDatabaseRecord, indexType, valueContainerAlgorithm, metadata);
  51. }
  52. private OIndexInternal<?> createLuceneIndex(ODatabaseRecord oDatabaseRecord, String indexType, String valueContainerAlgorithm,
  53. ODocument metadata) {
  54. if (OClass.INDEX_TYPE.FULLTEXT.toString().equals(indexType)) {
  55. return new OLuceneFullTextIndex(indexType, LUCENE_ALGORITHM, new OLuceneIndexEngine<Set<OIdentifiable>>(
  56. new OLuceneFullTextIndexManager(), indexType), valueContainerAlgorithm, metadata);
  57. } else if (OClass.INDEX_TYPE.SPATIAL.toString().equals(indexType)) {
  58. return new OLuceneSpatialIndex(indexType, LUCENE_ALGORITHM, new OLuceneIndexEngine<Set<OIdentifiable>>(
  59. new OLuceneSpatialIndexManager(new OShapeFactoryImpl()), indexType), valueContainerAlgorithm);
  60. }
  61. throw new OConfigurationException("Unsupported type : " + indexType);
  62. }
  63. }

To plug your factory create in your project under META-INF/services a text file called com.orientechnologies.orient.core.index.OIndexFactory and write inside your factory

Example

  1. com.orientechnologies.lucene.OLuceneIndexFactory

Implementing OIndexEngine

To write a new Index Engine implements the OIndexEngine interface.

The main methods are:

  • get
  • put

get V get(Object key);

You have to return a Set of OIdentifiable or OIdentifiable if your index is unique, associated with the key. The key could be:

  • The value if you are indexing a single field (Integer,String,Double..etc).
  • OCompositeKey if you are indexing two or more fields

put void put(Object key, V value);

  • The key is the value to be indexed. Could be as written before
  • The value is a Set of OIdentifiable or OIdentifiable associated with the key

Create Index from SQL

You can create an index with your Index Engine with sql with this syntax

  1. CREATE INDEX Foo.bar ON Foo (bar) NOTUNIQUE ENGINE CUSTOM

where CUSTOM is the name of your index engine