簡體   English   中英

在 JSON 中 Elastic Search 映射后結果 null

[英]Result null after mapping in Elastic Search in JSON

我有這個 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

我需要獲得男性雇員和女性的 AVG 進行比較

Salary 字段是 Text 類型,所以我做了一個映射,將字段轉換為 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 項
max_score: null...

顯然我需要一個數字,而不是 null。

感謝您的幫助:)

Tldr;

您沒有查看響應中的正確位置。 您應該查看響應的aggregations部分。

演示

我將相同的映射創建到索引中。 在您的示例中批量攝取了兩個文檔。 使用了您的搜索查詢。

這給了我下面的結果

{
  "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 項

max_score: null...

這是因為size: 0 但這應該不會影響 rest。

如您所見,我有 3 個聚合:

  1. 工資按工作
  2. 按性別划分的工資
  3. 平均工資

按此順序嵌套。

所以它似乎工作得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM