Ejemplo n.º 1
0
 def test_dynamic_document(self):
     self.assertIsInstance(DynamicDocument._id, AttributedField)
     self.assertNotIsInstance(DynamicDocument._id, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument._id.get_field().get_type(), String)
     self.assertEqual(collect_doc_classes(DynamicDocument._id), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.name, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.name.get_field().get_type(), Type)
     self.assert_expression(DynamicDocument.name, 'name')
     self.assertEqual(collect_doc_classes(DynamicDocument.name), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.status, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.status.get_field().get_type(), Type)
     self.assert_expression(DynamicDocument.status, 'status')
     self.assertEqual(collect_doc_classes(DynamicDocument.status), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.group, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.group.get_field().get_type(), Type)
     self.assert_expression(DynamicDocument.group, 'group')
     self.assertEqual(collect_doc_classes(DynamicDocument.group), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.group.name, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.group.name.get_field().get_type(), Type)
     self.assertEqual(DynamicDocument.group.name.get_field().get_name(), 'group.name')
     self.assert_expression(DynamicDocument.group.name, 'group.name')
     self.assertEqual(collect_doc_classes(DynamicDocument.group.name), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.group.name.raw, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.group.name.raw.get_field().get_type(), Type)
     self.assertEqual(DynamicDocument.group.name.raw.get_field().get_name(), 'group.name.raw')
     self.assert_expression(DynamicDocument.group.name.raw, 'group.name.raw')
     self.assertEqual(collect_doc_classes(DynamicDocument.group.name.raw), {DynamicDocument})
Ejemplo n.º 2
0
 def test_dynamic_document(self):
     self.assertIsInstance(DynamicDocument._id, AttributedField)
     self.assertNotIsInstance(DynamicDocument._id, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument._id.get_field().get_type(), String)
     self.assertEqual(collect_doc_classes(DynamicDocument._id), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.name, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.name.get_field().get_type(), Type)
     self.assert_expression(DynamicDocument.name, 'name')
     self.assertEqual(collect_doc_classes(DynamicDocument.name), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.status, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.status.get_field().get_type(), Type)
     self.assert_expression(DynamicDocument.status, 'status')
     self.assertEqual(collect_doc_classes(DynamicDocument.status), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.group, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.group.get_field().get_type(), Type)
     self.assert_expression(DynamicDocument.group, 'group')
     self.assertEqual(collect_doc_classes(DynamicDocument.group), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.group.name, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.group.name.get_field().get_type(), Type)
     self.assertEqual(DynamicDocument.group.name.get_field().get_name(), 'group.name')
     self.assert_expression(DynamicDocument.group.name, 'group.name')
     self.assertEqual(collect_doc_classes(DynamicDocument.group.name), {DynamicDocument})
     self.assertIsInstance(DynamicDocument.group.name.raw, DynamicAttributedField)
     self.assertIsInstance(DynamicDocument.group.name.raw.get_field().get_type(), Type)
     self.assertEqual(DynamicDocument.group.name.raw.get_field().get_name(), 'group.name.raw')
     self.assert_expression(DynamicDocument.group.name.raw, 'group.name.raw')
     self.assertEqual(collect_doc_classes(DynamicDocument.group.name.raw), {DynamicDocument})
Ejemplo n.º 3
0
    def test_multi_type_search(self):
        def seller_mapper(ids):
            return {id: '{0}-{0}'.format(id) for id in ids}

        def customer_mapper(ids):
            return {id: '{0}:{0}'.format(id) for id in ids}

        sq = (
            self.index.query(
                self.index.seller.name.first.match('Alex'),
                doc_cls=(self.index.seller, self.index.customer)
            )
            .with_instance_mapper({self.index.seller: seller_mapper,
                                   self.index.customer: customer_mapper})
            .filter(self.index.customer.birthday >= datetime.date(1960, 1, 1))
            .limit(2)
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.seller, self.index.customer})

        self.client.search = MagicMock(
            return_value={
                'hits': {
                    'hits': [
                        {
                            '_id': '3',
                            '_type': 'customer',
                            '_index': 'test',
                            '_score': 2.437682,
                            '_source': {
                                'name': {
                                    'first': 'Alex',
                                    'last': 'Exler'
                                },
                                'birthday': '1966-10-04'
                            },
                        },
                        {
                            '_id': '21',
                            '_type': 'seller',
                            '_index': 'test',
                            '_score': 2.290845,
                            '_source': {
                                'name': {
                                    'first': 'Alexa',
                                    'last': 'Chung'
                                },
                                'birthday': '1983-10-05',
                                'rating': 4.8
                            },
                        }
                    ],
                    'max_score': 2.437682,
                    'total': 73
                },
                'timed_out': False,
                'took': 25
            }
        )
        results = sq.get_result()
        self.assertEquals(len(results), 2)

        self.client.search.assert_called_with(
            index='test',
            doc_type='seller,customer',
            body={
                'query': {
                    'filtered': {
                        'query': {
                            'match': {'name.first': 'Alex'}
                        },
                        'filter': {
                            'range': {'birthday': {'gte': datetime.date(1960, 1, 1)}}
                        }
                    }
                },
                'size': 2
            },
        )

        self.assertEqual(len(sq.get_result().hits), 2)
        doc = sq.get_result().hits[0]
        self.assertIsInstance(doc, self.index.customer)
        self.assertEqual(doc._id, '3')
        self.assertEqual(doc._type, 'customer')
        self.assertEqual(doc._index, 'test')
        self.assertAlmostEqual(doc._score, 2.437682)
        self.assertEqual(doc.name.first, 'Alex')
        self.assertEqual(doc.name.last, 'Exler')
        self.assertEqual(doc.birthday, '1966-10-04')
        self.assertEqual(doc.instance, '3:3')
        doc = sq.get_result().hits[1]
        self.assertIsInstance(doc, self.index.seller)
        self.assertEqual(doc._id, '21')
        self.assertEqual(doc._type, 'seller')
        self.assertEqual(doc._index, 'test')
        self.assertAlmostEqual(doc._score, 2.290845)
        self.assertEqual(doc.name.first, 'Alexa')
        self.assertEqual(doc.name.last, 'Chung')
        self.assertEqual(doc.birthday, '1983-10-05')
        self.assertAlmostEqual(doc.rating, 4.8)
        self.assertEqual(doc.instance, '21-21')
Ejemplo n.º 4
0
    def test_search(self):
        class CarObject(object):
            def __init__(self, id):
                self.id = id
                self.name = '{0}:{0}'.format(id)

        def _obj_mapper(ids):
            return {id: CarObject(int(id)) for id in ids}
        obj_mapper = Mock(wraps=_obj_mapper)

        class NameDocument(Document):
            first = Field(String)
            last = Field(String)

        class CarSellerDocument(Document):
            name = Field(Object(NameDocument))
            rating = Field(Float)

        class CarDocument(Document):
            __doc_type__ = 'car'

            vendor = Field(String)
            model = Field(String)
            year = Field(Integer)
            seller = Field(Object(CarSellerDocument))

        self.client.search = MagicMock(
            return_value={
                'hits': {
                    'hits': [
                        {
                            '_id': '31888815',
                            '_type': 'car',
                            '_index': 'ads',
                            '_score': 4.675524,
                            '_source': {
                                'vendor': 'Subaru',
                                'model': 'Imprezza',
                                'year': 2004,
                            },
                        },
                        {
                            '_id': '987321',
                            '_type': 'car',
                            '_index': 'ads',
                            '_score': 3.654321,
                            '_source': {
                                'vendor': 'Subaru',
                                'model': 'Forester',
                                'year': 2007,
                            },
                        }
                    ],
                    'max_score': 4.675524,
                    'total': 6234
                },
                'timed_out': False,
                'took': 47
            }
        )
        sq = (
            self.index.query(
                CarDocument.seller.name.first.match('Alex'),
                search_type='dfs_query_then_fetch',
            )
            .filter(CarDocument.seller.rating > 4)
            .with_instance_mapper(obj_mapper)
        )
        self.assertEqual(collect_doc_classes(sq), {CarDocument})
        results = sq.get_result()
        self.assertEquals(len(results), 2)

        self.client.search.assert_called_with(
            index='test',
            doc_type='car',
            body={
                'query': {
                    'filtered': {
                        'query': {
                            'match': {'seller.name.first': 'Alex'}
                        },
                        'filter': {
                            'range': {'seller.rating': {'gt': 4.0}}
                        }
                    }
                }
            },
            search_type='dfs_query_then_fetch',
        )

        self.assertEqual(len(sq.get_result().hits), 2)
        doc = sq.get_result().hits[0]
        self.assertIsInstance(doc, CarDocument)
        self.assertEqual(doc._id, '31888815')
        self.assertEqual(doc._type, 'car')
        self.assertEqual(doc._index, 'ads')
        self.assertAlmostEqual(doc._score, 4.675524)
        self.assertEqual(doc.vendor, 'Subaru')
        self.assertEqual(doc.model, 'Imprezza')
        self.assertEqual(doc.year, 2004)
        self.assertEqual(doc.instance.id, 31888815)
        self.assertEqual(doc.instance.name, '31888815:31888815')
        doc = sq.get_result().hits[1]
        self.assertIsInstance(doc, CarDocument)
        self.assertEqual(doc._id, '987321')
        self.assertEqual(doc._type, 'car')
        self.assertEqual(doc._index, 'ads')
        self.assertAlmostEqual(doc._score, 3.654321)
        self.assertEqual(doc.vendor, 'Subaru')
        self.assertEqual(doc.model, 'Forester')
        self.assertEqual(doc.year, 2007)
        self.assertEqual(doc.instance.id, 987321)
        self.assertEqual(doc.instance.name, '987321:987321')
        self.assertEqual(obj_mapper.call_count, 1)
Ejemplo n.º 5
0
    def test_aggregations(self):
        f = DynamicDocument.fields

        sq = SearchQuery().aggregations(min_price=agg.Min(f.price))
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "min_price": {
                        "min": {"field": "price"}
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = SearchQuery().aggregations(genders=agg.Terms(f.gender))
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "genders": {
                        "terms": {"field": "gender"}
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .aggregations(
                type=agg.Terms(f.type, aggs={'min_price': agg.Min(f.price)})
            )
        )
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "type": {
                        "terms": {"field": "type"},
                        "aggregations": {
                            "min_price": {
                                "min": {"field": "price"}
                            }
                        }
                    }
                }
            },
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .aggregations(
                top_tags=(
                    agg.Terms(
                        f.tags,
                        size=3,
                        aggs={
                            'top_tag_hits': agg.TopHits(
                                sort=f.last_activity_date.desc(),
                                size=1,
                                _source=Params(include=[f.title]))
                        }
                    )
                )
            )
        )
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "top_tags": {
                        "terms": {
                            "field": "tags",
                            "size": 3
                        },
                        "aggregations": {
                            "top_tag_hits": {
                                "top_hits": {
                                    "sort": {
                                        "last_activity_date": "desc"
                                    },
                                    "_source": {
                                        "include": ["title"]
                                    },
                                    "size" : 1
                                }
                            }
                        }
                    }
                }
            }  
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .aggregations({
                'top_sites': agg.Terms(
                    f.domain,
                    order=Sort('top_hit', 'desc'),
                    aggs={
                        'top_tags_hits': agg.TopHits(),
                        'top_hit': agg.Max(script='_doc.score'),
                    }
                )
            })
        )
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "top_sites": {
                        "terms": {
                            "field": "domain",
                            "order": {
                                "top_hit": "desc"
                            }
                        },
                        "aggregations": {
                            "top_tags_hits": {
                                "top_hits": {}
                            },
                            "top_hit" : {
                                "max": {
                                    "script": "_doc.score"
                                }
                            }
                        }
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})
Ejemplo n.º 6
0
    def test_search_query_compile(self):
        f = DynamicDocument.fields

        sq = SearchQuery()
        self.assert_expression(sq, {})
        self.assertEqual(collect_doc_classes(sq), set())

        sq = SearchQuery(Term(f.user, 'kimchy')).limit(10).offset(0)
        self.assert_expression(
            sq,
            {
                "from": 0,
                "size": 10,
                "query": {
                    "term": {"user": "******"}
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = SearchQuery(Term(f.user, 'kimchy')).filter(f.age >= 16)
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "query": {
                            "term": {"user": "******"}
                        },
                        "filter": {
                            "range": {
                                "age": {"gte": 16}
                            }
                        }
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = SearchQuery(Term(f.user, 'kimchy'), _compiler=QueryCompiled20).filter(f.age >= 16)
        self.assert_expression(
            sq,
            {
                "query": {
                    "bool": {
                        "must": {
                            "term": {"user": "******"}
                        },
                        "filter": {
                            "range": {
                                "age": {"gte": 16}
                            }
                        }
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery(Term(f.user, 'kimchy'))
            .query(f.user != 'kimchy')
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "bool": {
                        "must_not": [
                            {
                                "term": {"user": "******"}
                            }
                        ]
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery(Term(f.user, 'kimchy'))
            .query(None)
        )
        self.assert_expression(sq, {})
        self.assertEqual(collect_doc_classes(sq), set())

        sq = (
            SearchQuery(Term(f.user, 'kimchy'))
            .filter(f.age >= 16)
            .filter(f.lang == 'English')
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "query": {
                            "term": {"user": "******"}
                        },
                        "filter": {
                            "bool": {
                                "must": [
                                    {
                                        "range": {
                                            "age": {"gte": 16}
                                        }
                                    },
                                    {
                                        "term": {
                                            "lang": "English"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .order_by(
                f.opinion_rating.desc(missing='_last'),
                f.opinion_count.desc(),
                f.id
            )
        )
        self.assert_expression(
            sq,
            {
                "sort": [
                    {
                        "opinion_rating": {
                            "order": "desc",
                            "missing": "_last"
                        }
                    },
                    {
                        "opinion_count": "desc"
                    },
                    "id"
                ]
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .order_by(
                f.opinion_rating.desc(missing='_last'),
                f.opinion_count.desc(),
                f.id
            )
            .order_by(None)
            .order_by(None)
        )
        self.assert_expression(sq, {})
        self.assertEqual(collect_doc_classes(sq), set())

        sq = SearchQuery().source(f.name, f.company)
        self.assert_expression(
            sq,
            {
                "_source": ["name", "company"]
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = SearchQuery().source(exclude=[f.name, f.company])
        self.assert_expression(
            sq,
            {
                "_source": {
                    "exclude": ["name", "company"]
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .source(
                include=[f.obj1.wildcard('*'), f.obj2.wildcard('*')],
                # FIXME: f.wildcard('*')
                exclude=DynamicDocument.wildcard('*').description
            )
        )
        self.assert_expression(
            sq,
            {
                "_source": {
                    "include": ["obj1.*", "obj2.*"],
                    "exclude": "*.description"
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .source(None)
            .source(f.name, f.company)
            .source(None)
        )
        self.assert_expression(sq, {})
        self.assertEqual(collect_doc_classes(sq), set())

        sq = (
            SearchQuery()
            .source(f.name, f.company)
            .source(False)
        )
        self.assert_expression(
            sq,
            {
                "_source": False
            }
        )
        self.assertEqual(collect_doc_classes(sq), set())

        sq = (
            SearchQuery()
            .source(True)
        )
        self.assert_expression(
            sq,
            {
                "_source": True
            }
        )
        self.assertEqual(collect_doc_classes(sq), set())

        sq = SearchQuery().fields(f.name, f.company)
        self.assert_expression(
            sq,
            {
                "fields": ["name", "company"]
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .fields(True)
        )
        self.assert_expression(
            sq,
            {
                "fields": '*'
            }
        )
        self.assertEqual(collect_doc_classes(sq), set())

        sq = (
            SearchQuery()
            .fields(None)
            .fields(f.name, f.company)
            .fields(None)
        )
        self.assert_expression(sq, {})
        self.assertEqual(collect_doc_classes(sq), set())

        sq = (
            SearchQuery()
            .fields(f.name, f.company)
            .fields(False)
        )
        self.assert_expression(
            sq,
            {
                "fields": []
            }
        )
        self.assertEqual(collect_doc_classes(sq), set())

        self.assert_expression(
            SearchQuery()
            .function_score({'random_score': {"seed": 1234}}),
            {
                "query": {
                    "function_score": {
                        "functions": [
                            {
                                "random_score": {"seed": 1234}
                            }
                        ],
                    }
                }
            }
        )

        sq = (
            SearchQuery(MultiMatch('Iphone 6', fields=[f.name, f.description]))
            .filter(f.status == 0)
            .function_score(None)
            .function_score({'_score': {"seed": 1234}})
            .function_score(None)
            .function_score({'field_value_factor': {'field': f.popularity,
                                                    'factor': 1.2,
                                                    'modifier': 'sqrt'}},
                            boost_mode='sum')
            .function_score({'boost_factor': 3,
                             'filter': f.region == 12})
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "query": {
                            "function_score": {
                                "query": {
                                    "multi_match": {
                                        "query": "Iphone 6",
                                        "fields": ["name", "description"]
                                    }
                                },
                                "functions": [
                                    {
                                        "field_value_factor": {
                                            "field": "popularity",
                                            "factor": 1.2,
                                            "modifier": "sqrt"
                                        }
                                    },
                                    {
                                        "filter": {
                                            "term": {"region": 12}
                                        },
                                        "boost_factor": 3
                                    }
                                ],
                                "boost_mode": "sum"
                            }
                        },
                        "filter": {
                            "term": {"status": 0}
                        }
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .filter(f.status == 0)
            .boost_score(
                {'filter': f.discount_percent >= 10, 'weight': 1000},
                {'filter': f.discount_percent >= 50, 'weight': 2000},
                {'filter': f.presence == 'available', 'weight': 10000},
            )
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "query": {
                            "function_score": {
                                "functions": [
                                    {
                                        "filter": {"range": {"discount_percent": {"gte": 10}}},
                                        "weight": 1000
                                    },
                                    {
                                        "filter": {"range": {"discount_percent": {"gte": 50}}},
                                        "weight": 2000
                                    },
                                    {
                                        "filter": {"term": {"presence": "available"}},
                                        "weight": 10000
                                    },
                                ],
                                "score_mode": "sum",
                                "boost_mode": "sum"
                            }
                        },
                        "filter": {
                            "term": {"status": 0}
                        }
                    }
                }
            }
        )

        sq = (
            SearchQuery(f.name.match('test'))
            .filter(f.status == 0)
            .function_score(
                {'field_value_factor': {'field': f.popularity}},
            )
            .boost_score(
                {'filter': f.discount_percent >= 10, 'weight': 100},
            )
            .boost_score(None)
            .boost_score(
                {'filter': f.discount_percent >= 10, 'weight': 1000},
                {'filter': f.discount_percent >= 50, 'weight': 2000},
                score_mode='max',
            )
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "query": {
                            "function_score": {
                                "query": {
                                    "function_score": {
                                        "query": {
                                            "match": {
                                                "name": "test"
                                            }
                                        },
                                        "functions": [
                                            {
                                                "field_value_factor": {
                                                    "field": "popularity"
                                                }
                                            }
                                        ]
                                    }
                                },
                                "functions": [
                                    {
                                        "filter": {"range": {"discount_percent": {"gte": 10}}},
                                        "weight": 1000
                                    },
                                    {
                                        "filter": {"range": {"discount_percent": {"gte": 50}}},
                                        "weight": 2000
                                    },
                                ],
                                "score_mode": "max",
                                "boost_mode": "sum"
                            }
                        },
                        "filter": {
                            "term": {"status": 0}
                        }
                    }
                }
            }
        )

        sq = (
            SearchQuery()
            .rescore(
                QueryRescorer(
                    self.index.t.field1.match('the quick brown', type='phrase', slop=2)
                )
            )
            .rescore(None)
            .rescore(
                QueryRescorer(
                    self.index.t.field1.match('the quick brown fox', type='phrase', slop=2),
                    query_weight=0.7,
                    rescore_query_weight=1.2
                ),
                window_size=100,
            )
            .rescore(
                QueryRescorer(
                    FunctionScore(script_score={'script': "log10(doc['numeric'].value + 2)"}),
                    score_mode='multiply'
                ),
                window_size=10,
            )
        )
        self.assert_expression(
            sq,
            {
                "rescore": [
                    {
                        "window_size": 100,
                        "query": {
                            "rescore_query": {
                                "match": {
                                    "field1": {
                                        "query": "the quick brown fox",
                                        "type": "phrase",
                                        "slop": 2
                                    }
                                }
                            },
                            "query_weight": 0.7,
                            "rescore_query_weight": 1.2
                        }
                    },
                    {
                        "window_size": 10,
                        "query": {
                            "score_mode": "multiply",
                            "rescore_query": {
                                "function_score": {
                                    "script_score": {
                                        "script": "log10(doc['numeric'].value + 2)"
                                    }
                                }
                            }
                        }
                    }
                ]
            }
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.t})

        sq = SearchQuery().post_filter(self.index.shirt.color == 'red')
        self.assert_expression(
            sq,
            {
                "post_filter": {
                    "term": {"color": "red"}
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.shirt})

        sq = (
            SearchQuery()
            .filter(self.index.shirt.brand == 'gucci')
            .post_filter(self.index.shirt.color == 'red')
            .post_filter(self.index.shirt.model == 't-shirt')
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "filter": {
                            "term": {"brand": "gucci"}
                        }
                    }
                },
                "post_filter": {
                    "bool": {
                        "must": [
                            {"term": {"color": "red"}},
                            {"term": {"model": "t-shirt"}}
                        ]
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.shirt})
Ejemplo n.º 7
0
    def test_highlight(self):
        sq = SearchQuery()
        sq = sq.highlight(fields={'content': {}})
        self.assertEqual(collect_doc_classes(sq), set())
        self.assert_expression(
            sq,
            {
                "highlight": {
                    "fields": {
                        "content": {}
                    }
                }
            }
        )

        sq = SearchQuery()
        sq = sq.highlight(
            fields=[self.index.test.content],
            pre_tags=['[em]'],
            post_tags=['[/em]']
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.test})
        self.assert_expression(
            sq,
            {
                "highlight": {
                    "fields": [
                        {
                            "content": {}
                        }
                    ],
                    "pre_tags": ["[em]"],
                    "post_tags": ["[/em]"]
                }
            }
        )

        sq = SearchQuery()
        sq = sq.highlight(
            fields=[
                self.index.test.content.highlight(
                    matched_fields=[self.index.test.content, self.index.test.content.plain],
                    type='fvh',
                )
            ]
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.test})
        self.assert_expression(
            sq,
            {
                "highlight": {
                    "fields": [
                        {
                            "content": {
                                "matched_fields": ["content", "content.plain"],
                                "type": "fvh"
                            }
                        }
                    ]
                }
            }
        )
Ejemplo n.º 8
0
    def test_document(self):
        self.assertSameElements(list(ProductDocument.fields), [
            ProductDocument._uid,
            ProductDocument._id,
            ProductDocument._type,
            ProductDocument._source,
            ProductDocument._analyzer,
            ProductDocument._boost,
            ProductDocument._parent,
            ProductDocument._field_names,
            ProductDocument._routing,
            ProductDocument._index,
            ProductDocument._size,
            ProductDocument._timestamp,
            ProductDocument._ttl,
            ProductDocument._version,
            ProductDocument._score,
            ProductDocument._all,
            ProductDocument.name,
            ProductDocument.status,
            ProductDocument.group,
            ProductDocument.price,
            ProductDocument.tags,
            ProductDocument.date_created,
            ProductDocument.unused,
        ])
        self.assertSameElements(list(ProductDocument.user_fields), [
            ProductDocument.name,
            ProductDocument.status,
            ProductDocument.group,
            ProductDocument.price,
            ProductDocument.tags,
            ProductDocument.date_created,
            ProductDocument.unused,
        ])
        self.assertSameElements(list(ProductDocument.mapping_fields), [
            ProductDocument._uid,
            ProductDocument._id,
            ProductDocument._type,
            ProductDocument._source,
            ProductDocument._analyzer,
            ProductDocument._boost,
            ProductDocument._parent,
            ProductDocument._field_names,
            ProductDocument._routing,
            ProductDocument._index,
            ProductDocument._size,
            ProductDocument._timestamp,
            ProductDocument._ttl,
            ProductDocument._version,
            ProductDocument._score,
            ProductDocument._all,
        ])
        self.assertIsInstance(ProductDocument._id, AttributedField)
        self.assertIsInstance(ProductDocument._id.get_field().get_type(),
                              String)
        self.assertEqual(ProductDocument._id.get_field().get_name(), '_id')
        self.assertEqual(ProductDocument._id.get_attr_name(), '_id')
        self.assertIs(ProductDocument._id.get_parent(), ProductDocument)
        self.assert_expression(ProductDocument._id, '_id')

        self.assertIsInstance(ProductDocument._all, AttributedField)
        self.assertIsInstance(ProductDocument._all.get_field().get_type(),
                              String)
        self.assertEqual(ProductDocument._all.get_field().get_name(), '_all')
        self.assertEqual(ProductDocument._all.get_attr_name(), '_all')
        self.assertIs(ProductDocument._all.get_parent(), ProductDocument)
        self.assert_expression(ProductDocument._all, '_all')

        self.assertIsInstance(ProductDocument._score, AttributedField)
        self.assertIsInstance(ProductDocument._score.get_field().get_type(),
                              Float)
        self.assertEqual(ProductDocument._score.get_field().get_name(),
                         '_score')
        self.assertEqual(ProductDocument._score.get_attr_name(), '_score')
        self.assertIs(ProductDocument._score.get_parent(), ProductDocument)
        self.assert_expression(ProductDocument._score, '_score')

        self.assertIsInstance(ProductDocument.name, AttributedField)
        self.assertIsInstance(ProductDocument.name.get_field().get_type(),
                              String)
        self.assertEqual(ProductDocument.name.get_field().get_name(),
                         'test_name')
        self.assertEqual(ProductDocument.name.get_attr_name(), 'name')
        self.assertIs(ProductDocument.name.get_parent(), ProductDocument)
        self.assert_expression(ProductDocument.name, 'test_name')
        self.assertEqual(list(ProductDocument.name.fields),
                         [ProductDocument.name.raw])
        self.assertEqual(collect_doc_classes(ProductDocument.name),
                         {ProductDocument})

        self.assertIsInstance(ProductDocument.name.raw, AttributedField)
        self.assertIsInstance(ProductDocument.name.raw.get_field().get_type(),
                              String)
        self.assert_expression(ProductDocument.name.raw, 'test_name.raw')
        self.assertEqual(ProductDocument.name.raw.get_field().get_name(),
                         'test_name.raw')
        self.assertEqual(ProductDocument.name.raw.get_attr_name(), 'raw')
        self.assertIsInstance(ProductDocument.name.raw.get_parent(),
                              AttributedField)
        self.assertEqual(collect_doc_classes(ProductDocument.name.raw),
                         {ProductDocument})

        self.assertIsInstance(ProductDocument.status, AttributedField)
        self.assertIsInstance(ProductDocument.status.get_field().get_type(),
                              Integer)

        self.assertIsInstance(ProductDocument.price, AttributedField)
        self.assertIsInstance(ProductDocument.price.get_field().get_type(),
                              Float)

        self.assertIsInstance(ProductDocument.group, AttributedField)
        self.assertIsInstance(ProductDocument.group.get_field().get_type(),
                              Object)
        self.assertEqual(
            list(ProductDocument.group.fields),
            [ProductDocument.group.id, ProductDocument.group.name])

        self.assertIsInstance(ProductDocument.group.name, AttributedField)
        self.assertEqual(list(ProductDocument.group.name.fields),
                         [ProductDocument.group.name.raw])
        self.assertEqual(ProductDocument.group.name.get_field().get_name(),
                         'group.test_name')
        self.assertIsInstance(
            ProductDocument.group.name.get_field().get_type(), String)
        self.assertIs(ProductDocument.group.name.get_parent(), ProductDocument)
        self.assertEqual(collect_doc_classes(ProductDocument.group.name),
                         {ProductDocument})

        self.assertEqual(ProductDocument.group.name.raw.get_attr_name(), 'raw')
        self.assertEqual(ProductDocument.group.name.raw.get_field().get_name(),
                         'group.test_name.raw')
        self.assertIsInstance(
            ProductDocument.group.name.raw.get_field().get_type(), String)
        self.assertIsInstance(ProductDocument.group.name.raw.get_parent(),
                              AttributedField)
        self.assertEqual(collect_doc_classes(ProductDocument.group.name.raw),
                         {ProductDocument})

        self.assertIsInstance(ProductDocument.tags, AttributedField)
        self.assertIsInstance(ProductDocument.tags.get_field().get_type(),
                              List)
        self.assertEqual(list(ProductDocument.tags.fields), [
            ProductDocument.tags.id, ProductDocument.tags.name,
            ProductDocument.tags.group
        ])
        self.assertEqual(ProductDocument.tags.get_field().get_name(), 'tags')
        self.assert_expression(ProductDocument.tags, 'tags')

        self.assertIsInstance(ProductDocument.tags.group, AttributedField)
        self.assertIsInstance(
            ProductDocument.tags.group.get_field().get_type(), Object)
        self.assertEqual(
            list(ProductDocument.tags.group.fields),
            [ProductDocument.tags.group.id, ProductDocument.tags.group.name])
        self.assertEqual(ProductDocument.tags.group.get_field().get_name(),
                         'tags.group')
        self.assert_expression(ProductDocument.tags.group, 'tags.group')

        self.assertIsInstance(ProductDocument.tags.group.name, AttributedField)
        self.assertIsInstance(
            ProductDocument.tags.group.name.get_field().get_type(), String)
        self.assertEqual(list(ProductDocument.tags.group.name.fields),
                         [ProductDocument.tags.group.name.raw])
        self.assertEqual(
            ProductDocument.tags.group.name.get_field().get_name(),
            'tags.group.test_name')
        self.assert_expression(ProductDocument.tags.group.name,
                               'tags.group.test_name')

        self.assertIsInstance(ProductDocument.tags.group.name.raw,
                              AttributedField)
        self.assertIsInstance(
            ProductDocument.tags.group.name.raw.get_field().get_type(), String)
        self.assertEqual(list(ProductDocument.tags.group.name.raw.fields), [])
        self.assertEqual(
            ProductDocument.tags.group.name.raw.get_field().get_name(),
            'tags.group.test_name.raw')
        self.assert_expression(ProductDocument.tags.group.name.raw,
                               'tags.group.test_name.raw')

        self.assertIsInstance(ProductDocument.tags.group.group_id_1,
                              AttributedField)
        self.assertIsInstance(
            ProductDocument.tags.group.group_id_1.get_field().get_type(),
            Integer)
        self.assertEqual(list(ProductDocument.tags.group.group_id_1.fields),
                         [])
        self.assertEqual(
            ProductDocument.tags.group.group_id_1.get_field().get_name(),
            'tags.group.group_id_1')
        self.assert_expression(ProductDocument.tags.group.group_id_1,
                               'tags.group.group_id_1')

        self.assertRaises(AttributeError, lambda: ProductDocument.group._id)
        self.assertRaises(KeyError,
                          lambda: ProductDocument.group.fields['_id'])

        self.assertRaises(AttributeError,
                          lambda: ProductDocument.group.missing_field)
        self.assertRaises(
            KeyError, lambda: ProductDocument.group.fields['missing_field'])

        self.assertIsInstance(ProductDocument.i_attr_2, AttributedField)
        self.assertIsInstance(ProductDocument.i_attr_2.get_field().get_type(),
                              Integer)
        self.assertEqual(collect_doc_classes(ProductDocument.i_attr_2),
                         {ProductDocument})
        self.assertEqual(ProductDocument.i_attr_2.get_field().get_name(),
                         'i_attr_2')
        self.assert_expression(ProductDocument.i_attr_2, 'i_attr_2')

        self.assertIsInstance(ProductDocument.b_attr_1, AttributedField)
        self.assertIsInstance(ProductDocument.b_attr_1.get_field().get_type(),
                              Boolean)
        self.assertEqual(collect_doc_classes(ProductDocument.b_attr_1),
                         {ProductDocument})
        self.assertEqual(ProductDocument.b_attr_1.get_field().get_name(),
                         'b_attr_1')
        self.assert_expression(ProductDocument.b_attr_1, 'b_attr_1')

        self.assertRaises(AttributeError, lambda: ProductDocument.fake_attr_1)

        self.assertIsInstance(ProductDocument.wildcard('date_*'),
                              AttributedField)
        self.assertIsInstance(
            ProductDocument.wildcard('date_*').get_field().get_type(), Type)
        self.assert_expression(ProductDocument.wildcard('date_*'), 'date_*')
        self.assertEqual(
            ProductDocument.wildcard('date_*').get_field().get_name(),
            'date_*')
        self.assertEqual(
            collect_doc_classes(ProductDocument.wildcard('date_*')),
            {ProductDocument})

        self.assertIsInstance(ProductDocument.group.wildcard('date_*'),
                              AttributedField)
        self.assertIsInstance(
            ProductDocument.group.wildcard('date_*').get_field().get_type(),
            Type)
        self.assert_expression(ProductDocument.group.wildcard('date_*'),
                               'group.date_*')
        self.assertEqual(
            ProductDocument.group.wildcard('date_*').get_field().get_name(),
            'group.date_*')
        self.assertEqual(
            collect_doc_classes(ProductDocument.group.wildcard('date_*')),
            {ProductDocument})

        self.assertIsInstance(
            ProductDocument.wildcard('group_*').id, AttributedField)
        self.assertIsInstance(
            ProductDocument.wildcard('group_*').id.get_field().get_type(),
            Type)
        self.assert_expression(
            ProductDocument.wildcard('group_*').id, 'group_*.id')
        self.assertEqual(
            ProductDocument.wildcard('group_*').id.get_field().get_name(),
            'group_*.id')
        self.assertEqual(
            collect_doc_classes(ProductDocument.wildcard('group_*').id),
            {ProductDocument})

        self.assertIsInstance(
            ProductDocument.tags.group.dynamic_fields['group_id_*'],
            AttributedField)
        self.assertIsInstance(
            ProductDocument.tags.group.dynamic_fields['group_id_*'].get_type(),
            Integer)
        self.assert_expression(
            ProductDocument.tags.group.dynamic_fields['group_id_*'],
            'tags.group.group_id_*')
        self.assertEqual(
            ProductDocument.tags.group.dynamic_fields['group_id_*'].get_field(
            ).get_name(), 'tags.group.group_id_*')
        self.assertEqual(
            collect_doc_classes(
                ProductDocument.tags.group.dynamic_fields['group_id_*']),
            {ProductDocument})

        self.assertRaises(
            KeyError, lambda: ProductDocument.tags.group.dynamic_fields['*'])

        self.assertIs(ProductDocument._id, ProductDocument._id)
        self.assertIs(ProductDocument.name, ProductDocument.name)
        self.assertIs(ProductDocument.group.name, ProductDocument.group.name)
        self.assertIs(ProductDocument.tags.group.name,
                      ProductDocument.tags.group.name)
        self.assertIs(ProductDocument.tags.group.name.raw,
                      ProductDocument.tags.group.name.raw)
        # TODO: May be we should cache dynamic fields?
        self.assertIsNot(ProductDocument.i_attr_2, ProductDocument.i_attr_2)
        self.assertIsNot(ProductDocument._id, GroupDocument._id)
        self.assertIsNot(GroupDocument.name, ProductDocument.group.name)
        self.assertIsNot(GroupDocument.name, ProductDocument.tags.group.name)
        self.assertIsNot(ProductDocument.group.name,
                         ProductDocument.tags.group.name)
        self.assertIsNot(TagDocument.name, ProductDocument.tags.name)

        doc = ProductDocument()
        self.assertIs(doc._id, None)
        self.assertIs(doc.name, None)
        self.assertIs(doc.status, None)
        doc._id = 123
        self.assertIsInstance(doc._id, int)
        self.assertEqual(doc._id, 123)

        doc = ProductDocument(_id=123)
        self.assertIsInstance(doc._id, int)
        self.assertEqual(doc._id, 123)
        self.assertIs(doc.name, None)
        self.assertIs(doc.status, None)

        doc = ProductDocument(_id=123,
                              name='Test name',
                              status=0,
                              group=GroupDocument(name='Test group'),
                              price=99.99,
                              tags=[
                                  TagDocument(id=1, name='Test tag'),
                                  TagDocument(id=2, name='Just tag')
                              ])
        self.assertIsInstance(doc._id, int)
        self.assertEqual(doc._id, 123)
        self.assertIsInstance(doc.name, string_types)
        self.assertEqual(doc.name, 'Test name')
        self.assertIsInstance(doc.status, int)
        self.assertEqual(doc.status, 0)
        self.assertIsInstance(doc.group, GroupDocument)
        self.assertIsInstance(doc.group.name, string_types)
        self.assertIsInstance(doc.price, float)
        self.assertAlmostEqual(doc.price, 99.99)
        self.assertEqual(doc.group.name, 'Test group')
        self.assertIsInstance(doc.tags, list)
        self.assertIsInstance(doc.tags[0].name, string_types)
        self.assertEqual(doc.tags[0].name, 'Test tag')

        hit_doc = ProductDocument(
            _hit={
                '_id': '123',
                '_score': 1.23,
                '_source': {
                    'test_name':
                    'Test name',
                    'status':
                    0,
                    'group': {
                        'test_name': 'Test group'
                    },
                    'price':
                    101.5,
                    'tags': [{
                        'id': 1,
                        'name': 'Test tag'
                    }, {
                        'id': 2,
                        'name': 'Just tag'
                    }],
                    'date_created':
                    '2014-08-14T14:05:28.789Z',
                },
                'highlight': {
                    'test_name': '<em>Test</em> name'
                },
                'matched_queries': ['field_1', 'field_2']
            })
        self.assertEqual(hit_doc._id, '123')
        self.assertAlmostEqual(hit_doc._score, 1.23)
        self.assertEqual(hit_doc.name, 'Test name')
        self.assertEqual(hit_doc.status, 0)
        self.assertIsInstance(hit_doc.group, GroupDocument)
        self.assertEqual(hit_doc.group.name, 'Test group')
        self.assertAlmostEqual(hit_doc.price, 101.5)
        self.assertIsInstance(hit_doc.tags, list)
        self.assertIsInstance(hit_doc.tags[0], TagDocument)
        self.assertEqual(hit_doc.tags[0].id, 1)
        self.assertEqual(hit_doc.tags[0].name, 'Test tag')
        self.assertEqual(hit_doc.tags[1].id, 2)
        self.assertEqual(hit_doc.tags[1].name, 'Just tag')
        self.assertEqual(
            hit_doc.date_created,
            datetime.datetime(2014, 8, 14, 14, 5, 28, 789000,
                              dateutil.tz.tzutc()))
        self.assertIs(hit_doc.unused, None)
        self.assertEqual(hit_doc.get_highlight(),
                         {'test_name': '<em>Test</em> name'})
        self.assertIn('field_1', hit_doc.get_matched_queries())
        self.assertIn('field_2', hit_doc.get_matched_queries())

        hit_doc = ProductDocument(
            _hit={
                '_id': '123',
                '_score': 1.23,
                'fields': {
                    'test_name': ['Test name'],
                    'status': [0],
                    'group.test_name': ['Test group'],
                    'price': [101.5],
                    'tags.id': [1, 2],
                    'tags.name': ['Test tag', 'Just tag'],
                    'date_created': ['2014-08-14T14:05:28.789Z'],
                    'not_mapped': ['Test'],
                }
            })
        self.assertEqual(hit_doc._id, '123')
        self.assertAlmostEqual(hit_doc._score, 1.23)
        self.assertEqual(
            hit_doc.get_hit_fields(), {
                'test_name': ['Test name'],
                'status': [0],
                'group.test_name': ['Test group'],
                'price': [101.5],
                'tags.id': [1, 2],
                'tags.name': ['Test tag', 'Just tag'],
                'date_created': [
                    datetime.datetime(2014, 8, 14, 14, 5, 28, 789000,
                                      dateutil.tz.tzutc())
                ],
                'not_mapped': ['Test'],
            })

        hit_doc = ProductDocument(_hit={'_id': '123'})
        self.assertEqual(hit_doc._id, '123')
        self.assertIs(hit_doc.name, None)
        self.assertEqual(hit_doc.get_highlight(), {})

        doc = ProductDocument(_id=123,
                              name='Test name',
                              status=0,
                              group=GroupDocument(name='Test group'),
                              price=101.5,
                              tags=[
                                  TagDocument(id=1, name='Test tag'),
                                  TagDocument(id=2, name='Just tag')
                              ],
                              i_attr_1=None,
                              i_attr_2='',
                              i_attr_3=[],
                              i_attr_4=45)
        self.assertEqual(
            doc.to_source(), {
                'test_name':
                'Test name',
                'status':
                0,
                'group': {
                    'test_name': 'Test group'
                },
                'price':
                101.5,
                'tags': [
                    {
                        'id': 1,
                        'name': 'Test tag'
                    },
                    {
                        'id': 2,
                        'name': 'Just tag'
                    },
                ],
                'i_attr_4':
                45
            })
Ejemplo n.º 9
0
    def test_inheritance(self):
        class InheritedDocument(ProductDocument):
            description = Field(String)

        self.assertSameElements(list(InheritedDocument.fields), [
            InheritedDocument._uid,
            InheritedDocument._id,
            InheritedDocument._type,
            InheritedDocument._source,
            InheritedDocument._analyzer,
            InheritedDocument._boost,
            InheritedDocument._parent,
            InheritedDocument._field_names,
            InheritedDocument._routing,
            InheritedDocument._index,
            InheritedDocument._size,
            InheritedDocument._timestamp,
            InheritedDocument._ttl,
            InheritedDocument._version,
            InheritedDocument._score,
            InheritedDocument._all,
            InheritedDocument.name,
            InheritedDocument.status,
            InheritedDocument.group,
            InheritedDocument.price,
            InheritedDocument.tags,
            InheritedDocument.date_created,
            InheritedDocument.unused,
            InheritedDocument.description,
        ])
        self.assertIsInstance(InheritedDocument.name, AttributedField)
        self.assertIsInstance(InheritedDocument.name.get_field().get_type(),
                              String)
        self.assertEqual(collect_doc_classes(InheritedDocument.name),
                         {InheritedDocument})
        self.assertIsInstance(InheritedDocument.name.raw, AttributedField)
        self.assertIsInstance(
            InheritedDocument.name.raw.get_field().get_type(), String)
        self.assertEqual(collect_doc_classes(InheritedDocument.name.raw),
                         {InheritedDocument})
        self.assertIsInstance(InheritedDocument.description, AttributedField)
        self.assertEqual(collect_doc_classes(InheritedDocument.description),
                         {InheritedDocument})

        doc = InheritedDocument(_id=123)
        self.assertIsInstance(doc._id, int)
        self.assertEqual(doc._id, 123)
        self.assertIs(doc.name, None)
        self.assertIs(doc.status, None)
        self.assertIs(doc.description, None)
        self.assertEqual(doc.to_source(), {})

        doc = InheritedDocument(_id=123,
                                status=0,
                                name='Test',
                                i_attr_1=1,
                                i_attr_2=2,
                                face_attr_3=3)
        self.assertEqual(doc.to_source(), {
            'status': 0,
            'test_name': 'Test',
            'i_attr_1': 1,
            'i_attr_2': 2,
        })
Ejemplo n.º 10
0
    def test_document(self):
        self.assertSameElements(
            list(ProductDocument.fields),
            [
                ProductDocument._uid,
                ProductDocument._id,
                ProductDocument._type,
                ProductDocument._source,
                ProductDocument._analyzer,
                ProductDocument._boost,
                ProductDocument._parent,
                ProductDocument._field_names,
                ProductDocument._routing,
                ProductDocument._index,
                ProductDocument._size,
                ProductDocument._timestamp,
                ProductDocument._ttl,
                ProductDocument._version,
                ProductDocument._score,
                ProductDocument._all,
                ProductDocument.name,
                ProductDocument.status,
                ProductDocument.group,
                ProductDocument.price,
                ProductDocument.tags,
                ProductDocument.date_created,
                ProductDocument.unused,
            ]
        )
        self.assertSameElements(
            list(ProductDocument.user_fields),
            [
                ProductDocument.name,
                ProductDocument.status,
                ProductDocument.group,
                ProductDocument.price,
                ProductDocument.tags,
                ProductDocument.date_created,
                ProductDocument.unused,
            ]
        )
        self.assertSameElements(
            list(ProductDocument.mapping_fields),
            [
                ProductDocument._uid,
                ProductDocument._id,
                ProductDocument._type,
                ProductDocument._source,
                ProductDocument._analyzer,
                ProductDocument._boost,
                ProductDocument._parent,
                ProductDocument._field_names,
                ProductDocument._routing,
                ProductDocument._index,
                ProductDocument._size,
                ProductDocument._timestamp,
                ProductDocument._ttl,
                ProductDocument._version,
                ProductDocument._score,
                ProductDocument._all,
            ]
        )
        self.assertIsInstance(ProductDocument._id, AttributedField)
        self.assertIsInstance(ProductDocument._id.get_field().get_type(), String)
        self.assertEqual(ProductDocument._id.get_field().get_name(), '_id')
        self.assertEqual(ProductDocument._id.get_attr_name(), '_id')
        self.assertIs(ProductDocument._id.get_parent(), ProductDocument)
        self.assert_expression(ProductDocument._id, '_id')

        self.assertIsInstance(ProductDocument._all, AttributedField)
        self.assertIsInstance(ProductDocument._all.get_field().get_type(), String)
        self.assertEqual(ProductDocument._all.get_field().get_name(), '_all')
        self.assertEqual(ProductDocument._all.get_attr_name(), '_all')
        self.assertIs(ProductDocument._all.get_parent(), ProductDocument)
        self.assert_expression(ProductDocument._all, '_all')

        self.assertIsInstance(ProductDocument._score, AttributedField)
        self.assertIsInstance(ProductDocument._score.get_field().get_type(), Float)
        self.assertEqual(ProductDocument._score.get_field().get_name(), '_score')
        self.assertEqual(ProductDocument._score.get_attr_name(), '_score')
        self.assertIs(ProductDocument._score.get_parent(), ProductDocument)
        self.assert_expression(ProductDocument._score, '_score')

        self.assertIsInstance(ProductDocument.name, AttributedField)
        self.assertIsInstance(ProductDocument.name.get_field().get_type(), String)
        self.assertEqual(ProductDocument.name.get_field().get_name(), 'test_name')
        self.assertEqual(ProductDocument.name.get_attr_name(), 'name')
        self.assertIs(ProductDocument.name.get_parent(), ProductDocument)
        self.assert_expression(ProductDocument.name, 'test_name')
        self.assertEqual(list(ProductDocument.name.fields), [ProductDocument.name.raw])
        self.assertEqual(collect_doc_classes(ProductDocument.name), {ProductDocument})

        self.assertIsInstance(ProductDocument.name.raw, AttributedField)
        self.assertIsInstance(ProductDocument.name.raw.get_field().get_type(), String)
        self.assert_expression(ProductDocument.name.raw, 'test_name.raw')
        self.assertEqual(ProductDocument.name.raw.get_field().get_name(), 'test_name.raw')
        self.assertEqual(ProductDocument.name.raw.get_attr_name(), 'raw')
        self.assertIsInstance(ProductDocument.name.raw.get_parent(), AttributedField)
        self.assertEqual(collect_doc_classes(ProductDocument.name.raw), {ProductDocument})

        self.assertIsInstance(ProductDocument.status, AttributedField)
        self.assertIsInstance(ProductDocument.status.get_field().get_type(), Integer)

        self.assertIsInstance(ProductDocument.price, AttributedField)
        self.assertIsInstance(ProductDocument.price.get_field().get_type(), Float)

        self.assertIsInstance(ProductDocument.group, AttributedField)
        self.assertIsInstance(ProductDocument.group.get_field().get_type(), Object)
        self.assertEqual(list(ProductDocument.group.fields), [ProductDocument.group.id, ProductDocument.group.name])

        self.assertIsInstance(ProductDocument.group.name, AttributedField)
        self.assertEqual(list(ProductDocument.group.name.fields), [ProductDocument.group.name.raw])
        self.assertEqual(ProductDocument.group.name.get_field().get_name(), 'group.test_name')
        self.assertIsInstance(ProductDocument.group.name.get_field().get_type(), String)
        self.assertIs(ProductDocument.group.name.get_parent(), ProductDocument)
        self.assertEqual(collect_doc_classes(ProductDocument.group.name), {ProductDocument})

        self.assertEqual(ProductDocument.group.name.raw.get_attr_name(), 'raw')
        self.assertEqual(ProductDocument.group.name.raw.get_field().get_name(), 'group.test_name.raw')
        self.assertIsInstance(ProductDocument.group.name.raw.get_field().get_type(), String)
        self.assertIsInstance(ProductDocument.group.name.raw.get_parent(), AttributedField)
        self.assertEqual(collect_doc_classes(ProductDocument.group.name.raw), {ProductDocument})

        self.assertIsInstance(ProductDocument.tags, AttributedField)
        self.assertIsInstance(ProductDocument.tags.get_field().get_type(), List)
        self.assertEqual(list(ProductDocument.tags.fields), [ProductDocument.tags.id, ProductDocument.tags.name, ProductDocument.tags.group])
        self.assertEqual(ProductDocument.tags.get_field().get_name(), 'tags')
        self.assert_expression(ProductDocument.tags, 'tags')

        self.assertIsInstance(ProductDocument.tags.group, AttributedField)
        self.assertIsInstance(ProductDocument.tags.group.get_field().get_type(), Object)
        self.assertEqual(list(ProductDocument.tags.group.fields), [ProductDocument.tags.group.id, ProductDocument.tags.group.name])
        self.assertEqual(ProductDocument.tags.group.get_field().get_name(), 'tags.group')
        self.assert_expression(ProductDocument.tags.group, 'tags.group')

        self.assertIsInstance(ProductDocument.tags.group.name, AttributedField)
        self.assertIsInstance(ProductDocument.tags.group.name.get_field().get_type(), String)
        self.assertEqual(list(ProductDocument.tags.group.name.fields), [ProductDocument.tags.group.name.raw])
        self.assertEqual(ProductDocument.tags.group.name.get_field().get_name(), 'tags.group.test_name')
        self.assert_expression(ProductDocument.tags.group.name, 'tags.group.test_name')

        self.assertIsInstance(ProductDocument.tags.group.name.raw, AttributedField)
        self.assertIsInstance(ProductDocument.tags.group.name.raw.get_field().get_type(), String)
        self.assertEqual(list(ProductDocument.tags.group.name.raw.fields), [])
        self.assertEqual(ProductDocument.tags.group.name.raw.get_field().get_name(), 'tags.group.test_name.raw')
        self.assert_expression(ProductDocument.tags.group.name.raw, 'tags.group.test_name.raw')

        self.assertIsInstance(ProductDocument.tags.group.group_id_1, AttributedField)
        self.assertIsInstance(ProductDocument.tags.group.group_id_1.get_field().get_type(), Integer)
        self.assertEqual(list(ProductDocument.tags.group.group_id_1.fields), [])
        self.assertEqual(ProductDocument.tags.group.group_id_1.get_field().get_name(), 'tags.group.group_id_1')
        self.assert_expression(ProductDocument.tags.group.group_id_1, 'tags.group.group_id_1')

        self.assertRaises(AttributeError, lambda: ProductDocument.group._id)
        self.assertRaises(KeyError, lambda: ProductDocument.group.fields['_id'])

        self.assertRaises(AttributeError, lambda: ProductDocument.group.missing_field)
        self.assertRaises(KeyError, lambda: ProductDocument.group.fields['missing_field'])

        self.assertIsInstance(ProductDocument.i_attr_2, AttributedField)
        self.assertIsInstance(ProductDocument.i_attr_2.get_field().get_type(), Integer)
        self.assertEqual(collect_doc_classes(ProductDocument.i_attr_2), {ProductDocument})
        self.assertEqual(ProductDocument.i_attr_2.get_field().get_name(), 'i_attr_2')
        self.assert_expression(ProductDocument.i_attr_2, 'i_attr_2')

        self.assertIsInstance(ProductDocument.b_attr_1, AttributedField)
        self.assertIsInstance(ProductDocument.b_attr_1.get_field().get_type(), Boolean)
        self.assertEqual(collect_doc_classes(ProductDocument.b_attr_1), {ProductDocument})
        self.assertEqual(ProductDocument.b_attr_1.get_field().get_name(), 'b_attr_1')
        self.assert_expression(ProductDocument.b_attr_1, 'b_attr_1')

        self.assertRaises(AttributeError, lambda: ProductDocument.fake_attr_1)

        self.assertIsInstance(ProductDocument.wildcard('date_*'), AttributedField)
        self.assertIsInstance(ProductDocument.wildcard('date_*').get_field().get_type(), Type)
        self.assert_expression(ProductDocument.wildcard('date_*'), 'date_*')
        self.assertEqual(ProductDocument.wildcard('date_*').get_field().get_name(), 'date_*')
        self.assertEqual(collect_doc_classes(ProductDocument.wildcard('date_*')), {ProductDocument})

        self.assertIsInstance(ProductDocument.group.wildcard('date_*'), AttributedField)
        self.assertIsInstance(ProductDocument.group.wildcard('date_*').get_field().get_type(), Type)
        self.assert_expression(ProductDocument.group.wildcard('date_*'), 'group.date_*')
        self.assertEqual(ProductDocument.group.wildcard('date_*').get_field().get_name(), 'group.date_*')
        self.assertEqual(collect_doc_classes(ProductDocument.group.wildcard('date_*')), {ProductDocument})

        self.assertIsInstance(ProductDocument.wildcard('group_*').id, AttributedField)
        self.assertIsInstance(ProductDocument.wildcard('group_*').id.get_field().get_type(), Type)
        self.assert_expression(ProductDocument.wildcard('group_*').id, 'group_*.id')
        self.assertEqual(ProductDocument.wildcard('group_*').id.get_field().get_name(), 'group_*.id')
        self.assertEqual(collect_doc_classes(ProductDocument.wildcard('group_*').id), {ProductDocument})

        self.assertIsInstance(ProductDocument.tags.group.dynamic_fields['group_id_*'], AttributedField)
        self.assertIsInstance(ProductDocument.tags.group.dynamic_fields['group_id_*'].get_type(), Integer)
        self.assert_expression(ProductDocument.tags.group.dynamic_fields['group_id_*'], 'tags.group.group_id_*')
        self.assertEqual(ProductDocument.tags.group.dynamic_fields['group_id_*'].get_field().get_name(), 'tags.group.group_id_*')
        self.assertEqual(collect_doc_classes(ProductDocument.tags.group.dynamic_fields['group_id_*']), {ProductDocument})

        self.assertRaises(KeyError, lambda: ProductDocument.tags.group.dynamic_fields['*'])

        self.assertIs(ProductDocument._id, ProductDocument._id)
        self.assertIs(ProductDocument.name, ProductDocument.name)
        self.assertIs(ProductDocument.group.name, ProductDocument.group.name)
        self.assertIs(ProductDocument.tags.group.name, ProductDocument.tags.group.name)
        self.assertIs(ProductDocument.tags.group.name.raw, ProductDocument.tags.group.name.raw)
        # TODO: May be we should cache dynamic fields?
        self.assertIsNot(ProductDocument.i_attr_2, ProductDocument.i_attr_2)
        self.assertIsNot(ProductDocument._id, GroupDocument._id)
        self.assertIsNot(GroupDocument.name, ProductDocument.group.name)
        self.assertIsNot(GroupDocument.name, ProductDocument.tags.group.name)
        self.assertIsNot(ProductDocument.group.name, ProductDocument.tags.group.name)
        self.assertIsNot(TagDocument.name, ProductDocument.tags.name)

        doc = ProductDocument()
        self.assertIs(doc._id, None)
        self.assertIs(doc.name, None)
        self.assertIs(doc.status, None)
        doc._id = 123
        self.assertIsInstance(doc._id, int)
        self.assertEqual(doc._id, 123)

        doc = ProductDocument(_id=123)
        self.assertIsInstance(doc._id, int)
        self.assertEqual(doc._id, 123)
        self.assertIs(doc.name, None)
        self.assertIs(doc.status, None)

        doc = ProductDocument(_id=123, name='Test name', status=0,
                           group=GroupDocument(name='Test group'),
                           price=99.99,
                           tags=[TagDocument(id=1, name='Test tag'),
                                 TagDocument(id=2, name='Just tag')])
        self.assertIsInstance(doc._id, int)
        self.assertEqual(doc._id, 123)
        self.assertIsInstance(doc.name, string_types)
        self.assertEqual(doc.name, 'Test name')
        self.assertIsInstance(doc.status, int)
        self.assertEqual(doc.status, 0)
        self.assertIsInstance(doc.group, GroupDocument)
        self.assertIsInstance(doc.group.name, string_types)
        self.assertIsInstance(doc.price, float)
        self.assertAlmostEqual(doc.price, 99.99)
        self.assertEqual(doc.group.name, 'Test group')
        self.assertIsInstance(doc.tags, list)
        self.assertIsInstance(doc.tags[0].name, string_types)
        self.assertEqual(doc.tags[0].name, 'Test tag')

        hit_doc = ProductDocument(
            _hit={
                '_id':'123',
                '_score': 1.23,
                '_source': {
                    'test_name': 'Test name',
                    'status': 0,
                    'group': {'test_name': 'Test group'},
                    'price': 101.5,
                    'tags': [{'id': 1, 'name': 'Test tag'},
                             {'id': 2, 'name': 'Just tag'}],
                    'date_created': '2014-08-14T14:05:28.789Z',
                },
                'highlight': {
                    'test_name': '<em>Test</em> name'
                },
                'matched_queries': ['field_1', 'field_2']
            }
        )
        self.assertEqual(hit_doc._id, '123')
        self.assertAlmostEqual(hit_doc._score, 1.23)
        self.assertEqual(hit_doc.name, 'Test name')
        self.assertEqual(hit_doc.status, 0)
        self.assertIsInstance(hit_doc.group, GroupDocument)
        self.assertEqual(hit_doc.group.name, 'Test group')
        self.assertAlmostEqual(hit_doc.price, 101.5)
        self.assertIsInstance(hit_doc.tags, list)
        self.assertIsInstance(hit_doc.tags[0], TagDocument)
        self.assertEqual(hit_doc.tags[0].id, 1)
        self.assertEqual(hit_doc.tags[0].name, 'Test tag')
        self.assertEqual(hit_doc.tags[1].id, 2)
        self.assertEqual(hit_doc.tags[1].name, 'Just tag')
        self.assertEqual(hit_doc.date_created,
                         datetime.datetime(2014, 8, 14, 14, 5, 28, 789000, dateutil.tz.tzutc()))
        self.assertIs(hit_doc.unused, None)
        self.assertEqual(hit_doc.get_highlight(), {'test_name': '<em>Test</em> name'})
        self.assertIn('field_1', hit_doc.get_matched_queries())
        self.assertIn('field_2', hit_doc.get_matched_queries())

        hit_doc = ProductDocument(
            _hit={
                '_id':'123',
                '_score': 1.23,
                'fields': {
                    'test_name': ['Test name'],
                    'status': [0],
                    'group.test_name': ['Test group'],
                    'price': [101.5],
                    'tags.id': [1, 2],
                    'tags.name': ['Test tag', 'Just tag'],
                    'date_created': ['2014-08-14T14:05:28.789Z'],
                    'not_mapped': ['Test'],
                }
            }
        )
        self.assertEqual(hit_doc._id, '123')
        self.assertAlmostEqual(hit_doc._score, 1.23)
        self.assertEqual(
            hit_doc.get_hit_fields(),
            {
                'test_name': ['Test name'],
                'status': [0],
                'group.test_name': ['Test group'],
                'price': [101.5],
                'tags.id': [1, 2],
                'tags.name': ['Test tag', 'Just tag'],
                'date_created': [datetime.datetime(2014, 8, 14, 14, 5, 28, 789000, dateutil.tz.tzutc())],
                'not_mapped': ['Test'],
            }
        )

        hit_doc = ProductDocument(
            _hit={
                '_id':'123'
            }
        )
        self.assertEqual(hit_doc._id, '123')
        self.assertIs(hit_doc.name, None)
        self.assertEqual(hit_doc.get_highlight(), {})

        doc = ProductDocument(_id=123, name='Test name', status=0,
                           group=GroupDocument(name='Test group'),
                           price=101.5,
                           tags=[TagDocument(id=1, name='Test tag'),
                                 TagDocument(id=2, name='Just tag')],
                           i_attr_1=None,
                           i_attr_2='',
                           i_attr_3=[],
                           i_attr_4=45)
        self.assertEqual(
            doc.to_source(),
            {
                'test_name': 'Test name',
                'status': 0,
                'group': {
                    'test_name': 'Test group'
                },
                'price': 101.5,
                'tags': [
                    {'id': 1, 'name': 'Test tag'},
                    {'id': 2, 'name': 'Just tag'},
                ],
                'i_attr_4': 45
            }
        )
Ejemplo n.º 11
0
    def test_inheritance(self):
        class InheritedDocument(ProductDocument):
            description = Field(String)

        self.assertSameElements(
            list(InheritedDocument.fields),
            [
                InheritedDocument._uid,
                InheritedDocument._id,
                InheritedDocument._type,
                InheritedDocument._source,
                InheritedDocument._analyzer,
                InheritedDocument._boost,
                InheritedDocument._parent,
                InheritedDocument._field_names,
                InheritedDocument._routing,
                InheritedDocument._index,
                InheritedDocument._size,
                InheritedDocument._timestamp,
                InheritedDocument._ttl,
                InheritedDocument._version,
                InheritedDocument._score,
                InheritedDocument._all,
                InheritedDocument.name,
                InheritedDocument.status,
                InheritedDocument.group,
                InheritedDocument.price,
                InheritedDocument.tags,
                InheritedDocument.date_created,
                InheritedDocument.unused,
                InheritedDocument.description,
            ]
        )
        self.assertIsInstance(InheritedDocument.name, AttributedField)
        self.assertIsInstance(InheritedDocument.name.get_field().get_type(), String)
        self.assertEqual(collect_doc_classes(InheritedDocument.name), {InheritedDocument})
        self.assertIsInstance(InheritedDocument.name.raw, AttributedField)
        self.assertIsInstance(InheritedDocument.name.raw.get_field().get_type(), String)
        self.assertEqual(collect_doc_classes(InheritedDocument.name.raw), {InheritedDocument})
        self.assertIsInstance(InheritedDocument.description, AttributedField)
        self.assertEqual(collect_doc_classes(InheritedDocument.description), {InheritedDocument})

        doc = InheritedDocument(_id=123)
        self.assertIsInstance(doc._id, int)
        self.assertEqual(doc._id, 123)
        self.assertIs(doc.name, None)
        self.assertIs(doc.status, None)
        self.assertIs(doc.description, None)
        self.assertEqual(
            doc.to_source(),
            {}
        )

        doc = InheritedDocument(_id=123, status=0, name='Test', i_attr_1=1, i_attr_2=2, face_attr_3=3)
        self.assertEqual(
            doc.to_source(),
            {
                'status': 0,
                'test_name': 'Test',
                'i_attr_1': 1,
                'i_attr_2': 2,
            }
        )
Ejemplo n.º 12
0
    def test_multi_type_search(self):
        def seller_mapper(ids):
            return {id: '{0}-{0}'.format(id) for id in ids}

        def customer_mapper(ids):
            return {id: '{0}:{0}'.format(id) for id in ids}
        
        sq = (
            self.index.query(
                self.index.seller.name.first.match('Alex'),
                doc_cls=(self.index.seller, self.index.customer)
            )
            .with_instance_mapper({self.index.seller: seller_mapper,
                                   self.index.customer: customer_mapper})
            .filter(self.index.customer.birthday >= datetime.date(1960, 1, 1))
            .limit(2)
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.seller, self.index.customer})

        self.client.search = MagicMock(
            return_value={
                'hits': {
                    'hits': [
                        {
                            '_id': '3',
                            '_type': 'customer',
                            '_index': 'test',
                            '_score': 2.437682,
                            '_source': {
                                'name': {
                                    'first': 'Alex',
                                    'last': 'Exler'
                                },
                                'birthday': '1966-10-04'
                            },
                        },
                        {
                            '_id': '21',
                            '_type': 'seller',
                            '_index': 'test',
                            '_score': 2.290845,
                            '_source': {
                                'name': {
                                    'first': 'Alexa',
                                    'last': 'Chung'
                                },
                                'birthday': '1983-10-05',
                                'rating': 4.8
                            },
                        }
                    ],
                    'max_score': 2.437682,
                    'total': 73
                },
                'timed_out': False,
                'took': 25
            }
        )
        results = sq.result

        self.client.search.assert_called_with(
            index='test',
            doc_type='seller,customer',
            body={
                'query': {
                    'filtered': {
                        'query': {
                            'match': {'name.first': 'Alex'}
                        },
                        'filter': {
                            'range': {'birthday': {'gte': datetime.date(1960, 1, 1)}}
                        }
                    }
                },
                'size': 2
            },
        )

        self.assertEqual(len(sq.result.hits), 2)
        doc = sq.result.hits[0]
        self.assertIsInstance(doc, self.index.customer)
        self.assertEqual(doc._id, '3')
        self.assertEqual(doc._type, 'customer')
        self.assertEqual(doc._index, 'test')
        self.assertAlmostEqual(doc._score, 2.437682)
        self.assertEqual(doc.name.first, 'Alex')
        self.assertEqual(doc.name.last, 'Exler')
        self.assertEqual(doc.birthday, '1966-10-04')
        self.assertEqual(doc.instance, '3:3')
        doc = sq.result.hits[1]
        self.assertIsInstance(doc, self.index.seller)
        self.assertEqual(doc._id, '21')
        self.assertEqual(doc._type, 'seller')
        self.assertEqual(doc._index, 'test')
        self.assertAlmostEqual(doc._score, 2.290845)
        self.assertEqual(doc.name.first, 'Alexa')
        self.assertEqual(doc.name.last, 'Chung')
        self.assertEqual(doc.birthday, '1983-10-05')
        self.assertAlmostEqual(doc.rating, 4.8)
        self.assertEqual(doc.instance, '21-21')
Ejemplo n.º 13
0
    def test_search(self):
        class CarObject(object):
            def __init__(self, id):
                self.id = id
                self.name = '{0}:{0}'.format(id)

        def _obj_mapper(ids):
            return {id: CarObject(int(id)) for id in ids}
        obj_mapper = Mock(wraps=_obj_mapper)

        class NameDocument(Document):
            first = Field(String)
            last = Field(String)

        class CarSellerDocument(Document):
            name = Field(Object(NameDocument))
            rating = Field(Float)
        
        class CarDocument(Document):
            __doc_type__ = 'car'

            vendor = Field(String)
            model = Field(String)
            year = Field(Integer)
            seller = Field(Object(CarSellerDocument))
            
        self.client.search = MagicMock(
            return_value={
                'hits': {
                    'hits': [
                        {
                            '_id': '31888815',
                            '_type': 'car',
                            '_index': 'ads',
                            '_score': 4.675524,
                            '_source': {
                                'vendor': 'Subaru',
                                'model': 'Imprezza',
                                'year': 2004,
                            },
                        },
                        {
                            '_id': '987321',
                            '_type': 'car',
                            '_index': 'ads',
                            '_score': 3.654321,
                            '_source': {
                                'vendor': 'Subaru',
                                'model': 'Forester',
                                'year': 2007,
                            },
                        }
                    ],
                    'max_score': 4.675524,
                    'total': 6234
                },
                'timed_out': False,
                'took': 47
            }
        )
        sq = (
            self.index.query(
                CarDocument.seller.name.first.match('Alex'),
                search_type='dfs_query_then_fetch',
            )
            .filter(CarDocument.seller.rating > 4)
            .with_instance_mapper(obj_mapper)
        )
        self.assertEqual(collect_doc_classes(sq), {CarDocument})
        results = sq.result

        self.client.search.assert_called_with(
            index='test',
            doc_type='car',
            body={
                'query': {
                    'filtered': {
                        'query': {
                            'match': {'seller.name.first': 'Alex'}
                        },
                        'filter': {
                            'range': {'seller.rating': {'gt': 4.0}}
                        }
                    }
                }
            },
            search_type='dfs_query_then_fetch',
        )

        self.assertEqual(len(sq.result.hits), 2)
        doc = sq.result.hits[0]
        self.assertIsInstance(doc, CarDocument)
        self.assertEqual(doc._id, '31888815')
        self.assertEqual(doc._type, 'car')
        self.assertEqual(doc._index, 'ads')
        self.assertAlmostEqual(doc._score, 4.675524)
        self.assertEqual(doc.vendor, 'Subaru')
        self.assertEqual(doc.model, 'Imprezza')
        self.assertEqual(doc.year, 2004)
        self.assertEqual(doc.instance.id, 31888815)
        self.assertEqual(doc.instance.name, '31888815:31888815')
        doc = sq.result.hits[1]
        self.assertIsInstance(doc, CarDocument)
        self.assertEqual(doc._id, '987321')
        self.assertEqual(doc._type, 'car')
        self.assertEqual(doc._index, 'ads')
        self.assertAlmostEqual(doc._score, 3.654321)
        self.assertEqual(doc.vendor, 'Subaru')
        self.assertEqual(doc.model, 'Forester')
        self.assertEqual(doc.year, 2007)
        self.assertEqual(doc.instance.id, 987321)
        self.assertEqual(doc.instance.name, '987321:987321')
        self.assertEqual(obj_mapper.call_count, 1)
Ejemplo n.º 14
0
    def test_aggregations(self):
        f = DynamicDocument.fields

        sq = SearchQuery().aggregations(min_price=agg.Min(f.price))
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "min_price": {
                        "min": {"field": "price"}
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = SearchQuery().aggregations(genders=agg.Terms(f.gender))
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "genders": {
                        "terms": {"field": "gender"}
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .aggregations(
                type=agg.Terms(f.type, aggs={'min_price': agg.Min(f.price)})
            )
        )
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "type": {
                        "terms": {"field": "type"},
                        "aggregations": {
                            "min_price": {
                                "min": {"field": "price"}
                            }
                        }
                    }
                }
            },
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .aggregations(
                top_tags=(
                    agg.Terms(
                        f.tags,
                        size=3,
                        aggs={
                            'top_tag_hits': agg.TopHits(
                                sort=f.last_activity_date.desc(),
                                size=1,
                                _source=Params(include=[f.title]))
                        }
                    )
                )
            )
        )
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "top_tags": {
                        "terms": {
                            "field": "tags",
                            "size": 3
                        },
                        "aggregations": {
                            "top_tag_hits": {
                                "top_hits": {
                                    "sort": {
                                        "last_activity_date": "desc"
                                    },
                                    "_source": {
                                        "include": ["title"]
                                    },
                                    "size" : 1
                                }
                            }
                        }
                    }
                }
            }  
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .aggregations({
                'top_sites': agg.Terms(
                    f.domain,
                    order=Sort('top_hit', 'desc'),
                    aggs={
                        'top_tags_hits': agg.TopHits(),
                        'top_hit': agg.Max(script='_doc.score'),
                    }
                )
            })
        )
        self.assert_expression(
            sq,
            {
                "aggregations": {
                    "top_sites": {
                        "terms": {
                            "field": "domain",
                            "order": {
                                "top_hit": "desc"
                            }
                        },
                        "aggregations": {
                            "top_tags_hits": {
                                "top_hits": {}
                            },
                            "top_hit" : {
                                "max": {
                                    "script": "_doc.score"
                                }
                            }
                        }
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})
Ejemplo n.º 15
0
    def test_search_query_compile(self):
        f = DynamicDocument.fields

        sq = SearchQuery()
        self.assert_expression(sq, {})
        self.assertEqual(collect_doc_classes(sq), set())

        sq = SearchQuery(Term(f.user, 'kimchy')).limit(10).offset(0)
        self.assert_expression(
            sq,
            {
                "from": 0,
                "size": 10,
                "query": {
                    "term": {"user": "******"}
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = SearchQuery(Term(f.user, 'kimchy')).filter(f.age >= 16)
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "query": {
                            "term": {"user": "******"}
                        },
                        "filter": {
                            "range": {
                                "age": {"gte": 16}
                            }
                        }
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery(Term(f.user, 'kimchy'))
            .query(f.user != 'kimchy')
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "bool": {
                        "must_not": [
                            {
                                "term": {"user": "******"}
                            }
                        ]
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery(Term(f.user, 'kimchy'))
            .query(None)
        )
        self.assert_expression(sq, {})
        self.assertEqual(collect_doc_classes(sq), set())

        sq = (
            SearchQuery(Term(f.user, 'kimchy'))
            .filter(f.age >= 16)
            .filter(f.lang == 'English')
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "query": {
                            "term": {"user": "******"}
                        },
                        "filter": {
                            "bool": {
                                "must": [
                                    {
                                        "range": {
                                            "age": {"gte": 16}
                                        }
                                    },
                                    {
                                        "term": {
                                            "lang": "English"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .order_by(
                f.opinion_rating.desc(missing='_last'),
                f.opinion_count.desc(),
                f.id
            )
        )
        self.assert_expression(
            sq,
            {
                "sort": [
                    {
                        "opinion_rating": {
                            "order": "desc",
                            "missing": "_last"
                        }
                    },
                    {
                        "opinion_count": "desc"
                    },
                    "id"
                ]
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .order_by(
                f.opinion_rating.desc(missing='_last'),
                f.opinion_count.desc(),
                f.id
            )
            .order_by(None)
            .order_by(None)
        )
        self.assert_expression(sq, {})
        self.assertEqual(collect_doc_classes(sq), set())

        sq = SearchQuery().source(f.name, f.company)
        self.assert_expression(
            sq,
            {
                "_source": ["name", "company"]
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = SearchQuery().source(exclude=[f.name, f.company])
        self.assert_expression(
            sq,
            {
                "_source": {
                    "exclude": ["name", "company"]
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .source(
                include=[f.obj1.wildcard('*'), f.obj2.wildcard('*')],
                # FIXME: f.wildcard('*')
                exclude=DynamicDocument.wildcard('*').description
            )
        )
        self.assert_expression(
            sq,
            {
                "_source": {
                    "include": ["obj1.*", "obj2.*"],
                    "exclude": "*.description"
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .source(None)
            .source(f.name, f.company)
            .source(None)
        )
        self.assert_expression(sq, {})
        self.assertEqual(collect_doc_classes(sq), set())

        sq = (
            SearchQuery()
            .source(f.name, f.company)
            .source(False)
        )
        self.assert_expression(
            sq,
            {
                "_source": False
            }
        )
        self.assertEqual(collect_doc_classes(sq), set())

        self.assert_expression(
            SearchQuery()
            .function_score({'random_score': {"seed": 1234}}),
            {
                "query": {
                    "function_score": {
                        "functions": [
                            {
                                "random_score": {"seed": 1234}
                            }
                        ],
                    }
                }
            }
        )

        sq = (
            SearchQuery(MultiMatch('Iphone 6', fields=[f.name, f.description]))
            .filter(f.status == 0)
            .function_score(None)
            .function_score({'_score': {"seed": 1234}})
            .function_score(None)
            .function_score({'field_value_factor': {'field': f.popularity,
                                                    'factor': 1.2,
                                                    'modifier': 'sqrt'}},
                            boost_mode='sum')
            .function_score({'boost_factor': 3,
                             'filter': f.region == 12})
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "query": {
                            "function_score": {
                                "query": {
                                    "multi_match": {
                                        "query": "Iphone 6",
                                        "fields": ["name", "description"]
                                    }
                                },
                                "functions": [
                                    {
                                        "field_value_factor": {
                                            "field": "popularity",
                                            "factor": 1.2,
                                            "modifier": "sqrt"
                                        }
                                    },
                                    {
                                        "filter": {
                                            "term": {"region": 12}
                                        },
                                        "boost_factor": 3
                                    }
                                ],
                                "boost_mode": "sum"
                            }
                        },
                        "filter": {
                            "term": {"status": 0}
                        }
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {DynamicDocument})

        sq = (
            SearchQuery()
            .rescore(
                QueryRescorer(
                    self.index.t.field1.match('the quick brown', type='phrase', slop=2)
                )
            )
            .rescore(None)
            .rescore(
                QueryRescorer(
                    self.index.t.field1.match('the quick brown fox', type='phrase', slop=2),
                    query_weight=0.7,
                    rescore_query_weight=1.2
                ),
                window_size=100,
            )
            .rescore(
                QueryRescorer(
                    FunctionScore(script_score={'script': "log10(doc['numeric'].value + 2)"}),
                    score_mode='multiply'
                ),
                window_size=10,
            )
        )
        self.assert_expression(
            sq,
            {
                "rescore": [
                    {
                        "window_size": 100,
                        "query": {
                            "rescore_query": {
                                "match": {
                                    "field1": {
                                        "query": "the quick brown fox",
                                        "type": "phrase",
                                        "slop": 2
                                    }
                                }
                            },
                            "query_weight": 0.7,
                            "rescore_query_weight": 1.2
                        }
                    },
                    {
                        "window_size": 10,
                        "query": {
                            "score_mode": "multiply",
                            "rescore_query": {
                                "function_score": {
                                    "script_score": {
                                        "script": "log10(doc['numeric'].value + 2)"
                                    }
                                }
                            }
                        }
                    }
                ]
            }
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.t})

        sq = SearchQuery().post_filter(self.index.shirt.color == 'red')
        self.assert_expression(
            sq,
            {
                "post_filter": {
                    "term": {"color": "red"}
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.shirt})

        sq = (
            SearchQuery()
            .filter(self.index.shirt.brand == 'gucci')
            .post_filter(self.index.shirt.color == 'red')
            .post_filter(self.index.shirt.model == 't-shirt')
        )
        self.assert_expression(
            sq,
            {
                "query": {
                    "filtered": {
                        "filter": {
                            "term": {"brand": "gucci"}
                        }
                    }
                },
                "post_filter": {
                    "bool": {
                        "must": [
                            {"term": {"color": "red"}},
                            {"term": {"model": "t-shirt"}}
                        ]
                    }
                }
            }
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.shirt})
Ejemplo n.º 16
0
    def test_highlight(self):
        sq = SearchQuery()
        sq = sq.highlight(fields={'content': {}})
        self.assertEqual(collect_doc_classes(sq), set())
        self.assert_expression(
            sq,
            {
                "highlight": {
                    "fields": {
                        "content": {}
                    }
                }
            }
        )

        sq = SearchQuery()
        sq = sq.highlight(
            fields=[self.index.test.content],
            pre_tags=['[em]'],
            post_tags=['[/em]']
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.test})
        self.assert_expression(
            sq,
            {
                "highlight": {
                    "fields": [
                        {
                            "content": {}
                        }
                    ],
                    "pre_tags": ["[em]"],
                    "post_tags": ["[/em]"]
                }
            }
        )

        sq = SearchQuery()
        sq = sq.highlight(
            fields=[
                self.index.test.content.highlight(
                    matched_fields=[self.index.test.content, self.index.test.content.plain],
                    type='fvh',
                )
            ]
        )
        self.assertEqual(collect_doc_classes(sq), {self.index.test})
        self.assert_expression(
            sq,
            {
                "highlight": {
                    "fields": [
                        {
                            "content": {
                                "matched_fields": ["content", "content.plain"],
                                "type": "fvh"
                            }
                        }
                    ]
                }
            }
        )