import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
public static final String FACET_STATUSES = "statuses";
public static final String FACET_TYPES = "types";
public static final String FACET_OLD_DEFAULT = "true";
+ private static Pattern SPECIAL_REGEX_CHARS = Pattern.compile("[{}()\\[\\].+*?^$\\\\|]");
public static final List<String> ALL_STATUSES_EXCEPT_REMOVED = ImmutableList.copyOf(
Collections2.filter(Collections2.transform(Arrays.asList(RuleStatus.values()), RuleStatusToString.INSTANCE), NotRemoved.INSTANCE));
qb.should(simpleQueryStringQuery(query.getQueryText())
.field(FIELD_RULE_NAME + "." + SEARCH_WORDS_SUFFIX, 20f)
.field(FIELD_RULE_HTML_DESCRIPTION, 3f)
- .defaultOperator(SimpleQueryStringBuilder.Operator.AND)
- ).boost(20f);
+ .defaultOperator(SimpleQueryStringBuilder.Operator.AND)).boost(20f);
// Match and partial Match queries
// Search by key uses the "sortable" sub-field as it requires to be case-insensitive (lower-case filtering)
.size(size)
.minDocCount(1);
if (query != null) {
- termsAggregation.include(".*" + query + ".*");
+ termsAggregation.include(".*" + escapeSpecialRegexChars(query) + ".*");
}
SearchRequestBuilder request = getClient()
.prepareSearch(INDEX)
return terms;
}
+ private static String escapeSpecialRegexChars(String str) {
+ return SPECIAL_REGEX_CHARS.matcher(str).replaceAll("\\\\$0");
+ }
+
private enum ToRuleKey implements Function<String, RuleKey> {
INSTANCE;
@Test
public void listTags_returns_tags_filtered_by_name() {
- insertRule(RuleKey.of("javascript", "S001"), newHashSet("tag1"), newHashSet("sys1", "sys2"));
+ insertRule(RuleKey.of("javascript", "S001"), newHashSet("tag1", "misra++"), newHashSet("sys1", "sys2"));
insertRule(RuleKey.of("java", "S001"), newHashSet("tag2"), newHashSet());
assertThat(service.listTags("missing", 10)).isEmpty();
+ assertThat(service.listTags("", 10)).containsOnly("tag1", "misra++", "tag2", "sys1", "sys2");
assertThat(service.listTags("tag", 10)).containsOnly("tag1", "tag2");
assertThat(service.listTags("sys", 10)).containsOnly("sys1", "sys2");
+ assertThat(service.listTags("misra", 10)).containsOnly("misra++");
+ assertThat(service.listTags("misra+", 10)).containsOnly("misra++");
+ assertThat(service.listTags("++", 10)).containsOnly("misra++");
// LIMITATION: case sensitive
assertThat(service.listTags("TAG", 10)).isEmpty();
assertThat(service.listTags("TAg", 10)).isEmpty();
assertThat(service.listTags("MISSing", 10)).isEmpty();
+
+ assertThat(service.listTags("misra-", 10)).isEmpty();
+ }
+
+ @Test
+ public void listTags_returns_empty_results_if_filter_contains_regexp_special_characters() {
+ insertRule(RuleKey.of("javascript", "S001"), newHashSet("misra++"), newHashSet("sys1", "sys2"));
+
+ assertThat(service.listTags("mis[", 10)).isEmpty();
+ assertThat(service.listTags("mis\\d", 10)).isEmpty();
+ assertThat(service.listTags(".*", 10)).isEmpty();
}
@Test