ES如何在界面创建index和type

传送站:es的批量传输和增删改查

es本质上也是数据库,只不过是分析型数据库,可以快速响应大数据量查询,其中index类似于数据库,type类似于表

那么如何在应用界面端创建index和type呢

首先打开主页,点击复合查询,在红框位置输入对应的index_name和在代码里输入对应的type_name以及相关字段即可创建index_name和type_name

注意:es并不是严格意义上的结构化数据,所以对表结构要求不高,因此title那里可任意设置字段,后续可以根据实际业务自动更改
其实也可以在大数据平台通过数据交换来把数据传进es的同时直接创建index和type

附json创建语句:

{
    "settings": {
        "index": {
            "number_of_shards": "5",
            "number_of_replicas": "1"
        }
    },
    "mappings": {
        "type_name": {
            "properties": {
                "created": {
                    "type": "date"
                },
                "name": {
                    "analyzer": "standard",
                    "boost": 2,
                    "type": "text"
                },
                "title": {
                    "type": "text"
                },
                "age": {
                    "type": "integer"
                }
            }
        }
    }
}

最后创建完毕后,需要开启index,否则会报index_closed_exception异常,开启方式如下

POST /index_name/_open

返回结果

{
  "acknowledged": true,
  "shards_acknowledged": true
}

即为开启正常

好的,我可以为您提供一些指导。 首先,您需要安装Elasticsearch和Kibana。Elasticsearch是一个开源的搜索引擎,可以非常方便地对文本进行索引和搜索。而Kibana是一个可视化工具,可以用来展示Elasticsearch中的数据。 安装Elasticsearch和Kibana的方法可以参考官方文档:https://www.elastic.co/guide/index.html 安装完成后,您需要创建一个索引,用来存储文件的元数据。可以使用Elasticsearch提供的REST API或者Kibana的Dev Tools来创建索引。以下是一个创建索引的示例: ``` PUT /files { "mappings": { "properties": { "name": {"type": "text"}, "path": {"type": "text"}, "content": {"type": "text"}, "size": {"type": "integer"}, "created_at": {"type": "date"}, "modified_at": {"type": "date"} } } } ``` 上面的代码创建了一个名为“files”的索引,包含了文件的名称、路径、内容、大小、创建时间和修改时间等元数据信息。 然后,您需要编写一个脚本,用来将文件的元数据添加到索引中。可以使用Python、Java或者其他语言来编写脚本。以下是一个Python脚本的示例: ```python import os from elasticsearch import Elasticsearch es = Elasticsearch() for root, dirs, files in os.walk('/path/to/files'): for file in files: path = os.path.join(root, file) with open(path, 'r') as f: content = f.read() doc = { 'name': file, 'path': path, 'content': content, 'size': os.path.getsize(path), 'created_at': os.path.getctime(path), 'modified_at': os.path.getmtime(path) } es.index(index='files', body=doc) ``` 上面的代码会遍历指定目录下的所有文件,将文件的元数据添加到名为“files”的索引中。 最后,您可以编写一个简单的Web界面,让用户可以搜索文件。可以使用Python的Flask框架或者其他框架来编写Web应用。以下是一个Flask应用的示例: ```python from flask import Flask, request from elasticsearch import Elasticsearch app = Flask(__name__) es = Elasticsearch() @app.route('/') def index(): query = request.args.get('q', '') if query: res = es.search(index='files', body={'query': {'match': {'content': query}}}) hits = res['hits']['hits'] else: hits = [] return render_template('index.html', hits=hits) if __name__ == '__main__': app.run() ``` 上面的代码创建了一个简单的搜索页面,可以通过GET请求传递搜索关键词“q”,然后查询Elasticsearch中的“files”索引,返回匹配的文件元数据。最后,可以在页面上展示搜索结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神芷迦蓝寺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值