Search in sources :

Example 46 with SolrIndexSearcher

use of org.apache.solr.search.SolrIndexSearcher in project lucene-solr by apache.

the class ClassificationUpdateProcessorIntegrationTest method getDoc.

private Document getDoc(String id) throws IOException {
    try (SolrQueryRequest req = req()) {
        SolrIndexSearcher searcher = req.getSearcher();
        TermQuery query = new TermQuery(new Term(ID, id));
        TopDocs doc1 = searcher.search(query, 1);
        ScoreDoc scoreDoc = doc1.scoreDocs[0];
        return searcher.doc(scoreDoc.doc);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Term(org.apache.lucene.index.Term) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 47 with SolrIndexSearcher

use of org.apache.solr.search.SolrIndexSearcher in project lucene-solr by apache.

the class TestQuerySenderNoQuery method testSearcherEvents.

// Determine that when the query lists are commented out of both new and
// first searchers in the config, we don't throw an NPE
@Test
public void testSearcherEvents() throws Exception {
    SolrCore core = h.getCore();
    SolrEventListener newSearcherListener = core.newSearcherListeners.get(0);
    assertTrue("Not an instance of QuerySenderListener", newSearcherListener instanceof QuerySenderListener);
    QuerySenderListener qsl = (QuerySenderListener) newSearcherListener;
    RefCounted<SolrIndexSearcher> currentSearcherRef = core.getSearcher();
    SolrIndexSearcher currentSearcher = currentSearcherRef.get();
    SolrIndexSearcher dummy = null;
    //test first Searcher (since param is null)
    qsl.newSearcher(currentSearcher, dummy);
    MockQuerySenderListenerReqHandler mock = (MockQuerySenderListenerReqHandler) core.getRequestHandler("mock");
    assertNotNull("Mock is null", mock);
    assertNull("Req (firstsearcher) is not null", mock.req);
    SolrIndexSearcher newSearcher = new SolrIndexSearcher(core, core.getNewIndexDir(), core.getLatestSchema(), core.getSolrConfig().indexConfig, "testQuerySenderNoQuery", false, core.getDirectoryFactory());
    // get newSearcher.
    qsl.newSearcher(newSearcher, currentSearcher);
    assertNull("Req (newsearcher) is not null", mock.req);
    newSearcher.close();
    currentSearcherRef.decref();
}
Also used : SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Test(org.junit.Test)

Example 48 with SolrIndexSearcher

use of org.apache.solr.search.SolrIndexSearcher in project lucene-solr by apache.

the class WordBreakSolrSpellCheckerTest method testStandAlone.

@Test
public void testStandAlone() throws Exception {
    SolrCore core = h.getCore();
    WordBreakSolrSpellChecker checker = new WordBreakSolrSpellChecker();
    NamedList<String> params = new NamedList<>();
    params.add("field", "lowerfilt");
    params.add(WordBreakSolrSpellChecker.PARAM_BREAK_WORDS, "true");
    params.add(WordBreakSolrSpellChecker.PARAM_COMBINE_WORDS, "true");
    params.add(WordBreakSolrSpellChecker.PARAM_MAX_CHANGES, "10");
    checker.init(params, core);
    RefCounted<SolrIndexSearcher> searcher = core.getSearcher();
    QueryConverter qc = new SpellingQueryConverter();
    qc.setAnalyzer(new MockAnalyzer(random()));
    {
        //Prior to SOLR-8175, the required term would cause an AIOOBE.
        Collection<Token> tokens = qc.convert("+pine apple good ness");
        SpellingOptions spellOpts = new SpellingOptions(tokens, searcher.get().getIndexReader(), 10);
        SpellingResult result = checker.getSuggestions(spellOpts);
        searcher.decref();
        assertTrue(result != null && result.getSuggestions() != null);
        assertTrue(result.getSuggestions().size() == 5);
    }
    Collection<Token> tokens = qc.convert("paintable pine apple good ness");
    SpellingOptions spellOpts = new SpellingOptions(tokens, searcher.get().getIndexReader(), 10);
    SpellingResult result = checker.getSuggestions(spellOpts);
    searcher.decref();
    assertTrue(result != null && result.getSuggestions() != null);
    assertTrue(result.getSuggestions().size() == 9);
    for (Map.Entry<Token, LinkedHashMap<String, Integer>> s : result.getSuggestions().entrySet()) {
        Token orig = s.getKey();
        String[] corr = s.getValue().keySet().toArray(new String[0]);
        if (orig.toString().equals("paintable")) {
            assertTrue(orig.startOffset() == 0);
            assertTrue(orig.endOffset() == 9);
            assertTrue(orig.length() == 9);
            assertTrue(corr.length == 3);
            //1 op ; max doc freq=5
            assertTrue(corr[0].equals("paint able"));
            //1 op ; max doc freq=2      
            assertTrue(corr[1].equals("pain table"));
            //2 ops
            assertTrue(corr[2].equals("pa in table"));
        } else if (orig.toString().equals("pine apple")) {
            assertTrue(orig.startOffset() == 10);
            assertTrue(orig.endOffset() == 20);
            assertTrue(orig.length() == 10);
            assertTrue(corr.length == 1);
            assertTrue(corr[0].equals("pineapple"));
        } else if (orig.toString().equals("paintable pine")) {
            assertTrue(orig.startOffset() == 0);
            assertTrue(orig.endOffset() == 14);
            assertTrue(orig.length() == 14);
            assertTrue(corr.length == 1);
            assertTrue(corr[0].equals("paintablepine"));
        } else if (orig.toString().equals("good ness")) {
            assertTrue(orig.startOffset() == 21);
            assertTrue(orig.endOffset() == 30);
            assertTrue(orig.length() == 9);
            assertTrue(corr.length == 1);
            assertTrue(corr[0].equals("goodness"));
        } else if (orig.toString().equals("pine apple good ness")) {
            assertTrue(orig.startOffset() == 10);
            assertTrue(orig.endOffset() == 30);
            assertTrue(orig.length() == 20);
            assertTrue(corr.length == 1);
            assertTrue(corr[0].equals("pineapplegoodness"));
        } else if (orig.toString().equals("pine")) {
            assertTrue(orig.startOffset() == 10);
            assertTrue(orig.endOffset() == 14);
            assertTrue(orig.length() == 4);
            assertTrue(corr.length == 1);
            assertTrue(corr[0].equals("pi ne"));
        } else if (orig.toString().equals("pine")) {
            assertTrue(orig.startOffset() == 10);
            assertTrue(orig.endOffset() == 14);
            assertTrue(orig.length() == 4);
            assertTrue(corr.length == 1);
            assertTrue(corr[0].equals("pi ne"));
        } else if (orig.toString().equals("apple")) {
            assertTrue(orig.startOffset() == 15);
            assertTrue(orig.endOffset() == 20);
            assertTrue(orig.length() == 5);
            assertTrue(corr.length == 0);
        } else if (orig.toString().equals("good")) {
            assertTrue(orig.startOffset() == 21);
            assertTrue(orig.endOffset() == 25);
            assertTrue(orig.length() == 4);
            assertTrue(corr.length == 0);
        } else if (orig.toString().equals("ness")) {
            assertTrue(orig.startOffset() == 26);
            assertTrue(orig.endOffset() == 30);
            assertTrue(orig.length() == 4);
            assertTrue(corr.length == 0);
        } else {
            fail("Unexpected original result: " + orig);
        }
    }
}
Also used : SolrCore(org.apache.solr.core.SolrCore) NamedList(org.apache.solr.common.util.NamedList) Token(org.apache.lucene.analysis.Token) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) LinkedHashMap(java.util.LinkedHashMap) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 49 with SolrIndexSearcher

use of org.apache.solr.search.SolrIndexSearcher in project lucene-solr by apache.

the class UninvertDocValuesMergePolicyTest method withNewRawReader.

private static void withNewRawReader(TestHarness h, DirectoryReaderConsumer consumer) {
    try (SolrCore core = h.getCoreInc()) {
        final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
        final SolrIndexSearcher searcher = searcherRef.get();
        try {
            try {
                consumer.accept(searcher.getRawReader());
            } catch (Exception e) {
                fail(e.toString());
            }
        } finally {
            searcherRef.decref();
        }
    }
}
Also used : SolrCore(org.apache.solr.core.SolrCore) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher)

Example 50 with SolrIndexSearcher

use of org.apache.solr.search.SolrIndexSearcher in project lucene-solr by apache.

the class VersionInfo method getVersionFromIndex.

/**
   * Returns the latest version from the index, searched by the given id (bytes) as seen from the realtime searcher.
   * Returns null if no document can be found in the index for the given id.
   */
public Long getVersionFromIndex(BytesRef idBytes) {
    // TODO: we could cache much of this and invalidate during a commit.
    // TODO: most DocValues classes are threadsafe - expose which.
    RefCounted<SolrIndexSearcher> newestSearcher = ulog.uhandler.core.getRealtimeSearcher();
    try {
        SolrIndexSearcher searcher = newestSearcher.get();
        long lookup = searcher.lookupId(idBytes);
        // this means the doc doesn't exist in the index yet
        if (lookup < 0)
            return null;
        ValueSource vs = versionField.getType().getValueSource(versionField, null);
        Map context = ValueSource.newContext(searcher);
        vs.createWeight(context, searcher);
        FunctionValues fv = vs.getValues(context, searcher.getTopReaderContext().leaves().get((int) (lookup >> 32)));
        long ver = fv.longVal((int) lookup);
        return ver;
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading version from index", e);
    } finally {
        if (newestSearcher != null) {
            newestSearcher.decref();
        }
    }
}
Also used : ValueSource(org.apache.lucene.queries.function.ValueSource) FunctionValues(org.apache.lucene.queries.function.FunctionValues) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) IOException(java.io.IOException) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException)

Aggregations

SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)97 SolrException (org.apache.solr.common.SolrException)29 NamedList (org.apache.solr.common.util.NamedList)29 SolrCore (org.apache.solr.core.SolrCore)28 IOException (java.io.IOException)23 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 SolrParams (org.apache.solr.common.params.SolrParams)19 SchemaField (org.apache.solr.schema.SchemaField)18 IndexReader (org.apache.lucene.index.IndexReader)13 DocList (org.apache.solr.search.DocList)13 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)12 Map (java.util.Map)11 Document (org.apache.lucene.document.Document)11 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)11 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)11 IndexSchema (org.apache.solr.schema.IndexSchema)11 LeafReader (org.apache.lucene.index.LeafReader)10 Query (org.apache.lucene.search.Query)10 HashMap (java.util.HashMap)9