初识pyes(实现elasticsearch数据的插入)

名词解释:

boost:

boost参数被用来增加一个子句的相对权重(当boost大于1时),或者减小相对权重(当boost介于0到1时),但是增加或者减小不是线性的。换言之,boost设为2并不会让最终的_score加倍。

相反,新的_score会在适用了boost后被归一化(Normalized)。每种查询都有自己的归一化算法(Normalization Algorithm)。但是能够说一个高的boost值会产生一个高的_score。

 
 
如果你在实现你自己的不基于TF/IDF的相关度分值模型并且你需要对提升过程拥有更多的控制,你可以使用 function_score查询,它不通过归一化步骤对文档的boost进行操作。
index: 如果是no,则无法通过检索查询到该字段; 如果设置为not_analyzed则会将整个字段存储为关键词,常用于汉字短语、邮箱等复杂的字符串; 如果设置为analyzed则将会通过默认的standard分析器进行分析,详细的分析规则参考这里 store: true 独立存储 false(默认)不存储,从_source中解析

看代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2017-11-20
@author: Negen
"""
import pyes
#创建ES连接,这是我的ip地址,个人本机上就用localhost,默认端口9200
conn = pyes.ES(['10.139.32.155:9200'])

#判断索引是否存在
if conn.indices.exists_index("test-index"):
    print '该索引已存在'
else:
    # 创建一个新的索引(即sql中的create database)
    conn.indices.create_index("test-index")

#定义索引存储结构mapping相当于sql中的字段
mapping = {
    u'parsedtext':{
        'boost':1.0,
        'index':'analyzed',
        'store':'yes',
        'type':u'string',
        'term_vector':'with_positions_offsets'
        },
    u'name':{
        'boost':1.0,
        'index':'analyzed',
        'store':'yes',
        'type':u'string',
        'term_vector':'with_positions_offsets'
    },
    u'titlte':{
        'boost': 1.0,
        'index': 'analyzed',
        'store': 'yes',
        'type': u'string',
        'term_vector': 'with_positions_offsets'
    },
    u'position':{
        'store': 'yes',
        'type': u'integer',

    },
    u'uuid':{
        'boost': 1.0,
        'index': 'analyzed',
        'store': 'yes',
        'type': u'string'
    }
}
#定义test-type      相当于sql 中的 create table test-type
conn.indices.put_mapping("test-type",{"properties":mapping},["test-index"])
#继承test-type      相当于sql 中的 create table test-type2
conn.indices.put_mapping("test-type2",{"_parent":{"type":"test-type"}},["test-index"])
#插入数据 相当于sql中的insert
conn.index({"name":"Joe Tester", "parsedtext":"Joe Testere nice guy", "uuid":"11111", "position":1}, "test-index", "test-type", 1)
conn.index({"name":u"百 度 中 国"}, "test-index", "test-type")



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值