Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "JNoSQL/Diana/Elasticsearch"
(Created page with "== 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 sc...") |
|||
Line 65: | Line 65: | ||
DocumentQuery query = DocumentQuery.of(DOCUMENT_COLLECTION); | DocumentQuery query = DocumentQuery.of(DOCUMENT_COLLECTION); | ||
query.and(DocumentCondition.eq(id.get())); | query.and(DocumentCondition.eq(id.get())); | ||
− | List<DocumentEntity> documentsFound = collectionManager. | + | List<DocumentEntity> documentsFound = collectionManager.select(query); |
Line 95: | Line 95: | ||
QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "some string"); | QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "some string"); | ||
− | List<DocumentEntity> documentsFound = collectionManager. | + | List<DocumentEntity> documentsFound = collectionManager.select(query, "type"); |
Revision as of 12:55, 30 June 2017
Contents
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 = DocumentQuery.of(DOCUMENT_COLLECTION);
query.and(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");
}
}
}