blueflood-elasticsearch/src/main/java/com/rackspacecloud/blueflood/io/ElasticsearchRestHelper.java
private String getTokenQueryDslString(String tenantId, String query) {
        String[] queryTokens = query.split(Locator.METRIC_TOKEN_SEPARATOR_REGEX);
        String lastToken = queryTokens[queryTokens.length - 1];

        /**
         * Builds parent part of the query for the given input query glob tokens.
         * For a given query foo.bar.*, parent part is foo.bar
         *
         * For example:
         *
         *  For query = foo.bar.*
         *          { "term": {  "parent": "foo.bar" }}
         *
         *  For query = foo.*.*
         *          { "regexp": {  "parent": "foo.[^.]+" }}
         *
         *  For query = foo.b*.*
         *          { "regexp": {  "parent": "foo.b[^.]*" }}
         */

        String parentString = getParentString(queryTokens);
        String tenantIdTermQueryString = getTermQueryString(ESFieldLabel.tenantId.name(), tenantId);
        List<String> mustNodes = new ArrayList<>();
        mustNodes.add(tenantIdTermQueryString);
        mustNodes.add(parentString);

        // For example: if query=foo.bar.*, we can just get every token for the parent=foo.bar
        // but if query=foo.bar.b*, we want to add the token part of the query for "b*"
        if (!lastToken.equals("*")) {

            String tokenQString;

            GlobPattern pattern = new GlobPattern(lastToken);

            if (pattern.hasWildcard()) {
                String compiledString = pattern.compiled().toString();
                tokenQString = getRegexpQueryString(ESFieldLabel.token.name(), compiledString);
            } else {
                tokenQString = getTermQueryString(ESFieldLabel.token.name(), lastToken);
            }

            mustNodes.add(tokenQString);
        }

        String mustQueryString = getMustQueryString(mustNodes);
        String boolQueryString = getBoolQueryString(mustQueryString);
        // replace one '\' char with two '\\'
        boolQueryString = boolQueryString.replaceAll("\\\\", "\\\\\\\\");

        return boolQueryString;
    }
private String getTokenQueryDslString(String tenantId, String query) {
        String[] queryTokens = query.split(Locator.METRIC_TOKEN_SEPARATOR_REGEX);
        String lastToken = queryTokens[queryTokens.length - 1];

        /**
         * Builds parent part of the query for the given input query glob tokens.
         * For a given query foo.bar.*, parent part is foo.bar
         *
         * For example:
         *
         *  For query = foo.bar.*
         *          { "term": {  "parent": "foo.bar" }}
         *
         *  For query = foo.*.*
         *          { "regexp": {  "parent": "foo.[^.]+" }}
         *
         *  For query = foo.b*.*
         *          { "regexp": {  "parent": "foo.b[^.]*" }}
         */

        String parentString = getParentString(queryTokens);
        String tenantIdTermQueryString = getTermQueryString(ESFieldLabel.tenantId.name(), tenantId);
        List<String> mustNodes = new ArrayList<>();
        mustNodes.add(tenantIdTermQueryString);
        mustNodes.add(parentString);

        // For example: if query=foo.bar.*, we can just get every token for the parent=foo.bar
        // but if query=foo.bar.b*, we want to add the token part of the query for "b*"
        if (!lastToken.equals("*")) {

            String tokenQString;

            GlobPattern pattern = new GlobPattern(lastToken);

            if (pattern.hasWildcard()) {
                String compiledString = pattern.compiled().toString();
                tokenQString = getRegexpQueryString(ESFieldLabel.token.name(), compiledString);
            } else {
                tokenQString = getTermQueryString(ESFieldLabel.token.name(), lastToken);
            }

            mustNodes.add(tokenQString);
        }

        String mustQueryString = getMustQueryString(mustNodes);
        String boolQueryString = getBoolQueryString(mustQueryString);
        // replace one '\' char with two '\\'
        boolQueryString = boolQueryString.replaceAll("\\\\", "\\\\\\\\");

        return boolQueryString;
    }
Builds ES query to grab tokens corresponding to the given query glob.
For a given query foo.bar.*, we would like to grab all the tokens with
parent as foo.bar

Sample ES query for a query glob = foo.bar.*:

"query": {
"bool" : {
"must" : [
{ "term": {  "tenantId": "<tenantId>" }},
{ "term": {  "parent": "foo.bar" }}
]
}
}

@param tenantId
@param query
@return