Skip to content

Proof of concept on how to parse a JSON query to a Elastic QueryBuilder

Notifications You must be signed in to change notification settings

findinpath/elastic-percolate-query-searcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elastic Percolator Query Searcher

A common use case in working with Elasticsearch are the so called "search alerts" which can be used (as Google Alerts puts it) to:

Monitor the web for interesting new content

A percolate query can be executed to find out which registered search queries match the specified document for the query.

This functionality can be useful in case of dealing with alerts which require immediate notification. For each queried document/ batch of documents, there will be issued one notification for the matching registered search queries.

Assuming that the notifications for the search alerts should be batched so that the user gets notified only after a certain time window (e.g.: hour, day, week) elapses (in case that the search alert matches at least one from the indexed documents during the time window), there should be retrieved the JSON search query corresponding to the alert and execute it against the search index so that the latest documents matching the search query to be retrieved and used in the body of the user notification.

It takes quite some time to find on the Internet how to convert a JSON query in a org.elasticsearch.index.query.QueryBuilder so that it can be used in a org.elasticsearch.action.search.SearchRequest. This simple project deals exactly with this problem.

Converting a JSON query to a org.elasticsearch.index.query.QueryBuilder can be done in the same fashion as in the BoolQueryBuilderTests.java.

By looking after the string testFromJson() in the Elasticsearch GitHub repository there can be found plentifully examples on how to parse a JSON string to a org.elasticsearch.index.query.QueryBuilder instance.

Parsing a JSON query to a QueryBuilder

    private static final NamedXContentRegistry XCONTENT_REGISTRY = new NamedXContentRegistry(
            new SearchModule(
                    Settings.EMPTY,
                    false,
                    Collections.emptyList()
            ).getNamedXContents()
    );

    // ....

    private static QueryBuilder parseQuery(String queryAsString) throws IOException {
        var parser = JsonXContent.jsonXContent.createParser(
                XCONTENT_REGISTRY,
                LoggingDeprecationHandler.INSTANCE, queryAsString
        );
        return AbstractQueryBuilder.parseInnerQueryBuilder(parser);
    }

Customize a parsed QueryBuilder from a JSON query

This project contains the ElasticPercolateQuerySearcherTest JUnit test class which:

  • retrieves the details of a news alert
  • parses the percolate query belonging to the news alert to a org.elasticsearch.index.query.QueryBuilder
  • modifies the query builder by adding an extra filter (e.g. : only the documents published after a certain date)
  • executes the query on the news index

Try out the tests

The tests on the project are based on testcontainers library for providing a lightweight, throwaway instance of Elasticsearch.

Run the tests on the project by executing: ./gradlew test

About

Proof of concept on how to parse a JSON query to a Elastic QueryBuilder

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages