问题描述
我无法使用elasticsearch_dsl加入联接。从根本上来说,我认为我不知道如何在ElasticsSearch 7+中构建连接
这是我正在做的一个精简示例:
from elasticsearch import Elasticsearch
from elasticsearch_dsl import (Document,Integer,Join,Text)
class BaseDocument(Document):
class Index:
name = "my_index"
using = Elasticsearch("http...")
class Purchase(BaseDocument):
amount = Integer()
class User(BaseDocument):
name = Text()
user_purchases = Join(relations={"user": "purchase"})
def add_purchase(self,*,amount: int):
purchase = Purchase(
_routing=self.Meta.id,user={"name": "purchase","parent": self.Meta.id},amount=amount,)
purchase.save()
return purchase
User.init()
user = User(_id=1,name="yas")
user.save()
user.add_purchase(amount=100)
哪个映射为:
{
"my_index": {
"mappings": {
"properties": {
"amount": {
"type": "long"
},"user": {
"properties": {
"name": {
"type": "text","fields": {
"keyword": {
"type": "keyword","ignore_above": 256
}
}
},"parent": {
"type": "long"
}
}
},"user_purchases": {
"type": "join","eager_global_ordinals": true,"relations": {
"user": "purchase"
}
}
}
}
}
}
文件:
{
"took": 578,"timed_out": false,"_shards": {
"total": 1,"successful": 1,"skipped": 0,"Failed": 0
},"hits": {
"total": {
"value": 2,"relation": "eq"
},"max_score": 1.0,"hits": [
{
"_index": "my_index","_type": "_doc","_id": "1","_score": 1.0,"_source": {
"name": "yas"
}
},{
"_index": "my_index","_id": "bSTqDHQBsgOLaltlJlWo","_routing": "1","_source": {
"user": {
"name": "purchase","parent": "1"
},"amount": 100
}
}
]
}
}
但是子搜索失败:
{
"query": {
"has_child": {
"type": "purchase","query": {
"match_all": {}
}
}
}
}
我预计会有1次命中,但是得到零:
"hits": {
"total": {
"value": 0,"max_score": null,"hits": []
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)