def _xapian_database_connect(host, port, timeout, writable, data='.', log=logging): try: if writable: database = xapian.remote_open_writable(host, port, timeout) else: database = xapian.remote_open(host, port, timeout) database.keep_alive() except xapian.NetworkError as exc: raise InvalidIndexError("Unable to connect to index at %s:%s: %s" % (host, port, exc)) except xapian.DatabaseLockError as exc: raise InvalidIndexError("Unable to lock index at %s:%s %s" % (host, port, exc)) except xapian.DatabaseOpeningError as exc: raise InvalidIndexError("Unable to open index at %s:%s %s" % (host, port, exc)) except xapian.DatabaseError as exc: raise InvalidIndexError("Unable to use index at %s:%s %s" % (host, port, exc)) return database
def is_db_valid(self, entered_db): # - returns True if db can be opened by Xapian # - changes image_connected try: logging.debug('Checking if db %s is valid' % entered_db) # FIXME: use self.db instead of db, so that we avoid repeating # the opening. use keep_alive if remote if self.search_options['selected_localdb']: db = xapian.Database(entered_db) else: db_host, port = entered_db.split(':') db = xapian.remote_open(db_host, int(port)) except xapian.DatabaseOpeningError, e: logging.error('Error while opening %s (%s)' % (entered_db, e)) return False
def refresh_results(self, search_options): stemmer = xapian.Stem(search_options['selected_language']) if search_options['selected_localdb']: db = xapian.Database(search_options['selected_db']) else: db_host, port = search_options['selected_db'].split(':') db = xapian.remote_open(db_host, int(port)) qp = xapian.QueryParser() qp.set_stemmer(stemmer) qp.set_database(db) qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME) date_processor = xapian.DateValueRangeProcessor(MODEL_DOC_DATE) qp.add_valuerangeprocessor(date_processor) # FIXME: handle xapian.QueryParserError, xapian.NetworkTimeoutError # show error message in statusbar, user should be able to clear search with 'stop' query_search = qp.parse_query(search_options['entry_text'], xapian.QueryParser.FLAG_BOOLEAN) query_lang = xapian.Query(xapian.Query.OP_VALUE_RANGE, MODEL_DOC_LANG, search_options['selected_language'], search_options['selected_language']) query = xapian.Query(xapian.Query.OP_AND, query_search, query_lang) if search_options['allsources'] == False: for i, id in enumerate(search_options['selected_sources']): if i == 0: query_sources = xapian.Query(xapian.Query.OP_VALUE_RANGE, MODEL_DOC_SOURCEID, id, id) else: query_source_id = xapian.Query(xapian.Query.OP_VALUE_RANGE, MODEL_DOC_SOURCEID, id, id) query_sources = xapian.Query(xapian.Query.OP_OR, query_sources, query_source_id) query = xapian.Query(xapian.Query.OP_AND, query, query_sources) logging.debug("Setting query: %s" % query) enquire = xapian.Enquire(db) enquire.set_query(query) search_options['enquire'] = enquire search_options['db'] = db self.component.run_and_display(search_options, self.searchform.progressbar)
def get_sources_list(self, db_url): if self.search_options['selected_localdb']: db = xapian.Database(db_url) else: db_host, port = db_url.split(':') db = xapian.remote_open(db_host, int(port)) query = xapian.Query(xapian.Query.OP_VALUE_RANGE, 0, 'a', 'z') qp = xapian.QueryParser() qp.set_database(db) enquire = xapian.Enquire(db) enquire.set_query(query) enquire.set_collapse_key(MODEL_DOC_SOURCEID) mset = enquire.get_mset(0, 100, 0) list = [] for m in mset: list.append([m.document.get_value(4), m.document.get_value(5)]) return list
def create(dbpath): # return xapian.remote_open('ssh', dbpath, XAPIAN_REMOTE_OPEN_TIMEOUT) host, port = dbpath.split(':') return xapian.remote_open(host, int(port), XAPIAN_REMOTE_OPEN_TIMEOUT)