elasticsearch 从index中获取所有只包含字段名的值

jhkqcmku  于 8个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(86)

如何在Elasticsearch中从索引中获取所有字段名(键名),而不是它的值
我试着使用以下请求。
GET /my_index/_field_caps?fields=*&filter_path=fields.*
但上述请求还返回字段的数据类型。
预期输出(数组或所有字段的JSON)

{  
  fields:{  
   "field_1",  
   "field_2.subfield_1",  
   "field_2.subfield_2",  
   "field_3.subfield_1.another_field"  
  }  
}
9jyewag0

9jyewag01#

您可以使用_mapping API获取结果。

PUT test_ip/_mapping
{
  "properties": {
    "IP": {
      "type": "ip"
    },
    "other_fields": {
      "type": "keyword"
    },
    "date": {
      "type": "date"
    }
  }
}

GET test_ip/_mapping

输出:

{
  "test_ip": {
    "mappings": {
      "properties": {
        "IP": {
          "type": "ip",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "date": {
          "type": "date"
        },
        "other_fields": {
          "type": "keyword"
        },
        "text": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

如果你想看到更多漂亮的结果,你可以使用一些脚本来修改结果。

以下是python脚本结果:

musab@192 elasticsearch % python3 get_fields.py
{
  "IP.keyword": "keyword",
  "date": "date",
  "other_fields": "keyword",
  "text.keyword": "keyword"
}

以下是Python代码:

import requests
import json

def parse_properties(properties, parent_key='', path_dict={}):
    for k, v in properties.items():
        new_key = f"{parent_key}.{k}" if parent_key else k
        if 'properties' in v:
            parse_properties(v['properties'], new_key, path_dict)
        elif 'fields' in v:
            parse_properties(v['fields'], new_key, path_dict)
        else:
            path_dict[new_key] = v['type']
    return path_dict

response = requests.get('http://localhost:9200/test_ip/_mapping').json()
properties = response['test_ip']['mappings']['properties']
fields = parse_properties(properties)
print(json.dumps(fields, indent=2))

编辑:这里是更准确的一个

vim get_fields.py
#this script is listing ALL field names and field types of the selected index.
import requests
import json

# Define Elasticsearch endpoint, username, and password
ES_URL = 'https://localhost:9200'
USERNAME = 'elastic'
PASSWORD = 'your_elasticsearch_password'

def parse_properties(properties, parent_key='', path_dict={}):
    for k, v in properties.items():
        new_key = f"{parent_key}.{k}" if parent_key else k
        path_dict[new_key] = v['type']
        if 'properties' in v:
            parse_properties(v['properties'], new_key, path_dict)
        elif 'fields' in v:
            parse_properties(v['fields'], new_key, path_dict)
    return path_dict

def index_exists(index_name):
    url = f'{ES_URL}/{index_name}'
    response = requests.head(url, auth=(USERNAME, PASSWORD))
    return response.status_code == 200

def get_mapping(index_name):
    url = f'{ES_URL}/{index_name}/_mapping'
    response = requests.get(url, auth=(USERNAME, PASSWORD)).json()
    properties = response[index_name]['mappings']['properties']
    fields = parse_properties(properties)
    return fields

def main():
    index_name = input("Enter the index name: ")
    if not index_exists(index_name):
        print(f"Error: Index '{index_name}' does not exist.")
        return

    fields = get_mapping(index_name)
    print(json.dumps(fields, indent=2))

if __name__ == "__main__":
    main()

更新后脚本的输出:

python3 get_fields.py

Enter the index name: test_ip
{
  "IP": "ip",
  "IP.keyword": "keyword",
  "date": "date",
  "other_fields": "keyword",
  "test2": "geo_point",
  "text": "text",
  "text.keyword": "keyword"
}
GET test_ip/_mapping
{
  "test_ip": {
    "mappings": {
      "properties": {
        "IP": {
          "type": "ip",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "date": {
          "type": "date"
        },
        "other_fields": {
          "type": "keyword"
        },
        "test2": {
          "type": "geo_point"
        },
        "text": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

相关问题