コード例 #1
0
ファイル: query.py プロジェクト: Blevene/binarypig
def get_query(s):
    import re
    queries = []

    i = 0
    freetext = ""
    for mat in re.finditer(r'(?P<name>\S+):(?P<value>"[^"]+"|\S+)\s*', s):
        freetext += s[i:mat.start()]
        i = mat.end()
        q = mat.groupdict()
        value = q['value'].strip('"')
        if value.endswith("*"):
            queries.append(WildcardQuery(field=q['name'], value=value))
        else:
            queries.append(TermQuery(field=q['name'], value=value))
    freetext += s[i:]
    freetext = freetext.strip()
    if freetext:
        if freetext == '*':
            queries.append(MatchAllQuery())
        else:
            queries.append(TextQuery("_all", freetext, operator='and'))

    if len(queries) == 1:
        return queries[0]
    else:
        q = BoolQuery()
        for query in queries:
            q.add_must(query)
        return q
コード例 #2
0
    def search_name(cls, search_phrase, limit=100, fields=None):
        """
        Search on elasticsearch server for the given search phrase
        """
        config = Pool().get('elasticsearch.configuration')(1)

        # Create connection with elastic server
        conn = config.get_es_connection(timeout=5)

        if conn is None:
            return []

        search_phrase = search_phrase.replace('%', '')

        # Search the query string in all fields
        query = BoolQuery(should=[
            QueryStringQuery(search_phrase, default_field='name'),
            QueryStringQuery(search_phrase, default_field='name.partial'),
            QueryStringQuery(search_phrase, default_field='name.metaphone'),
        ])

        # Handle the zip codes specially
        zip_codes = ZIP_RE.findall(search_phrase)
        if filter(None, zip_codes):
            query.add_should(
                QueryStringQuery(' '.join(zip_codes),
                                 default_field='addresses.full_address',
                                 boost=4))

        # TODO: Handle fields
        return conn.search(query,
                           doc_types=[config.make_type_name('party.party')],
                           size=limit)
コード例 #3
0
ファイル: query.py プロジェクト: zldww2011/binarypig
def get_query(s):
    import re
    queries = []

    i = 0
    freetext = ""
    for mat in re.finditer(r'(?P<name>\S+):(?P<value>"[^"]+"|\S+)\s*', s):
        freetext += s[i:mat.start()]
        i = mat.end()
        q = mat.groupdict()
        value = q['value'].strip('"')
        if value.endswith("*"):
            queries.append(WildcardQuery(field=q['name'], value=value))
        else:
            queries.append(TermQuery(field=q['name'], value=value))
    freetext += s[i:]
    freetext = freetext.strip()
    if freetext:
        if freetext == '*':
            queries.append(MatchAllQuery())
        else:
            queries.append(TextQuery("_all", freetext, operator='and'))

    if len(queries) == 1:
        return queries[0]
    else:
        q = BoolQuery()
        for query in queries:
            q.add_must(query)
        return q
コード例 #4
0
    def test_nested_filter(self):

        q = FilteredQuery(
            MatchAllQuery(),
            NestedFilter(
                'shares',
                BoolQuery(must=[
                    PrefixQuery('shares.orgid', 'abc'),
                    PrefixQuery('shares.role', '11')
                ])))
        resultset = self.conn.search(query=q,
                                     indices=self.index_name,
                                     doc_types=[self.document_type])
        self.assertEquals(resultset.total, 3)
        print ', '.join([r['body'] for r in resultset])

        q = FilteredQuery(
            MatchAllQuery(),
            NestedFilter(
                'shares',
                BoolQuery(must=[
                    PrefixQuery('shares.orgid', 'abc.de'),
                    PrefixQuery('shares.role', '111')
                ])))
        resultset = self.conn.search(query=q,
                                     indices=self.index_name,
                                     doc_types=[self.document_type])
        self.assertEquals(resultset.total, 1)
        print ', '.join([r['body'] for r in resultset])

        q = FilteredQuery(
            MatchAllQuery(),
            NestedFilter(
                'shares',
                BoolQuery(must=[
                    PrefixQuery('shares.orgid', 'abc.de.1'),
                    PrefixQuery('shares.role', '11')
                ])))
        resultset = self.conn.search(query=q,
                                     indices=self.index_name,
                                     doc_types=[self.document_type])
        self.assertEquals(resultset.total, 0)
        print ', '.join([r['body'] for r in resultset])

        q = FilteredQuery(
            MatchAllQuery(),
            NestedFilter(
                'shares',
                BoolQuery(must=[
                    PrefixQuery('shares.orgid', 'abc'),
                    PrefixQuery('shares.role', '111')
                ])))
        resultset = self.conn.search(query=q,
                                     indices=self.index_name,
                                     doc_types=[self.document_type])
        self.assertEquals(resultset.total, 2)
        print ', '.join([r['body'] for r in resultset])

        print
コード例 #5
0
ファイル: views.py プロジェクト: dcbtechnologies/hyperion
def parse(query):
    clauses = query.split(' ')
    q = BoolQuery()
    for clause in clauses:
        key, value = clause.split('=')
        if key == 'earliest':
            q.add_must(RangeQuery(ESRange('created_at', from_value='now' + value)))
        else:
            q.add_must(TermQuery(key, value))
    return q
コード例 #6
0
ファイル: test_nested.py プロジェクト: trb116/pythonanalyzer
    def test_nested_filter(self):
        q = FilteredQuery(MatchAllQuery(), TermFilter('_all', 'n_value1_1'))
        resultset = self.conn.search(query=q,
                                     indices=self.index_name,
                                     doc_types=[self.document_type])
        self.assertEqual(resultset.total, 2)

        q = FilteredQuery(MatchAllQuery(),
                          TermFilter('nested1.n_field1', 'n_value1_1'))
        resultset = self.conn.search(query=q,
                                     indices=self.index_name,
                                     doc_types=[self.document_type])
        self.assertEqual(resultset.total, 0)

        q = FilteredQuery(MatchAllQuery(),
                          TermFilter('nested1.n_field1', 'n_value1_1'))
        resultset = self.conn.search(query=q,
                                     indices=self.index_name,
                                     doc_types=[self.document_type])
        self.assertEqual(resultset.total, 0)

        q = FilteredQuery(
            MatchAllQuery(),
            NestedFilter(
                'nested1',
                BoolQuery(must=[TermQuery('nested1.n_field1', 'n_value1_1')])))
        resultset = self.conn.search(query=q,
                                     indices=self.index_name,
                                     doc_types=[self.document_type])
        self.assertEqual(resultset.total, 2)

        q = FilteredQuery(
            MatchAllQuery(),
            NestedFilter(
                'nested1',
                BoolQuery(must=[
                    TermQuery('nested1.n_field1', 'n_value1_1'),
                    TermQuery('nested1.n_field2', 'n_value2_1')
                ])))
        resultset = self.conn.search(query=q,
                                     indices=self.index_name,
                                     doc_types=[self.document_type])
        self.assertEqual(resultset.total, 1)
コード例 #7
0
ファイル: snippet.py プロジェクト: wangjiaxi/snippet-note
 def build_query(self):
     query_string = self.get_query_param()
     language = self.get_argument("language", None)
     if query_string:
         query = BoolQuery()
         query.add_should(TextQuery("description", query_string))
         query.add_should(TextQuery("snippet", query_string))
         query.add_should(TextQuery("user", query_string))
         if language and language in languages:
             query.add_must(TermQuery("language", language))
         else:
             query_languages = [l for l in query_string.split(' ') if l in languages]
             query.add_should(TextQuery("language", ' '.join(query_languages)))
     else:
         query = {}
         if language and language in languages:
             query["language"] = language
     return query