PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / advsearching / SortingExample.py
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
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
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 # ====================================================================
14
15 import os
16
17 from lucene import \
18     SimpleFSDirectory, Document, Field, IndexSearcher, StandardAnalyzer, \
19     MatchAllDocsQuery, Sort, SortField, DecimalFormat, System, File, \
20     TopFieldCollector, QueryParser, Version, BooleanQuery, BooleanClause
21
22
23 class SortingExample(object):
24
25     def __init__(self, directory):
26
27         self.directory = directory
28
29     def displayResults(self, query, sort):
30
31         searcher = IndexSearcher(self.directory, True)
32
33         fillFields = False
34         computeMaxScore = False
35         docsScoredInOrder = False
36         computeScores = True
37
38         collector = TopFieldCollector.create(sort, 20,
39                                              fillFields,
40                                              computeScores,
41                                              computeMaxScore,
42                                              docsScoredInOrder)
43
44         searcher.search(query, None, collector)
45         scoreDocs = collector.topDocs().scoreDocs
46
47         print "\nResults for:", query, "sorted by", sort
48         print "Title".rjust(30), "pubmonth".rjust(10), \
49               "id".center(4), "score".center(15)
50
51         scoreFormatter = DecimalFormat("0.######")
52         for scoreDoc in scoreDocs:
53             doc = searcher.doc(scoreDoc.doc)
54             title = doc["title"]
55             if len(title) > 30:
56                 title = title[:30]
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)
63
64         searcher.close()
65
66     def main(cls, argv):
67
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)
74
75         indexDir = System.getProperty("index.dir")
76         directory = SimpleFSDirectory(File(indexDir))
77
78         example = SortingExample(directory)
79
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)))
86
87         example.displayResults(query,
88                                Sort([SortField("category", SortField.STRING),
89                                      SortField.FIELD_SCORE,
90                                      SortField("pubmonth", SortField.INT, True)]))
91
92         example.displayResults(query,
93                                Sort([SortField.FIELD_SCORE,
94                                      SortField("category", SortField.STRING)]))
95         directory.close()
96
97     main = classmethod(main)