Skip to content

Commit

Permalink
feat: fix wildcard Query case sensitive in ES (#1614)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelAHM authored Dec 24, 2024
1 parent 93f7301 commit e86e482
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public static XContentBuilder getSettings(final int nodes) throws IOException {
.field("filter", new String[]{"my_word_email_delimiter_graph", "lowercase"})
.endObject()
.endObject()
.startObject("normalizer")
.startObject("my_lowercase_normalizer")
.field("type", "custom")
.field("filter", new String[]{"lowercase"})
.endObject()
.endObject()
.startObject("filter")
.startObject("english_stop")
.field("type", "stop")
Expand Down Expand Up @@ -74,6 +80,11 @@ public static XContentBuilder getMappings() throws IOException {
.field("type", "keyword")
.field("ignore_above", 10922)
.endObject()
.startObject("lowercase")
.field("type", "keyword")
.field("normalizer", "my_lowercase_normalizer")
.field("ignore_above", 10922)
.endObject()
.endObject()
.endObject()
.endObject()
Expand All @@ -96,6 +107,11 @@ public static XContentBuilder getMappings() throws IOException {
.field("type", "keyword")
.field("ignore_above", 10922)
.endObject()
.startObject("lowercase")
.field("type", "keyword")
.field("normalizer", "my_lowercase_normalizer")
.field("ignore_above", 10922)
.endObject()
.endObject()
.endObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,14 @@ protected List<RpslObject> doSearch() throws IOException {

private QueryBuilder getQueryBuilder(final String[] fields, final String term) {
if (term.indexOf('*') == -1 && term.indexOf('?') == -1) {
final MultiMatchQueryBuilder multiMatchQuery = new MultiMatchQueryBuilder(term, fields)
return new MultiMatchQueryBuilder(term, fields)
.type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX)
.operator(Operator.AND);
return multiMatchQuery;
}

final BoolQueryBuilder wildCardBuilder = QueryBuilders.boolQuery();
for (String field : fields) {
wildCardBuilder.should(QueryBuilders.wildcardQuery(String.format("%s.raw", field), term));
wildCardBuilder.should(QueryBuilders.wildcardQuery(String.format("%s.lowercase", field), term.toLowerCase()));
}
return wildCardBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,83 @@ public void lookup_person_acl_counted() throws Exception {
}


@Test
public void search_wildcard_is_case_insensitive() {
databaseHelper.addObject("""
person: DIGITALOCEAN NOC
nic-hdl: EH3832-RIPE
mnt-by: OWNER-MNT
source: TEST
created: 2022-08-14T11:48:28Z
last-modified: 2022-10-25T12:22:39Z
""");

databaseHelper.addObject("""
person: DigitalOcean Inc
nic-hdl: DI2361-RIPE
mnt-by: OWNER-MNT
source: TEST
created: 2022-08-14T11:48:28Z
last-modified: 2022-10-25T12:22:39Z
""");

databaseHelper.addObject("""
person: DigitalOcean Inc
nic-hdl: DI2362-RIPE
mnt-by: OWNER-MNT
source: TEST
created: 2022-08-14T11:48:28Z
last-modified: 2022-10-25T12:22:39Z
""");

databaseHelper.addObject("""
organisation: ORG-DOI2-RIPE
org-name: DigitalOcean, LLC
country: US
org-type: OTHER
mnt-by: OWNER-MNT
abuse-c: DI2362-RIPE
e-mail: [email protected]
notify: [email protected]
language: EN
source: TEST
created: 2022-08-14T11:48:28Z
last-modified: 2022-10-25T12:22:39Z
""");

databaseHelper.addObject("""
person: DigitalOcean Network Operations
nic-hdl: PT7353-RIPE
mnt-by: OWNER-MNT
source: TEST
created: 2022-08-14T11:48:28Z
last-modified: 2022-10-25T12:22:39Z
""");

rebuildIndex();


final SearchResult resultUppercase = createResource("entities?fn=DIGITALOCEAN*")
.request(MediaType.APPLICATION_JSON_TYPE)
.get(SearchResult.class);

final SearchResult resultLowercase = createResource("entities?fn=digitalocean*")
.request(MediaType.APPLICATION_JSON_TYPE)
.get(SearchResult.class);

final SearchResult resultMix = createResource("entities?fn=DigItAlocEan*")
.request(MediaType.APPLICATION_JSON_TYPE)
.get(SearchResult.class);

assertThat(resultUppercase.getEntitySearchResults().size(), is(5));
assertThat(resultLowercase.getEntitySearchResults().size(), is(5));
assertThat(resultMix.getEntitySearchResults().size(), is(5));

assertThat(resultUppercase.getEntitySearchResults(), is(resultLowercase.getEntitySearchResults()));
assertThat(resultLowercase.getEntitySearchResults(), is(resultMix.getEntitySearchResults()));
}


// search - ips

@Test
Expand Down

0 comments on commit e86e482

Please sign in to comment.