public void testReallyNoNormsForDrillDown() throws Exception { Directory dir = newDirectory(); Directory taxoDir = newDirectory(); IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random())); iwc.setSimilarity(new PerFieldSimilarityWrapper() { final Similarity sim = new DefaultSimilarity(); @Override public Similarity get(String name) { assertEquals("field", name); return sim; } }); TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE); RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc); FacetsConfig config = new FacetsConfig(); Document doc = new Document(); doc.add(newTextField("field", "text", Field.Store.NO)); doc.add(new FacetField("a", "path")); writer.addDocument(config.build(taxoWriter, doc)); IOUtils.close(writer, taxoWriter, dir, taxoDir); }
@Override public void setUp() throws Exception { super.setUp(); directory = newDirectory(); PerFieldSimilarityWrapper sim = new ExampleSimilarityProvider(); IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random())).setSimilarity(sim); RandomIndexWriter iw = new RandomIndexWriter(random(), directory, iwc); Document doc = new Document(); Field field = newTextField("foo", "", Field.Store.NO); doc.add(field); Field field2 = newTextField("bar", "", Field.Store.NO); doc.add(field2); field.setStringValue("quick brown fox"); field2.setStringValue("quick brown fox"); iw.addDocument(doc); field.setStringValue("jumps over lazy brown dog"); field2.setStringValue("jumps over lazy brown dog"); iw.addDocument(doc); reader = iw.getReader(); iw.close(); searcher = newSearcher(reader); searcher.setSimilarity(sim); }
public void testReallyNoNormsForDrillDown() throws Exception { Directory dir = newDirectory(); Directory taxoDir = newDirectory(); IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())); iwc.setSimilarity(new PerFieldSimilarityWrapper() { final Similarity sim = new DefaultSimilarity(); @Override public Similarity get(String name) { assertEquals("field", name); return sim; } }); RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc); TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE); FacetFields facetFields = new FacetFields(taxoWriter); Document doc = new Document(); doc.add(newTextField("field", "text", Field.Store.NO)); facetFields.addFields(doc, Collections.singletonList(new CategoryPath("a/path", '/'))); writer.addDocument(doc); writer.close(); taxoWriter.close(); dir.close(); taxoDir.close(); }
@Override public void setUp() throws Exception { super.setUp(); directory = newDirectory(); PerFieldSimilarityWrapper sim = new ExampleSimilarityProvider(); IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setSimilarity(sim); RandomIndexWriter iw = new RandomIndexWriter(random(), directory, iwc); Document doc = new Document(); Field field = newTextField("foo", "", Field.Store.NO); doc.add(field); Field field2 = newTextField("bar", "", Field.Store.NO); doc.add(field2); field.setStringValue("quick brown fox"); field2.setStringValue("quick brown fox"); iw.addDocument(doc); field.setStringValue("jumps over lazy brown dog"); field2.setStringValue("jumps over lazy brown dog"); iw.addDocument(doc); reader = iw.getReader(); iw.close(); searcher = newSearcher(reader); searcher.setSimilarity(sim); }
public void testNonDefaultSimilarity() throws Exception { QueryShardContext shardContext = createShardContext(); HasChildQueryBuilder hasChildQueryBuilder = QueryBuilders.hasChildQuery(CHILD_TYPE, new TermQueryBuilder("custom_string", "value"), ScoreMode.None); HasChildQueryBuilder.LateParsingQuery query = (HasChildQueryBuilder.LateParsingQuery) hasChildQueryBuilder.toQuery(shardContext); Similarity expected = SimilarityService.BUILT_IN.get(similarity) .apply(similarity, Settings.EMPTY, Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build()) .get(); assertThat(((PerFieldSimilarityWrapper) query.getSimilarity()).get("custom_string"), instanceOf(expected.getClass())); }
static TFIDFSimilarity asTFIDF(Similarity sim, String field) { while (sim instanceof PerFieldSimilarityWrapper) { sim = ((PerFieldSimilarityWrapper)sim).get(field); } if (sim instanceof TFIDFSimilarity) { return (TFIDFSimilarity)sim; } else { return null; } }
/** returns the similarity in use for the field */ protected Similarity getSimilarity(String field) { SolrCore core = h.getCore(); RefCounted<SolrIndexSearcher> searcher = core.getSearcher(); Similarity sim = searcher.get().getSimilarity(); searcher.decref(); while (sim instanceof PerFieldSimilarityWrapper) { sim = ((PerFieldSimilarityWrapper)sim).get(field); } return sim; }
private PerFieldSimilarityWrapper getSimilarity(final QueryWithFilters queryWithFilters) { return new PerFieldSimilarityWrapper() { @Override public Similarity get(String name) { AnalyzerSettings analyzerSettings = indexConfig.getAnalyzerSettingsForIndexField(name); AnalyzerSettings.Similarity similarity = AnalyzerSettings.Similarity.BM25; if (analyzerSettings != null) { similarity = analyzerSettings.getSimilarity(); } AnalyzerSettings.Similarity fieldSimilarityOverride = queryWithFilters.getFieldSimilarityOverride(name); if (fieldSimilarityOverride != null) { similarity = fieldSimilarityOverride; } if (AnalyzerSettings.Similarity.TFIDF.equals(similarity)) { return new ClassicSimilarity(); } else if (AnalyzerSettings.Similarity.BM25.equals(similarity)) { return new BM25Similarity(); } else if (AnalyzerSettings.Similarity.CONSTANT.equals(similarity)) { return new ConstantSimilarity(); } else if (AnalyzerSettings.Similarity.TF.equals(similarity)) { return new TFSimilarity(); } else { throw new RuntimeException("Unknown similarity type <" + similarity + ">"); } } }; }