From 475fa94d409da98d11a89929f48f309911fb8f8a Mon Sep 17 00:00:00 2001 From: Nicolas Rod <Nicolas.Rod@unige.ch> Date: Wed, 21 Aug 2024 16:59:19 +0200 Subject: [PATCH] feat(index): new WILDCARD SearchConditionType to allow searching on exact terms, but with the use of wilcard characters such as * and ? --- .../indexing/elasticsearch/ElasticsearchService.java | 11 +++++++++++ .../ch/unige/solidify/rest/SearchConditionType.java | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/solidify-index-controller/src/main/java/ch/unige/solidify/index/indexing/elasticsearch/ElasticsearchService.java b/solidify-index-controller/src/main/java/ch/unige/solidify/index/indexing/elasticsearch/ElasticsearchService.java index dd3d7fb73..dcad09ec8 100644 --- a/solidify-index-controller/src/main/java/ch/unige/solidify/index/indexing/elasticsearch/ElasticsearchService.java +++ b/solidify-index-controller/src/main/java/ch/unige/solidify/index/indexing/elasticsearch/ElasticsearchService.java @@ -268,6 +268,8 @@ public abstract class ElasticsearchService<T extends IndexMetadata> extends Inde builder = this.getMatchQueryBuilder(condition); } else if (condition.getType() == SearchConditionType.TERM) { builder = this.getTermQueryBuilder(condition); + } else if (condition.getType() == SearchConditionType.WILDCARD) { + builder = this.getWildcardQueryBuilder(condition); } else if (condition.getType() == SearchConditionType.RANGE) { builder = this.getRangeQueryBuilder(condition); } else if (condition.getType() == SearchConditionType.QUERY) { @@ -316,6 +318,15 @@ public abstract class ElasticsearchService<T extends IndexMetadata> extends Inde } } + private QueryBuilder getWildcardQueryBuilder(SearchCondition condition) { + if (!StringTool.isNullOrEmpty(condition.getField()) && !StringTool.isNullOrEmpty(condition.getValue())) { + // 'wildcard' query + return QueryBuilders.wildcardQuery(condition.getField(), condition.getValue()); + } else { + throw new SolidifyRuntimeException("Invalid 'wildcard' search condition"); + } + } + private QueryBuilder getRangeQueryBuilder(SearchCondition condition) { if (StringTool.isNullOrEmpty(condition.getLowerValue()) && StringTool.isNullOrEmpty(condition.getUpperValue())) { throw new SolidifyRuntimeException("Invalid 'range' search condition"); diff --git a/solidify-model/src/main/java/ch/unige/solidify/rest/SearchConditionType.java b/solidify-model/src/main/java/ch/unige/solidify/rest/SearchConditionType.java index ec6ffd5c2..d51f4cffc 100644 --- a/solidify-model/src/main/java/ch/unige/solidify/rest/SearchConditionType.java +++ b/solidify-model/src/main/java/ch/unige/solidify/rest/SearchConditionType.java @@ -9,12 +9,12 @@ * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-2.0.html>. @@ -26,6 +26,7 @@ package ch.unige.solidify.rest; public enum SearchConditionType { MATCH, // fulltext search TERM, // search on exact term + WILDCARD, // search on exact term, but allow the use of wildcard characters such as * and ? RANGE, // search values within a provided range QUERY, // search with query syntax SIMPLE_QUERY, // search with simple query syntax -- GitLab