/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.opensearch.hadoop.rest.query; import org.opensearch.hadoop.OpenSearchHadoopIllegalArgumentException; import org.opensearch.hadoop.cfg.Settings; import org.opensearch.hadoop.util.IOUtils; import org.opensearch.hadoop.util.SettingsUtils; import org.opensearch.hadoop.util.StringUtils; import org.opensearch.hadoop.util.regex.Regex; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; public abstract class QueryUtils { public static QueryBuilder parseQuery(Settings settings) { String query = settings.getQuery(); if (!StringUtils.hasText(query)) { return MatchAllQueryBuilder.MATCH_ALL; } query = query.trim(); if (query.startsWith("?") == false && query.startsWith("{") == false) { try { // must be a resource InputStream in = settings.loadResource(query); if (in == null) { throw new IOException(); } query = IOUtils.asString(in); } catch (IOException ex) { throw new OpenSearchHadoopIllegalArgumentException( String.format( "Cannot determine specified query - doesn't appear to be URI or JSON based and location [%s] cannot be opened", query)); } } try { return SimpleQueryParser.parse(query, true); } catch (IOException e) { throw new IllegalArgumentException("Failed to parse query: " + query, e); } } public static List parseFilters(Settings settings) { SettingsUtils settingsUtils = new SettingsUtils(); String[] rawFilters = settingsUtils.getFilters(settings); if (rawFilters == null) { return Collections.emptyList(); } List filters = new ArrayList(); for (String filter : rawFilters) { filter = filter.trim(); try { filters.add(SimpleQueryParser.parse(filter, false)); } catch (IOException e) { throw new IllegalArgumentException("Failed to parse filter: " + filter, e); } } return filters; } public static QueryBuilder parseQueryAndFilters(Settings settings) { QueryBuilder query = QueryUtils.parseQuery(settings); List filters = QueryUtils.parseFilters(settings); if (filters == null || filters.isEmpty()) { return query; } return new BoolQueryBuilder().must(query).filters(filters); } /** * Checks if the provided candidate is explicitly contained in the provided indices. */ public static boolean isExplicitlyRequested(String candidate, String... indices) { boolean result = false; for (String indexOrAlias : indices) { boolean include = true; if (indexOrAlias.charAt(0) == '+' || indexOrAlias.charAt(0) == '-') { include = indexOrAlias.charAt(0) == '+'; indexOrAlias = indexOrAlias.substring(1); } if (indexOrAlias.equals("*") || indexOrAlias.equals("_all")) { return false; } if (Regex.isSimpleMatchPattern(indexOrAlias)) { if (Regex.simpleMatch(indexOrAlias, candidate)) { if (include) { result = true; } else { return false; } } } else { if (candidate.equals(indexOrAlias)) { if (include) { result = true; } else { return false; } } } } return result; } }