Python whoosh.qparser.MultifieldParser() Examples

The following are 6 code examples of whoosh.qparser.MultifieldParser(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module whoosh.qparser , or try the search function .
Example #1
Source File: search.py    From markdown-search with GNU General Public License v2.0 6 votes vote down vote up
def search(self, query_list, fields=None):
        with self.ix.searcher() as searcher:
            query_string = " ".join(query_list)
            query = None
            if "\"" in query_string or ":" in query_string:
                query = QueryParser("content", self.schema).parse(query_string)
            elif len(fields) == 1 and fields[0] == "filename":
                pass
            elif len(fields) == 1 and fields[0] == "tags":
                pass
            elif len(fields) == 2:
                pass
            else:
                fields = ["tags", "headlines", "content", "filename", "doubleemphasiswords", "emphasiswords"]
            if not query:
                query = MultifieldParser(fields, schema=self.ix.schema).parse(query_string)
            parsed_query = "%s" % query
            print "query: %s" % parsed_query
            results = searcher.search(query, terms=False, scored=True, groupedby="path")
            key_terms = results.key_terms("tags", docs=100, numterms=100)
            tag_cloud = [keyword for keyword, score in key_terms]
            search_result = self.create_search_result(results)

        return parsed_query, search_result, tag_cloud 
Example #2
Source File: whoosh_backend.py    From flask-msearch with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def msearch(self, m, query, fields=None, limit=None, or_=True, **kwargs):
        '''
        set limit make search faster
        '''
        ix = self.index(m)
        if fields is None:
            fields = ix.fields

        def _parser(fieldnames, schema, group, **kwargs):
            return MultifieldParser(fieldnames, schema, group=group, **kwargs)

        group = OrGroup if or_ else AndGroup
        parser = getattr(m, "__msearch_parser__", _parser)(
            fields,
            ix.schema,
            group,
            **kwargs,
        )
        return ix.search(parser.parse(query), limit=limit) 
Example #3
Source File: db.py    From fac with MIT License 5 votes vote down vote up
def search(self, query, sortedby=None, limit=None):
        parser = qparser.MultifieldParser(
            ['owner', 'name', 'title', 'summary'],
            schema=self.schema
        )
        parser.add_plugin(qparser.FuzzyTermPlugin())

        if not isinstance(query, Query):
            query = parser.parse(query or 'name:*')

        with self.index.searcher() as searcher:
            if sortedby:
                facets = []
                for field in sortedby.split(','):
                    reverse = field.startswith('-')
                    if reverse:
                        field = field[1:]

                    if 'sort_' + field in self.schema:
                        field = 'sort_' + field
                    facets.append(FieldFacet(field, reverse=reverse))

                if len(facets) == 1:
                    sortedby = facets[0]
                else:
                    sortedby = MultiFacet(facets)

            for result in searcher.search(
                    query,
                    limit=limit,
                    sortedby=sortedby):

                d = JSONDict(self.db.mods[result['name_id']])
                d.score = result.score
                yield d 
Example #4
Source File: models.py    From realms-wiki with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, index_path, language):
        from whoosh import index as whoosh_index
        from whoosh.fields import Schema, TEXT, ID
        from whoosh import qparser
        from whoosh.highlight import UppercaseFormatter
        from whoosh.analysis import SimpleAnalyzer, LanguageAnalyzer
        from whoosh.lang import has_stemmer, has_stopwords
        import os

        if not has_stemmer(language) or not has_stopwords(language):
            # TODO Display a warning?
            analyzer = SimpleAnalyzer()
        else:
            analyzer = LanguageAnalyzer(language)

        self.schema = Schema(path=ID(unique=True, stored=True), body=TEXT(analyzer=analyzer))
        self.formatter = UppercaseFormatter()

        self.index_path = index_path

        if not os.path.exists(index_path):
            try:
                os.mkdir(index_path)
            except OSError as e:
                sys.exit("Error creating Whoosh index: %s" % e)

        if whoosh_index.exists_in(index_path):
            try:
                self.search_index = whoosh_index.open_dir(index_path)
            except whoosh_index.IndexError as e:
                sys.exit("Error opening whoosh index: {0}".format(e))
        else:
            self.search_index = whoosh_index.create_in(index_path, self.schema)

        self.query_parser = qparser.MultifieldParser(["body", "path"], schema=self.schema)
        self.query_parser.add_plugin(qparser.FuzzyTermPlugin()) 
Example #5
Source File: __init__.py    From pixelated-user-agent with GNU Affero General Public License v3.0 5 votes vote down vote up
def prepare_query(self, query):
        query = (
            query
            .replace('-in:', 'AND NOT tag:')
            .replace('in:all', '*')
        )
        return MultifieldParser(['body', 'subject', 'raw'], self._index.schema).parse(query) 
Example #6
Source File: query.py    From kerko with GNU General Public License v3.0 5 votes vote down vote up
def build_keywords_query(keywords):
    """
    Build parsers for a query.

    :param MultiDict keywords: The search texts keyed by scope key. If empty,
        the query will match every documents.
    """
    queries = []
    if keywords:
        composer = current_app.config['KERKO_COMPOSER']
        text_plugins = [
            plugins.PhrasePlugin(),
            plugins.GroupPlugin(),
            plugins.OperatorsPlugin(
                And=r"(?<=\s)" + re.escape(gettext("AND")) + r"(?=\s)",
                Or=r"(?<=\s)" + re.escape(gettext("OR")) + r"(?=\s)",
                Not=r"(^|(?<=(\s|[()])))" + re.escape(gettext("NOT")) + r"(?=\s)",
                AndNot=None,
                AndMaybe=None,
                Require=None
            ),
            plugins.BoostPlugin(),
        ]
        for key, value in keywords.items(multi=True):
            fields = [spec.key for spec in composer.fields.values() if key in spec.scopes]
            if not fields:
                raise KeyError  # No known field for that scope key.
            parser = MultifieldParser(
                fields, schema=composer.schema, plugins=text_plugins
            )
            queries.append(parser.parse(value))
    else:
        queries.append(Every())
    return And(queries)