⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmultisearcher.java

📁 Lucene a java open-source SearchEngine Framework
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        mSearcher3.close();        indexStoreA.close();        indexStoreB.close();    }        private static Document createDocument(String contents1, String contents2) {        Document document=new Document();                document.add(new Field("contents", contents1, Field.Store.YES, Field.Index.UN_TOKENIZED));      document.add(new Field("other", "other contents", Field.Store.YES, Field.Index.UN_TOKENIZED));        if (contents2!=null) {            document.add(new Field("contents", contents2, Field.Store.YES, Field.Index.UN_TOKENIZED));        }                return document;    }        private static void initIndex(Directory directory, int nDocs, boolean create, String contents2) throws IOException {        IndexWriter indexWriter=null;                try {            indexWriter=new IndexWriter(directory, new KeywordAnalyzer(), create);                        for (int i=0; i<nDocs; i++) {                indexWriter.addDocument(createDocument("doc" + i, contents2));            }        } finally {            if (indexWriter!=null) {                indexWriter.close();            }        }    }  public void testFieldSelector() throws Exception {    RAMDirectory ramDirectory1, ramDirectory2;    IndexSearcher indexSearcher1, indexSearcher2;    ramDirectory1 = new RAMDirectory();    ramDirectory2 = new RAMDirectory();    Query query = new TermQuery(new Term("contents", "doc0"));    // Now put the documents in a different index    initIndex(ramDirectory1, 10, true, null); // documents with a single token "doc0", "doc1", etc...    initIndex(ramDirectory2, 10, true, "x"); // documents with two tokens "doc0" and "x", "doc1" and x, etc...    indexSearcher1 = new IndexSearcher(ramDirectory1);    indexSearcher2 = new IndexSearcher(ramDirectory2);    MultiSearcher searcher = getMultiSearcherInstance(new Searcher[]{indexSearcher1, indexSearcher2});    assertTrue("searcher is null and it shouldn't be", searcher != null);    Hits hits = searcher.search(query);    assertTrue("hits is null and it shouldn't be", hits != null);    assertTrue(hits.length() + " does not equal: " + 2, hits.length() == 2);    Document document = searcher.doc(hits.id(0));    assertTrue("document is null and it shouldn't be", document != null);    assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 2, document.getFields().size() == 2);    //Should be one document from each directory    //they both have two fields, contents and other    Set ftl = new HashSet();    ftl.add("other");    SetBasedFieldSelector fs = new SetBasedFieldSelector(ftl, Collections.EMPTY_SET);    document = searcher.doc(hits.id(0), fs);    assertTrue("document is null and it shouldn't be", document != null);    assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 1, document.getFields().size() == 1);    String value = document.get("contents");    assertTrue("value is not null and it should be", value == null);    value = document.get("other");    assertTrue("value is null and it shouldn't be", value != null);    ftl.clear();    ftl.add("contents");    fs = new SetBasedFieldSelector(ftl, Collections.EMPTY_SET);    document = searcher.doc(hits.id(1), fs);    value = document.get("contents");    assertTrue("value is null and it shouldn't be", value != null);        value = document.get("other");    assertTrue("value is not null and it should be", value == null);  }  /* uncomment this when the highest score is always normalized to 1.0, even when it was < 1.0 public void testNormalization1() throws IOException {     testNormalization(1, "Using 1 document per index:"); }  */        public void testNormalization10() throws IOException {        testNormalization(10, "Using 10 documents per index:");    }        private void testNormalization(int nDocs, String message) throws IOException {        Query query=new TermQuery(new Term("contents", "doc0"));                RAMDirectory ramDirectory1;        IndexSearcher indexSearcher1;        Hits hits;                ramDirectory1=new MockRAMDirectory();                // First put the documents in the same index        initIndex(ramDirectory1, nDocs, true, null); // documents with a single token "doc0", "doc1", etc...        initIndex(ramDirectory1, nDocs, false, "x"); // documents with two tokens "doc0" and "x", "doc1" and x, etc...                indexSearcher1=new IndexSearcher(ramDirectory1);                hits=indexSearcher1.search(query);                assertEquals(message, 2, hits.length());                assertEquals(message, 1, hits.score(0), 1e-6); // hits.score(0) is 0.594535 if only a single document is in first index                // Store the scores for use later        float[] scores={ hits.score(0), hits.score(1) };                assertTrue(message, scores[0] > scores[1]);                indexSearcher1.close();        ramDirectory1.close();        hits=null;                                RAMDirectory ramDirectory2;        IndexSearcher indexSearcher2;                ramDirectory1=new MockRAMDirectory();        ramDirectory2=new MockRAMDirectory();                // Now put the documents in a different index        initIndex(ramDirectory1, nDocs, true, null); // documents with a single token "doc0", "doc1", etc...        initIndex(ramDirectory2, nDocs, true, "x"); // documents with two tokens "doc0" and "x", "doc1" and x, etc...                indexSearcher1=new IndexSearcher(ramDirectory1);        indexSearcher2=new IndexSearcher(ramDirectory2);                Searcher searcher=getMultiSearcherInstance(new Searcher[] { indexSearcher1, indexSearcher2 });                hits=searcher.search(query);                assertEquals(message, 2, hits.length());                // The scores should be the same (within reason)        assertEquals(message, scores[0], hits.score(0), 1e-6); // This will a document from ramDirectory1        assertEquals(message, scores[1], hits.score(1), 1e-6); // This will a document from ramDirectory2                                // Adding a Sort.RELEVANCE object should not change anything        hits=searcher.search(query, Sort.RELEVANCE);                assertEquals(message, 2, hits.length());                assertEquals(message, scores[0], hits.score(0), 1e-6); // This will a document from ramDirectory1        assertEquals(message, scores[1], hits.score(1), 1e-6); // This will a document from ramDirectory2                searcher.close();                ramDirectory1.close();        ramDirectory2.close();    }        /**     * test that custom similarity is in effect when using MultiSearcher (LUCENE-789).     * @throws IOException      */    public void testCustomSimilarity () throws IOException {        RAMDirectory dir = new RAMDirectory();        initIndex(dir, 10, true, "x"); // documents with two tokens "doc0" and "x", "doc1" and x, etc...        IndexSearcher srchr = new IndexSearcher(dir);        MultiSearcher msrchr = getMultiSearcherInstance(new Searcher[]{srchr});                Similarity customSimilarity = new DefaultSimilarity() {            // overide all            public float idf(int docFreq, int numDocs) { return 100.0f; }            public float coord(int overlap, int maxOverlap) { return 1.0f; }            public float lengthNorm(String fieldName, int numTokens) { return 1.0f; }            public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }            public float sloppyFreq(int distance) { return 1.0f; }            public float tf(float freq) { return 1.0f; }        };                srchr.setSimilarity(customSimilarity);        msrchr.setSimilarity(customSimilarity);          Query query=new TermQuery(new Term("contents", "doc0"));          // Get a score from IndexSearcher        TopDocs topDocs = srchr.search(query, null, 1);        float score1 = topDocs.getMaxScore();                // Get the score from MultiSearcher        topDocs = msrchr.search(query, null, 1);        float scoreN = topDocs.getMaxScore();                // The scores from the IndexSearcher and Multisearcher should be the same        // if the same similarity is used.        assertEquals("MultiSearcher score must be equal to single esrcher score!", score1, scoreN, 1e-6);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -