def test_boolean_query(self): prefix_q = search.PrefixQuery('someterm', boost=2) bool_q = search.BooleanQuery(must=prefix_q, must_not=prefix_q, should=prefix_q) exp = {'prefix': 'someterm', 'boost': 2.0} self.assertEqual({'conjuncts': [exp]}, bool_q.must.encodable) self.assertEqual({ 'min': 1, 'disjuncts': [exp] }, bool_q.should.encodable) self.assertEqual({ 'min': 1, 'disjuncts': [exp] }, bool_q.must_not.encodable) # Test multiple criteria in must and must_not pq_1 = search.PrefixQuery('someterm', boost=2) pq_2 = search.PrefixQuery('otherterm') bool_q = search.BooleanQuery(must=[pq_1, pq_2]) exp = { 'conjuncts': [{ 'prefix': 'someterm', 'boost': 2.0 }, { 'prefix': 'otherterm' }] } self.assertEqual({'must': exp}, bool_q.encodable)
def test_conjunction_query(self): cq = search.ConjunctionQuery() self.assertRaises(search.NoChildrenException, getattr, cq, 'encodable') cq.conjuncts.append(search.PrefixQuery('somePrefix')) self.assertEqual({'conjuncts': [{ 'prefix': 'somePrefix' }]}, cq.encodable)
def test_disjunction_query(self): dq = search.DisjunctionQuery() self.assertEqual(1, dq.min) self.assertRaises(search.NoChildrenException, getattr, dq, 'encodable') dq.disjuncts.append(search.PrefixQuery('somePrefix')) self.assertEqual({'min': 1, 'disjuncts': [{'prefix': 'somePrefix'}]}, dq.encodable) self.assertRaises(ValueError, setattr, dq, 'min', 0) dq.min = 2 self.assertRaises(search.NoChildrenException, getattr, dq, 'encodable')
def test_prefix_query(self): pq = search.PrefixQuery('someterm', boost=1.5) self.assertEqual({'prefix': 'someterm', 'boost': 1.5}, pq.encodable)
for row in result.rows(): print("Found row: {}".format(row)) print("Reported total rows: {}".format( result.metadata().metrics.total_rows)) except CouchbaseException as ex: import traceback traceback.print_exc() # end::search_basic_example[] # tag::search_result[] result = cluster.search_query("travel-sample-index", search.PrefixQuery("swim"), SearchOptions(fields=["description"])) for row in result.rows(): print("Score: {}".format(row.score)) print("Document Id: {}".format(row.id)) # print fields included in query: print(row.fields) # end::search_result[] # tag::limit_and_skip[] result = cluster.search_query("travel-sample-index", search.TermQuery("downtown"), SearchOptions(limit=4, skip=3)) # end::limit_and_skip[]