ElasticsearchIndexer implements Indexer { static boolean hasLuceneOperators(String query) throws ParseException { String sanitizedQueryForLucene = query.replaceAll("\\^(?!\\d)", "\\^1"); org.apache.lucene.queryparser.classic.QueryParser parser = new org.apache.lucene.queryparser.classic.QueryParser("", new StandardAnalyzer(new CharArraySet(0, false))); parser.setAllowLeadingWildcard(true); return hasOperator(parser.parse(sanitizedQueryForLucene)); } @Inject  ElasticsearchIndexer(final RestHighLevelClient esClient, final PropertiesProvider propertiesProvider); @Override void close(); @Override boolean bulkAdd(final String indexName, Pipeline.Type nerType, List<NamedEntity> namedEntities, Document parent); @Override boolean bulkUpdate(String indexName, List<? extends Entity> entities); @Override void add(final String indexName, T obj); @Override void update(String indexName, T obj); @Override String executeRaw(String method, String url, String rawJson); T get(String indexName, String id); @Override T get(String indexName, String id, String root); @Override boolean tag(Project prj, String documentId, String rootDocument, Tag... tags); @Override boolean untag(Project prj, String documentId, String rootDocument, Tag... tags); @Override boolean tag(Project prj, List<String> documentIds, Tag... tags); @Override boolean untag(Project prj, List<String> documentIds, Tag... tags); @Override Searcher search(final String indexName, Class<? extends Entity> entityClass); @Override boolean createIndex(final String indexName); @Override boolean deleteAll(String indexName); ElasticsearchIndexer withRefresh(WriteRequest.RefreshPolicy refresh); @Override boolean getHealth(); final RestHighLevelClient client; }
@Test public void test_query_with_lucene_reserved_chars() throws Exception { assertThat(hasLuceneOperators("a b")).isFalse(); assertThat(hasLuceneOperators(" a b ")).isFalse(); assertThat(hasLuceneOperators("a\\/b")).isFalse(); assertThat(hasLuceneOperators("a & b")).isFalse(); assertThat(hasLuceneOperators("a + b")).isFalse(); assertThat(hasLuceneOperators("a - b")).isFalse(); assertThat(hasLuceneOperators("a | b")).isFalse(); assertThat(hasLuceneOperators("a \\\"b c\\\"")).isFalse(); assertThat(hasLuceneOperators("a b, c. de f.g")).isFalse(); assertThat(hasLuceneOperators("a || b")).isFalse(); assertThat(hasLuceneOperators("a OR b")).isFalse(); assertThat(hasLuceneOperators("a /b/")).isTrue(); assertThat(hasLuceneOperators("f:a")).isTrue(); assertThat(hasLuceneOperators("a f:b")).isTrue(); assertThat(hasLuceneOperators("n:[1 TO 5]")).isTrue(); assertThat(hasLuceneOperators("n:>10")).isTrue(); assertThat(hasLuceneOperators("*a*")).isTrue(); assertThat(hasLuceneOperators("?a?")).isTrue(); assertThat(hasLuceneOperators("+a -b")).isTrue(); assertThat(hasLuceneOperators("a \"b c\"")).isTrue(); assertThat(hasLuceneOperators("a && b")).isTrue(); assertThat(hasLuceneOperators("\"a || b\"")).isTrue(); assertThat(hasLuceneOperators("a AND b")).isTrue(); assertThat(hasLuceneOperators("a NOT b")).isTrue(); assertThat(hasLuceneOperators("a^")).isTrue(); assertThat(hasLuceneOperators("a^2")).isTrue(); assertThat(hasLuceneOperators("a~")).isTrue(); assertThat(hasLuceneOperators("a~2")).isTrue(); }