Search in sources :

Example 41 with SolrIndexSearcher

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

the class IndexBasedSpellCheckerTest method testExtendedResults.

@Test
public void testExtendedResults() throws Exception {
    IndexBasedSpellChecker checker = new IndexBasedSpellChecker();
    NamedList spellchecker = new NamedList();
    spellchecker.add("classname", IndexBasedSpellChecker.class.getName());
    File indexDir = createTempDir().toFile();
    indexDir.mkdirs();
    spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath());
    spellchecker.add(AbstractLuceneSpellChecker.FIELD, "title");
    spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker);
    SolrCore core = h.getCore();
    String dictName = checker.init(spellchecker, core);
    assertTrue(dictName + " is not equal to " + SolrSpellChecker.DEFAULT_DICTIONARY_NAME, dictName.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME) == true);
    RefCounted<SolrIndexSearcher> holder = core.getSearcher();
    SolrIndexSearcher searcher = holder.get();
    try {
        checker.build(core, searcher);
        IndexReader reader = searcher.getIndexReader();
        Collection<Token> tokens = queryConverter.convert("documemt");
        SpellingOptions spellOpts = new SpellingOptions(tokens, reader, 1, SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX, true, 0.5f, null);
        SpellingResult result = checker.getSuggestions(spellOpts);
        assertTrue("result is null and it shouldn't be", result != null);
        //should be lowercased, b/c we are using a lowercasing analyzer
        Map<String, Integer> suggestions = result.get(spellOpts.tokens.iterator().next());
        assertTrue("documemt is null and it shouldn't be", suggestions != null);
        assertTrue("documemt Size: " + suggestions.size() + " is not: " + 1, suggestions.size() == 1);
        Map.Entry<String, Integer> entry = suggestions.entrySet().iterator().next();
        assertTrue(entry.getKey() + " is not equal to " + "document", entry.getKey().equals("document") == true);
        assertTrue(entry.getValue() + " does not equal: " + 2, entry.getValue() == 2);
        //test something not in the spell checker
        spellOpts.tokens = queryConverter.convert("super");
        result = checker.getSuggestions(spellOpts);
        assertTrue("result is null and it shouldn't be", result != null);
        suggestions = result.get(spellOpts.tokens.iterator().next());
        assertTrue("suggestions size should be 0", suggestions.size() == 0);
        spellOpts.tokens = queryConverter.convert("document");
        result = checker.getSuggestions(spellOpts);
        assertTrue("result is null and it shouldn't be", result != null);
        suggestions = result.get(spellOpts.tokens.iterator().next());
        assertTrue("suggestions is not null and it should be", suggestions == null);
    } finally {
        holder.decref();
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrCore(org.apache.solr.core.SolrCore) Token(org.apache.lucene.analysis.Token) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) IndexReader(org.apache.lucene.index.IndexReader) File(java.io.File) Map(java.util.Map) Test(org.junit.Test)

Example 42 with SolrIndexSearcher

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

the class IndexBasedSpellCheckerTest method testAlternateDistance.

@Test
public void testAlternateDistance() throws Exception {
    TestSpellChecker checker = new TestSpellChecker();
    NamedList spellchecker = new NamedList();
    spellchecker.add("classname", IndexBasedSpellChecker.class.getName());
    File indexDir = createTempDir().toFile();
    spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath());
    spellchecker.add(AbstractLuceneSpellChecker.FIELD, "title");
    spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker);
    spellchecker.add(AbstractLuceneSpellChecker.STRING_DISTANCE, JaroWinklerDistance.class.getName());
    SolrCore core = h.getCore();
    String dictName = checker.init(spellchecker, core);
    assertTrue(dictName + " is not equal to " + SolrSpellChecker.DEFAULT_DICTIONARY_NAME, dictName.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME) == true);
    RefCounted<SolrIndexSearcher> holder = core.getSearcher();
    SolrIndexSearcher searcher = holder.get();
    try {
        checker.build(core, searcher);
        SpellChecker sc = checker.getSpellChecker();
        assertTrue("sc is null and it shouldn't be", sc != null);
        StringDistance sd = sc.getStringDistance();
        assertTrue("sd is null and it shouldn't be", sd != null);
        assertTrue("sd is not an instance of " + JaroWinklerDistance.class.getName(), sd instanceof JaroWinklerDistance);
    } finally {
        holder.decref();
    }
}
Also used : JaroWinklerDistance(org.apache.lucene.search.spell.JaroWinklerDistance) StringDistance(org.apache.lucene.search.spell.StringDistance) NamedList(org.apache.solr.common.util.NamedList) SolrCore(org.apache.solr.core.SolrCore) SpellChecker(org.apache.lucene.search.spell.SpellChecker) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) File(java.io.File) Test(org.junit.Test)

Example 43 with SolrIndexSearcher

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

the class AbstractFullDistribZkTestBase method waitForReplicationFromReplicas.

protected void waitForReplicationFromReplicas(String collectionName, ZkStateReader zkStateReader, TimeOut timeout) throws KeeperException, InterruptedException, IOException {
    zkStateReader.forceUpdateCollection(collectionName);
    DocCollection collection = zkStateReader.getClusterState().getCollection(collectionName);
    Map<String, CoreContainer> containers = new HashMap<>();
    for (JettySolrRunner runner : jettys) {
        if (!runner.isRunning()) {
            continue;
        }
        containers.put(runner.getNodeName(), runner.getCoreContainer());
    }
    for (Slice s : collection.getSlices()) {
        Replica leader = s.getLeader();
        long leaderIndexVersion = -1;
        while (!timeout.hasTimedOut()) {
            leaderIndexVersion = getIndexVersion(leader);
            if (leaderIndexVersion >= 0) {
                break;
            }
            Thread.sleep(1000);
        }
        if (timeout.hasTimedOut()) {
            fail("Unable to get leader indexVersion");
        }
        for (Replica pullReplica : s.getReplicas(EnumSet.of(Replica.Type.PULL, Replica.Type.TLOG))) {
            if (!zkStateReader.getClusterState().liveNodesContain(pullReplica.getNodeName())) {
                continue;
            }
            while (true) {
                long replicaIndexVersion = getIndexVersion(pullReplica);
                if (leaderIndexVersion == replicaIndexVersion) {
                    log.debug("Leader replica's version ({}) in sync with replica({}): {} == {}", leader.getName(), pullReplica.getName(), leaderIndexVersion, replicaIndexVersion);
                    // Make sure the host is serving the correct version
                    try (SolrCore core = containers.get(pullReplica.getNodeName()).getCore(pullReplica.getCoreName())) {
                        RefCounted<SolrIndexSearcher> ref = core.getRegisteredSearcher();
                        try {
                            SolrIndexSearcher searcher = ref.get();
                            String servingVersion = searcher.getIndexReader().getIndexCommit().getUserData().get(SolrIndexWriter.COMMIT_TIME_MSEC_KEY);
                            if (Long.parseLong(servingVersion) == replicaIndexVersion) {
                                break;
                            } else {
                                log.debug("Replica {} has the correct version replicated, but the searcher is not ready yet. Replicated version: {}, Serving version: {}", pullReplica.getName(), replicaIndexVersion, servingVersion);
                            }
                        } finally {
                            if (ref != null)
                                ref.decref();
                        }
                    }
                } else {
                    if (timeout.hasTimedOut()) {
                        logReplicaTypesReplicationInfo(collectionName, zkStateReader);
                        fail(String.format(Locale.ROOT, "Timed out waiting for replica %s (%d) to replicate from leader %s (%d)", pullReplica.getName(), replicaIndexVersion, leader.getName(), leaderIndexVersion));
                    }
                    if (leaderIndexVersion > replicaIndexVersion) {
                        log.debug("{} version is {} and leader's is {}, will wait for replication", pullReplica.getName(), replicaIndexVersion, leaderIndexVersion);
                    } else {
                        log.debug("Leader replica's version ({}) is lower than pull replica({}): {} < {}", leader.getName(), pullReplica.getName(), leaderIndexVersion, replicaIndexVersion);
                    }
                }
                Thread.sleep(1000);
            }
        }
    }
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) HashMap(java.util.HashMap) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) Slice(org.apache.solr.common.cloud.Slice) SolrCore(org.apache.solr.core.SolrCore) DocCollection(org.apache.solr.common.cloud.DocCollection) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Replica(org.apache.solr.common.cloud.Replica)

Example 44 with SolrIndexSearcher

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

the class TestCodecSupport method assertCompressionMode.

protected void assertCompressionMode(String expectedModeString, SolrCore core) throws IOException {
    RefCounted<SolrIndexSearcher> ref = null;
    SolrIndexSearcher searcher = null;
    try {
        ref = core.getSearcher();
        searcher = ref.get();
        SegmentInfos infos = SegmentInfos.readLatestCommit(searcher.getIndexReader().directory());
        SegmentInfo info = infos.info(infos.size() - 1).info;
        assertEquals("Expecting compression mode string to be " + expectedModeString + " but got: " + info.getAttribute(Lucene50StoredFieldsFormat.MODE_KEY) + "\n SegmentInfo: " + info + "\n SegmentInfos: " + infos + "\n Codec: " + core.getCodec(), expectedModeString, info.getAttribute(Lucene50StoredFieldsFormat.MODE_KEY));
    } finally {
        if (ref != null)
            ref.decref();
    }
}
Also used : SegmentInfos(org.apache.lucene.index.SegmentInfos) SegmentInfo(org.apache.lucene.index.SegmentInfo) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher)

Example 45 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)

Aggregations

SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)125 SolrCore (org.apache.solr.core.SolrCore)33 NamedList (org.apache.solr.common.util.NamedList)32 SolrException (org.apache.solr.common.SolrException)31 IOException (java.io.IOException)29 ArrayList (java.util.ArrayList)24 SolrParams (org.apache.solr.common.params.SolrParams)22 SchemaField (org.apache.solr.schema.SchemaField)22 Test (org.junit.Test)22 Document (org.apache.lucene.document.Document)21 SolrInputDocument (org.apache.solr.common.SolrInputDocument)17 Term (org.apache.lucene.index.Term)16 IndexReader (org.apache.lucene.index.IndexReader)14 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)14 IndexSchema (org.apache.solr.schema.IndexSchema)14 DocList (org.apache.solr.search.DocList)14 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)13 SolrDocument (org.apache.solr.common.SolrDocument)13 FieldType (org.apache.solr.schema.FieldType)13 Query (org.apache.lucene.search.Query)12