def _create_query_for_field(self, field, value, analyzer=None): """generate a field query this functions creates a field->value query :param field: The fieldname to be used :type field: str :param value: The wanted value of the field :type value: str :param analyzer: The analyzer to be used Possible analyzers are: - :attr:`CommonDatabase.ANALYZER_TOKENIZE` the field value is splitted to be matched word-wise - :attr:`CommonDatabase.ANALYZER_PARTIAL` the field value must start with the query string - :attr:`CommonDatabase.ANALYZER_EXACT` keep special characters and the like :type analyzer: bool :return: resulting query object :rtype: PyLucene.Query """ if analyzer is None: analyzer = self.analyzer if analyzer == self.ANALYZER_EXACT: analyzer_obj = PyLucene.KeywordAnalyzer() else: value = self._escape_term_value(value) analyzer_obj = PyLucene.StandardAnalyzer() qp = PyLucene.QueryParser(field, analyzer_obj) if (analyzer & self.ANALYZER_PARTIAL > 0): # PyLucene uses explicit wildcards for partial matching value += "*" return qp.parse(value)
def _create_query_for_string(self, text, require_all=True, analyzer=None): """generate a query for a plain term of a string query basically this function parses the string and returns the resulting query @param text: the query string @type text: str @param require_all: boolean operator (True -> AND (default) / False -> OR) @type require_all: bool @param analyzer: the analyzer to be used possible analyzers are: - L{CommonDatabase.ANALYZER_TOKENIZE} the field value is splitted to be matched word-wise - L{CommonDatabase.ANALYZER_PARTIAL} the field value must start with the query string - L{CommonDatabase.ANALYZER_EXACT} keep special characters and the like @type analyzer: bool @return: resulting query object @rtype: PyLucene.Query """ if analyzer is None: analyzer = self.analyzer if analyzer == self.ANALYZER_EXACT: analyzer_obj = PyLucene.KeywordAnalyzer() else: text = _escape_term_value(text) analyzer_obj = PyLucene.StandardAnalyzer() qp = PyLucene.QueryParser(UNNAMED_FIELD_NAME, analyzer_obj) if (analyzer & self.ANALYZER_PARTIAL > 0): # PyLucene uses explicit wildcards for partial matching text += "*" if require_all: qp.setDefaultOperator(qp.Operator.AND) else: qp.setDefaultOperator(qp.Operator.OR) return qp.parse(text)