3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.es.textsearch;
22 import java.util.List;
23 import java.util.Locale;
25 import java.util.concurrent.atomic.AtomicBoolean;
26 import java.util.stream.Stream;
27 import org.apache.commons.lang.StringUtils;
28 import org.elasticsearch.index.query.BoolQueryBuilder;
29 import org.elasticsearch.index.query.MatchQueryBuilder;
30 import org.elasticsearch.index.query.QueryBuilder;
31 import org.sonar.server.es.newindex.DefaultIndexSettings;
32 import org.sonar.server.es.newindex.DefaultIndexSettingsElement;
33 import org.sonar.server.es.textsearch.ComponentTextSearchQueryFactory.ComponentTextSearchQuery;
35 import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
36 import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
37 import static org.elasticsearch.index.query.QueryBuilders.termsQuery;
38 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_GRAMS_ANALYZER;
39 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_PREFIX_ANALYZER;
40 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SEARCH_PREFIX_CASE_INSENSITIVE_ANALYZER;
41 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
42 import static org.sonar.server.es.textsearch.ComponentTextSearchFeature.UseCase.CHANGE_ORDER_OF_RESULTS;
43 import static org.sonar.server.es.textsearch.ComponentTextSearchFeature.UseCase.GENERATE_RESULTS;
45 public enum ComponentTextSearchFeatureRepertoire implements ComponentTextSearchFeature {
47 EXACT_IGNORE_CASE(CHANGE_ORDER_OF_RESULTS) {
49 public QueryBuilder getQuery(ComponentTextSearchQuery query) {
50 return matchQuery(SORTABLE_ANALYZER.subField(query.getFieldName()), query.getQueryText())
54 PREFIX(CHANGE_ORDER_OF_RESULTS) {
56 public Stream<QueryBuilder> getQueries(ComponentTextSearchQuery query) {
57 List<String> tokens = query.getQueryTextTokens();
58 if (tokens.isEmpty()) {
59 return Stream.empty();
61 BoolQueryBuilder queryBuilder = prefixAndPartialQuery(tokens, query.getFieldName(), SEARCH_PREFIX_ANALYZER)
63 return Stream.of(queryBuilder);
66 PREFIX_IGNORE_CASE(GENERATE_RESULTS) {
68 public Stream<QueryBuilder> getQueries(ComponentTextSearchQuery query) {
69 List<String> tokens = query.getQueryTextTokens();
70 if (tokens.isEmpty()) {
71 return Stream.empty();
73 List<String> lowerCaseTokens = tokens.stream().map(t -> t.toLowerCase(Locale.ENGLISH)).toList();
74 BoolQueryBuilder queryBuilder = prefixAndPartialQuery(lowerCaseTokens, query.getFieldName(), SEARCH_PREFIX_CASE_INSENSITIVE_ANALYZER)
76 return Stream.of(queryBuilder);
79 PARTIAL(GENERATE_RESULTS) {
81 public Stream<QueryBuilder> getQueries(ComponentTextSearchQuery query) {
82 List<String> tokens = query.getQueryTextTokens();
83 if (tokens.isEmpty()) {
84 return Stream.empty();
86 BoolQueryBuilder queryBuilder = boolQuery().boost(0.5F);
88 .map(text -> tokenQuery(text, query.getFieldName(), SEARCH_GRAMS_ANALYZER))
89 .forEach(queryBuilder::must);
90 return Stream.of(queryBuilder);
93 KEY(GENERATE_RESULTS) {
95 public QueryBuilder getQuery(ComponentTextSearchQuery query) {
96 return matchQuery(SORTABLE_ANALYZER.subField(query.getFieldKey()), query.getQueryText())
100 RECENTLY_BROWSED(CHANGE_ORDER_OF_RESULTS) {
102 public Stream<QueryBuilder> getQueries(ComponentTextSearchQuery query) {
103 Set<String> recentlyBrowsedKeys = query.getRecentlyBrowsedKeys();
104 if (recentlyBrowsedKeys.isEmpty()) {
105 return Stream.empty();
107 return Stream.of(termsQuery(query.getFieldKey(), recentlyBrowsedKeys).boost(100F));
110 FAVORITE(CHANGE_ORDER_OF_RESULTS) {
112 public Stream<QueryBuilder> getQueries(ComponentTextSearchQuery query) {
113 Set<String> favoriteKeys = query.getFavoriteKeys();
114 if (favoriteKeys.isEmpty()) {
115 return Stream.empty();
117 return Stream.of(termsQuery(query.getFieldKey(), favoriteKeys).boost(1000F));
121 private final UseCase useCase;
123 ComponentTextSearchFeatureRepertoire(UseCase useCase) {
124 this.useCase = useCase;
128 public QueryBuilder getQuery(ComponentTextSearchQuery query) {
129 throw new UnsupportedOperationException();
132 protected BoolQueryBuilder prefixAndPartialQuery(List<String> tokens, String originalFieldName, DefaultIndexSettingsElement analyzer) {
133 BoolQueryBuilder queryBuilder = boolQuery();
134 AtomicBoolean first = new AtomicBoolean(true);
138 if (first.getAndSet(false)) {
139 return tokenQuery(queryTerm, originalFieldName, analyzer);
142 return tokenQuery(queryTerm, originalFieldName, SEARCH_GRAMS_ANALYZER);
144 .forEach(queryBuilder::must);
148 protected MatchQueryBuilder tokenQuery(String queryTerm, String fieldName, DefaultIndexSettingsElement analyzer) {
149 // We will truncate the search to the maximum length of nGrams in the index.
150 // Otherwise the search would for sure not find any results.
151 String truncatedQuery = StringUtils.left(queryTerm, DefaultIndexSettings.MAXIMUM_NGRAM_LENGTH);
152 return matchQuery(analyzer.subField(fieldName), truncatedQuery);
156 public UseCase getUseCase() {