Class ElasticsearchQuerySanitizer


  • public class ElasticsearchQuerySanitizer
    extends java.lang.Object
    The ElasticsearchQuerySanitizer class is responsible for sanitizing Elasticsearch queries by escaping or removing reserved characters and keywords. This is essential to ensure proper handling of Elasticsearch queries and to prevent syntax issues caused by special characters or reserved words.

    This class provides utility methods to sanitize query strings by escaping predefined reserved characters, removing certain reserved characters, and excluding specific keywords if provided. The reserved characters and keywords are predefined and managed internally.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static java.util.Map<java.lang.String,​java.lang.String> excludeKeywords​(java.lang.String[] exclude)  
      protected static java.util.Map<java.lang.String,​java.lang.String> prepareReservedKeywords()  
      static java.lang.String sanitize​(java.lang.String query)
      Sanitizes the provided Elasticsearch query string by escaping or removing certain reserved characters and excluding specific keywords if applicable.
      static java.lang.String sanitize​(java.lang.String query, java.lang.String[] exclude)
      Sanitizes the given query string by replacing reserved keywords with their sanitized equivalents, excluding the specified keywords from sanitization.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • RESERVED_CHARACTERS_TO_ESCAPE

        public static final java.lang.String[] RESERVED_CHARACTERS_TO_ESCAPE
      • RESERVED_CHARACTERS_TO_REMOVE

        public static final java.lang.String[] RESERVED_CHARACTERS_TO_REMOVE
      • RESERVED_KEYWORDS

        public static final java.util.Map<java.lang.String,​java.lang.String> RESERVED_KEYWORDS
    • Constructor Detail

      • ElasticsearchQuerySanitizer

        public ElasticsearchQuerySanitizer()
    • Method Detail

      • sanitize

        public static java.lang.String sanitize​(java.lang.String query)
        Sanitizes the provided Elasticsearch query string by escaping or removing certain reserved characters and excluding specific keywords if applicable.

        This method applies default sanitization rules and does not consider keyword exclusions.

        Parameters:
        query - the Elasticsearch query string to sanitize, such as a search query or filter. It must not be null to ensure proper sanitization.
        Returns:
        the sanitized query string with reserved characters handled appropriately. If the input is empty or null, the behavior depends on the implemented sanitization logic.
      • sanitize

        public static java.lang.String sanitize​(java.lang.String query,
                                                java.lang.String[] exclude)
        Sanitizes the given query string by replacing reserved keywords with their sanitized equivalents, excluding the specified keywords from sanitization.
        Parameters:
        query - the query string to sanitize, which may contain reserved characters and keywords. This string must not be null.
        exclude - an array of keywords to exclude from sanitization. If null or empty, all reserved keywords will be considered for sanitization.
        Returns:
        the sanitized query string with reserved keywords appropriately replaced, and excluded keywords untouched.
      • prepareReservedKeywords

        protected static java.util.Map<java.lang.String,​java.lang.String> prepareReservedKeywords()
      • excludeKeywords

        protected static java.util.Map<java.lang.String,​java.lang.String> excludeKeywords​(java.lang.String[] exclude)