Mangoo I/O Elasticsearch extension

Elasticsearch extension for Mangoo I/O

License

License

Categories

Categories

Search Business Logic Libraries Elasticsearch
GroupId

GroupId

com.markvink
ArtifactId

ArtifactId

mangooio-elasticsearch-extension
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Mangoo I/O Elasticsearch extension
Elasticsearch extension for Mangoo I/O
Project URL

Project URL

https://github.com/MarkVink/mangooio-elasticsearch-extension
Source Code Management

Source Code Management

https://github.com/MarkVink/mangooio-elasticsearch-extension

Download mangooio-elasticsearch-extension

How to add to project

<!-- https://jarcasting.com/artifacts/com.markvink/mangooio-elasticsearch-extension/ -->
<dependency>
    <groupId>com.markvink</groupId>
    <artifactId>mangooio-elasticsearch-extension</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.markvink/mangooio-elasticsearch-extension/
implementation 'com.markvink:mangooio-elasticsearch-extension:1.1.0'
// https://jarcasting.com/artifacts/com.markvink/mangooio-elasticsearch-extension/
implementation ("com.markvink:mangooio-elasticsearch-extension:1.1.0")
'com.markvink:mangooio-elasticsearch-extension:jar:1.1.0'
<dependency org="com.markvink" name="mangooio-elasticsearch-extension" rev="1.1.0">
  <artifact name="mangooio-elasticsearch-extension" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.markvink', module='mangooio-elasticsearch-extension', version='1.1.0')
)
libraryDependencies += "com.markvink" % "mangooio-elasticsearch-extension" % "1.1.0"
[com.markvink/mangooio-elasticsearch-extension "1.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.7.1-1
com.fasterxml.jackson.core : jackson-annotations jar 2.7.1
org.elasticsearch : elasticsearch jar 2.2.0

provided (1)

Group / Artifact Type Version
io.mangoo : mangooio-core jar 2.4.0

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.mockito : mockito-core jar 2.0.42-beta

Project Modules

There are no modules declared in this project.

Maven Central Travis Build Status

Elasticsearch extension for Mangoo I/O

This is an easly plugable extension for the mangoo I/O framework to work with Elasticsearch.

Setup

  1. Add the mangooio-elasticsearch-extension dependency to your pom.xml:

    com.markvink mangooio-elasticsearch-extension x.x.x
  2. Configure the Elasticsearch client inapplication.yaml

2.1) Node client

elasticsearch:
    cluster:
        name: elasticsearch
    path:
        home: /path/to/elasticsearch/home/dir
    index:
        store:
            type: memory|simplefs|niofs|mmapfs|default
    http:
        enabled: true|false

2.1) Transport client

elasticsearch:
    cluser:
        name: elasticsearch
    node:
        host: 127.0.0.1
        port: 9300
    client:
        transport:
            sniff: true|false
            ignore_cluster_name: true|false
            ping_timeout: 5s
            nodes_sampler_interval: 5s
  1. Inject the Elasticsearch client where needed

    @Inject Elasticsearch elasticsearch;

Basic usage

  1. Adding and removing nodes on the transport client

    elasticsearch.addNode(InetAddress address, int port); elasticsearch.removeNode(InetAddress address, int port);

  2. Wrapper methods

    elasticsearch.getClient(); elasticsearch.createIndex(String indexName); elasticsearch.indexDocument(String indexName, Document document); elasticsearch.getDocument(String indexName, String documentType, String documentId); elasticsearch.deleteDocument(String indexName, String documentType, String documentId);

Indexing documents

  1. Indexing fields using jackson annotations

    import com.fasterxml.jackson.annotation.JsonProperty; import com.markvink.mangooio.elasticsearch.document.Document;

    public class User implements Document { @JsonProperty("username") private String username;

     public User(String username) {
         this.username = username;
     }
    

    }

    User user = new User("my-username"); elasticsearch.indexDocument("my-index", user); // Indexed username=my-username with generated ID

  2. Specifing ID of the document

    import com.fasterxml.jackson.annotation.JsonProperty; import com.markvink.mangooio.elasticsearch.document.DocumentWithId;

    public class User implements DocumentWithId { @JsonProperty("username") private String username;

     public User(String username) {
         this.username = username;
     }
     
     @Override
     public String getDocumentId() {
         return username;
     }
    

    }

    User user = new User("my-username"); elasticsearch.indexDocument("my-index", user); // indexed using ID=my-username

  3. Specifing source of the document

    import com.markvink.mangooio.elasticsearch.document.DocumentWithSource;

    public class User implements DocumentWithSource { private String username;

     public User(String username) {
         this.username = username;
     }
     
     @Override
     public Map<String, Object> getDocumentSource() {
         Map<String, Object> map = new HashMap<String, Object>();
         map.put("username", username);
         map.put("key1", "value1");
         map.put("key2", "value2");
         return map;
     }
    

    }

    User user = new User("my-username"); elasticsearch.indexDocument("my-index", user); // indexed using provided source

Using client

  1. Use the normal Java client for searching for example

    SearchResponse response = elasticsearch.getClient().prepareSearch("my-index") .setTypes("User") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.termQuery("username", "my-username")) .setFrom(0).setSize(60).setExplain(true) .execute() .actionGet();

    long hits = response.getHits().getTotalHits(); SearchHit[] results = response.getHits().getHits();

Versions

Version
1.1.0
1.0.0