Skip to content

ElasticSearch

Mahdi Sheikh Hosseini edited this page Aug 1, 2025 · 2 revisions

πŸ”Ž Elasticsearch Integration in MicroFox

MicroFox offers a minimal, type-safe, and repository-style Elasticsearch API, making it easy to index, search, update, and delete documents without relying on external libraries like Spring Data Elasticsearch.

Everything is explicitly configured via code, following MicroFox’s no-reflection, no-annotation philosophy.

βš™οΈ Enabling ElasticSearch

To enable ElasticSearch in your MicroFox application, simply add the following Maven dependency:

<dependency>
    <groupId>ir.moke.microfox</groupId>
    <artifactId>microfox-elastic</artifactId>
    <version>${microfox.version}</version>
</dependency>

🧠 Core Elasticsearch Concepts

  • Explicit configuration:
    Connection settings are registered programmatically using ElasticConfig.

  • Generic repository access:
    The repository layer (ElasticRepository<T>) is type-safe and directly linked to your model class.

  • Full CRUD support:
    Supports indexing, getting, updating, searching, deleting (by ID or query), and bulk operations.

  • Criteria-based search:
    Search is performed using a fluent and readable ElasticCriteria DSL.

🧩 API Overview

// Register a connection
ElasticFactory.register(String identity, ElasticConfig config);
// Get a repository instance
<T> ElasticRepository<T> elastic(String identity, Class<T> entityClass);

βš™οΈ Configuration via ElasticConfig

ElasticConfig config = new ElasticConfig(
    "127.0.0.1", // Host
    9200,        // Port
    "admin",     // Username
    "adminpass", // Password
    false        // Use HTTPS
);
ElasticFactory.register("el", config);

πŸ§ͺ Repository Usage Example

ElasticRepository<Person> repo = MicroFox.elastic("el", Person.class);

// Create index
repo.indexCreate();

// Save a document
repo.save("a1", new Person(1, "Mahdi", "Sheikh Hosseini", 23));

// Get by ID
Person p = repo.get("a1");

// Update document
repo.update("a1", new Person(1, "Javad", "Mohammadi", 12));

// Delete by ID
repo.delete("a1");

// Delete by query
ElasticCriteria criteria = ElasticCriteria.builder().match("name", "Mahdi").build();
repo.deleteByQuery(criteria);

πŸ” Search Example

ElasticCriteria criteria = ElasticCriteria.builder()
    .match("name", "Javad")
    .build();

List<Person> results = repo.search(criteria);
results.forEach(System.out::println);

🧺 Bulk Operations

Person p1 = new Person(1, "Mahdi", "Sheikh Hosseini", 33);
Person p2 = new Person(2, "Ali", "Mohammadi", 33);
Person p3 = new Person(3, "Vahid", "Jafari", 33);

List<BulkOperation<Person>> operations = List.of(
    new BulkOperation<>(BulkActionType.SAVE, "1", p1),
    new BulkOperation<>(BulkActionType.SAVE, "2", p2),
    new BulkOperation<>(BulkActionType.SAVE, "3", p3)
);

repo.bulk(operations);

πŸ“š Full API Capabilities

Method Description
indexCreate() Creates the index for the entity
indexRefresh(String index) Refreshes the index (e.g. after bulk operations)
indexDelete(String index) Deletes the index entirely
save(String id, T entity) Saves a document with a specific ID
get(String id) Retrieves a document by ID
update(String id, T entity) Updates the document with the given ID
delete(String id) Deletes a document by ID
deleteByQuery(ElasticCriteria criteria) Deletes all documents matching the criteria
search(ElasticCriteria criteria) Searches using DSL-style criteria
bulk(List<BulkOperation<T>> operations) Performs batch save/update/delete

🧠 ElasticCriteria DSL

The ElasticCriteria builder supports various search operations:

ElasticCriteria criteria = ElasticCriteria.builder()
    .match("field", "value")
    .range("age", 18, 30)
    .term("status", "active")
    .sort("age", SortOrder.DESC)
    .limit(10)
    .build();

βœ… Benefits

  • Type-safe and generic: One repository per entity class
  • No dependencies on Spring or Hibernate
  • Full control over Elasticsearch features
  • Criteria DSL for readable queries
  • Supports bulk processing

Clone this wiki locally