Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Adding support to NOT REGEXP_QUERY, which is currently failing when r…
Browse files Browse the repository at this point in the history
…un with "Negative operator [REGEXP] is not supported." (#1097)
  • Loading branch information
FreCap authored May 17, 2021
1 parent dd13156 commit 4283257
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ public void scoreQueryTest() throws IOException {
"{\"filter\":{\"match\":{\"address\":{\"query\":\"Street\"")));
}

@Test
public void regexpQueryTest() throws IOException {
final String result = explainQuery(String.format(Locale.ROOT,
"SELECT * FROM %s WHERE address=REGEXP_QUERY('.*')",
TestsConstants.TEST_INDEX_ACCOUNT));
Assert.assertThat(result,
containsString("{\"bool\":{\"must\":[{\"regexp\":"
+ "{\"address\":{\"value\":\".*\",\"flags_value\":255,\"max_determinized_states\":10000,\"boost\":1.0}}}"));
}

@Test
public void negativeRegexpQueryTest() throws IOException {
final String result = explainQuery(String.format(Locale.ROOT,
"SELECT * FROM %s WHERE NOT(address=REGEXP_QUERY('.*'))",
TestsConstants.TEST_INDEX_ACCOUNT));
Assert.assertThat(result,
containsString("{\"bool\":{\"must_not\":[{\"regexp\":"
+ "{\"address\":{\"value\":\".*\",\"flags_value\":255,\"max_determinized_states\":10000,\"boost\":1.0}}}"));
}

/**
* wildcardQuery 是用通配符的方式查找某个term  比如例子中 l*e means leae ltae ....
* "wildcard": { "address" : { "wildcard" : "l*e" } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,22 @@ public void regexQueryTest() throws IOException {
Assert.assertEquals(4, hitSource.getInt("age"));
}

@Test
public void negativeRegexQueryTest() throws IOException {
JSONObject response = executeQuery(
String.format(Locale.ROOT, "SELECT * " +
"FROM %s " +
"WHERE NOT(dog_name = REGEXP_QUERY('sn.*', 'INTERSECTION|COMPLEMENT|EMPTY', 10000))",
TestsConstants.TEST_INDEX_DOG));

JSONArray hits = getHits(response);
Assert.assertEquals(1, hits.length());

JSONObject hitSource = getSource(hits.getJSONObject(0));
Assert.assertNotEquals("snoopy", hitSource.getString("dog_name"));
Assert.assertEquals("rex", hitSource.getString("dog_name"));
}

@Test
public void doubleNotTest() throws IOException {
JSONObject response1 = executeQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public enum OPERATOR {
CHILDREN_COMPLEX,
SCRIPT,
NIN_TERMS,
NTERM;
NTERM,
NREGEXP;

public static Map<String, OPERATOR> methodNameToOpear;

Expand Down Expand Up @@ -136,6 +137,7 @@ public enum OPERATOR {
negatives.put(IN, NIN);
negatives.put(BETWEEN, NBETWEEN);
negatives.put(NESTED_COMPLEX, NOT_EXISTS_NESTED_COMPLEX);
negatives.put(REGEXP, NREGEXP);
}

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public abstract class Maker {
private static final Set<Condition.OPERATOR> NOT_OPERATOR_SET = ImmutableSet.of(
Condition.OPERATOR.N, Condition.OPERATOR.NIN, Condition.OPERATOR.ISN, Condition.OPERATOR.NBETWEEN,
Condition.OPERATOR.NLIKE, Condition.OPERATOR.NIN_TERMS, Condition.OPERATOR.NTERM,
Condition.OPERATOR.NOT_EXISTS_NESTED_COMPLEX
Condition.OPERATOR.NOT_EXISTS_NESTED_COMPLEX, Condition.OPERATOR.NREGEXP
);

protected Maker(Boolean isQuery) {
Expand Down Expand Up @@ -217,6 +217,7 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
toXContent = QueryBuilders.wildcardQuery(name, queryStr);
break;
case REGEXP:
case NREGEXP:
Object[] values = (Object[]) value;
RegexpQueryBuilder regexpQuery = QueryBuilders.regexpQuery(name, values[0].toString());
if (1 < values.length) {
Expand Down

0 comments on commit 4283257

Please sign in to comment.