import org.sonar.server.es.IndexDefinition;
import org.sonar.server.es.NewIndex;
-import static org.sonar.server.es.DefaultIndexSettingsElement.FUZZY_ANALYZER;
import static org.sonar.server.es.DefaultIndexSettingsElement.SEARCH_GRAMS_ANALYZER;
import static org.sonar.server.es.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
mapping.setAttribute("_routing", ImmutableMap.of("required", "true"));
mapping.stringFieldBuilder(FIELD_PROJECT_UUID).build();
mapping.stringFieldBuilder(FIELD_KEY).enable(SORTABLE_ANALYZER).build();
- mapping.stringFieldBuilder(FIELD_NAME).enable(SEARCH_GRAMS_ANALYZER, FUZZY_ANALYZER).build();
+ mapping.stringFieldBuilder(FIELD_NAME).enable(SEARCH_GRAMS_ANALYZER).build();
mapping.stringFieldBuilder(FIELD_QUALIFIER).build();
// do not store document but only indexation of information
package org.sonar.server.component.index;
import java.util.Arrays;
-import java.util.function.Function;
import java.util.stream.Stream;
import org.apache.commons.lang.StringUtils;
-import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.sonar.server.es.DefaultIndexSettings;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
-import static org.elasticsearch.index.query.QueryBuilders.prefixQuery;
import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_KEY;
import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME;
-import static org.sonar.server.es.DefaultIndexSettingsElement.FUZZY_ANALYZER;
import static org.sonar.server.es.DefaultIndexSettingsElement.SEARCH_GRAMS_ANALYZER;
import static org.sonar.server.es.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
@Override
public QueryBuilder getQuery(String queryText) {
BoolQueryBuilder query = boolQuery();
-
split(queryText)
.map(queryTerm -> {
return query;
}
},
- FUZZY {
- @Override
- public QueryBuilder getQuery(String queryText) {
- BoolQueryBuilder query = boolQuery();
-
- split(queryText)
- .map(((Function<String, QueryBuilder>) queryTerm -> matchQuery(FUZZY_ANALYZER.subField(FIELD_NAME), queryTerm).fuzziness(Fuzziness.AUTO))::apply)
- .forEach(query::must);
- return query;
- }
- },
- FUZZY_PREFIX {
- @Override
- public QueryBuilder getQuery(String queryText) {
- return prefixQuery(FUZZY_ANALYZER.subField(FIELD_NAME), queryText);
- }
- },
KEY {
@Override
public QueryBuilder getQuery(String queryText) {
set(PATTERN, "\\.");
}
},
- FUZZY_TOKENIZER(TOKENIZER) {
-
- @Override
- protected void setup() {
- set(TYPE, PATTERN);
- set(PATTERN, ComponentIndexSearchFeature.SEARCH_TERM_TOKENIZER_PATTERN);
- }
- },
// Analyzers
setArray(FILTER, TRIM);
}
},
- FUZZY_ANALYZER(ANALYZER) {
-
- @Override
- protected void setup() {
- set(TOKENIZER, FUZZY_TOKENIZER);
- setArray(FILTER, LOWERCASE);
- }
-
- @Override
- public SortedMap<String, String> fieldMapping() {
- return ImmutableSortedMap.of(
- TYPE, STRING,
- INDEX, ANALYZED,
- ANALYZER, getName());
- }
- },
;
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.component.index;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.db.component.ComponentDto;
-
-public class ComponentIndexFeatureFuzzyTest extends ComponentIndexTest {
-
- @Before
- public void before() {
- features.set(ComponentIndexSearchFeature.FUZZY, ComponentIndexSearchFeature.FUZZY_PREFIX);
- }
-
- @Test
- public void should_find_item_despite_missing_character() {
- ComponentDto project = indexProject("key-1", "SonarQube");
-
- assertSearchResults("SonrQube", project);
- }
-
- @Test
- public void should_find_item_despite_missing_character_and_lowercase() {
- ComponentDto project = indexProject("key-1", "SonarQube");
-
- assertSearchResults("sonrqube", project);
- }
-
- @Test
- public void should_find_item_despite_two_missing_characters_and_lowercase() {
- ComponentDto project = indexProject("key-1", "SonarQube");
-
- assertSearchResults("sonqube", project);
- }
-
- @Test
- public void missing_characters_should_reduce_score() {
- assertResultOrder("SonarQube.java",
- "sonarqube.java",
- "sonaqube.java",
- "sonqube.java");
- }
-
- @Test
- public void fuzziness_for_filename_without_suffix() {
- assertFileMatches("StringUtils",
- "StringUtils.java",
- "StringUils.java",
- "StringUls.java");
- }
-}
"Struts.java");
}
- @Test
- public void should_find_fuzzy_match() {
- features.set(ComponentIndexSearchFeature.FUZZY);
- assertResultOrder("StrutX ProjecX",
- "Struts Project");
- }
-
@Test
public void should_find_partial_match() {
features.set(ComponentIndexSearchFeature.PARTIAL);
"Struts");
}
- @Test
- public void should_require_all_words_to_match_for_fuzziness() {
- features.set(ComponentIndexSearchFeature.FUZZY);
- assertNoFileMatches("struts java",
- "Struts");
- }
-
@Test
public void should_require_all_words_to_match_for_partial() {
features.set(ComponentIndexSearchFeature.PARTIAL);
public class ComponentIndexScoreTest extends ComponentIndexTest {
- @Test
- public void should_order_results_by_score() {
- assertResultOrder("struts",
- "Struts",
- "Apache Struts",
- "Apache Struts Two");
- }
-
@Test
public void should_prefer_components_without_prefix() {
assertResultOrder("File.java",
assertExactResults("sonarqube", project2, project1);
}
-
- @Test
- public void should_prefer_prefix_to_partial() {
- assertResultOrder("corem",
- "CoreMetrics.java",
- "ScoreMatrix.java");
- }
}