예제 #1
0
    def build(self, pf_query):
        """Builds a boolean query for elasticsearch from a postfix ordered lists.

        :param pf_query: Postfix ordered query list,
        :return: Elasticsearch Q object"""
        # Single term query
        stack = Stack()

        if len(pf_query) == 1:
            stack.push(Q('term', rake_keywords=pf_query[0][1]))

        for token in pf_query:
            if token in bool_values:
                q1 = stack.pop()
                q2 = stack.pop()

                result = q1 & q2 if token == 'AND' else q1 | q2
                stack.push(result)
            else:
                q = None
                if token[0] == 'KEYWORD':
                    q = Q('term', rake_keywords=token[1])
                else:
                    q = Q('term', rake_keywords=" ".join(token[1]))
                stack.push(q)

        return stack.pop()
예제 #2
0
파일: builder.py 프로젝트: morlick/BFEX
    def build(self, pf_query):
        """Builds a boolean query for elasticsearch from a postfix ordered lists.

        :param pf_query: Postfix ordered query list,
        :return: Elasticsearch Q object"""
        stack = Stack()

        if len(pf_query) == 1:
            # TODO: Pass in the search keyword as an argument
            stack.push(Q('match', keywords=pf_query[0][1]))

        for token in pf_query:
            if token in bool_values:
                q1 = stack.pop()
                q2 = stack.pop()

                result = q1 & q2 if token == 'AND' else q1 | q2
                stack.push(result)
            else:
                q = None
                if token[0] == 'KEYWORD':
                    q = Q('match', keywords=token[1])
                else:
                    q = Q('match', keywords=" ".join(token[1]))
                stack.push(q)

        return stack.pop()
예제 #3
0
    def build(self, pf_query, search_field="keywords"):
        """Builds a boolean query for elasticsearch from a postfix ordered lists.

        :param pf_query: Postfix ordered query list
        :param search_field: The field to search in the elastic search objects.
            Defaults to 'keywords'. Allows creation of queries for other objects.
        :return: Elasticsearch Q object"""
        stack = Stack()

        if len(pf_query) == 1:
            stack.push(Q('match', **{search_field: pf_query[0][1]}))

        for token in pf_query:
            if token in bool_values:
                q1 = stack.pop()
                q2 = stack.pop()

                result = q1 & q2 if token == 'AND' else q1 | q2
                stack.push(result)
            else:
                q = None
                if token[0] == 'KEYWORD':
                    q = Q('match', **{search_field: token[1]})
                else:
                    q = Q('match', **{search_field: " ".join(token[1])})
                stack.push(q)

        return stack.pop()
예제 #4
0
 def test_empty(self):
     stk = Stack()
     stk.push(5)
     stk.push(8)
     assert len(stk.stack) == 2
     stk.empty()
     assert len(stk.stack) == 0
예제 #5
0
    def test_peek(self):
        stk = Stack()
        assert stk.peek() is None

        stk.push(3)
        assert stk.peek() == 3
예제 #6
0
    def test_pop(self):
        stk = Stack()
        stk.push(5)

        assert stk.pop() == 5
        assert stk.pop() is None
예제 #7
0
 def test_push(self):
     stk = Stack()
     stk.push(3)
     assert len(stk.stack) == 1