|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | + |
| 7 | +package org.opensearch.sql.ppl; |
| 8 | + |
| 9 | +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WILDCARD; |
| 10 | +import static org.opensearch.sql.util.MatcherUtils.rows; |
| 11 | +import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import org.json.JSONObject; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +public class LikeQueryIT extends PPLIntegTestCase { |
| 18 | + |
| 19 | + @Override |
| 20 | + public void init() throws IOException { |
| 21 | + loadIndex(Index.WILDCARD); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void test_like_with_percent() throws IOException { |
| 26 | + String query = "source=" + TEST_INDEX_WILDCARD + " | WHERE Like(KeywordBody, 'test wildcard%') | fields KeywordBody"; |
| 27 | + JSONObject result = executeQuery(query); |
| 28 | + verifyDataRows(result, |
| 29 | + rows("test wildcard"), |
| 30 | + rows("test wildcard in the end of the text%"), |
| 31 | + rows("test wildcard in % the middle of the text"), |
| 32 | + rows("test wildcard %% beside each other"), |
| 33 | + rows("test wildcard in the end of the text_"), |
| 34 | + rows("test wildcard in _ the middle of the text"), |
| 35 | + rows("test wildcard __ beside each other")); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void test_like_with_escaped_percent() throws IOException { |
| 40 | + String query = "source=" + TEST_INDEX_WILDCARD + " | WHERE Like(KeywordBody, '\\\\%test wildcard%') | fields KeywordBody"; |
| 41 | + JSONObject result = executeQuery(query); |
| 42 | + verifyDataRows(result, |
| 43 | + rows("%test wildcard in the beginning of the text")); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void test_like_in_where_with_escaped_underscore() throws IOException { |
| 48 | + String query = "source=" + TEST_INDEX_WILDCARD + " | WHERE Like(KeywordBody, '\\\\_test wildcard%') | fields KeywordBody"; |
| 49 | + JSONObject result = executeQuery(query); |
| 50 | + verifyDataRows(result, |
| 51 | + rows("_test wildcard in the beginning of the text")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void test_like_on_text_field_with_one_word() throws IOException { |
| 56 | + String query = "source=" + TEST_INDEX_WILDCARD + " | WHERE Like(TextBody, 'test*') | fields TextBody"; |
| 57 | + JSONObject result = executeQuery(query); |
| 58 | + assertEquals(9, result.getInt("total")); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void test_like_on_text_keyword_field_with_one_word() throws IOException { |
| 63 | + String query = "source=" + TEST_INDEX_WILDCARD + " | WHERE Like(TextKeywordBody, 'test*') | fields TextKeywordBody"; |
| 64 | + JSONObject result = executeQuery(query); |
| 65 | + assertEquals(8, result.getInt("total")); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void test_like_on_text_keyword_field_with_greater_than_one_word() throws IOException { |
| 70 | + String query = "source=" + TEST_INDEX_WILDCARD + " | WHERE Like(TextKeywordBody, 'test wild*') | fields TextKeywordBody"; |
| 71 | + JSONObject result = executeQuery(query); |
| 72 | + assertEquals(7, result.getInt("total")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void test_like_on_text_field_with_greater_than_one_word() throws IOException { |
| 77 | + String query = "source=" + TEST_INDEX_WILDCARD + " | WHERE Like(TextBody, 'test wild*') | fields TextBody"; |
| 78 | + JSONObject result = executeQuery(query); |
| 79 | + assertEquals(0, result.getInt("total")); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void test_convert_field_text_to_keyword() throws IOException { |
| 84 | + String query = "source=" + TEST_INDEX_WILDCARD + " | WHERE Like(TextKeywordBody, '*') | fields TextKeywordBody"; |
| 85 | + String result = explainQueryToString(query); |
| 86 | + assertTrue(result.contains("TextKeywordBody.keyword")); |
| 87 | + } |
| 88 | +} |
0 commit comments