gpt4 book ai didi

elasticsearch - 如何从 Elasticsearch 中获取数据,如 jon 查询

转载 作者:行者123 更新时间:2023-12-03 01:42:55 24 4
gpt4 key购买 nike

假设我有用户、团队和运动员文档。用户文档有一组与团队 ID 相关的对象。将 key 与用户字段匹配后,我需要在单个查询中从团队获取相关数据。

我有类似下面的东西

"size": 20,
"query": {
"bool": {
"filter" : [
{"terms" : { "_type" :["users","athletes"] }}
],
"should":[{
"multi_match" : {
"query":searchQuery,
"type":"cross_fields",
"fields":["firstName", "email","lastName","name"],
"minimum_should_match": "80%"
}
}
],
"minimum_should_match" : 1
}
}

最佳答案

ElasticSearch 在这方面受到限制,但您可以使用 has_child 查询运动员并获取相应的团队。询问。

为了使用 has_child查询您将需要在索引定义中建立团队和运动员之间的父子关系:

把运动

{
"mappings" : {
"team" : {
"properties" : {
"name" : {
"type" : "text"
}
}
},
"athlete" : {
"dynamic" : "false",
"_parent" : {
"type" : "team"
},
"_routing" : {
"required" : true
},
"properties" : {
"name" : {
"type" : "text"
}
}
}
}
}

注意 "_parent"元素。

然后您需要向父实体(团队)添加一些数据:

PUT 运动/团队
{
"name" : "A"
}

然后你需要索引一些将这些与父级相关联的运动员:

PUT 运动/运动员?parent=dc2b3e8f-7dea-4898-9852-538c0d0367f4
{
"name" : "Gil Fernandes"
}

请注意,“dc2b3e8f-7dea-4898-9852-538c0d0367f4”是 Elastic Search 中团队的 ID

最后你可以执行 has_child 查询:
{
"from" : 0,
"size" : 20,
"_source" : true,
"query" : {
"bool" : {
"must" : [ {
"has_child" : {
"type" : "athlete",
"query" : {
"match" : {
"name" : "Gil Fernandes"
}
}
}
} ]
}
}
}

结果,您将获得运动员的团队:
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "sports",
"_type" : "team",
"_id" : "dc2b3e8f-7dea-4898-9852-538c0d0367f4",
"_score" : 1.0,
"_source" : {
"name" : "A"
}
} ]
}
}

关于elasticsearch - 如何从 Elasticsearch 中获取数据,如 jon 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46278000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com