1 # ====================================================================
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 # ====================================================================
18 SimpleFSDirectory, Document, Field, IndexSearcher, StandardAnalyzer, \
19 MatchAllDocsQuery, Sort, SortField, DecimalFormat, System, File, \
20 TopFieldCollector, QueryParser, Version, BooleanQuery, BooleanClause
23 class SortingExample(object):
25 def __init__(self, directory):
27 self.directory = directory
29 def displayResults(self, query, sort):
31 searcher = IndexSearcher(self.directory, True)
34 computeMaxScore = False
35 docsScoredInOrder = False
38 collector = TopFieldCollector.create(sort, 20,
44 searcher.search(query, None, collector)
45 scoreDocs = collector.topDocs().scoreDocs
47 print "\nResults for:", query, "sorted by", sort
48 print "Title".rjust(30), "pubmonth".rjust(10), \
49 "id".center(4), "score".center(15)
51 scoreFormatter = DecimalFormat("0.######")
52 for scoreDoc in scoreDocs:
53 doc = searcher.doc(scoreDoc.doc)
57 print title.encode('ascii', 'replace').rjust(30), \
58 doc["pubmonth"].rjust(10), \
59 str(scoreDoc.doc).center(4), \
60 scoreFormatter.format(scoreDoc.score).ljust(12)
61 print " ", doc["category"]
62 # print searcher.explain(query, scoreDoc.doc)
68 allBooks = MatchAllDocsQuery()
69 parser = QueryParser(Version.LUCENE_CURRENT, "contents",
70 StandardAnalyzer(Version.LUCENE_CURRENT))
71 query = BooleanQuery()
72 query.add(allBooks, BooleanClause.Occur.SHOULD)
73 query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD)
75 indexDir = System.getProperty("index.dir")
76 directory = SimpleFSDirectory(File(indexDir))
78 example = SortingExample(directory)
80 example.displayResults(query, Sort.RELEVANCE)
81 example.displayResults(query, Sort.INDEXORDER)
82 example.displayResults(query,
83 Sort(SortField("category", SortField.STRING)))
84 example.displayResults(query,
85 Sort(SortField("pubmonth", SortField.INT, True)))
87 example.displayResults(query,
88 Sort([SortField("category", SortField.STRING),
89 SortField.FIELD_SCORE,
90 SortField("pubmonth", SortField.INT, True)]))
92 example.displayResults(query,
93 Sort([SortField.FIELD_SCORE,
94 SortField("category", SortField.STRING)]))
97 main = classmethod(main)