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/11/25 06:53:49 UTC

svn commit: r883984 - in /lucene/pylucene/trunk: ./ samples/LuceneInAction/lia/advsearching/ samples/LuceneInAction/lia/analysis/ samples/LuceneInAction/lia/searching/ samples/LuceneInAction/lia/tools/

Author: vajda
Date: Wed Nov 25 05:53:48 2009
New Revision: 883984

URL: http://svn.apache.org/viewvc?rev=883984&view=rev
Log:
refreshed more Lucene in Action samples to 3.0 API

Modified:
    lucene/pylucene/trunk/Makefile
    lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/BooksLikeThis.py
    lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/SortingExample.py
    lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerDemo.py
    lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerUtils.py
    lucene/pylucene/trunk/samples/LuceneInAction/lia/searching/Explainer.py
    lucene/pylucene/trunk/samples/LuceneInAction/lia/tools/HighlightIt.py

Modified: lucene/pylucene/trunk/Makefile
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/Makefile?rev=883984&r1=883983&r2=883984&view=diff
==============================================================================
--- lucene/pylucene/trunk/Makefile (original)
+++ lucene/pylucene/trunk/Makefile Wed Nov 25 05:53:48 2009
@@ -197,6 +197,7 @@
            --package java.util \
                      java.util.Arrays \
                      java.text.SimpleDateFormat \
+                     java.text.DecimalFormat \
                      java.text.Collator \
            --package java.io java.io.StringReader \
                              java.io.InputStreamReader \
@@ -254,6 +255,12 @@
 test: install-test samples/LuceneInAction/index
 	find test -name 'test_*.py' | PYTHONPATH=$(BUILD_TEST) xargs -t -n 1 $(PYTHON)
 	ls samples/LuceneInAction/*Test.py | PYTHONPATH=$(BUILD_TEST) xargs -t -n 1 $(PYTHON)
+	$(PYTHON) samples/LuceneInAction/AnalyzerDemo.py
+	$(PYTHON) samples/LuceneInAction/AnalyzerUtils.py
+	$(PYTHON) samples/LuceneInAction/BooksLikeThis.py
+	$(PYTHON) samples/LuceneInAction/Explainer.py samples/LuceneInAction/index programming
+	$(PYTHON) samples/LuceneInAction/HighlightIt.py
+	$(PYTHON) samples/LuceneInAction/SortingExample.py
 
 
 ARCHIVE=pylucene-$(VERSION)-src.tar.gz

Modified: lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/BooksLikeThis.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/BooksLikeThis.py?rev=883984&r1=883983&r2=883984&view=diff
==============================================================================
--- lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/BooksLikeThis.py (original)
+++ lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/BooksLikeThis.py Wed Nov 25 05:53:48 2009
@@ -16,7 +16,7 @@
 
 from lucene import \
      Document, IndexReader, Term, BooleanQuery, IndexSearcher, TermQuery, \
-     FSDirectory, System, BooleanClause, Hit
+     SimpleFSDirectory, File, System, BooleanClause
 
 
 class BooksLikeThis(object):
@@ -24,9 +24,9 @@
     def main(cls, argv):
 
         indexDir = System.getProperty("index.dir")
-        directory = FSDirectory.getDirectory(indexDir, False)
+        directory = SimpleFSDirectory(File(indexDir))
 
-        reader = IndexReader.open(directory)
+        reader = IndexReader.open(directory, True)
         blt = BooksLikeThis(reader)
 
         for id in xrange(reader.maxDoc()):
@@ -73,12 +73,11 @@
                           BooleanClause.Occur.MUST_NOT)
 
         print "  Query:", likeThisQuery.toString("contents")
-        hits = self.searcher.search(likeThisQuery)
+        scoreDocs = self.searcher.search(likeThisQuery, 50).scoreDocs
 
         docs = []
-        for hit in hits:
-            hit = Hit.cast_(hit)
-            doc = hit.getDocument()
+        for scoreDoc in scoreDocs:
+            doc = self.searcher.doc(scoreDoc.doc)
             if len(docs) < max:
                 docs.append(doc)
             else:

Modified: lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/SortingExample.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/SortingExample.py?rev=883984&r1=883983&r2=883984&view=diff
==============================================================================
--- lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/SortingExample.py (original)
+++ lucene/pylucene/trunk/samples/LuceneInAction/lia/advsearching/SortingExample.py Wed Nov 25 05:53:48 2009
@@ -15,8 +15,9 @@
 import os
 
 from lucene import \
-     FSDirectory, Document, Field, IndexSearcher, SimpleAnalyzer, \
-     RangeQuery, Sort, SortField, DecimalFormat, System, Term
+    SimpleFSDirectory, Document, Field, IndexSearcher, StandardAnalyzer, \
+    MatchAllDocsQuery, Sort, SortField, DecimalFormat, System, File, \
+    TopFieldCollector, QueryParser, Version, BooleanQuery, BooleanClause
 
 
 class SortingExample(object):
@@ -25,51 +26,71 @@
 
         self.directory = directory
 
-    def displayHits(self, query, sort):
+    def displayResults(self, query, sort):
 
-        searcher = IndexSearcher(self.directory)
-        hits = searcher.search(query, sort)
+        searcher = IndexSearcher(self.directory, True)
+
+        fillFields = False
+        computeMaxScore = False
+        docsScoredInOrder = False
+        computeScores = True
+
+        collector = TopFieldCollector.create(sort, 20,
+                                             fillFields,
+                                             computeScores,
+                                             computeMaxScore,
+                                             docsScoredInOrder)
+
+        searcher.search(query, None, collector)
+        scoreDocs = collector.topDocs().scoreDocs
 
         print "\nResults for:", query, "sorted by", sort
         print "Title".rjust(30), "pubmonth".rjust(10), \
               "id".center(4), "score".center(15)
 
         scoreFormatter = DecimalFormat("0.######")
-        for i, doc in hits:
+        for scoreDoc in scoreDocs:
+            doc = searcher.doc(scoreDoc.doc)
             title = doc["title"]
             if len(title) > 30:
                 title = title[:30]
             print title.encode('ascii', 'replace').rjust(30), \
                   doc["pubmonth"].rjust(10), \
-                  str(hits.id(i)).center(4), \
-                  scoreFormatter.format(hits.score(i)).ljust(12)
+                  str(scoreDoc.doc).center(4), \
+                  scoreFormatter.format(scoreDoc.score).ljust(12)
             print "  ", doc["category"]
-            # print searcher.explain(query, hits.id(i))
+            # print searcher.explain(query, scoreDoc.doc)
 
         searcher.close()
 
     def main(cls, argv):
 
-        earliest = Term("pubmonth", "190001")
-        latest = Term("pubmonth", "201012")
-        allBooks = RangeQuery(earliest, latest, True)
+        allBooks = MatchAllDocsQuery()
+        parser = QueryParser(Version.LUCENE_CURRENT, "contents",
+                             StandardAnalyzer(Version.LUCENE_CURRENT))
+        query = BooleanQuery()
+        query.add(allBooks, BooleanClause.Occur.SHOULD)
+        query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD)
 
         indexDir = System.getProperty("index.dir")
-        directory = FSDirectory.getDirectory(indexDir, False)
-        example = SortingExample(directory)
+        directory = SimpleFSDirectory(File(indexDir))
 
-        example.displayHits(allBooks, Sort.RELEVANCE)
-        example.displayHits(allBooks, Sort.INDEXORDER)
-        example.displayHits(allBooks, Sort("category"))
-        example.displayHits(allBooks, Sort("pubmonth", True))
-
-        example.displayHits(allBooks,
-                            Sort([SortField("category"),
-                                  SortField.FIELD_SCORE,
-                                  SortField("pubmonth", SortField.INT, True)]))
-
-        example.displayHits(allBooks,
-                            Sort([SortField.FIELD_SCORE,
-                                  SortField("category")]))
+        example = SortingExample(directory)
+        example.displayResults(query, Sort.RELEVANCE)
+        example.displayResults(query, Sort.INDEXORDER)
+        example.displayResults(query, Sort(SortField("category",
+                                                     SortField.STRING)))
+        example.displayResults(query, Sort(SortField("pubmonth",
+                                                     SortField.INT, True)))
+
+        example.displayResults(query,
+                               Sort([SortField("category", SortField.STRING),
+                                     SortField.FIELD_SCORE,
+                                     SortField("pubmonth", SortField.INT, True)]))
+
+        example.displayResults(query,
+                               Sort([SortField.FIELD_SCORE,
+                                     SortField("category", SortField.STRING)]))
+        directory.close()
 
     main = classmethod(main)

Modified: lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerDemo.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerDemo.py?rev=883984&r1=883983&r2=883984&view=diff
==============================================================================
--- lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerDemo.py (original)
+++ lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerDemo.py Wed Nov 25 05:53:48 2009
@@ -14,7 +14,7 @@
 
 
 from lia.analysis.AnalyzerUtils import AnalyzerUtils
-from lucene import \
+from lucene import Version, \
      StopAnalyzer, SimpleAnalyzer, WhitespaceAnalyzer, StandardAnalyzer
 
 
@@ -25,8 +25,8 @@
     
     analyzers = [WhitespaceAnalyzer(),
                  SimpleAnalyzer(),
-                 StopAnalyzer(),
-                 StandardAnalyzer()]
+                 StopAnalyzer(Version.LUCENE_CURRENT),
+                 StandardAnalyzer(Version.LUCENE_CURRENT)]
 
     def main(cls, argv):
 
@@ -42,14 +42,14 @@
 
     def analyze(cls, text):
 
-        print'"Analyzing "', text, '"'
+        print 'Analyzing "%s"' %(text)
 
         for analyzer in cls.analyzers:
             name = type(analyzer).__name__
             print " %s:" %(name),
             AnalyzerUtils.displayTokens(analyzer, text)
-            print ''
-        print ''
+            print
+        print
 
     main = classmethod(main)
     analyze = classmethod(analyze)

Modified: lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerUtils.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerUtils.py?rev=883984&r1=883983&r2=883984&view=diff
==============================================================================
--- lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerUtils.py (original)
+++ lucene/pylucene/trunk/samples/LuceneInAction/lia/analysis/AnalyzerUtils.py Wed Nov 25 05:53:48 2009
@@ -59,7 +59,7 @@
         term = tokenStream.addAttribute(TermAttribute.class_)
 
         while tokenStream.incrementToken():
-            print "[%s]" %(term.term())
+            print "[%s]" %(term.term()),
 
     def displayTokensWithPositions(cls, analyzer, text):
 

Modified: lucene/pylucene/trunk/samples/LuceneInAction/lia/searching/Explainer.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/samples/LuceneInAction/lia/searching/Explainer.py?rev=883984&r1=883983&r2=883984&view=diff
==============================================================================
--- lucene/pylucene/trunk/samples/LuceneInAction/lia/searching/Explainer.py (original)
+++ lucene/pylucene/trunk/samples/LuceneInAction/lia/searching/Explainer.py Wed Nov 25 05:53:48 2009
@@ -16,7 +16,7 @@
 
 from lucene import \
      SimpleAnalyzer, Document, QueryParser, Explanation, \
-     IndexSearcher, FSDirectory, Hit
+     IndexSearcher, SimpleFSDirectory, File, Version
 
 
 class Explainer(object):
@@ -30,21 +30,18 @@
             indexDir = argv[1]
             queryExpression = argv[2]
 
-            directory = FSDirectory.getDirectory(indexDir, False)
-
-            query = QueryParser("contents",
+            directory = SimpleFSDirectory(File(indexDir))
+            query = QueryParser(Version.LUCENE_CURRENT, "contents",
                                 SimpleAnalyzer()).parse(queryExpression)
 
             print "Query:", queryExpression
 
             searcher = IndexSearcher(directory)
-            hits = searcher.search(query)
+            scoreDocs = searcher.search(query, 50).scoreDocs
 
-            for hit in hits:
-                hit = Hit.cast_(hit)
-                doc = hit.getDocument()
-                id = hit.getId()
-                explanation = searcher.explain(query, id)
+            for scoreDoc in scoreDocs:
+                doc = searcher.doc(scoreDoc.doc)
+                explanation = searcher.explain(query, scoreDoc.doc)
                 print "----------"
                 print doc["title"].encode('utf-8')
                 print explanation

Modified: lucene/pylucene/trunk/samples/LuceneInAction/lia/tools/HighlightIt.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/samples/LuceneInAction/lia/tools/HighlightIt.py?rev=883984&r1=883983&r2=883984&view=diff
==============================================================================
--- lucene/pylucene/trunk/samples/LuceneInAction/lia/tools/HighlightIt.py (original)
+++ lucene/pylucene/trunk/samples/LuceneInAction/lia/tools/HighlightIt.py Wed Nov 25 05:53:48 2009
@@ -15,7 +15,7 @@
 from sys import stdout
 
 from lucene import \
-     StandardAnalyzer, Term, TermQuery, StringReader, \
+     StandardAnalyzer, Term, TermQuery, StringReader, Version, \
      Fragmenter, Highlighter, QueryScorer, SimpleFragmenter, SimpleHTMLFormatter
 
 
@@ -51,7 +51,7 @@
         fragmenter = SimpleFragmenter(50)
         highlighter.setTextFragmenter(fragmenter)
 
-        analyzer = StandardAnalyzer()
+        analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
         tokenStream = analyzer.tokenStream("f", StringReader(cls.text))
         result = highlighter.getBestFragments(tokenStream, cls.text, 5, "...")