DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Build a Plugin and Add a Listener to an Index in ElasticSearch 5

Build a Plugin and Add a Listener to an Index in ElasticSearch 5

The WebSocket Change Feed plugin is not naturally compatible with ElasticSearch, but you can build your own code to make it work.

Chris Shayan user avatar by
Chris Shayan
·
Apr. 23, 17 · Tutorial
Like (2)
Save
Tweet
Share
6.80K Views

Join the DZone community and get the full member experience.

Join For Free

ElasticSearch 5.x is pretty exciting release with many cool features. However, this new release is very different than the older versions of ES, especially when it comes to the plugin. Today, we needed to use a plugin called WebSocket Change Feed Plugin (by ForgeRock/Chris Clifton) (it was also recommended in ES5 documentations).

We thought it would be adaptable with ES5. However, it is not compatible (and perhaps ES did not update their documentation). I'll try to upgrade the WebSocket plugin to ES5 and will share the results later, too. In the meantime, I googled to see were there any guidelines or documentation on how to build an ES5 plugin where you can attach a listener to an index lifecycle. For some reason, I could not find any. Hence, I spent a few hours and finally, I managed to make it work.

You can see the simple code on GitHub.

POM 

You need to get your dependencies right. Please have a look at my pom.xml.

ES Resources and the Plug Assembly File

You will need to have a properties file that tells to ES about the metadata of this plugin, which is as below:

description=${project.description}.
version=${project.version}
name=${project.artifactId}
classname=com.chrisshayan.plugins.ElasticSearchPlugin
java.version=1.8
elasticsearch.version=${elasticsearch.version}

Line 4 (classname) is the full qualified classname of your main Plugin file. ES needs to know this while it is running your plugin. Also, there's the plugin.xml file that tells to Maven how to treat it in the packaging:

<?xml version="1.0"?>
<assembly>
	<id>plugin</id>
	<formats>
		<format>zip</format>
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>
	<files>
		<file>
			<source>${basedir}/src/main/resources/plugin-descriptor.properties</source>
			<outputDirectory>elasticsearch</outputDirectory>
			<filtered>true</filtered>
		</file>
	</files>
	<dependencySets>
		<dependencySet>
			<outputDirectory>elasticsearch</outputDirectory>
			<useProjectArtifact>true</useProjectArtifact>
			<useTransitiveFiltering>true</useTransitiveFiltering>
		</dependencySet>
	</dependencySets>
</assembly>

How to Add a Listener to an Index?

It's quite straightforward. You need to extend the Plugin class and then add your IndexEventListener to the IndexModule as shown in following code (Lines 13-25):

public class ElasticSearchPlugin extends Plugin {
    private final Logger logger = Loggers.getLogger(ElasticSearchPlugin.class);

    @Override
    public Collection<Module> createGuiceModules() {
        Collection<Module> modules = new ArrayList<>(1);
        modules.add(new PluginModule());

        return modules;
    }

    @Override
    public void onIndexModule(IndexModule indexModule) {
        indexModule.addIndexEventListener(new IndexEventListener() {
            @Override
            public void afterIndexCreated(IndexService indexService) {
                logger.info("afterIndexCreated :" + indexService.getMetaData().getIndex().getName());
            }

            @Override
            public void afterIndexRemoved(Index index, IndexSettings indexSettings, IndicesClusterStateService.AllocatedIndices.IndexRemovalReason reason) {
                logger.info("afterIndexRemoved :" + index.getName());
            }
        });
    }
}

Hopefully, this quick post saves a few hours of yours!

Elasticsearch Build (game engine)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • REST vs. Messaging for Microservices
  • How to Submit a Post to DZone
  • HTTP vs Messaging for Microservices Communications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: