List of usage examples for org.apache.lucene.search IndexSearcher setSimilarity
public void setSimilarity(Similarity similarity)
From source file:org.elasticsearch.index.shard.IndexSearcherWrapperTests.java
License:Apache License
public void testNoWrap() throws IOException { Directory dir = newDirectory();//from ww w . j ava 2s . c o m IndexWriterConfig iwc = newIndexWriterConfig(); IndexWriter writer = new IndexWriter(dir, iwc); Document doc = new Document(); doc.add(new StringField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO)); doc.add(new TextField("field", "doc", random().nextBoolean() ? Field.Store.YES : Field.Store.NO)); writer.addDocument(doc); DirectoryReader open = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(writer, true), new ShardId("foo", 1)); IndexSearcher searcher = new IndexSearcher(open); assertEquals(1, searcher.search(new TermQuery(new Term("field", "doc")), 1).totalHits); searcher.setSimilarity(iwc.getSimilarity()); IndexSearcherWrapper wrapper = new IndexSearcherWrapper() { @Override public DirectoryReader wrap(DirectoryReader reader) throws IOException { return reader; } @Override public IndexSearcher wrap(EngineConfig engineConfig, IndexSearcher searcher) throws EngineException { return searcher; } }; try (Engine.Searcher engineSearcher = new Engine.Searcher("foo", searcher)) { final Engine.Searcher wrap = new IndexSearcherWrappingService(Collections.singleton(wrapper)) .wrap(ENGINE_CONFIG, engineSearcher); assertSame(wrap, engineSearcher); } IOUtils.close(open, writer, dir); }
From source file:org.elasticsearch.index.shard.IndexSearcherWrapperTests.java
License:Apache License
public void testWrappedReaderMustDelegateCoreCacheKey() throws IOException { Directory dir = newDirectory();/* ww w. j ava 2s .c om*/ IndexWriterConfig iwc = newIndexWriterConfig(); IndexWriter writer = new IndexWriter(dir, iwc); Document doc = new Document(); doc.add(new StringField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO)); doc.add(new TextField("field", "doc", random().nextBoolean() ? Field.Store.YES : Field.Store.NO)); writer.addDocument(doc); DirectoryReader open = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(writer, true), new ShardId("foo", 1)); IndexSearcher searcher = new IndexSearcher(open); assertEquals(1, searcher.search(new TermQuery(new Term("field", "doc")), 1).totalHits); searcher.setSimilarity(iwc.getSimilarity()); IndexSearcherWrapper wrapper = new IndexSearcherWrapper() { @Override public DirectoryReader wrap(DirectoryReader reader) throws IOException { return new BrokenWrapper(reader, false); } @Override public IndexSearcher wrap(EngineConfig engineConfig, IndexSearcher searcher) throws EngineException { return searcher; } }; try (Engine.Searcher engineSearcher = new Engine.Searcher("foo", searcher)) { try { new IndexSearcherWrappingService(Collections.singleton(wrapper)).wrap(ENGINE_CONFIG, engineSearcher); fail("reader must delegate cache key"); } catch (IllegalStateException ex) { // all is well } } wrapper = new IndexSearcherWrapper() { @Override public DirectoryReader wrap(DirectoryReader reader) throws IOException { return new BrokenWrapper(reader, true); } @Override public IndexSearcher wrap(EngineConfig engineConfig, IndexSearcher searcher) throws EngineException { return searcher; } }; try (Engine.Searcher engineSearcher = new Engine.Searcher("foo", searcher)) { try { new IndexSearcherWrappingService(Collections.singleton(wrapper)).wrap(ENGINE_CONFIG, engineSearcher); fail("reader must delegate cache key"); } catch (IllegalStateException ex) { // all is well } } IOUtils.close(open, writer, dir); }
From source file:org.elasticsearch.index.similarity.ScriptedSimilarityTests.java
License:Apache License
public void testBasics() throws IOException { final AtomicBoolean called = new AtomicBoolean(); SimilarityScript.Factory scriptFactory = () -> { return new SimilarityScript() { @Override/* w w w. ja v a 2 s . c o m*/ public double execute(double weight, ScriptedSimilarity.Query query, ScriptedSimilarity.Field field, ScriptedSimilarity.Term term, ScriptedSimilarity.Doc doc) throws IOException { assertEquals(1, weight, 0); assertNotNull(doc); assertEquals(2f, doc.getFreq(), 0); assertEquals(3, doc.getLength(), 0); assertNotNull(field); assertEquals(3, field.getDocCount()); assertEquals(5, field.getSumDocFreq()); assertEquals(6, field.getSumTotalTermFreq()); assertNotNull(term); assertEquals(2, term.getDocFreq()); assertEquals(3, term.getTotalTermFreq()); assertNotNull(query); assertEquals(3.2f, query.getBoost(), 0); called.set(true); return 42f; } }; }; ScriptedSimilarity sim = new ScriptedSimilarity("foobar", null, "foobaz", scriptFactory, true); Directory dir = new RAMDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim)); Document doc = new Document(); doc.add(new TextField("f", "foo bar", Store.NO)); doc.add(new StringField("match", "no", Store.NO)); w.addDocument(doc); doc = new Document(); doc.add(new TextField("f", "foo foo bar", Store.NO)); doc.add(new StringField("match", "yes", Store.NO)); w.addDocument(doc); doc = new Document(); doc.add(new TextField("f", "bar", Store.NO)); doc.add(new StringField("match", "no", Store.NO)); w.addDocument(doc); IndexReader r = DirectoryReader.open(w); w.close(); IndexSearcher searcher = new IndexSearcher(r); searcher.setSimilarity(sim); Query query = new BoostQuery( new BooleanQuery.Builder().add(new TermQuery(new Term("f", "foo")), Occur.SHOULD) .add(new TermQuery(new Term("match", "yes")), Occur.FILTER).build(), 3.2f); TopDocs topDocs = searcher.search(query, 1); assertEquals(1, topDocs.totalHits); assertTrue(called.get()); assertEquals(42, topDocs.scoreDocs[0].score, 0); w.close(); dir.close(); }
From source file:org.elasticsearch.index.similarity.ScriptedSimilarityTests.java
License:Apache License
public void testInitScript() throws IOException { final AtomicBoolean initCalled = new AtomicBoolean(); SimilarityWeightScript.Factory weightScriptFactory = () -> { return new SimilarityWeightScript() { @Override/* www . jav a2s . co m*/ public double execute(ScriptedSimilarity.Query query, ScriptedSimilarity.Field field, ScriptedSimilarity.Term term) throws IOException { assertNotNull(field); assertEquals(3, field.getDocCount()); assertEquals(5, field.getSumDocFreq()); assertEquals(6, field.getSumTotalTermFreq()); assertNotNull(term); assertEquals(2, term.getDocFreq()); assertEquals(3, term.getTotalTermFreq()); assertNotNull(query); assertEquals(3.2f, query.getBoost(), 0); initCalled.set(true); return 28; } }; }; final AtomicBoolean called = new AtomicBoolean(); SimilarityScript.Factory scriptFactory = () -> { return new SimilarityScript() { @Override public double execute(double weight, ScriptedSimilarity.Query query, ScriptedSimilarity.Field field, ScriptedSimilarity.Term term, ScriptedSimilarity.Doc doc) throws IOException { assertEquals(28, weight, 0d); assertNotNull(doc); assertEquals(2f, doc.getFreq(), 0); assertEquals(3, doc.getLength(), 0); assertNotNull(field); assertEquals(3, field.getDocCount()); assertEquals(5, field.getSumDocFreq()); assertEquals(6, field.getSumTotalTermFreq()); assertNotNull(term); assertEquals(2, term.getDocFreq()); assertEquals(3, term.getTotalTermFreq()); assertNotNull(query); assertEquals(3.2f, query.getBoost(), 0); called.set(true); return 42; } }; }; ScriptedSimilarity sim = new ScriptedSimilarity("foobar", weightScriptFactory, "foobaz", scriptFactory, true); Directory dir = new RAMDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim)); Document doc = new Document(); doc.add(new TextField("f", "foo bar", Store.NO)); doc.add(new StringField("match", "no", Store.NO)); w.addDocument(doc); doc = new Document(); doc.add(new TextField("f", "foo foo bar", Store.NO)); doc.add(new StringField("match", "yes", Store.NO)); w.addDocument(doc); doc = new Document(); doc.add(new TextField("f", "bar", Store.NO)); doc.add(new StringField("match", "no", Store.NO)); w.addDocument(doc); IndexReader r = DirectoryReader.open(w); w.close(); IndexSearcher searcher = new IndexSearcher(r); searcher.setSimilarity(sim); Query query = new BoostQuery( new BooleanQuery.Builder().add(new TermQuery(new Term("f", "foo")), Occur.SHOULD) .add(new TermQuery(new Term("match", "yes")), Occur.FILTER).build(), 3.2f); TopDocs topDocs = searcher.search(query, 1); assertEquals(1, topDocs.totalHits); assertTrue(initCalled.get()); assertTrue(called.get()); assertEquals(42, topDocs.scoreDocs[0].score, 0); w.close(); dir.close(); }
From source file:org.elasticsearch.painless.SimilarityScriptTests.java
License:Apache License
public void testBasics() throws IOException { SimilarityScript.Factory factory = scriptEngine.compile("foobar", "return query.boost * doc.freq / doc.length", SimilarityScript.CONTEXT, Collections.emptyMap()); ScriptedSimilarity sim = new ScriptedSimilarity("foobar", null, "foobaz", factory::newInstance, true); Directory dir = new RAMDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim)); Document doc = new Document(); doc.add(new TextField("f", "foo bar", Store.NO)); doc.add(new StringField("match", "no", Store.NO)); w.addDocument(doc);//from ww w. ja v a 2s . c o m doc = new Document(); doc.add(new TextField("f", "foo foo bar", Store.NO)); doc.add(new StringField("match", "yes", Store.NO)); w.addDocument(doc); doc = new Document(); doc.add(new TextField("f", "bar", Store.NO)); doc.add(new StringField("match", "no", Store.NO)); w.addDocument(doc); IndexReader r = DirectoryReader.open(w); w.close(); IndexSearcher searcher = new IndexSearcher(r); searcher.setSimilarity(sim); Query query = new BoostQuery( new BooleanQuery.Builder().add(new TermQuery(new Term("f", "foo")), Occur.SHOULD) .add(new TermQuery(new Term("match", "yes")), Occur.FILTER).build(), 3.2f); TopDocs topDocs = searcher.search(query, 1); assertEquals(1, topDocs.totalHits); assertEquals((float) (3.2 * 2 / 3), topDocs.scoreDocs[0].score, 0); w.close(); dir.close(); }
From source file:org.elasticsearch.painless.SimilarityScriptTests.java
License:Apache License
public void testWeightScript() throws IOException { SimilarityWeightScript.Factory weightFactory = scriptEngine.compile("foobar", "return query.boost", SimilarityWeightScript.CONTEXT, Collections.emptyMap()); SimilarityScript.Factory factory = scriptEngine.compile("foobar", "return weight * doc.freq / doc.length", SimilarityScript.CONTEXT, Collections.emptyMap()); ScriptedSimilarity sim = new ScriptedSimilarity("foobar", weightFactory::newInstance, "foobaz", factory::newInstance, true); Directory dir = new RAMDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim)); Document doc = new Document(); doc.add(new TextField("f", "foo bar", Store.NO)); doc.add(new StringField("match", "no", Store.NO)); w.addDocument(doc);// w ww . java 2 s . co m doc = new Document(); doc.add(new TextField("f", "foo foo bar", Store.NO)); doc.add(new StringField("match", "yes", Store.NO)); w.addDocument(doc); doc = new Document(); doc.add(new TextField("f", "bar", Store.NO)); doc.add(new StringField("match", "no", Store.NO)); w.addDocument(doc); IndexReader r = DirectoryReader.open(w); w.close(); IndexSearcher searcher = new IndexSearcher(r); searcher.setSimilarity(sim); Query query = new BoostQuery( new BooleanQuery.Builder().add(new TermQuery(new Term("f", "foo")), Occur.SHOULD) .add(new TermQuery(new Term("match", "yes")), Occur.FILTER).build(), 3.2f); TopDocs topDocs = searcher.search(query, 1); assertEquals(1, topDocs.totalHits); assertEquals((float) (3.2 * 2 / 3), topDocs.scoreDocs[0].score, 0); w.close(); dir.close(); }
From source file:org.elasticsearch.xpack.core.security.authz.accesscontrol.SecurityIndexSearcherWrapper.java
License:Open Source License
@Override protected IndexSearcher wrap(IndexSearcher searcher) throws EngineException { if (licenseState.isSecurityEnabled() == false || licenseState.isDocumentAndFieldLevelSecurityAllowed() == false) { return searcher; }//from w w w . jav a 2 s . com final DirectoryReader directoryReader = (DirectoryReader) searcher.getIndexReader(); if (directoryReader instanceof DocumentSubsetDirectoryReader) { // The reasons why we return a custom searcher: // 1) in the case the role query is sparse then large part of the main query can be skipped // 2) If the role query doesn't match with any docs in a segment, that a segment can be skipped IndexSearcher indexSearcher = new IndexSearcherWrapper((DocumentSubsetDirectoryReader) directoryReader); indexSearcher.setQueryCache(indexSearcher.getQueryCache()); indexSearcher.setQueryCachingPolicy(indexSearcher.getQueryCachingPolicy()); indexSearcher.setSimilarity(indexSearcher.getSimilarity(true)); return indexSearcher; } return searcher; }
From source file:org.eu.bitzone.Leia.java
License:Apache License
/** * Pop up a modal dialog explaining the selected result. * * @param sTable Thinlet table widget containing selected search result. *//*w w w . j av a2 s . c om*/ public void explainResult(final Object sTable) { final Object row = getSelectedItem(sTable); if (row == null) { return; } final Integer docid = (Integer) getProperty(row, "docid"); if (docid == null) { return; } if (ir == null) { showStatus(MSG_NOINDEX); return; } final Query q = (Query) getProperty(sTable, "query"); if (q == null) { return; } final Thread t = new Thread() { @Override public void run() { try { final IndexSearcher is = new IndexSearcher(ir); final Similarity sim = createSimilarity(find("srchOptTabs")); is.setSimilarity(sim); final Explanation expl = is.explain(q, docid.intValue()); final Object dialog = addComponent(null, "/xml/explain.xml", null, null); final Object eTree = find(dialog, "eTree"); addNode(eTree, expl); // setBoolean(eTree, "expand", true); add(dialog); } catch (final Exception e) { e.printStackTrace(); errorMsg(e.getMessage()); } } }; if (slowAccess) { t.start(); } else { t.run(); } }
From source file:org.exoplatform.services.jcr.impl.core.query.lucene.SearchIndex.java
License:Apache License
/** * @see org.exoplatform.services.jcr.impl.core.query.QueryHandler#executeQuery(org.apache.lucene.search.Query, * boolean, org.exoplatform.services.jcr.datamodel.InternalQName[], * boolean[])//ww w . ja v a 2 s .c o m */ public QueryHits executeQuery(Query query) throws IOException { waitForResuming(); checkOpen(); workingThreads.incrementAndGet(); try { IndexReader reader = getIndexReader(true); IndexSearcher searcher = new IndexSearcher(reader); searcher.setSimilarity(getSimilarity()); return new LuceneQueryHits(reader, searcher, query, true); } finally { workingThreads.decrementAndGet(); if (isSuspended.get() && workingThreads.get() == 0) { synchronized (workingThreads) { workingThreads.notifyAll(); } } } }
From source file:org.getopt.luke.Luke.java
License:Apache License
/** * Pop up a modal dialog explaining the selected result. * @param sTable Thinlet table widget containing selected search result. *//*from w w w . j ava 2s . c o m*/ public void explainResult(Object sTable) { Object row = getSelectedItem(sTable); if (row == null) return; final Integer docid = (Integer) getProperty(row, "docid"); if (docid == null) return; if (ir == null) { showStatus(MSG_NOINDEX); return; } final Query q = (Query) getProperty(sTable, "query"); if (q == null) return; Thread t = new Thread() { public void run() { try { IndexSearcher is = new IndexSearcher(ir); Similarity sim = createSimilarity(find("srchOptTabs")); is.setSimilarity(sim); Explanation expl = is.explain(q, docid.intValue()); Object dialog = addComponent(null, "/xml/explain.xml", null, null); Object eTree = find(dialog, "eTree"); addNode(eTree, expl); //setBoolean(eTree, "expand", true); add(dialog); } catch (Exception e) { e.printStackTrace(); errorMsg(e.getMessage()); } } }; if (slowAccess) { t.start(); } else { t.run(); } }