- Added a comment and cleaned up.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150099 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Otis Gospodnetic 2003-10-13 14:31:38 +00:00
parent 6600da8d84
commit 6fe013aa7d
7 changed files with 239 additions and 251 deletions

View File

@ -506,7 +506,7 @@ PARSER_END(QueryParser)
} }
<Boost> TOKEN : { <Boost> TOKEN : {
<NUMBER: (<_NUM_CHAR>)+ ( "." (<_NUM_CHAR>)+ )? > : DEFAULT <NUMBER: (["+","-"])? (<_NUM_CHAR>)+ ( "." (<_NUM_CHAR>)+ )? > : DEFAULT
} }
<RangeIn> TOKEN : { <RangeIn> TOKEN : {

View File

@ -62,11 +62,14 @@ import org.apache.lucene.store.OutputStream;
/** Optimized implementation of a vector of bits. This is more-or-less like /** Optimized implementation of a vector of bits. This is more-or-less like
java.util.BitSet, but also includes the following: java.util.BitSet, but also includes the following:
<UL> <ul>
<LI>a count() method, which efficiently computes the number of one bits;</LI> <li>a count() method, which efficiently computes the number of one bits;</li>
<LI>optimized read from and write to disk;</LI> <li>optimized read from and write to disk;</li>
<LI>inlinable get() method;</LI> <li>inlinable get() method;</li>
</UL> </ul>
@author Doug Cutting
@version $Id$
*/ */
public final class BitVector { public final class BitVector {
@ -108,11 +111,12 @@ public final class BitVector {
computed and cached, so that, if the vector is not changed, no computed and cached, so that, if the vector is not changed, no
recomputation is done for repeated calls. */ recomputation is done for repeated calls. */
public final int count() { public final int count() {
// if the vector has been modified
if (count == -1) { if (count == -1) {
int c = 0; int c = 0;
int end = bits.length; int end = bits.length;
for (int i = 0; i < end; i++) for (int i = 0; i < end; i++)
c += BYTE_COUNTS[bits[i] & 0xFF]; // sum bits per byte c += BYTE_COUNTS[bits[i] & 0xFF]; // sum bits per byte
count = c; count = c;
} }
return count; return count;

View File

@ -71,18 +71,18 @@ class SearchTest {
IndexWriter writer = new IndexWriter(directory, analyzer, true); IndexWriter writer = new IndexWriter(directory, analyzer, true);
String[] docs = { String[] docs = {
"a b c d e", "a b c d e",
"a b c d e a b c d e", "a b c d e a b c d e",
"a b c d e f g h i j", "a b c d e f g h i j",
"a c e", "a c e",
"e c a", "e c a",
"a c e a c e", "a c e a c e",
"a c e a b c" "a c e a b c"
}; };
for (int j = 0; j < docs.length; j++) { for (int j = 0; j < docs.length; j++) {
Document d = new Document(); Document d = new Document();
d.add(Field.Text("contents", docs[j])); d.add(Field.Text("contents", docs[j]));
writer.addDocument(d); writer.addDocument(d);
} }
writer.close(); writer.close();
@ -94,30 +94,30 @@ class SearchTest {
// "\"a b c\"", // "\"a b c\"",
// "a c", // "a c",
// "\"a c\"", // "\"a c\"",
"\"a c e\"", "\"a c e\"",
}; };
Hits hits = null; Hits hits = null;
QueryParser parser = new QueryParser("contents", analyzer); QueryParser parser = new QueryParser("contents", analyzer);
parser.setPhraseSlop(4); parser.setPhraseSlop(4);
for (int j = 0; j < queries.length; j++) { for (int j = 0; j < queries.length; j++) {
Query query = parser.parse(queries[j]); Query query = parser.parse(queries[j]);
System.out.println("Query: " + query.toString("contents")); System.out.println("Query: " + query.toString("contents"));
//DateFilter filter = //DateFilter filter =
// new DateFilter("modified", Time(1997,0,1), Time(1998,0,1)); // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
//DateFilter filter = DateFilter.Before("modified", Time(1997,00,01)); //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
//System.out.println(filter); //System.out.println(filter);
hits = searcher.search(query); hits = searcher.search(query);
System.out.println(hits.length() + " total results"); System.out.println(hits.length() + " total results");
for (int i = 0 ; i < hits.length() && i < 10; i++) { for (int i = 0 ; i < hits.length() && i < 10; i++) {
Document d = hits.doc(i); Document d = hits.doc(i);
System.out.println(i + " " + hits.score(i) System.out.println(i + " " + hits.score(i)
// + " " + DateField.stringToDate(d.get("modified")) // + " " + DateField.stringToDate(d.get("modified"))
+ " " + d.get("contents")); + " " + d.get("contents"));
} }
} }
searcher.close(); searcher.close();

View File

@ -1,5 +1,59 @@
package org.apache.lucene.index; package org.apache.lucene.index;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Lucene" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Lucene", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException; import java.io.IOException;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -625,77 +679,4 @@ public class TestCompoundFile extends TestCase
is.close(); is.close();
cr.close(); cr.close();
} }
// ===========================================================
// More extensive tests involving an IndexWriter
// ===========================================================
public void testIWCreate() throws IOException {
// create index writer
IndexWriter iw = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
int created = 0;
for (int i=0; i<150; i++) {
iw.addDocument(createTestDoc(String.valueOf(i)));
created ++;
}
assertEquals(created, iw.docCount());
iw.close();
// delete 500 documents
IndexReader reader = IndexReader.open(dir);
int deleted = 0;
for (int i = 10; i < created-7; i+=7) {
reader.delete(i);
deleted ++;
}
reader.close();
int remains = created - deleted;
iw = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
assertEquals(created, iw.docCount());
iw.close();
reader = IndexReader.open(dir);
assertEquals(created, reader.maxDoc());
assertEquals(remains, reader.numDocs());
for (int i = 10; i < created-7; i+=7) {
assertTrue("deleted: " + i, reader.isDeleted(i));
assertFalse("deleted+1: " + i, reader.isDeleted(i + 1));
assertFalse("deleted-1: " + i, reader.isDeleted(i - 1));
}
reader.close();
iw = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
iw.optimize();
assertEquals(remains, iw.docCount());
iw.close();
reader = IndexReader.open(dir);
assertEquals(remains, reader.maxDoc());
assertEquals(remains, reader.numDocs());
reader.close();
}
private Document createTestDoc(String id) {
Document doc = new Document();
doc.add(Field.Keyword("keyword_id", id));
doc.add(Field.Text("text_id", id));
doc.add(Field.Keyword("keyword_text", "KeywordText"));
doc.add(Field.Text("text", "This is a text field"));
doc.add(Field.UnIndexed("unindexed", "This is some payload unindexed text"));
doc.add(Field.UnStored("unstored", "This is unstored text"));
return doc;
}
private void verifyDoc(Document doc, String id) {
assertEquals("keyword_id", doc.get("keyword_id"), id);
assertEquals("text_id", id);
assertEquals("keyword_text", doc.get("keyword_text"), "KeywordText");
assertEquals("text", doc.get("text"), "This is some payload unindexed text");
assertEquals("unindexed", doc.get("unindexed"), "This is some payload unindexed text");
assertNull("unstored", doc.get("unstored"));
}
} }

View File

@ -37,30 +37,33 @@ public class TestIndexWriter extends TestCase
assertEquals(100, writer.docCount()); assertEquals(100, writer.docCount());
writer.close(); writer.close();
// delete 50 documents // delete 40 documents
reader = IndexReader.open(dir); reader = IndexReader.open(dir);
for (i = 0; i < 50; i++) { for (i = 0; i < 40; i++) {
reader.delete(i); reader.delete(i);
} }
reader.close(); reader.close();
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false); // test doc count before segments are merged/index is optimized
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
assertEquals(100, writer.docCount()); assertEquals(100, writer.docCount());
writer.close(); writer.close();
reader = IndexReader.open(dir); reader = IndexReader.open(dir);
assertEquals(100, reader.maxDoc()); assertEquals(100, reader.maxDoc());
assertEquals(50, reader.numDocs()); assertEquals(60, reader.numDocs());
reader.close(); reader.close();
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false); // optimize the index and check that the new doc count is correct
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
writer.optimize(); writer.optimize();
assertEquals(50, writer.docCount()); assertEquals(60, writer.docCount());
writer.close(); writer.close();
// check that the index reader gives the same numbers.
reader = IndexReader.open(dir); reader = IndexReader.open(dir);
assertEquals(50, reader.maxDoc()); assertEquals(60, reader.maxDoc());
assertEquals(50, reader.numDocs()); assertEquals(60, reader.numDocs());
reader.close(); reader.close();
} }
catch (IOException e) { catch (IOException e) {

View File

@ -5,7 +5,6 @@ import java.io.IOException;
/** This class provides access to package-level features defined in the /** This class provides access to package-level features defined in the
* store package. It is used for testing only. * store package. It is used for testing only.
*/ */
public class _TestHelper { public class _TestHelper {
/** Returns true if the instance of the provided input stream is actually /** Returns true if the instance of the provided input stream is actually

View File

@ -196,7 +196,8 @@ public class TestBitVector extends TestCase
} }
/** /**
* Compare two BitVectors (really, this should be an equals method on the BitVector itself... * Compare two BitVectors.
* This should really be an equals method on the BitVector itself.
* @param bv One bit vector * @param bv One bit vector
* @param compare The second to compare * @param compare The second to compare
*/ */