def testLowercasing(self): q = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer).parse("PrefixQuery*") self.assertEqual("prefixquery*", q.toString("field"), "lowercased") qp = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer) qp.setLowercaseExpandedTerms(False) q = qp.parse("PrefixQuery*") self.assertEqual("PrefixQuery*", q.toString("field"), "not lowercased")
def testSlop(self): q = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer).parse('"exact phrase"') self.assertEqual('"exact phrase"', q.toString("field"), "zero slop") qp = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer) qp.setPhraseSlop(5) q = qp.parse('"sloppy phrase"') self.assertEqual('"sloppy phrase"~5', q.toString("field"), "sloppy, implicitly")
def testSlop(self): q = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer).parse('"exact phrase"') self.assertEqual("\"exact phrase\"", q.toString("field"), "zero slop") qp = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer) qp.setPhraseSlop(5) q = qp.parse('"sloppy phrase"') self.assertEqual("\"sloppy phrase\"~5", q.toString("field"), "sloppy, implicitly")
def testPhraseQuery(self): analyzer = StandardAnalyzer(Version.LUCENE_24) q = QueryParser(Version.LUCENE_24, "field", analyzer).parse('"This is Some Phrase*"') self.assertEqual('"some phrase"', q.toString("field"), "analyzed") q = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer).parse('"term"') self.assert_(TermQuery.instance_(q), "reduced to TermQuery")
def main(cls): query = QueryParser(Version.LUCENE_CURRENT, "content", cls.synonymAnalyzer).parse('"fox jumps"') print "\"fox jumps\" parses to ", query.toString("content") print "From AnalyzerUtils.tokensFromAnalysis: " AnalyzerUtils.displayTokens(cls.synonymAnalyzer, "\"fox jumps\"") print ''
def testPhraseQuery(self): analyzer = StandardAnalyzer(Version.LUCENE_24) q = QueryParser(Version.LUCENE_24, "field", analyzer).parse('"This is Some Phrase*"') self.assertEqual("\"some phrase\"", q.toString("field"), "analyzed") q = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer).parse('"term"') self.assert_(TermQuery.instance_(q), "reduced to TermQuery")
def testPhraseQuery(self): analyzer = StandardAnalyzer(Version.LUCENE_24) q = QueryParser(Version.LUCENE_24, "field", analyzer).parse('"This is Some Phrase*"') self.assertEqual("\"some phrase\"", q.toString("field"), "analyzed") q = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer).parse('"term"') self.assert_("TermQuery" == q.getClassName(), "reduced to TermQuery")
def testAnalyzer(self): analyzer = StandardAnalyzer(Version.LUCENE_CURRENT) queryString = "category:/philosophy/eastern" query = QueryParser(Version.LUCENE_CURRENT, "contents", analyzer).parse(queryString) self.assertEqual("category:\"philosophy eastern\"", query.toString("contents"), "path got split, yikes!") perFieldAnalyzer = PerFieldAnalyzerWrapper(analyzer) perFieldAnalyzer.addAnalyzer("category", WhitespaceAnalyzer()) query = QueryParser(Version.LUCENE_CURRENT, "contents", perFieldAnalyzer).parse(queryString) self.assertEqual("category:/philosophy/eastern", query.toString("contents"), "leave category field alone")
def testBasicQueryParser(self): analyzer = SimpleAnalyzer() query = QueryParser(Version.LUCENE_CURRENT, "description", analyzer).parse("partnum:Q36 AND SPACE") scoreDocs = self.searcher.search(query, 50).scoreDocs self.assertEqual("+partnum:q +space", query.toString("description"), "note Q36 -> q") self.assertEqual(0, len(scoreDocs), "doc not found :(")
def testAnalyzer(self): analyzer = StandardAnalyzer(Version.LUCENE_CURRENT) queryString = "category:/philosophy/eastern" parser = QueryParser(Version.LUCENE_CURRENT, "contents", analyzer) parser.setAutoGeneratePhraseQueries(True) query = parser.parse(queryString) self.assertEqual("category:\"philosophy eastern\"", query.toString("contents"), "path got split, yikes!") perFieldAnalyzer = PerFieldAnalyzerWrapper(analyzer) perFieldAnalyzer.addAnalyzer("category", WhitespaceAnalyzer()) query = QueryParser(Version.LUCENE_CURRENT, "contents", perFieldAnalyzer).parse(queryString) self.assertEqual("category:/philosophy/eastern", query.toString("contents"), "leave category field alone")
def testBoost(self): q = QueryParser(Version.LUCENE_CURRENT, "field", self.analyzer).parse("term^2") self.assertEqual("term^2.0", q.toString("field"))