|
8 | 8 |
|
9 | 9 | package org.opensearch.plugin.insights.core.service;
|
10 | 10 |
|
| 11 | +import static org.opensearch.plugin.insights.core.service.QueryInsightsService.QUERY_INSIGHTS_INDEX_TAG_NAME; |
11 | 12 | import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.MAX_DELETE_AFTER_VALUE;
|
12 | 13 | import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.MIN_DELETE_AFTER_VALUE;
|
13 | 14 | import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.QUERY_INSIGHTS_EXECUTOR;
|
|
28 | 29 | import java.util.List;
|
29 | 30 | import java.util.Locale;
|
30 | 31 | import java.util.Map;
|
| 32 | +import java.util.Objects; |
31 | 33 | import java.util.concurrent.PriorityBlockingQueue;
|
32 | 34 | import java.util.concurrent.atomic.AtomicReference;
|
33 | 35 | import java.util.function.Predicate;
|
|
37 | 39 | import org.apache.logging.log4j.Logger;
|
38 | 40 | import org.opensearch.action.admin.indices.delete.DeleteIndexRequest;
|
39 | 41 | import org.opensearch.client.Client;
|
| 42 | +import org.opensearch.cluster.metadata.IndexMetadata; |
40 | 43 | import org.opensearch.common.unit.TimeValue;
|
41 | 44 | import org.opensearch.core.action.ActionListener;
|
42 | 45 | import org.opensearch.plugin.insights.core.exporter.QueryInsightsExporter;
|
|
64 | 67 | public class TopQueriesService {
|
65 | 68 | public static final String TOP_QUERIES_LOCAL_INDEX_EXPORTER_ID = "top_queries_local_index_exporter";
|
66 | 69 | public static final String TOP_QUERIES_LOCAL_INDEX_READER_ID = "top_queries_local_index_reader";
|
| 70 | + public static final String TOP_QUERIES_INDEX_TAG_VALUE = "top_n_queries"; |
67 | 71 | private static final String METRIC_TYPE_TAG = "metric_type";
|
68 | 72 | private static final String GROUPBY_TAG = "groupby";
|
69 | 73 |
|
@@ -538,35 +542,55 @@ public void onFailure(Exception e) {
|
538 | 542 | }
|
539 | 543 |
|
540 | 544 | /**
|
541 |
| - * Validates if the input string is a Query Insights local index name |
542 |
| - * in the format "top_queries-YYYY.MM.dd-XXXXX". |
| 545 | + * Validates if the input string is a Query Insights local index |
| 546 | + * in the format "top_queries-YYYY.MM.dd-XXXXX", and has the expected index metadata. |
543 | 547 | *
|
544 |
| - * @param indexName the string to validate. |
| 548 | + * @param indexName the index name to validate. |
| 549 | + * @param indexMetadata the metadata associated with the index |
545 | 550 | * @return {@code true} if the string is valid, {@code false} otherwise.
|
546 | 551 | */
|
547 |
| - public static boolean isTopQueriesIndex(String indexName) { |
548 |
| - // Split the input string by '-' |
549 |
| - String[] parts = indexName.split("-"); |
| 552 | + public static boolean isTopQueriesIndex(String indexName, IndexMetadata indexMetadata) { |
| 553 | + try { |
| 554 | + if (indexMetadata == null || indexMetadata.mapping() == null) { |
| 555 | + return false; |
| 556 | + } |
| 557 | + Map<String, Object> sourceMap = Objects.requireNonNull(indexMetadata.mapping()).getSourceAsMap(); |
| 558 | + if (sourceMap == null || !sourceMap.containsKey("_meta")) { |
| 559 | + return false; |
| 560 | + } |
| 561 | + Map<String, Object> metaMap = (Map<String, Object>) sourceMap.get("_meta"); |
| 562 | + if (metaMap == null || !metaMap.containsKey(QUERY_INSIGHTS_INDEX_TAG_NAME)) { |
| 563 | + return false; |
| 564 | + } |
| 565 | + if (!metaMap.get(QUERY_INSIGHTS_INDEX_TAG_NAME).equals(TOP_QUERIES_INDEX_TAG_VALUE)) { |
| 566 | + return false; |
| 567 | + } |
550 | 568 |
|
551 |
| - // Check if the string has exactly 3 parts |
552 |
| - if (parts.length != 3) { |
553 |
| - return false; |
554 |
| - } |
| 569 | + // Split the input string by '-' |
| 570 | + String[] parts = indexName.split("-"); |
555 | 571 |
|
556 |
| - // Validate the first part is "top_queries" |
557 |
| - if (!"top_queries".equals(parts[0])) { |
558 |
| - return false; |
559 |
| - } |
| 572 | + // Check if the string has exactly 3 parts |
| 573 | + if (parts.length != 3) { |
| 574 | + return false; |
| 575 | + } |
560 | 576 |
|
561 |
| - // Validate the second part is a valid date in "YYYY.MM.dd" format |
562 |
| - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT); |
563 |
| - try { |
564 |
| - LocalDate.parse(parts[1], formatter); |
565 |
| - } catch (DateTimeParseException e) { |
| 577 | + // Validate the first part is "top_queries" |
| 578 | + if (!"top_queries".equals(parts[0])) { |
| 579 | + return false; |
| 580 | + } |
| 581 | + |
| 582 | + // Validate the second part is a valid date in "YYYY.MM.dd" format |
| 583 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT); |
| 584 | + try { |
| 585 | + LocalDate.parse(parts[1], formatter); |
| 586 | + } catch (DateTimeParseException e) { |
| 587 | + return false; |
| 588 | + } |
| 589 | + |
| 590 | + // Validate the third part is exactly 5 digits |
| 591 | + return parts[2].matches("\\d{5}"); |
| 592 | + } catch (Exception e) { |
566 | 593 | return false;
|
567 | 594 | }
|
568 |
| - |
569 |
| - // Validate the third part is exactly 5 digits |
570 |
| - return parts[2].matches("\\d{5}"); |
571 | 595 | }
|
572 | 596 | }
|
0 commit comments