You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by va...@apache.org on 2009/01/09 04:28:41 UTC
svn commit: r732916 [14/14] - in /lucene/pylucene/trunk: ./ java/ java/org/
java/org/osafoundation/ java/org/osafoundation/lucene/
java/org/osafoundation/lucene/analysis/
java/org/osafoundation/lucene/queryParser/
java/org/osafoundation/lucene/search/ ...
Added: lucene/pylucene/trunk/test/test_PyLuceneThread.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_PyLuceneThread.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_PyLuceneThread.py (added)
+++ lucene/pylucene/trunk/test/test_PyLuceneThread.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,128 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+import time, threading
+from unittest import TestCase, main
+from lucene import *
+
+
+class PyLuceneThreadTestCase(TestCase):
+ """
+ Test using threads in PyLucene with python threads
+ """
+
+ def setUp(self):
+
+ self.classLoader = Thread.currentThread().getContextClassLoader()
+
+ self.directory = RAMDirectory()
+ writer = IndexWriter(self.directory, StandardAnalyzer(), True)
+
+ doc1 = Document()
+ doc2 = Document()
+ doc3 = Document()
+ doc4 = Document()
+ doc1.add(Field("field", "one",
+ Field.Store.YES, Field.Index.TOKENIZED))
+ doc2.add(Field("field", "two",
+ Field.Store.YES, Field.Index.TOKENIZED))
+ doc3.add(Field("field", "three",
+ Field.Store.YES, Field.Index.TOKENIZED))
+ doc4.add(Field("field", "one",
+ Field.Store.YES, Field.Index.TOKENIZED))
+
+ writer.addDocument(doc1)
+ writer.addDocument(doc2)
+ writer.addDocument(doc3)
+ writer.addDocument(doc4)
+ writer.optimize()
+ writer.close()
+
+ self.testData = [('one',2), ('two',1), ('three', 1), ('five', 0)] * 500
+ self.lock = threading.Lock()
+ self.totalQueries = 0
+
+
+ def tearDown(self):
+
+ self.directory.close()
+
+
+ def testWithMainThread(self):
+ """ warm up test for runSearch in main thread """
+
+ self.runSearch(2000, True)
+
+
+ def testWithPyLuceneThread(self):
+ """ Run 5 threads with 2000 queries each """
+
+ threads = []
+ for i in xrange(5):
+ threads.append(threading.Thread(target=self.runSearch,
+ args=(2000,)))
+
+ for thread in threads:
+ thread.start()
+
+ for thread in threads:
+ thread.join()
+
+ # we survived!
+
+ # and all queries have ran successfully
+ self.assertEqual(10000, self.totalQueries)
+
+
+ def runSearch(self, runCount, mainThread=False):
+ """ search for runCount number of times """
+
+ # problem: if there are any assertion errors in the child
+ # thread, the calling thread is not notified and may still
+ # consider the test case pass. We are using self.totalQueries
+ # to double check that work has actually been done.
+
+ if not mainThread:
+ getVMEnv().attachCurrentThread()
+ time.sleep(0.5)
+
+ searcher = IndexSearcher(self.directory)
+ try:
+ self.query = PhraseQuery()
+ for word, count in self.testData[0:runCount]:
+ query = TermQuery(Term("field", word))
+ result = searcher.search(query)
+ self.assertEqual(result.length(), count)
+
+ self.lock.acquire()
+ self.totalQueries += 1
+ self.lock.release()
+ finally:
+ searcher.close()
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ main()
+ except:
+ pass
+ else:
+ main()
Propchange: lucene/pylucene/trunk/test/test_PyLuceneThread.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_PyLuceneThread.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_PythonDirectory.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_PythonDirectory.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_PythonDirectory.py (added)
+++ lucene/pylucene/trunk/test/test_PythonDirectory.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,249 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+import os, sys, unittest, shutil
+from threading import RLock
+import test_PyLucene
+
+from lucene import \
+ PythonLock, PythonIndexInput, PythonIndexOutput, PythonDirectory, \
+ JavaError, IOException
+
+"""
+The Directory Implementation here is for testing purposes only, not meant
+as an example of writing one, the implementation here suffers from a lack
+of safety when dealing with concurrent modifications as it does away with
+the file locking in the default lucene fsdirectory implementation.
+"""
+
+DEBUG = False
+
+class DebugWrapper(object):
+
+ def __init__(self, obj):
+ self.obj = obj
+
+ def __getattr__(self, name):
+ print self.obj.__class__.__name__, self.obj.name, name
+ sys.stdout.flush()
+ return getattr(self.obj, name)
+
+class DebugFactory(object):
+
+ def __init__(self, klass):
+ self.klass = klass
+
+ def __call__(self, *args, **kw):
+ instance = self.klass(*args, **kw)
+ return DebugWrapper(instance)
+
+
+class PythonDirLock(PythonLock):
+ # only safe for a single process
+
+ def __init__(self, name, path, lock):
+ super(PythonDirLock, self).__init__()
+ self.name = name
+ self.lock_file = path
+ self.lock = lock
+
+ def isLocked(self):
+ return self.lock.locked()
+
+ def obtain(self, timeout=None):
+ if timeout is not None:
+ return self.lock.acquire(timeout)
+ return self.lock.acquire()
+
+ def release(self):
+ return self.lock.release()
+
+
+class PythonFileStreamInput(PythonIndexInput):
+
+ def __init__(self, name, fh, size, clone=False):
+ if not clone:
+ super(PythonFileStreamInput, self).__init__()
+ self.name = name
+ self.fh = fh
+ self._length = size
+ self.isOpen = True
+ self.isClone = clone
+
+ def length(self):
+ return long(self._length)
+
+ def clone(self):
+ clone = PythonFileStreamInput(self.name, self.fh, self._length, True)
+ return super(PythonFileStreamInput, self).clone(clone)
+
+ def close(self):
+ if self.isOpen:
+ self.isOpen = False
+ if not self.isClone:
+ self.fh.close()
+
+ def readInternal(self, length, pos):
+ self.fh.seek(pos)
+ return self.fh.read(length)
+
+ def seekInternal(self, pos):
+ self.fh.seek(pos)
+
+
+class PythonFileStreamOutput(PythonIndexOutput):
+
+ def __init__(self, name, fh):
+ super(PythonFileStreamOutput, self).__init__()
+ self.name = name
+ self.fh = fh
+ self.isOpen = True
+ self._length = 0
+
+ def close(self):
+ if self.isOpen:
+ super(PythonFileStreamOutput, self).close()
+ self.isOpen = False
+ self.fh.close()
+
+ def length(self):
+ return long(self._length)
+
+ def seekInternal(self, pos):
+ self.fh.seek(pos)
+
+ def flushBuffer(self, buffer):
+ self.fh.write(''.join(buffer))
+ self.fh.flush()
+ self._length += len(buffer)
+
+
+class PythonFileDirectory(PythonDirectory):
+
+ def __init__(self, path):
+ super(PythonFileDirectory, self).__init__()
+ self.name = path
+ assert os.path.isdir(path)
+ self.path = path
+ self._locks = {}
+ self._streams = []
+
+ def close(self):
+ for stream in self._streams:
+ stream.close()
+ del self._streams[:]
+ self._locks.clear()
+
+ def createOutput(self, name):
+ file_path = os.path.join(self.path, name)
+ fh = open(file_path, "w")
+ stream = PythonFileStreamOutput(name, fh)
+ self._streams.append(stream)
+ return stream
+
+ def deleteFile(self, name):
+ if self.fileExists(name):
+ os.unlink(os.path.join(self.path, name))
+
+ def fileExists(self, name):
+ return os.path.exists(os.path.join(self.path, name))
+
+ def fileLength(self, name):
+ file_path = os.path.join(self.path, name)
+ return long(os.path.getsize(file_path))
+
+ def fileModified(self, name):
+ file_path = os.path.join(self.path, name)
+ return os.path.getmtime(file_path)
+
+ def list(self):
+ return os.listdir(self.path)
+
+ def makeLock(self, name):
+ lock = self._locks.get(name)
+ if lock is None:
+ lock = PythonDirLock(name, os.path.join(self.path, name), RLock())
+ self._locks[name] = lock
+ return lock
+
+ def openInput(self, name, bufferSize=0):
+ file_path = os.path.join(self.path, name)
+ try:
+ fh = open(file_path, 'r')
+ except IOError:
+ raise JavaError, IOException(name)
+ stream = PythonFileStreamInput(name, fh, os.path.getsize(file_path))
+ self._streams.append(stream)
+ return stream
+
+ def touchFile(self, name):
+ file_path = os.path.join(self.path, name)
+ os.utime(file_path, None)
+
+
+if DEBUG:
+ _globals = globals()
+ _globals['PythonFileDirectory'] = DebugFactory(PythonFileDirectory)
+ _globals['PythonFileStreamInput'] = DebugFactory(PythonFileStreamInput)
+ _globals['PythonFileStreamOutput'] = DebugFactory(PythonFileStreamOutput)
+ _globals['PythonDirLock'] = DebugFactory(PythonDirLock)
+ del _globals
+
+class PythonDirectoryTests(unittest.TestCase, test_PyLucene.Test_PyLuceneBase):
+
+ STORE_DIR = "testpyrepo"
+
+ def setUp(self):
+ if not os.path.exists(self.STORE_DIR):
+ os.mkdir(self.STORE_DIR)
+
+ def tearDown(self):
+ if os.path.exists(self.STORE_DIR):
+ shutil.rmtree(self.STORE_DIR)
+
+ def openStore(self):
+ return PythonFileDirectory(self.STORE_DIR)
+
+ def closeStore(self, store, *args):
+ for arg in args:
+ if arg is not None:
+ arg.close()
+ store.close()
+
+ def test_IncrementalLoop(self):
+ print "Testing Indexing Incremental Looping"
+ for i in range(100):
+ print "indexing ", i
+ sys.stdout.flush()
+ self.test_indexDocument()
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ env = lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ unittest.main()
+ except:
+ pass
+ print 'inputs', env._dumpRefs(True).get('class org.osafoundation.lucene.store.PythonIndexOutput', 0)
+ print 'outputs', env._dumpRefs(True).get('class org.osafoundation.lucene.store.PythonIndexInput', 0)
+ print 'locks', env._dumpRefs(True).get('class org.osafoundation.lucene.store.PythonLock', 0)
+ print 'dirs', env._dumpRefs(True).get('class org.osafoundation.lucene.store.PythonLock', 0)
+ else:
+ unittest.main()
Propchange: lucene/pylucene/trunk/test/test_PythonDirectory.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_PythonDirectory.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_RangeFilter.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_RangeFilter.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_RangeFilter.py (added)
+++ lucene/pylucene/trunk/test/test_RangeFilter.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,290 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+from random import seed, randint
+from unittest import TestCase, main
+
+from lucene import \
+ SimpleAnalyzer, Document, Field, IndexWriter, RAMDirectory, \
+ IndexReader, IndexSearcher, Term, TermQuery, RangeFilter
+
+
+class BaseTestRangeFilter(TestCase):
+
+ def __init__(self, *args):
+
+ super(BaseTestRangeFilter, self).__init__(*args)
+
+ self.index = RAMDirectory()
+
+ self.MAX_INT = 0x7fffffff
+ self.maxR = ~self.MAX_INT
+ self.minR = self.MAX_INT
+
+ self.minId = 0
+ self.maxId = 10000
+
+ self.build()
+
+ #
+ # a simple padding function that should work with any int
+ #
+
+ def pad(self, n):
+
+ if n < 0:
+ return "-%0.10d" % (self.MAX_INT + n + 1)
+ else:
+ return "0%0.10d" % n
+
+ def build(self):
+
+ # build an index
+ writer = IndexWriter(self.index, SimpleAnalyzer(), True)
+
+ seed(101)
+ for d in xrange(self.minId, self.maxId + 1):
+ doc = Document()
+ doc.add(Field("id", self.pad(d),
+ Field.Store.YES, Field.Index.UN_TOKENIZED))
+ r = randint(~self.MAX_INT, self.MAX_INT)
+ if self.maxR < r:
+ self.maxR = r
+ if r < self.minR:
+ self.minR = r
+ doc.add(Field("rand", self.pad(r),
+ Field.Store.YES, Field.Index.UN_TOKENIZED))
+ doc.add(Field("body", "body",
+ Field.Store.YES, Field.Index.UN_TOKENIZED))
+ writer.addDocument(doc)
+
+ writer.optimize()
+ writer.close()
+
+ def testPad(self):
+
+ tests = [-9999999, -99560, -100, -3, -1, 0, 3, 9, 10, 1000, 999999999]
+
+ for i in xrange(0, len(tests) - 1):
+ a = tests[i]
+ b = tests[i + 1]
+ aa = self.pad(a)
+ bb = self.pad(b)
+ label = "%s:%s vs %s:%s" %(a, aa, b, bb)
+ self.assertEqual(len(aa), len(bb), "length of %s" %label)
+ self.assert_(aa < bb, "compare less than %s" %label)
+
+
+ #
+ # A basic 'positive' Unit test class for the RangeFilter class.
+ #
+ # NOTE: at the moment, this class only tests for 'positive' results,
+ # it does not verify the results to ensure there are no 'false positives',
+ # nor does it adequately test 'negative' results. It also does not test
+ # that garbage in results in an Exception.
+ #
+
+class TestRangeFilter(BaseTestRangeFilter):
+
+ def testRangeFilterId(self):
+
+ reader = IndexReader.open(self.index);
+ search = IndexSearcher(reader)
+
+ medId = ((self.maxId - self.minId) / 2)
+
+ minIP = self.pad(self.minId)
+ maxIP = self.pad(self.maxId)
+ medIP = self.pad(medId)
+
+ numDocs = reader.numDocs()
+
+ self.assertEqual(numDocs, 1 + self.maxId - self.minId, "num of docs")
+
+ q = TermQuery(Term("body","body"))
+
+ # test id, bounded on both ends
+
+ result = search.search(q, RangeFilter("id", minIP, maxIP,
+ True, True))
+ self.assertEqual(numDocs, result.length(), "find all")
+
+ result = search.search(q, RangeFilter("id", minIP, maxIP,
+ True, False))
+ self.assertEqual(numDocs - 1, result.length(), "all but last")
+
+ result = search.search(q, RangeFilter("id", minIP, maxIP,
+ False, True))
+ self.assertEqual(numDocs - 1, result.length(), "all but first")
+
+ result = search.search(q, RangeFilter("id", minIP, maxIP,
+ False, False))
+ self.assertEqual(numDocs - 2, result.length(), "all but ends")
+
+ result = search.search(q, RangeFilter("id", medIP, maxIP,
+ True, True))
+ self.assertEqual(1 + self.maxId - medId, result.length(), "med and up")
+
+ result = search.search(q, RangeFilter("id", minIP, medIP,
+ True, True))
+ self.assertEqual(1 + medId - self.minId, result.length(), "up to med")
+
+ # unbounded id
+
+ result = search.search(q, RangeFilter("id", minIP, None,
+ True, False))
+ self.assertEqual(numDocs, result.length(), "min and up")
+
+ result = search.search(q, RangeFilter("id", None, maxIP,
+ False, True))
+ self.assertEqual(numDocs, result.length(), "max and down")
+
+ result = search.search(q, RangeFilter("id", minIP, None,
+ False, False))
+ self.assertEqual(numDocs - 1, result.length(), "not min, but up")
+
+ result = search.search(q, RangeFilter("id", None, maxIP,
+ False, False))
+ self.assertEqual(numDocs - 1, result.length(), "not max, but down")
+
+ result = search.search(q, RangeFilter("id",medIP, maxIP,
+ True, False))
+ self.assertEqual(self.maxId - medId, result.length(), "med and up, not max")
+
+ result = search.search(q, RangeFilter("id", minIP, medIP,
+ False, True))
+ self.assertEqual(medId - self.minId, result.length(), "not min, up to med")
+
+ # very small sets
+
+ result = search.search(q, RangeFilter("id", minIP, minIP,
+ False, False))
+ self.assertEqual(0, result.length(), "min, min, False, False")
+
+ result = search.search(q, RangeFilter("id", medIP, medIP,
+ False, False))
+ self.assertEqual(0, result.length(), "med, med, False, False")
+ result = search.search(q, RangeFilter("id", maxIP, maxIP,
+ False, False))
+ self.assertEqual(0, result.length(), "max, max, False, False")
+
+ result = search.search(q, RangeFilter("id", minIP, minIP,
+ True, True))
+ self.assertEqual(1, result.length(), "min, min, True, True")
+ result = search.search(q, RangeFilter("id", None, minIP,
+ False, True))
+ self.assertEqual(1, result.length(), "nul, min, False, True")
+
+ result = search.search(q, RangeFilter("id", maxIP, maxIP,
+ True, True))
+ self.assertEqual(1, result.length(), "max, max, True, True")
+ result = search.search(q, RangeFilter("id", maxIP, None,
+ True, False))
+ self.assertEqual(1, result.length(), "max, nul, True, True")
+
+ result = search.search(q, RangeFilter("id", medIP, medIP,
+ True, True))
+ self.assertEqual(1, result.length(), "med, med, True, True")
+
+ def testRangeFilterRand(self):
+
+ reader = IndexReader.open(self.index)
+ search = IndexSearcher(reader)
+
+ minRP = self.pad(self.minR)
+ maxRP = self.pad(self.maxR)
+
+ numDocs = reader.numDocs()
+
+ self.assertEqual(numDocs, 1 + self.maxId - self.minId, "num of docs")
+
+ q = TermQuery(Term("body", "body"))
+
+ # test extremes, bounded on both ends
+
+ result = search.search(q, RangeFilter("rand", minRP, maxRP,
+ True, True))
+ self.assertEqual(numDocs, result.length(), "find all")
+
+ result = search.search(q, RangeFilter("rand", minRP, maxRP,
+ True, False))
+ self.assertEqual(numDocs - 1, result.length(), "all but biggest")
+
+ result = search.search(q, RangeFilter("rand", minRP, maxRP,
+ False, True))
+ self.assertEqual(numDocs - 1, result.length(), "all but smallest")
+
+ result = search.search(q, RangeFilter("rand", minRP, maxRP,
+ False, False))
+ self.assertEqual(numDocs - 2, result.length(), "all but extremes")
+
+ # unbounded
+
+ result = search.search(q, RangeFilter("rand", minRP, None,
+ True, False))
+ self.assertEqual(numDocs, result.length(), "smallest and up")
+
+ result = search.search(q, RangeFilter("rand", None, maxRP,
+ False, True))
+ self.assertEqual(numDocs, result.length(), "biggest and down")
+
+ result = search.search(q, RangeFilter("rand", minRP, None,
+ False, False))
+ self.assertEqual(numDocs - 1, result.length(), "not smallest, but up")
+
+ result = search.search(q, RangeFilter("rand", None, maxRP,
+ False, False))
+ self.assertEqual(numDocs - 1, result.length(), "not biggest, but down")
+
+ # very small sets
+
+ result = search.search(q, RangeFilter("rand", minRP, minRP,
+ False, False))
+ self.assertEqual(0, result.length(), "min, min, False, False")
+
+ result = search.search(q, RangeFilter("rand", maxRP, maxRP,
+ False, False))
+ self.assertEqual(0, result.length(), "max, max, False, False")
+
+ result = search.search(q, RangeFilter("rand", minRP, minRP,
+ True, True))
+ self.assertEqual(1, result.length(), "min, min, True, True")
+
+ result = search.search(q, RangeFilter("rand", None, minRP,
+ False, True))
+ self.assertEqual(1, result.length(), "nul, min, False, True")
+
+ result = search.search(q, RangeFilter("rand", maxRP, maxRP,
+ True, True))
+ self.assertEqual(1, result.length(), "max, max, True, True")
+
+ result = search.search(q, RangeFilter("rand", maxRP, None,
+ True, False))
+ self.assertEqual(1, result.length(), "max, nul, True, True")
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ main(defaultTest='TestRangeFilter')
+ except:
+ pass
+ else:
+ main(defaultTest='TestRangeFilter')
Propchange: lucene/pylucene/trunk/test/test_RangeFilter.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_RangeFilter.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_RangeQuery.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_RangeQuery.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_RangeQuery.py (added)
+++ lucene/pylucene/trunk/test/test_RangeQuery.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,121 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+from unittest import TestCase, main
+from lucene import *
+
+
+class PrefixQueryTestCase(TestCase):
+ """
+ Unit tests ported from Java Lucene
+ """
+
+ def _initializeIndex(self, values):
+
+ writer = IndexWriter(self.dir, WhitespaceAnalyzer(), True)
+ for value in values:
+ self._insertDoc(writer, value)
+ writer.close()
+
+ def _insertDoc(self, writer, content):
+
+ doc = Document()
+
+ doc.add(Field("id", "id" + str(self.docCount),
+ Field.Store.YES, Field.Index.UN_TOKENIZED))
+ doc.add(Field("content", content,
+ Field.Store.NO, Field.Index.TOKENIZED))
+
+ writer.addDocument(doc)
+ self.docCount += 1
+
+ def _addDoc(self, content):
+
+ writer = IndexWriter(self.dir, WhitespaceAnalyzer(), False)
+ self._insertDoc(writer, content)
+ writer.close()
+
+ def setUp(self):
+
+ self.docCount = 0
+ self.dir = RAMDirectory()
+
+ def testExclusive(self):
+
+ query = RangeQuery(Term("content", "A"),
+ Term("content", "C"),
+ False)
+ self._initializeIndex(["A", "B", "C", "D"])
+ searcher = IndexSearcher(self.dir)
+ hits = searcher.search(query)
+ self.assertEqual(1, hits.length(),
+ "A,B,C,D, only B in range")
+ searcher.close()
+
+ self._initializeIndex(["A", "B", "D"])
+ searcher = IndexSearcher(self.dir)
+ hits = searcher.search(query)
+ self.assertEqual(1, hits.length(),
+ "A,B,D, only B in range")
+ searcher.close()
+
+ self._addDoc("C")
+ searcher = IndexSearcher(self.dir)
+ hits = searcher.search(query)
+ self.assertEqual(1, hits.length(),
+ "C added, still only B in range")
+ searcher.close()
+
+ def testInclusive(self):
+
+ query = RangeQuery(Term("content", "A"),
+ Term("content", "C"),
+ True)
+
+ self._initializeIndex(["A", "B", "C", "D"])
+ searcher = IndexSearcher(self.dir)
+ hits = searcher.search(query)
+ self.assertEqual(3, hits.length(),
+ "A,B,C,D - A,B,C in range")
+ searcher.close()
+
+ self._initializeIndex(["A", "B", "D"])
+ searcher = IndexSearcher(self.dir)
+ hits = searcher.search(query)
+ self.assertEqual(2, hits.length(),
+ "A,B,D - A and B in range")
+ searcher.close()
+
+ self._addDoc("C")
+ searcher = IndexSearcher(self.dir)
+ hits = searcher.search(query)
+ self.assertEqual(3, hits.length(),
+ "C added - A, B, C in range")
+ searcher.close()
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ main()
+ except:
+ pass
+ else:
+ main()
Propchange: lucene/pylucene/trunk/test/test_RangeQuery.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_RangeQuery.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_RegexQuery.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_RegexQuery.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_RegexQuery.py (added)
+++ lucene/pylucene/trunk/test/test_RegexQuery.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,93 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+from unittest import TestCase, main
+from lucene import *
+
+
+class TestRegexQuery(TestCase):
+
+ FN = "field"
+
+ def setUp(self):
+
+ directory = RAMDirectory()
+
+ writer = IndexWriter(directory, SimpleAnalyzer(), True)
+ doc = Document()
+ doc.add(Field(self.FN, "the quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.TOKENIZED))
+ writer.addDocument(doc)
+ writer.optimize()
+ writer.close()
+ self.searcher = IndexSearcher(directory)
+
+ def tearDown(self):
+
+ self.searcher.close()
+
+ def newTerm(self, value):
+
+ return Term(self.FN, value)
+
+ def regexQueryNrHits(self, regex):
+
+ query = RegexQuery(self.newTerm(regex))
+
+ return len(self.searcher.search(query))
+
+ def spanRegexQueryNrHits(self, regex1, regex2, slop, ordered):
+
+ srq1 = SpanRegexQuery(self.newTerm(regex1))
+ srq2 = SpanRegexQuery(self.newTerm(regex2))
+ query = SpanNearQuery([srq1, srq2], slop, ordered)
+
+ return len(self.searcher.search(query))
+
+ def testRegex1(self):
+
+ self.assertEqual(1, self.regexQueryNrHits("^q.[aeiou]c.*$"))
+
+ def testRegex2(self):
+
+ self.assertEqual(0, self.regexQueryNrHits("^.[aeiou]c.*$"))
+
+ def testRegex3(self):
+
+ self.assertEqual(0, self.regexQueryNrHits("^q.[aeiou]c$"))
+
+ def testSpanRegex1(self):
+
+ self.assertEqual(1, self.spanRegexQueryNrHits("^q.[aeiou]c.*$",
+ "dog", 6, True))
+
+ def testSpanRegex2(self):
+
+ self.assertEqual(0, self.spanRegexQueryNrHits("^q.[aeiou]c.*$",
+ "dog", 5, True))
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ main()
+ except:
+ pass
+ else:
+ main()
Propchange: lucene/pylucene/trunk/test/test_RegexQuery.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_RegexQuery.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_RewriteQuery.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_RewriteQuery.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_RewriteQuery.py (added)
+++ lucene/pylucene/trunk/test/test_RewriteQuery.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,52 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+import sys, lucene, unittest
+
+
+# Originally intended to demonstrate a memory leak. See
+# http://lists.osafoundation.org/pipermail/pylucene-dev/2008-October/002937.html
+# and followup
+
+class QueryRewriteTest(unittest.TestCase):
+
+ def setUp(self):
+
+ store = lucene.RAMDirectory()
+ writer = lucene.IndexWriter(store, lucene.StandardAnalyzer(), True)
+ writer.close()
+ self.reader = lucene.IndexSearcher(store).getIndexReader()
+ self.term = lucene.Term('all', 'foo')
+
+ def testQuery(self):
+
+ base_query = lucene.TermQuery(self.term)
+ new_query = base_query.rewrite(self.reader)
+
+ self.assertEquals(base_query, new_query)
+
+
+if __name__ == "__main__":
+ env = lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ unittest.main()
+ except:
+ pass
+ else:
+ unittest.main()
Propchange: lucene/pylucene/trunk/test/test_RewriteQuery.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_RewriteQuery.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_RuntimeException.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_RuntimeException.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_RuntimeException.py (added)
+++ lucene/pylucene/trunk/test/test_RuntimeException.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,33 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+import unittest
+from lucene import *
+
+class TestRuntimeError(unittest.TestCase):
+ def testRunTimeError(self):
+ def date_convert():
+ # should raise a java.lang.RuntimeException, because
+ # the date is before the start of the epoch
+ return DateField.dateToString(Date(0,1,1))
+ self.assertRaises(JavaError, date_convert)
+
+
+
+if __name__ == '__main__':
+ import lucene
+ lucene.initVM(lucene.CLASSPATH)
+ unittest.main()
Propchange: lucene/pylucene/trunk/test/test_RuntimeException.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_RuntimeException.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_Similarity.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_Similarity.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_Similarity.py (added)
+++ lucene/pylucene/trunk/test/test_Similarity.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,119 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+from unittest import TestCase, main
+from lucene import *
+
+
+class SimpleSimilarity(PythonSimilarity):
+
+ def lengthNorm(self, field, numTerms):
+ return 1.0
+
+ def queryNorm(self, sumOfSquaredWeights):
+ return 1.0
+
+ def tf(self, freq):
+ return freq
+
+ def sloppyFreq(self, distance):
+ return 2.0
+
+ def idfTerms(self, terms, searcher):
+ return 1.0
+
+ def idfTerm(self, term, searcher):
+ return 1.0
+
+ def idf(self, docFreq, numDocs):
+ return 1.0
+
+ def coord(self, overlap, maxOverlap):
+ return 1.0
+
+
+class SimilarityTestCase(TestCase):
+ """
+ Unit tests ported from Java Lucene
+ """
+
+ def testSimilarity(self):
+
+ store = RAMDirectory()
+ writer = IndexWriter(store, SimpleAnalyzer(), True)
+ writer.setSimilarity(SimpleSimilarity())
+
+ d1 = Document()
+ d1.add(Field("field", "a c",
+ Field.Store.YES, Field.Index.TOKENIZED))
+
+ d2 = Document()
+ d2.add(Field("field", "a b c",
+ Field.Store.YES, Field.Index.TOKENIZED))
+
+ writer.addDocument(d1)
+ writer.addDocument(d2)
+ writer.optimize()
+ writer.close()
+
+ searcher = IndexSearcher(store)
+ searcher.setSimilarity(SimpleSimilarity())
+
+ a = Term("field", "a")
+ b = Term("field", "b")
+ c = Term("field", "c")
+
+ class hitCollector1(PythonHitCollector):
+ def __init__(_self, score):
+ super(hitCollector1, _self).__init__()
+ _self.score = score
+ def collect(_self, doc, score):
+ self.assertEqual(score, _self.score)
+
+ searcher.search(TermQuery(b), hitCollector1(1.0))
+
+ bq = BooleanQuery()
+ bq.add(TermQuery(a), BooleanClause.Occur.SHOULD)
+ bq.add(TermQuery(b), BooleanClause.Occur.SHOULD)
+
+ class hitCollector2(PythonHitCollector):
+ def collect(_self, doc, score):
+ self.assertEqual(score, doc + 1)
+
+ searcher.search(bq, hitCollector2())
+
+ pq = PhraseQuery()
+ pq.add(a)
+ pq.add(c)
+
+ searcher.search(pq, hitCollector1(1.0))
+
+ pq.setSlop(2)
+ searcher.search(pq, hitCollector1(2.0))
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ main()
+ except:
+ pass
+ else:
+ main()
Propchange: lucene/pylucene/trunk/test/test_Similarity.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_Similarity.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_Sort.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_Sort.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_Sort.py (added)
+++ lucene/pylucene/trunk/test/test_Sort.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,630 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+import re
+
+from unittest import TestCase, main
+from lucene import *
+
+
+class SortTestCase(TestCase):
+ """
+ Unit tests for sorting code, ported from Java Lucene
+ """
+
+ # document data:
+ # the tracer field is used to determine which document was hit
+ # the contents field is used to search and sort by relevance
+ # the int field to sort by int
+ # the float field to sort by float
+ # the string field to sort by string
+
+ data = [
+ # tracer contents int float string custom
+ [ "A", "x a", "5", "4f", "c", "A-3" ],
+ [ "B", "y a", "5", "3.4028235E38", "i", "B-10" ],
+ [ "C", "x a b c", "2147483647", "1.0", "j", "A-2" ],
+ [ "D", "y a b c", "-1", "0.0f", "a", "C-0" ],
+ [ "E", "x a b c d", "5", "2f", "h", "B-8" ],
+ [ "F", "y a b c d", "2", "3.14159f", "g", "B-1" ],
+ [ "G", "x a b c d", "3", "-1.0", "f", "C-100" ],
+ [ "H", "y a b c d", "0", "1.4E-45", "e", "C-88" ],
+ [ "I", "x a b c d e f", "-2147483648", "1.0e+0", "d", "A-10" ],
+ [ "J", "y a b c d e f", "4", ".5", "b", "C-7" ],
+ [ "W", "g", "1", None, None, None ],
+ [ "X", "g", "1", "0.1", None, None ],
+ [ "Y", "g", "1", "0.2", None, None ],
+ [ "Z", "f g", None, None, None, None ]
+ ]
+
+
+ def _getIndex(self, even, odd):
+ """
+ Create an index of all the documents, or just the x,
+ or just the y documents
+ """
+
+ indexStore = RAMDirectory()
+ writer = IndexWriter(indexStore, SimpleAnalyzer(), True)
+
+ for i in xrange(0, len(self.data)):
+ if i % 2 == 0 and even or i % 2 == 1 and odd:
+ doc = Document()
+ doc.add(Field("tracer", self.data[i][0],
+ Field.Store.YES, Field.Index.NO))
+ doc.add(Field("contents", self.data[i][1],
+ Field.Store.NO, Field.Index.TOKENIZED))
+ if self.data[i][2] is not None:
+ doc.add(Field("int", self.data[i][2],
+ Field.Store.NO, Field.Index.UN_TOKENIZED))
+ if self.data[i][3] is not None:
+ doc.add(Field("float", self.data[i][3],
+ Field.Store.NO, Field.Index.UN_TOKENIZED))
+ if self.data[i][4] is not None:
+ doc.add(Field("string", self.data[i][4],
+ Field.Store.NO, Field.Index.UN_TOKENIZED))
+ if self.data[i][5] is not None:
+ doc.add(Field("custom", self.data[i][5],
+ Field.Store.NO, Field.Index.UN_TOKENIZED))
+ writer.addDocument(doc)
+
+ writer.optimize()
+ writer.close()
+
+ return IndexSearcher(indexStore)
+
+ def _getFullIndex(self):
+ return self._getIndex(True, True)
+
+ def _getXIndex(self):
+ return self._getIndex(True, False)
+
+ def _getYIndex(self):
+ return self._getIndex(False, True)
+
+ def _getEmptyIndex(self):
+ return self._getIndex(False, False)
+
+ def _assertMatches(self, searcher, query, sort, expectedResult):
+ """
+ Make sure the documents returned by the search match the expected list
+ """
+
+ buff = ''.join([''.join(Hit.cast_(hit).getDocument().getValues("tracer"))
+ for hit in searcher.search(query, sort)])
+
+ self.assertEqual(expectedResult, buff)
+
+ def _assertMatchesPattern(self, searcher, query, sort, pattern):
+ """
+ make sure the documents returned by the search match the expected
+ list pattern
+ """
+
+ buff = ''.join([''.join(Hit.cast_(hit).getDocument().getValues("tracer"))
+ for hit in searcher.search(query, sort)])
+
+ self.assert_(re.compile(pattern).match(buff))
+
+ def _getComparatorSource(self):
+ return self._getComparator()
+
+ def _getComparable(self, termtext):
+
+ class comparable(PythonComparable):
+ def __init__(self, termText):
+ super(comparable, self).__init__()
+ self.string_part, self.int_part = termText.split('-')
+ self.int_part = int(self.int_part)
+ def compareTo(self, o):
+ return (cmp(self.string_part, o.string_part) or
+ cmp(self.int_part, o.int_part))
+
+ return comparable(termtext)
+
+ def _getComparator(self):
+
+ class comparator(PythonSortComparator):
+
+ def getComparable(_self, termText):
+ return self._getComparable(termText)
+
+ def newComparator(_self, reader, fieldname):
+ enumerator = reader.terms(Term(fieldname, ""))
+
+ class comparator(PythonScoreDocComparator):
+ def __init__(_self, cache):
+ super(comparator, _self).__init__()
+ _self.cache = cache
+ def compare(_self, i, j):
+ return _self.cache[i.doc].compareTo(_self.cache[j.doc])
+ def sortType(_self):
+ return SortField.CUSTOM
+ def sortValue(_self, i):
+ return _self.cache[i.doc]
+
+ try:
+ cache = self._fillCache(reader, enumerator, fieldname)
+ return comparator(cache)
+ finally:
+ enumerator.close()
+
+ return comparator()
+
+ def _fillCache(self, reader, enumerator, fieldName):
+ """
+ Returns an array of objects which represent that natural order
+ of the term values in the given field.
+
+ @param reader Terms are in this index.
+ @param enumerator Use this to get the term values and TermDocs.
+ @param fieldname Comparables should be for this field.
+ @return Array of objects representing natural order of terms in field.
+ """
+
+ retArray = [None] * reader.maxDoc()
+ if len(retArray) > 0:
+ termDocs = reader.termDocs()
+ try:
+ if enumerator.term() is None:
+ raise RuntimeError, "no terms in field " + fieldName
+ while True:
+ term = enumerator.term()
+ if term.field() != fieldName:
+ break
+ termval = self._getComparable(term.text())
+ termDocs.seek(enumerator)
+ while termDocs.next():
+ retArray[termDocs.doc()] = termval
+ if not enumerator.next():
+ break
+ finally:
+ termDocs.close()
+
+ return retArray
+
+ def _runMultiSorts(self, multi):
+ """
+ runs a variety of sorts useful for multisearchers
+ """
+
+ sort = Sort()
+
+ sort.setSort(SortField.FIELD_DOC)
+ self._assertMatchesPattern(multi, self.queryA, sort,
+ "[AB]{2}[CD]{2}[EF]{2}[GH]{2}[IJ]{2}")
+
+ sort.setSort(SortField("int", SortField.INT))
+ self._assertMatchesPattern(multi, self.queryA, sort,
+ "IDHFGJ[ABE]{3}C")
+
+ sort.setSort([SortField("int", SortField.INT),
+ SortField.FIELD_DOC])
+ self._assertMatchesPattern(multi, self.queryA, sort,
+ "IDHFGJ[AB]{2}EC")
+
+ sort.setSort("int")
+ self._assertMatchesPattern(multi, self.queryA, sort,
+ "IDHFGJ[AB]{2}EC")
+
+ sort.setSort([SortField("float", SortField.FLOAT),
+ SortField.FIELD_DOC])
+ self._assertMatchesPattern(multi, self.queryA, sort,
+ "GDHJ[CI]{2}EFAB")
+
+ sort.setSort("float")
+ self._assertMatchesPattern(multi, self.queryA, sort,
+ "GDHJ[CI]{2}EFAB")
+
+ sort.setSort("string")
+ self._assertMatches(multi, self.queryA, sort, "DJAIHGFEBC")
+
+ sort.setSort("int", True)
+ self._assertMatchesPattern(multi, self.queryA, sort,
+ "C[AB]{2}EJGFHDI")
+
+ sort.setSort("float", True)
+ self._assertMatchesPattern(multi, self.queryA, sort,
+ "BAFE[IC]{2}JHDG")
+
+ sort.setSort("string", True)
+ self._assertMatches(multi, self.queryA, sort, "CBEFGHIAJD")
+
+ sort.setSort([SortField("string", Locale.US)])
+ self._assertMatches(multi, self.queryA, sort, "DJAIHGFEBC")
+
+ sort.setSort([SortField("string", Locale.US, True)])
+ self._assertMatches(multi, self.queryA, sort, "CBEFGHIAJD")
+
+ sort.setSort(["int", "float"])
+ self._assertMatches(multi, self.queryA, sort, "IDHFGJEABC")
+
+ sort.setSort(["float", "string"])
+ self._assertMatches(multi, self.queryA, sort, "GDHJICEFAB")
+
+ sort.setSort("int")
+ self._assertMatches(multi, self.queryF, sort, "IZJ")
+
+ sort.setSort("int", True)
+ self._assertMatches(multi, self.queryF, sort, "JZI")
+
+ sort.setSort("float")
+ self._assertMatches(multi, self.queryF, sort, "ZJI")
+
+ sort.setSort("string")
+ self._assertMatches(multi, self.queryF, sort, "ZJI")
+
+ sort.setSort("string", True)
+ self._assertMatches(multi, self.queryF, sort, "IJZ")
+
+ def _getScores(self, hits):
+
+ scoreMap = {}
+
+ for hit in hits:
+ hit = Hit.cast_(hit)
+ doc = hit.getDocument()
+ v = doc.getValues("tracer")
+ self.assertEqual(len(v), 1)
+ scoreMap[v[0]] = float(hit.getScore())
+
+ return scoreMap
+
+ def _assertSameValues(self, m1, m2):
+ """
+ make sure all the values in the maps match
+ """
+
+ n = len(m1)
+ m = len(m2)
+ self.assertEqual(n, m)
+
+ for key in m1.iterkeys():
+ self.assertEqual(m1[key], m2[key])
+
+ def setUp(self):
+
+ self.full = self._getFullIndex()
+ self.searchX = self._getXIndex()
+ self.searchY = self._getYIndex()
+ self.queryX = TermQuery(Term("contents", "x"))
+ self.queryY = TermQuery(Term("contents", "y"))
+ self.queryA = TermQuery(Term("contents", "a"))
+ self.queryF = TermQuery(Term("contents", "f"))
+
+ def tearDown(self):
+
+ del self.full
+ del self.queryX
+ del self.queryY
+ del self.queryA
+ del self.queryF
+ del self.searchX
+ del self.searchY
+
+ def testBuiltInSorts(self):
+ """
+ test the sorts by score and document number
+ """
+
+ sort = Sort()
+ self._assertMatches(self.full, self.queryX, sort, "ACEGI")
+ self._assertMatches(self.full, self.queryY, sort, "BDFHJ")
+
+ sort.setSort(SortField.FIELD_DOC)
+ self._assertMatches(self.full, self.queryX, sort, "ACEGI")
+ self._assertMatches(self.full, self.queryY, sort, "BDFHJ")
+
+ def testTypedSort(self):
+ """
+ test sorts where the type of field is specified
+ """
+
+ sort = Sort()
+ sort.setSort([SortField("int", SortField.INT),
+ SortField.FIELD_DOC])
+ self._assertMatches(self.full, self.queryX, sort, "IGAEC")
+ self._assertMatches(self.full, self.queryY, sort, "DHFJB")
+
+ sort.setSort([SortField("float", SortField.FLOAT),
+ SortField.FIELD_DOC])
+ self._assertMatches(self.full, self.queryX, sort, "GCIEA")
+ self._assertMatches(self.full, self.queryY, sort, "DHJFB")
+
+ sort.setSort([SortField("string", SortField.STRING),
+ SortField.FIELD_DOC])
+ self._assertMatches(self.full, self.queryX, sort, "AIGEC")
+ self._assertMatches(self.full, self.queryY, sort, "DJHFB")
+
+
+ def testEmptyIndex(self):
+ """
+ test sorts when there's nothing in the index
+ """
+
+ sort = Sort()
+ empty = self._getEmptyIndex()
+
+ self._assertMatches(empty, self.queryX, sort, "")
+
+ sort.setSort(SortField.FIELD_DOC)
+ self._assertMatches(empty, self.queryX, sort, "")
+
+ sort.setSort([SortField("int", SortField.INT),
+ SortField.FIELD_DOC])
+ self._assertMatches(empty, self.queryX, sort, "")
+
+ sort.setSort([SortField("string", SortField.STRING, True),
+ SortField.FIELD_DOC])
+ self._assertMatches(empty, self.queryX, sort, "")
+
+ sort.setSort([SortField("float", SortField.FLOAT),
+ SortField("string", SortField.STRING)])
+ self._assertMatches(empty, self.queryX, sort, "")
+
+
+ def testAutoSort(self):
+ """
+ test sorts where the type of field is determined dynamically
+ """
+
+ sort = Sort()
+
+ sort.setSort("int")
+ self._assertMatches(self.full, self.queryX, sort, "IGAEC")
+ self._assertMatches(self.full, self.queryY, sort, "DHFJB")
+
+ sort.setSort("float")
+ self._assertMatches(self.full, self.queryX, sort, "GCIEA")
+ self._assertMatches(self.full, self.queryY, sort, "DHJFB")
+
+ sort.setSort("string")
+ self._assertMatches(self.full, self.queryX, sort, "AIGEC")
+ self._assertMatches(self.full, self.queryY, sort, "DJHFB")
+
+ def testReverseSort(self):
+ """
+ test sorts in reverse
+ """
+
+ sort = Sort()
+
+ sort.setSort([SortField(None, SortField.SCORE, True),
+ SortField.FIELD_DOC])
+ self._assertMatches(self.full, self.queryX, sort, "IEGCA")
+ self._assertMatches(self.full, self.queryY, sort, "JFHDB")
+
+ sort.setSort(SortField(None, SortField.DOC, True))
+ self._assertMatches(self.full, self.queryX, sort, "IGECA")
+ self._assertMatches(self.full, self.queryY, sort, "JHFDB")
+
+ sort.setSort("int", True)
+ self._assertMatches(self.full, self.queryX, sort, "CAEGI")
+ self._assertMatches(self.full, self.queryY, sort, "BJFHD")
+
+ sort.setSort("float", True)
+ self._assertMatches(self.full, self.queryX, sort, "AECIG")
+ self._assertMatches(self.full, self.queryY, sort, "BFJHD")
+
+ sort.setSort("string", True)
+ self._assertMatches(self.full, self.queryX, sort, "CEGIA")
+ self._assertMatches(self.full, self.queryY, sort, "BFHJD")
+
+ def testEmptyFieldSort(self):
+ """
+ test sorting when the sort field is empty (undefined)
+ for some of the documents
+ """
+
+ sort = Sort()
+
+ sort.setSort("string")
+ self._assertMatches(self.full, self.queryF, sort, "ZJI")
+
+ sort.setSort("string", True)
+ self._assertMatches(self.full, self.queryF, sort, "IJZ")
+
+ sort.setSort("int")
+ self._assertMatches(self.full, self.queryF, sort, "IZJ")
+
+ sort.setSort("int", True)
+ self._assertMatches(self.full, self.queryF, sort, "JZI")
+
+ sort.setSort("float")
+ self._assertMatches(self.full, self.queryF, sort, "ZJI")
+
+ sort.setSort("float", True)
+ self._assertMatches(self.full, self.queryF, sort, "IJZ")
+
+ def testSortCombos(self):
+ """
+ test sorts using a series of fields
+ """
+
+ sort = Sort()
+
+ sort.setSort(["int", "float"])
+ self._assertMatches(self.full, self.queryX, sort, "IGEAC")
+
+ sort.setSort([SortField("int", True),
+ SortField(None, SortField.DOC, True)])
+ self._assertMatches(self.full, self.queryX, sort, "CEAGI")
+
+ sort.setSort(["float", "string"])
+ self._assertMatches(self.full, self.queryX, sort, "GICEA")
+
+ def testLocaleSort(self):
+ """
+ test using a Locale for sorting strings
+ """
+
+ sort = Sort()
+
+ sort.setSort([SortField("string", Locale.US)])
+ self._assertMatches(self.full, self.queryX, sort, "AIGEC")
+ self._assertMatches(self.full, self.queryY, sort, "DJHFB")
+
+ sort.setSort([SortField("string", Locale.US, True)])
+ self._assertMatches(self.full, self.queryX, sort, "CEGIA")
+ self._assertMatches(self.full, self.queryY, sort, "BFHJD")
+
+ def testCustomSorts(self):
+ """
+ test a custom sort function
+ """
+
+ sort = Sort()
+
+ sort.setSort(SortField("custom", self._getComparatorSource()))
+ self._assertMatches(self.full, self.queryX, sort, "CAIEG")
+
+ sort.setSort(SortField("custom", self._getComparatorSource(), True))
+ self._assertMatches(self.full, self.queryY, sort, "HJDBF")
+
+ custom = self._getComparator()
+ sort.setSort(SortField("custom", custom))
+ self._assertMatches(self.full, self.queryX, sort, "CAIEG")
+
+ sort.setSort(SortField("custom", custom, True))
+ self._assertMatches(self.full, self.queryY, sort, "HJDBF")
+
+ def testMultiSort(self):
+ """
+ test a variety of sorts using more than one searcher
+ """
+
+ searcher = MultiSearcher([self.searchX, self.searchY])
+ self._runMultiSorts(searcher)
+
+ def testParallelMultiSort(self):
+ """
+ test a variety of sorts using a parallel multisearcher
+ """
+
+ searcher = ParallelMultiSearcher([self.searchX, self.searchY])
+ self._runMultiSorts(searcher)
+
+ def testNormalizedScores(self):
+ """
+ test that the relevancy scores are the same even if
+ hits are sorted
+ """
+
+ full = self.full
+
+ # capture relevancy scores
+ scoresX = self._getScores(full.search(self.queryX))
+ scoresY = self._getScores(full.search(self.queryY))
+ scoresA = self._getScores(full.search(self.queryA))
+
+ # we'll test searching locally and multi
+ # note: the multi test depends on each separate index containing
+ # the same documents as our local index, so the computed normalization
+ # will be the same. so we make a multi searcher over two equal document
+ # sets - not realistic, but necessary for testing.
+
+ queryX = self.queryX
+ queryY = self.queryY
+ queryA = self.queryA
+
+ multi = MultiSearcher([self.searchX, self.searchY])
+
+ gs = self._getScores
+
+ # change sorting and make sure relevancy stays the same
+
+ sort = Sort()
+
+ self._assertSameValues(scoresX, gs(full.search(queryX, sort)))
+ self._assertSameValues(scoresX, gs(multi.search(queryX, sort)))
+ self._assertSameValues(scoresY, gs(full.search(queryY, sort)))
+ self._assertSameValues(scoresY, gs(multi.search(queryY, sort)))
+ self._assertSameValues(scoresA, gs(full.search(queryA, sort)))
+ self._assertSameValues(scoresA, gs(multi.search(queryA, sort)))
+
+ sort.setSort(SortField.FIELD_DOC)
+ self._assertSameValues(scoresX, gs(full.search(queryX,sort)))
+ self._assertSameValues(scoresX, gs(multi.search(queryX, sort)))
+ self._assertSameValues(scoresY, gs(full.search(queryY, sort)))
+ self._assertSameValues(scoresY, gs(multi.search(queryY, sort)))
+ self._assertSameValues(scoresA, gs(full.search(queryA, sort)))
+ self._assertSameValues(scoresA, gs(multi.search(queryA, sort)))
+
+ sort.setSort("int")
+ self._assertSameValues(scoresX, gs(full.search(queryX, sort)))
+ self._assertSameValues(scoresX, gs(multi.search(queryX, sort)))
+ self._assertSameValues(scoresY, gs(full.search(queryY, sort)))
+ self._assertSameValues(scoresY, gs(multi.search(queryY, sort)))
+ self._assertSameValues(scoresA, gs(full.search(queryA, sort)))
+ self._assertSameValues(scoresA, gs(multi.search(queryA, sort)))
+
+ sort.setSort("float")
+ self._assertSameValues(scoresX, gs(full.search(queryX, sort)))
+ self._assertSameValues(scoresX, gs(multi.search(queryX, sort)))
+ self._assertSameValues(scoresY, gs(full.search(queryY, sort)))
+ self._assertSameValues(scoresY, gs(multi.search(queryY, sort)))
+ self._assertSameValues(scoresA, gs(full.search(queryA, sort)))
+ self._assertSameValues(scoresA, gs(multi.search(queryA, sort)))
+
+ sort.setSort("string")
+ self._assertSameValues(scoresX, gs(full.search(queryX, sort)))
+ self._assertSameValues(scoresX, gs(multi.search(queryX, sort)))
+ self._assertSameValues(scoresY, gs(full.search(queryY, sort)))
+ self._assertSameValues(scoresY, gs(multi.search(queryY, sort)))
+ self._assertSameValues(scoresA, gs(full.search(queryA, sort)))
+ self._assertSameValues(scoresA, gs(multi.search(queryA, sort)))
+
+ sort.setSort(["int", "float"])
+ self._assertSameValues(scoresX, gs(full.search(queryX, sort)))
+ self._assertSameValues(scoresX, gs(multi.search(queryX, sort)))
+ self._assertSameValues(scoresY, gs(full.search(queryY, sort)))
+ self._assertSameValues(scoresY, gs(multi.search(queryY, sort)))
+ self._assertSameValues(scoresA, gs(full.search(queryA, sort)))
+ self._assertSameValues(scoresA, gs(multi.search(queryA, sort)))
+
+ sort.setSort([SortField("int", True),
+ SortField(None, SortField.DOC, True)])
+ self._assertSameValues(scoresX, gs(full.search(queryX, sort)))
+ self._assertSameValues(scoresX, gs(multi.search(queryX, sort)))
+ self._assertSameValues(scoresY, gs(full.search(queryY, sort)))
+ self._assertSameValues(scoresY, gs(multi.search(queryY, sort)))
+ self._assertSameValues(scoresA, gs(full.search(queryA, sort)))
+ self._assertSameValues(scoresA, gs(multi.search(queryA, sort)))
+
+ sort.setSort(["float", "string"])
+ self._assertSameValues(scoresX, gs(full.search(queryX, sort)))
+ self._assertSameValues(scoresX, gs(multi.search(queryX, sort)))
+ self._assertSameValues(scoresY, gs(full.search(queryY, sort)))
+ self._assertSameValues(scoresY, gs(multi.search(queryY, sort)))
+ self._assertSameValues(scoresA, gs(full.search(queryA, sort)))
+ self._assertSameValues(scoresA, gs(multi.search(queryA, sort)))
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ env = lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ main()
+ except:
+ pass
+# refs = sorted(env._dumpRefs(classes=True).items(),
+# key=lambda x: x[1], reverse=True)
+# print refs[0:4]
+ else:
+ main()
Propchange: lucene/pylucene/trunk/test/test_Sort.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_Sort.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_StopAnalyzer.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_StopAnalyzer.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_StopAnalyzer.py (added)
+++ lucene/pylucene/trunk/test/test_StopAnalyzer.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,75 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+from unittest import TestCase, main
+from lucene import *
+
+
+class StopAnalyzerTestCase(TestCase):
+ """
+ Unit tests ported from Java Lucene
+ """
+
+ def setUp(self):
+
+ self.stop = StopAnalyzer()
+ self.inValidTokens = StopAnalyzer.ENGLISH_STOP_WORDS[:]
+
+ def testDefaults(self):
+
+ self.assert_(self.stop is not None)
+ reader = StringReader("This is a test of the english stop analyzer")
+ stream = self.stop.tokenStream("test", reader)
+ self.assert_(stream is not None)
+
+ try:
+ for token in stream:
+ self.assert_(token.termText() not in self.inValidTokens)
+ except Exception, e:
+ self.fail(str(e))
+
+ def testStopList(self):
+
+ stopWordsSet = []
+ stopWordsSet.append("good")
+ stopWordsSet.append("test")
+ stopWordsSet.append("analyzer")
+
+ newStop = StopAnalyzer(stopWordsSet)
+ reader = StringReader("This is a good test of the english stop analyzer")
+ stream = newStop.tokenStream("test", reader)
+ self.assert_(stream is not None)
+
+ try:
+ for token in stream:
+ text = token.termText()
+ self.assert_(text not in stopWordsSet)
+ except Exception, e:
+ self.fail(str(e))
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ main()
+ except:
+ pass
+ else:
+ main()
Propchange: lucene/pylucene/trunk/test/test_StopAnalyzer.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_StopAnalyzer.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_StopWords.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_StopWords.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_StopWords.py (added)
+++ lucene/pylucene/trunk/test/test_StopWords.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,50 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+from unittest import TestCase, main
+from lucene import *
+
+# run with -loop to test fix for string local ref leak reported
+# by Aaron Lav.
+
+class StopWordsTestCase(TestCase):
+
+ def setUp(self):
+
+ self.stop_set = ['the', 'and', 's']
+ self.reader = StringReader('foo')
+
+ def testStopWords(self):
+
+ try:
+ result = StandardTokenizer(self.reader)
+ result = StopFilter(result, self.stop_set)
+ except Exception, e:
+ self.fail(str(e))
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ main()
+ except:
+ pass
+ else:
+ main()
Propchange: lucene/pylucene/trunk/test/test_StopWords.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_StopWords.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_ThaiAnalyzer.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_ThaiAnalyzer.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_ThaiAnalyzer.py (added)
+++ lucene/pylucene/trunk/test/test_ThaiAnalyzer.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+from unittest import TestCase, main
+from lucene import ThaiAnalyzer, StringReader
+
+
+class ThaiAnalyzerTestCase(TestCase):
+
+ def assertAnalyzesTo(self, analyzer, input, output):
+
+ tokenStream = analyzer.tokenStream("dummy", StringReader(input))
+
+ for termText in output:
+ token = tokenStream.next()
+ self.assert_(token is not None)
+ self.assertEqual(token.termText(), termText)
+
+ self.assert_(not list(tokenStream))
+ tokenStream.close()
+
+ def testAnalyzer(self):
+
+ analyzer = ThaiAnalyzer()
+
+ self.assertAnalyzesTo(analyzer, u"", [])
+
+ self.assertAnalyzesTo(analyzer,
+ u"à¸à¸²à¸£à¸à¸µà¹à¹à¸à¹à¸à¹à¸à¸à¹à¸ªà¸à¸à¸§à¹à¸²à¸à¸²à¸à¸à¸µ",
+ [ u"à¸à¸²à¸£", u"à¸à¸µà¹", u"à¹à¸à¹", u"à¸à¹à¸à¸",
+ u"à¹à¸ªà¸à¸", u"วà¹à¸²", u"à¸à¸²à¸", u"à¸à¸µ" ])
+
+ self.assertAnalyzesTo(analyzer,
+ u"à¸à¸£à¸´à¸©à¸±à¸à¸à¸·à¹à¸ XY&Z - à¸à¸¸à¸¢à¸à¸±à¸ xyz@demo.com",
+ [ u"à¸à¸£à¸´à¸©à¸±à¸", u"à¸à¸·à¹à¸", u"xy&z", u"à¸à¸¸à¸¢", u"à¸à¸±à¸", u"xyz@demo.com" ])
+
+ # English stop words
+ self.assertAnalyzesTo(analyzer,
+ u"à¸à¸£à¸°à¹à¸¢à¸à¸§à¹à¸² The quick brown fox jumped over the lazy dogs",
+ [ u"à¸à¸£à¸°à¹à¸¢à¸", u"วà¹à¸²", u"quick", u"brown", u"fox",
+ u"jumped", u"over", u"lazy", u"dogs" ])
+
+
+if __name__ == "__main__":
+ import sys, lucene
+ lucene.initVM(lucene.CLASSPATH)
+ if '-loop' in sys.argv:
+ sys.argv.remove('-loop')
+ while True:
+ try:
+ main()
+ except:
+ pass
+ else:
+ main()
Propchange: lucene/pylucene/trunk/test/test_ThaiAnalyzer.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_ThaiAnalyzer.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_bug1564.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_bug1564.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_bug1564.py (added)
+++ lucene/pylucene/trunk/test/test_bug1564.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,53 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+import unittest
+from lucene import *
+
+class Test_Bug1564(unittest.TestCase):
+
+ def setUp(self):
+
+ self.analyzer = StandardAnalyzer()
+ self.store = RAMDirectory()
+
+ writer = IndexWriter(self.store, self.analyzer, True)
+ doc = Document()
+ doc.add(Field('all', u'windowpane beplaster rapacious \
+ catatonia gauntlet wynn depressible swede pick dressmake supreme \
+ jeremy plumb theoretic bureaucracy causation chartres equipoise \
+ dispersible careen heard',
+ Field.Store.NO, Field.Index.TOKENIZED))
+ doc.add(Field('id', '1', Field.Store.YES, Field.Index.NO))
+ writer.addDocument(doc)
+ writer.optimize()
+ writer.close()
+
+ def tearDown(self):
+ pass
+
+ def test_bug1564(self):
+
+ searcher = IndexSearcher(self.store)
+ query = QueryParser('all', self.analyzer).parse('supreme')
+ hits = searcher.search(query)
+ self.assertEqual(hits.length(), 1)
+
+
+if __name__ == '__main__':
+ import lucene
+ lucene.initVM(lucene.CLASSPATH)
+ unittest.main()
Propchange: lucene/pylucene/trunk/test/test_bug1564.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_bug1564.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_bug1763.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_bug1763.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_bug1763.py (added)
+++ lucene/pylucene/trunk/test/test_bug1763.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,64 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+import unittest
+from lucene import *
+
+class Test_Bug1763(unittest.TestCase):
+
+ def setUp(self):
+
+ self.analyzer = StandardAnalyzer()
+ self.d1 = RAMDirectory()
+ self.d2 = RAMDirectory()
+
+ w1, w2 = [IndexWriter(d, self.analyzer, True)
+ for d in [self.d1, self.d2]]
+ doc1 = Document()
+ doc2 = Document()
+ doc1.add(Field("all", "blah blah double blah Gesundheit",
+ Field.Store.NO, Field.Index.TOKENIZED))
+ doc1.add(Field('id', '1', Field.Store.YES, Field.Index.NO))
+ doc2.add(Field("all", "a quick brown test ran over the lazy data",
+ Field.Store.NO, Field.Index.TOKENIZED))
+ doc2.add(Field('id', '2',
+ Field.Store.YES, Field.Index.NO))
+ w1.addDocument(doc1)
+ w2.addDocument(doc2)
+ for w in [w1, w2]:
+ w.optimize()
+ w.close()
+
+ def tearDown(self):
+ pass
+
+ def test_bug1763(self):
+
+ w1 = IndexWriter(self.d1, self.analyzer, True)
+ w1.addIndexes([self.d2])
+ w1.optimize()
+ w1.close()
+
+ searcher = IndexSearcher(self.d1)
+ q = QueryParser('all', self.analyzer).parse('brown')
+ hits = searcher.search(q)
+ self.assertEqual(hits.doc(0).get('id'), '2')
+
+
+if __name__ == '__main__':
+ import lucene
+ lucene.initVM(lucene.CLASSPATH)
+ unittest.main()
Propchange: lucene/pylucene/trunk/test/test_bug1763.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_bug1763.py
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: lucene/pylucene/trunk/test/test_bug1842.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_bug1842.py?rev=732916&view=auto
==============================================================================
--- lucene/pylucene/trunk/test/test_bug1842.py (added)
+++ lucene/pylucene/trunk/test/test_bug1842.py Thu Jan 8 19:28:33 2009
@@ -0,0 +1,57 @@
+# ====================================================================
+# Copyright (c) 2004-2008 Open Source Applications Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+
+import unittest
+from lucene import *
+
+class Test_Bug1842(unittest.TestCase):
+
+ def setUp(self):
+
+ self.analyzer = StandardAnalyzer()
+ self.d1 = RAMDirectory()
+
+ w1 = IndexWriter(self.d1, self.analyzer, True)
+ doc1 = Document()
+ doc1.add(Field("all", "blah blah blah Gesundheit",
+ Field.Store.NO, Field.Index.TOKENIZED,
+ Field.TermVector.YES))
+ doc1.add(Field('id', '1',
+ Field.Store.YES, Field.Index.UN_TOKENIZED))
+ w1.addDocument(doc1)
+ w1.optimize()
+ w1.close()
+
+ def tearDown(self):
+ pass
+
+ def test_bug1842(self):
+ reader = IndexReader.open(self.d1)
+ searcher = IndexSearcher(self.d1)
+ q = TermQuery(Term("id", '1'))
+ hits = searcher.search(q)
+ freqvec = reader.getTermFreqVector(hits.id(0), "all")
+ terms = list(freqvec.getTerms())
+ terms.sort()
+ self.assert_(terms == ['blah', 'gesundheit'])
+
+ freqs = freqvec.getTermFrequencies()
+ self.assert_(freqs == [3, 1])
+
+if __name__ == '__main__':
+ import lucene
+ lucene.initVM(lucene.CLASSPATH)
+ unittest.main()
Propchange: lucene/pylucene/trunk/test/test_bug1842.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: lucene/pylucene/trunk/test/test_bug1842.py
------------------------------------------------------------------------------
svn:mime-type = text/plain