elasticsearch ES使用文檔


1. 先說需求

有一批醫療數據,需要搭建搜索引擎數據庫,按照之前的管理,我優先選擇了python的whoosh,畢竟對自己熟悉的東西會最先使用

同時,對ES不是特別了解,用whoosh搭建了數據庫

 

問題:

由於數據有幾個G,數據量巨大,導致whoosh在用的時候,內存溢出,MemoryError。故此,我決定改用ES

 

2. ES使用文檔

參考:
es文檔
搭建
https://blog.csdn.net/zhezhebie/article/details/105482149
https://www.jianshu.com/p/da3c3612686a
下載
https://elasticsearch.cn/download/
使用
https://blog.csdn.net/diyiday/article/details/82153780


配置文件
https://www.cnblogs.com/hanyouchun/p/5163183.html
文件位置:
/etc/elasticsearch/elasticsearch.yml


創建索引有問題:
400, 'mapper_parsing_exception', 'Root mapping definition has unsupported parameters:
解決方案:
https://blog.csdn.net/h_sn9999/article/details/102767040

統計總數據量
https://blog.csdn.net/whq12789/article/details/101062968

 

下載ES,這里我選擇了最新版的

本地的下載速度比服務器下載的還快,需要等很久,我等了1h
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.0-x86_64.rpm

 

搭建rmp,我選擇直接在服務器直接搭建

rpm -Uvh /路徑/es_64位.rpm

systemctl enable elasticsearch  開機自啟動
systemctl start elasticsearch 啟動
systemctl status elasticsearch 啟動

查看日志
/var/log/elas.../elas....log 日志文件

 

修改配置文件

ES拒絕你連接, 怎么辦,修改配置文件

ES默認端口

9300端口: ES節點之間通訊使用

9200端口: ES節點 和 外部 通訊使用

 

9300是TCP協議端口號,ES集群之間通訊端口號

9200端口號,暴露ES RESTful接口端口號

 

修改配置文件

node.name: node-1

cluster.initial_master_nodes: ["node-1"]

path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 5000
discovery.seed_hosts: ["127.0.0.1"]

 

陳總的配置文件

 

 

修改ES host主機報錯

 

 看日志

 

 at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

按照上面配置文件修改就可以解決

 

使用ES

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from elasticsearch import Elasticsearch

es = Elasticsearch(["ip:5000"])

# journal-ref, report-no
# journal_ref, report_no
# columnName = ['id', 'submitter', 'authors', 'title', 'comments', 'journal_ref', 'doi', 'report_no', 'categories',
#               'license', 'abstract', 'versions', 'update_date', 'authors_parsed']

mappings = {
    'mappings': {
        'type_doc_test': {
            'properties': {
                'id': {
                    'type': 'text',
                },
                'submitter': {
                    'type': 'text',
                },
                'authors': {
                    'type': 'text',
                },
                'title': {
                    'type': 'text',
                },
                'comments': {
                    'type': 'text',
                },
                'journal_ref': {
                    'type': 'text',
                },
                'doi': {
                    'type': 'text',
                },
                'report_no': {
                    'type': 'text',
                },
                'categories': {
                    'type': 'text',
                },
                'license': {
                    'type': 'text',
                },
                'abstract': {
                    'type': 'text',
                },
                'versions': {
                    'type': 'text',
                },
                'update_date': {
                    'type': 'text',
                },
                'authors_parsed': {
                    'type': 'text',
                }
            }
        }
    }
}

mappings_1 = {      # 這個是7版本的方法,上面那個是6版本的方法
    'mappings': {
        'properties': {
            'id': {
                'type': 'text',
            },
            'submitter': {
                'type': 'text',
            },
            'authors': {
                'type': 'text',
            },
            'title': {
                'type': 'text',
            },
            'comments': {
                'type': 'text',
            },
            'journal_ref': {
                'type': 'text',
            },
            'doi': {
                'type': 'text',
            },
            'report_no': {
                'type': 'text',
            },
            'categories': {
                'type': 'text',
            },
            'license': {
                'type': 'text',
            },
            'abstract': {
                'type': 'text',
            },
            'versions': {
                'type': 'text',
            },
            'update_date': {
                'type': 'text',
            },
            'authors_parsed': {
                'type': 'text',
            }
        }
    }
}

res = es.indices.create(index="index_test", body=mappings_1)

 

具體可以參考鏈接:

Root mapping definition has unsupported parameters:  [product : {properties={title={type=text}}}

 

https://blog.csdn.net/h_sn9999/article/details/102767040

 

寫入數據

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 寫入索引數據
from decimal import Decimal
import pymysql, json
from elasticsearch import Elasticsearch


def insert_es_data():
    es = Elasticsearch(["ip:5000"])

    file_path = r"D:\files\612177_1419905_compressed_arxiv-metadata-oai-snapshot-2020-08-14/"
    file_name = r"arxiv-metadata-oai-snapshot-2020-08-14.json"
    file_path_name = file_path + file_name

    with open(file_path_name, "r", encoding='UTF-8') as f:
        for action in f.readlines():

            action = json.loads(action)
            action["journal_ref"] = action["journal-ref"]
            del action["journal-ref"]

            action["report_no"] = action["report-no"]
            del action["report-no"]

            for key in action:
                val_ = action[key]
                if not val_:
                    val_ = ""
                elif isinstance(val_, (Decimal,)):
                    val_ = str(val_)
                else:
                    val_ = pymysql.escape_string(json.dumps(val_))
                action[key] = val_

            es.index(index="index_test", body=action)

 

刪除數據

from elasticsearch import Elasticsearch

es = Elasticsearch(["ip:5000"])

res = es.delete(index="index_test", id ="oClia3QBQ2tDmCR81pYz")
print(res)

 

查詢數據

from elasticsearch import Elasticsearch

es = Elasticsearch(["ip:5000"])

doc = {
            "query": {
                "match": {
                    "comments": "published"
                }
            }
        }

import time
a = time.time()
res = es.search(index="index_test", body=doc)
print(res)
print(time.time() - a)

 

查詢總數據count

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from elasticsearch import Elasticsearch

es = Elasticsearch(["ip:5000"])


import time
a = time.time()
res = es.count(index="index_test")
print(res)
print(time.time() - a)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM