es 创建索引 指定id_ES实践

cfdca6e85382752b52d59b0c49bf22f4.png

最近使用ES做召回的一种工具,将实践分享给大家

1、ES安装和启动:

请参考博文:

MacOS中Elasticsearch的安装「借助Homebrew」 - niceyoo - 博客园​www.cnblogs.com
15f3a37af35ee9595e7ce2108b5f3495.png

2、创建索引:

from elasticsearch import Elasticsearch

es = Elasticsearch(['127.0.0.1:9200'])
mappings = {
    "mappings": {
        "properties": {
            "sp_id": {
                "type": "long",
                "index": "false"
            },
            "product_name": {
                "type": "text",
                "index": True
            },
            "third_category_id": {
                "type": "long",
                "index": True
            }
        }
    }
}

res = es.indices.create(index='cate_index', body=mappings)

# 判断索引有没有写入成功
if es.indices.exists("cate_index"):
    print('索引存在')
else:
    print('索引不存在')

3、数据写入ES:

from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
es = Elasticsearch(['127.0.0.1:9200'])
ACTIONS = []
file = open("数据.txt")

for line in file.readlines():
    line = line.strip("n")
    line_arr = line.split("t")
    print(line_arr)
    if line_arr[2] == 'NULL':
        continue
    action = {
          "_index": "cate_index",
          "_source": {
            "sp_id": line_arr[0],
            "product_name": line_arr[1],
            "third_category_id": line_arr[2]
          }
        }
    ACTIONS.append(action)

res, _ = bulk(es, ACTIONS, index="cate_index", raise_on_error=True)

注:使用bulk写入可以快速提高写入的速度,比使用index写入要快一个数量级

4、ES查询:

from elasticsearch import Elasticsearch

es = Elasticsearch(['127.0.0.1:9200'])

body = {
    "query": {
        "match": {
            "product_name": product_name
        }
    }
}

sort = {
    "_score": {
        "order": "desc"
    }
}
res = es.search(index="cate_index", body=body, size=50, sort=sort)
print(res)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值