基于ElasticSearch+Vue实现简易搜索

基于ElasticSearch+Vue实现简易搜索

一、模拟数据

产品名称 描述 价格 库存数量 品牌名称
智能手表 智能手表,具有健康跟踪和通知功能。 199.99 1000 TechWatch
4K智能电视 4K分辨率智能电视,提供出色的画质。 699.99 500 VisionTech
无线耳机 降噪无线耳机,提供高品质音频体验。 149.99 800 AudioMasters
笔记本电脑 高性能笔记本电脑,配备快速处理器。 999.99 300 TechLaptops
数码相机 高分辨率数码相机,支持多种拍摄模式。 449.99 200 PhotoPro
便携式充电器 便携式充电器,为移动设备提供电力。 29.99 2000 PowerBoost
无线路由器 高速无线路由器,适用于大型网络。 79.99 400 NetSpeed
游戏机 游戏机,支持多种游戏和娱乐功能。 399.99 100 GameZone
手机壳 手机壳,适用于各种手机型号。 19.99 1500 PhoneGuard
运动鞋 高性能运动鞋,适用于各种运动。 79.99 800 SportsTech
4K超高清显示器 4K超高清显示器,提供卓越的图像质量。 599.99 150 UltraView
智能家居设备 智能家居设备,实现智能化家居控制。 249.99 300 SmartLiving

二、python导入脚本

# coding=gbk
import pandas as pd
from elasticsearch import Elasticsearch
from elasticsearch.helpers import streaming_bulk

# 连接到Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

# 检查是否成功连接
if es.ping():
    print("Connected to Elasticsearch")
else:
    print("Failed to connect to Elasticsearch")

# 读取Excel文件
data = pd.read_excel('demoData.xls')

# 将DataFrame转换为字典格式
documents = data.to_dict(orient='records')

# 逐个文档导入数据到Elasticsearch
success, failed = 0, 0
total_documents = len(documents)
for doc in documents:
    index_action = {
        '_index': 'ecommerce',  # 修改为你的索引名称
        '_id': doc['产品名称'],  # 使用产品名称作为文档ID
        '_source': doc
    }
    try:
        result = next(streaming_bulk(es, [index_action], index=index_action['_index']))
        success += 1
    except Exception as e:
        print(f"Failed to index document {index_action['_id']}: {e}")
        failed += 1

print(f'Successfully indexed {success} documents, failed to index {failed} documents.')

三、vue代码





  
  
  
  



  

基于ElasticSearch+Vue实现简易搜索




产品名称: {{ result.productName }}
描述: {{ result.productDescription }}
价格: {{ result.productPrice }}
库存数量: {{ result.productStock }}
品牌名称: {{ result.productBrand }}

四、效果图

基于ElasticSearch+Vue实现简易搜索_第1张图片
基于ElasticSearch+Vue实现简易搜索_第2张图片

你可能感兴趣的:(elasticsearch,vue.js,大数据)