def main(args): # search corpus index db = datastore.corpus_db() sconn = xappy.SearchConnection(settings.XAPIAN_DB) query = ' '.join(args.query) print "Search {} documents for '{}'".format( sconn.get_doccount(), args.query ) q = sconn.query_parse(query, default_op=sconn.OP_AND) if args.category: qc = q.compose(q.OP_OR, [ sconn.query_field('category', c) for c in args.category ]) q = q & qc if args.date: qd = q.compose(q.OP_OR, [ sconn.query_field('date', d) for d in args.date ]) q = q & qd if args.date_start and args.date_end: qr = sconn.query_range('date', args.date_start, args.date_end) q = q.filter(qr) if args.sort: sortby = [tuple(args.sort.split(','))] else: sortby = None print 'Query: {!r}'.format(q) results = execute_query(sconn, q, args.offset, args.limit, getfacets=args.facet, allowfacets=('category',), sortby=sortby) if results.estimate_is_exact: print "Found {} results".format(results.matches_estimated) else: print "Found approximately {} results".format(results.matches_estimated) for i, result in enumerate(results, 1): doc = db[result.id] try: cat = result.get_terms('category').next() except StopIteration: cat = 'none' try: date = result.get_terms('date').next() except StopIteration: date = 'none' print "{:2}. {} -- {} -- {}\n\t{}\n\t{}\n".format( i, cat, doc['headline'], date, doc['url'], result.id) from IPython import embed; embed()
def _query(self, sconn, q, offset, limit): if q: q = sconn.query_parse(q) else: q = sconn.query_all() categories = self.get_args('category') if categories: qc = sconn.query_composite( sconn.OP_OR, [sconn.query_field('category', value) for value in categories]) #q = sconn.query_composite( # sconn.OP_AND, [q, qc]) q = q.filter(qc) date = self.get_arg('date') if date: qd = sconn.query_field('date', date) q = q.filter(qd) sortby = self.get_arg('sortby') if sortby: sortby = [sortby] else: sortby = None self.log('Query: {!r}'.format(q)) results = execute_query(sconn, q, offset, limit, sortby=sortby) self.success({ 'total': results.matches_estimated, 'is_exact': results.estimate_is_exact, 'docs': [ dict(id=r.id, category=r.get_terms('category').next()) for r in results ] })
def _query(self, sconn, q, offset, limit): if q: q = sconn.query_parse(q) else: q = sconn.query_all() categories = self.get_args('category') if categories: qc = sconn.query_composite( sconn.OP_OR, [ sconn.query_field('category', value) for value in categories ]) #q = sconn.query_composite( # sconn.OP_AND, [q, qc]) q = q.filter(qc) date = self.get_arg('date') if date: qd = sconn.query_field('date', date) q = q.filter(qd) sortby = self.get_arg('sortby') if sortby: sortby = [sortby] else: sortby = None self.log('Query: {!r}'.format(q)) results = execute_query(sconn, q, offset, limit, sortby=sortby) self.success({ 'total': results.matches_estimated, 'is_exact': results.estimate_is_exact, 'docs': [ dict(id=r.id, category=r.get_terms('category').next()) for r in results ] })
def main(args): # search corpus index db = datastore.corpus_db() sconn = xappy.SearchConnection(settings.XAPIAN_DB) query = ' '.join(args.query) print "Search {} documents for '{}'".format(sconn.get_doccount(), args.query) q = sconn.query_parse(query, default_op=sconn.OP_AND) if args.category: qc = q.compose( q.OP_OR, [sconn.query_field('category', c) for c in args.category]) q = q & qc if args.date: qd = q.compose(q.OP_OR, [sconn.query_field('date', d) for d in args.date]) q = q & qd if args.date_start and args.date_end: qr = sconn.query_range('date', args.date_start, args.date_end) q = q.filter(qr) if args.sort: sortby = [tuple(args.sort.split(','))] else: sortby = None print 'Query: {!r}'.format(q) results = execute_query(sconn, q, args.offset, args.limit, getfacets=args.facet, allowfacets=('category', ), sortby=sortby) if results.estimate_is_exact: print "Found {} results".format(results.matches_estimated) else: print "Found approximately {} results".format( results.matches_estimated) for i, result in enumerate(results, 1): doc = db[result.id] try: cat = result.get_terms('category').next() except StopIteration: cat = 'none' try: date = result.get_terms('date').next() except StopIteration: date = 'none' print "{:2}. {} -- {} -- {}\n\t{}\n\t{}\n".format( i, cat, doc['headline'], date, doc['url'], result.id) from IPython import embed embed()