JSON中ElasticSearchMap后的结果为空

yiytaume  于 2023-01-25  发布在  ElasticSearch
关注(0)|答案(1)|浏览(142)

我有这个JSON:

{"index":{"_index":"companydatabase"}}  
{"FirstName":"ELVA","LastName":"RECHKEMMER","Designation":"CEO","Salary":"154000","DateOfJoining":"1993-01-11","Address":"8417 Blue Spring St. Port Orange, FL 32127","Gender":"Female","Age":62,"MaritalStatus":"Unmarried","Interests":["Body Building","Illusion","Protesting","Taxidermy","TV watching","Cartooning","Skateboarding"]}
{"index":{"_index":"companydatabase"}}  
{"FirstName":"JENNEFER","LastName":"WENIG","Designation":"President","Salary":"110000","DateOfJoining":"2013-02-07","Address":"16 Manor Station Court Huntsville, AL 35803","Gender":"Female","Age":45,"MaritalStatus":"Unmarried","Interests":["String Figures","Working on cars","Button Collecting","Surf Fishing"]}
{"index":{"_index":"companydatabase"}}

etc

我需要获得男性员工和女性员工的平均值进行比较
Salary字段的类型是Text,因此我进行了Map,将字段转换为Integer
我的代码是:

mapping_type = {
    'mappings': {
        'properties': {
            'Adress': {
                'type': 'text',
                'fields': {
                    'keyword': {
                        'type': 'keyword', 
                        'ignore_above': 256
                    }
                }
            },
            'Age': {
                'type': 'long'
            },
            'DateOfJoining': {
                'type': 'date'
            },
            'Designation': {
                'type': 'text',
                'fields': {
                    'keyword': {
                        'type': 'keyword', 
                        'ignore_above': 256
                    }
                }
            },
            'FirstName': {
                'type': 'text',
                'fields': {
                    'keyword': {
                        'type': 'keyword', 
                        'ignore_above': 256
                    }
                }
            },
            'Gender': {
                'type': 'text',
                'fields': {
                    'keyword': {
                        'type': 'keyword', 
                        'ignore_above': 256
                    }
                }
            },
            'Interest': {
                'type': 'text',
                'fields': {
                    'keyword': {
                        'type': 'keyword', 
                        'ignore_above': 256
                    }
                }
            },
            'LastName': {
                'type': 'text',
                'fields': {
                    'keyword': {
                        'type': 'keyword', 
                        'ignore_above': 256
                    }
                }
            },
            'MaritalStatus': {
                'type': 'text',
                'fields': {
                    'keyword': {
                        'type': 'keyword', 
                        'ignore_above': 256
                    }
                }
            },
            'Salary': {
                'type': 'integer'
            }
        }
    }
}

es.indices.delete(index="companydatabase",ignore=[400,404])
es.indices.create(index="companydatabase",body=mapping_type)

及以后

request_body={
    "size": 0,
    "aggs": {
        "salary_by_job": {
            "terms": {
                "field": "Designation.keyword"
            },
            "aggs": {
                "salary_by_gender": {
                    "terms": {
                        "field": "Gender.keyword"
                    },
                    "aggs": {
                        "average_salary": {
                            "avg": {
                                "field": "Salary"
                            }
                        }
                    }
                }
            }
        }
    }
}

JSON(es.search(index="companydatabase", body=request_body))

但是密码把我
...点击:
点击次数:[] 0条
最大评分:空...
我需要一个号码,显然不是空的。
感谢您的帮助:)

nfg76nw0

nfg76nw01#

尾巴;

您没有查看响应中的正确位置。您应该查看响应的aggregations部分。

演示

我在索引中创建了相同的Map。在您的示例中,使用您的搜索查询批量摄取了两个文档。
结果如下

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "salary_by_job": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "CEO",
          "doc_count": 1,
          "salary_by_gender": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Female",
                "doc_count": 1,
                "average_salary": {
                  "value": 154000
                }
              }
            ]
          }
        },
        {
          "key": "President",
          "doc_count": 1,
          "salary_by_gender": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Female",
                "doc_count": 1,
                "average_salary": {
                  "value": 110000
                }
              }
            ]
          }
        }
      ]
    }
  }
}

我有:
...点击:
点击次数:[] 0条
最大评分:空...
这是因为size: 0。但这不应该影响其他部分。
如您所见,我有3个聚合:
1.按职务分列的薪金
1.按性别分列的薪金
1.平均工资
按这个顺序嵌套。
所以看起来效果很好。

相关问题