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

来源:互联网 发布:淘宝客服如何打开分流 编辑:程序博客网 时间:2024/05/10 21:00

名词解释:

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,默认端口9200conn = 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-typeconn.indices.put_mapping("test-type",{"properties":mapping},["test-index"])#继承test-type      相当于sql 中的 create table test-type2conn.indices.put_mapping("test-type2",{"_parent":{"type":"test-type"}},["test-index"])#插入数据 相当于sql中的insertconn.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")



原创粉丝点击