我在弹性搜索数据生产中有1个索引用于存储文档。该索引在每个名为 document_type 的文档中都有一个公共字段,用于过滤不同类型的数据。
a。 document_type =“ 用户”
b。 document_type =“ user_detail ”
{
"user_id" : "123",
"is_trial_active" : "true",
"updated_at" : "1577338950969",
"event_created_at" : "1577338950969",
"document_type" : "user"
}
{
"user_id" : "123",
"name" : "Shivam",
"gender" : "male",
"event_created_at" : 1575519449473,
"phone_number" : "+91-8383838383",
"document_type" : "user_detail",
"created_at" : 1576049770184
}
注意
如何从 document_type =“ user”中 is_trial_active 不正确的 document_type =“ user_details”获取用户详细信息?
答案 0 :(得分:1)
一个工作示例:
映射
PUT my_index
{
"mappings": {
"properties": {
"document_type": {
"type": "join",
"relations": {
"user": "user_detail"
}
}
}
}
}
发布一些文档
PUT my_index/_doc/1
{
"user_id": "123",
"is_trial_active": "false", ---> note i changed this to false for the example
"updated_at": "1577338950969",
"event_created_at": "1577338950969",
"document_type": "user"
}
PUT my_index/_doc/2?routing=1
{
"user_id": "123",
"name": "Shivam",
"gender": "male",
"event_created_at": 1575519449473,
"phone_number": "+91-8383838383",
"created_at": 1576049770184,
"document_type": {
"name": "user_detail",
"parent": "1" --> you can insert array of parents
}
}
搜索查询
GET my_index/_search
{
"query": {
"has_parent": {
"parent_type": "user",
"query": {
"bool": {
"must_not": [
{
"term": {
"is_trial_active": {
"value": "true"
}
}
}
]
}
}
}
}
}
结果
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_routing" : "1",
"_source" : {
"user_id" : "123",
"name" : "Shivam",
"gender" : "male",
"event_created_at" : 1575519449473,
"phone_number" : "+91-8383838383",
"created_at" : 1576049770184,
"document_type" : {
"name" : "user_detail",
"parent" : "1"
}
}
}
]
希望这会有所帮助
答案 1 :(得分:0)
您可以使用has_parent
查询来检索子文档
希望以下查询对您有用
GET data-production/_search
{
"query": {
"has_parent": {
"parent_type": "user",
"query": {
"bool": {
"must_not": [
{
"term": {
"is_trial_active": {
"value": "true"
}
}
}
]
}
}
}
}
}