Search in sources :

Example 1 with NonNegativeScoresSimilarity

use of org.opensearch.index.similarity.NonNegativeScoresSimilarity in project OpenSearch by opensearch-project.

the class IndexModuleTests method testAddSimilarity.

public void testAddSimilarity() throws IOException {
    Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put("index.similarity.my_similarity.type", "test_similarity").put("index.similarity.my_similarity.key", "there is a key").put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings);
    IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry);
    module.addSimilarity("test_similarity", (providerSettings, indexCreatedVersion, scriptService) -> new TestSimilarity(providerSettings.get("key")));
    IndexService indexService = newIndexService(module);
    SimilarityService similarityService = indexService.similarityService();
    Similarity similarity = similarityService.getSimilarity("my_similarity").get();
    assertNotNull(similarity);
    assertThat(similarity, Matchers.instanceOf(NonNegativeScoresSimilarity.class));
    similarity = ((NonNegativeScoresSimilarity) similarity).getDelegate();
    assertThat(similarity, Matchers.instanceOf(TestSimilarity.class));
    assertEquals("my_similarity", similarityService.getSimilarity("my_similarity").name());
    assertEquals("there is a key", ((TestSimilarity) similarity).key);
    indexService.close("simon says", false);
}
Also used : BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) Similarity(org.apache.lucene.search.similarities.Similarity) NonNegativeScoresSimilarity(org.opensearch.index.similarity.NonNegativeScoresSimilarity) SimilarityService(org.opensearch.index.similarity.SimilarityService) Settings(org.opensearch.common.settings.Settings) NonNegativeScoresSimilarity(org.opensearch.index.similarity.NonNegativeScoresSimilarity)

Example 2 with NonNegativeScoresSimilarity

use of org.opensearch.index.similarity.NonNegativeScoresSimilarity in project OpenSearch by opensearch-project.

the class IndicesServiceTests method testStandAloneMapperServiceWithPlugins.

/**
 * Tests that teh {@link MapperService} created by {@link IndicesService#createIndexMapperService(IndexMetadata)} contains
 * custom types and similarities registered by plugins
 */
public void testStandAloneMapperServiceWithPlugins() throws IOException {
    final String indexName = "test";
    final Index index = new Index(indexName, UUIDs.randomBase64UUID());
    final IndicesService indicesService = getIndicesService();
    final Settings idxSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()).put(IndexModule.SIMILARITY_SETTINGS_PREFIX + ".test.type", "fake-similarity").build();
    final IndexMetadata indexMetadata = new IndexMetadata.Builder(index.getName()).settings(idxSettings).numberOfShards(1).numberOfReplicas(0).build();
    MapperService mapperService = indicesService.createIndexMapperService(indexMetadata);
    assertNotNull(mapperService.documentMapperParser().parserContext().typeParser("fake-mapper"));
    Similarity sim = mapperService.documentMapperParser().parserContext().getSimilarity("test").get();
    assertThat(sim, instanceOf(NonNegativeScoresSimilarity.class));
    sim = ((NonNegativeScoresSimilarity) sim).getDelegate();
    assertThat(sim, instanceOf(BM25Similarity.class));
}
Also used : BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) Similarity(org.apache.lucene.search.similarities.Similarity) NonNegativeScoresSimilarity(org.opensearch.index.similarity.NonNegativeScoresSimilarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) Index(org.opensearch.index.Index) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) MapperService(org.opensearch.index.mapper.MapperService) NonNegativeScoresSimilarity(org.opensearch.index.similarity.NonNegativeScoresSimilarity)

Aggregations

BM25Similarity (org.apache.lucene.search.similarities.BM25Similarity)2 Similarity (org.apache.lucene.search.similarities.Similarity)2 Settings (org.opensearch.common.settings.Settings)2 NonNegativeScoresSimilarity (org.opensearch.index.similarity.NonNegativeScoresSimilarity)2 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.hasToString (org.hamcrest.Matchers.hasToString)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 Index (org.opensearch.index.Index)1 IndexSettings (org.opensearch.index.IndexSettings)1 MapperService (org.opensearch.index.mapper.MapperService)1 SimilarityService (org.opensearch.index.similarity.SimilarityService)1