通过嵌套字段属性之和搜索文档

问题描述 投票:0回答:1

在elasticsearch索引中我有以下文档列表:

{
  "id": 9653,
  "title": "dr",
  "first_name": "John",
  "last_name": "Doe",
  "work_experiences": [
    {
      "company": "Crazy Software",
      "start_date": "2020-12-01",
      "end_date": "2024-04-01",
      "duration": 40,
      "tiers": []
    },
    {
      "company": "Softcore",
      "start_date": "2018-05-01",
      "end_date": "2020-12-01",
      "duration": 31,
      "tiers": []
    },
    ...
  }

每个档案都有多种工作经验。我需要创建动态查询:

List all profiles that:
  were working in total at least 50 months in companies with wildcard match *name* in company name

在这种情况下,将会找到 john doe,因为他在

crazy SOFTware
工作了 40 个月,在
SOFTcore

工作了 31 个月
elasticsearch
1个回答
0
投票

我简化了你的映射

PUT /durations
{
    "mappings": {
        "properties": {
            "id": {
                "type": "integer"
            },
            "work_experiences": {
                "type": "nested",
                "properties": {
                    "duration": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}

并创建您的文档

    PUT /durations/_bulk
    {"create":{"_id":1}}
{"id":9653,"title":"dr","first_name":"John","last_name":"Doe","work_experiences":[{"company":"Crazy Software","start_date":"2020-12-01","end_date":"2024-04-01","duration":40,"tiers":[]},{"company":"Softcore","start_date":"2018-05-01","end_date":"2020-12-01","duration":31,"tiers":[]}]}

聚合查询

GET /durations/_search?filter_path=aggregations
{
    "aggs": {
        "by_id": {
            "terms": {
                "field": "id"
            },
            "aggs": {
                "user_duration": {
                    "nested": {
                        "path": "work_experiences"
                    },
                    "aggs": {
                        "total_duration": {
                            "sum": {
                                "field": "work_experiences.duration"
                            }
                        }
                    }
                },
                "duration_filter": {
                    "bucket_selector": {
                        "buckets_path": {
                            "total_duration": "user_duration>total_duration"
                        },
                        "script": "params.total_duration > 50"
                    }
                }
            }
        }
    }
}

回应

{
    "aggregations" : {
        "by_id" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
                {
                    "key" : 9653,
                    "doc_count" : 1,
                    "user_duration" : {
                        "doc_count" : 2,
                        "total_duration" : {
                            "value" : 71.0
                        }
                    }
                }
            ]
        }
    }
}

查看我的类似答案

  1. elasticsearch 按 2 个嵌套术语聚合的总和排序术语聚合
  2. elasticsearch-function-score-query-on-nested-array
© www.soinside.com 2019 - 2024. All rights reserved.