Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

JNoSQL/Diana/Elasticsearch

< JNoSQL‎ | Diana

Elasticsearch

Elasticsearch is a search engine based on Lucene. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents.


Maven Project

To use Elasticsearch driver in a Maven Project:

        <dependency>
            <groupId>org.jnosql.diana</groupId>
            <artifactId>elasticsearch-driver</artifactId>
            <version>version</version>
        </dependency>

E.g:

        <dependency>
            <groupId>org.jnosql.diana</groupId>
            <artifactId>elasticsearch-driver</artifactId>
            <version>0.0.1</version>
        </dependency>


Document

The ArangoDB document implementation uses the ElasticsearchDocumentConfiguration to the configuration. This class has configurations methods such as host, port, timeout, user, password. Beyond the method also it can read a configuration from diana-elasticsearch.properties file.

  • elasticsearch-host-n: the host to client connection, if necessary to define a different port than default just use colon, ':' eg: elasticsearch-host-1=172.17.0.2:1234
  • elasticsearch-settings-n: the is a prefix to put some configuration on elastisearch to be use {@link Settings.Builder#put(String, String)}


Sample Code


public class App {

    public static final String DATABASE = "database";
    public static final String DOCUMENT_COLLECTION = "person";

    public static void main(String[] args)  {
        DocumentConfiguration configuration = new ElasticsearchDocumentConfiguration();
        try(DocumentCollectionManagerFactory collectionFactory = configuration.get();) {
            DocumentCollectionManager collectionManager = collectionFactory.get(DATABASE);

            DocumentEntity entity = DocumentEntity.of(DOCUMENT_COLLECTION);
            entity.add(Document.of("name", "Daniel Soro"));
            entity.add(Document.of("age", 26));

            DocumentEntity entitySaved = collectionManager.save(entity);
            Optional<Document> id = entitySaved.find("_id");

            DocumentQuery query = select.from(DOCUMENT_COLLECTION).where(DocumentCondition.eq(id.get());
            List<DocumentEntity> documentsFound = collectionManager.select(query);


        }
    }
}

QueryBuilder

Elasticsearch provides a full Java query dsl in a similar manner to the REST Query DSL. The factory for query builders is QueryBuilders. Once your query is ready, you can use the Search API.


Sample Code


public class App {

    public static final String DATABASE = "database";
    public static final String DOCUMENT_COLLECTION = "person";

    public static void main(String[] args)  {
        ElasticsearchDocumentConfiguration configuration = new ElasticsearchDocumentConfiguration();
        try(ElasticsearchDocumentCollectionManagerFactory collectionFactory = configuration.get();) {
            ElasticsearchDocumentCollectionManager collectionManager = collectionFactory.get(DATABASE);

           QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "some string");
            List<DocumentEntity> documentsFound = collectionManager.select(query, "type");


        }
    }
}


Links

Back to the top