elastic_search 指令
#!/usr/bin/env python
# -*- coding: utf-8 -*- """ pass
""" import os
import sys
import jieba sys.path.append(os.path.dirname(os.path.split(os.path.realpath(__file__))[0])) from elasticsearch import Elasticsearch
from conf.settings import FAQ_ES_CONF # [{'host': '192.168.7.173', 'port': 9200}] es_ser = Elasticsearch(FAQ_ES_CONF) es_ser.indices.delete(index='customer', ignore=404) es_ser.indices.create(index='customer', ignore=400) body={"properties":{'about': {'type': 'string'},
'name': {'type': 'string'},
'age': {'type': 'integer'},
'score': {'type': 'integer'},
'company': {'type': 'string', 'index': 'not_analyzed'},
'interests': {'type': 'string'},
'timestamp': {'type': 'date'},
'id': {'type': 'integer'}}} es_ser.indices.put_mapping(index='customer', doc_type='round_FAQ2', body=body) es_ser.index(index='customer',
doc_type='round_FAQ2',
id=1,
body={"name":"wulangzhou",
"age": 25,
"score": [85,75,95],
"about": jieba.lcut('i like think deep'),
"company": 'zhangyue',
"interests": ["music"],
"timestamp": ''}) es_ser.index(index='customer',
doc_type='round_FAQ2',
id=2,
body={"name":"yanweihong",
"age": 28,
"about": jieba.lcut('i like exercise more'),
"score": [90,85,77],
"company": 'zhangyue',
"interests": ["forestry", 'i', 'like'],
"timestamp": ''}) es_ser.index(index='customer',
doc_type='round_FAQ2',
id=3,
body={"name":"liumin",
"age": 28,
"about": jieba.lcut('i like cat'),
"score": [80, 80, 80, 80],
"company": 'jindong',
"weight": 85,
"interests": ['game'],
"timestamp": ''}) import time
time.sleep(1) body={'query': {'multi_match': {'query': 'i like cat' ,
'fields': ['about', 'interests'],
'type': 'most_fields',}}}
#'tie_breaker': 0.2}}}
body={'query': {'match_phrase': {'about': 'i like'}}}
body={'query': {'range': {'age': {'gte': 18, 'lte': 35}}}}
body={'query': {'match_all': {}}}
body={'query': {'terms': {'age': [22, 20]}}}
body={'query': {'exists': {'field': 'weight'}}}
for sources in es_ser.search(index='customer', doc_type='round_FAQ2', body=body)['hits']['hits']:
for k, v in sources.items():
print k, v
print '' '''
http://www.tuicool.com/articles/uAbmuaU
match_phrase 可以看about 字段,如果该字段是string 且被设置为默认分词,可以看做是‘query_str‘ in ‘match_string’(查询字符和匹配字符都不分词进行匹配)?
match 可以看about 字段,表示 query_str分词后中的每一个词,与match_string分词后中的所有词,看能匹配到几个(查询字符和匹配字符都进行分词匹配)。
term 与 match_phrase 稍微有点区别 ‘query_str‘ == ‘match_string’ ?) (不进行分词的匹配)
multi_match 如果搭配 most_fields 表示fields中的所有字段,分词后尽量匹配多的词的和(不要带tie_breaker)
如果搭配 best_fields 表示完全匹配的分值最高 比如 i like cat 如果全部匹配到了则分高(带tie_breaker)
terms 与term 类似
bool 当我们需要and or 查询的时候,可以用 bool 查询,查询条件可以嵌套 { "bool" : { "must" : [], "should" : [], "must_not" : [], } }
def get_analyze_body(**kargs):
""" 将查询条件转成特殊的查询参数
"""
from faq.doc_idf import get_phrases_rate question = kargs.get('question')
if question and isinstance(question, str):
question = question.decode('utf-8') question = replace_string(question) question_args = get_right_phrases(filter_phrases(jieba_cut(question)))
channel_num_arg = kargs.get('channel_num')
version_arg = kargs.get('version') question_arg_rate = get_phrases_rate(question_args) should = []
for question_arg, rate in question_arg_rate.items():
should.append({'match_phrase': {'question': {'query': question_arg,
'boost': 10 * rate}}}) must_channel_num = []
must_channel_num.append({'match_phrase': {'channel_num': {'query': -1,
'boost': 1}}})
if channel_num_arg:
must_channel_num.append({'match_phrase': {'channel_num': {'query': int(channel_num_arg),
'boost': 1.5}}}) must_version = []
must_version.append({'match_phrase': {'version': {'query': -1,
'boost': 1}}})
if version_arg:
must_version.append({'match_phrase': {'version': {'query': int(version_arg),
'boost': 1.5}}}) return {'query': {'bool': {'should': should,
'must': [{'bool': {'should': must_channel_num}},
{'bool': {'should': must_version}}]}},
'min_score': 1}
elastic_search 指令的更多相关文章
- iOS逆向工程之Hopper中的ARM指令
虽然前段时间ARM被日本软银收购了,但是科技是无国界的,所以呢ARM相关知识该学的学.现在看ARM指令集还是倍感亲切的,毕竟大学里开了ARM这门课,并且做了不少的实验,当时自我感觉ARM这门课学的还是 ...
- 步入angularjs directive(指令)--点击按钮加入loading状态
今天我终于鼓起勇气写自己的博客了,激动与害怕并存,希望大家能多多批评指导,如果能够帮助大家,也希望大家点个赞!! 用angularjs 工作也有段时间了,总体感觉最有挑战性的还是指令,因为没有指令的a ...
- Git小技巧 - 指令别名及使用Beyond Compare作为差异比较工具
前言 本文主要写给使用命令行来操作Git的用户,用于提高Git使用的效率.至于使用命令还是GUI(Tortoise Git或VS的Git插件)就不在此讨论了,大家根据自己的的喜好选择就好.我个人是比较 ...
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- [Django]用户权限学习系列之User权限基本操作指令
针对Django 后台自带的用户管理系统,虽说感觉还可以,但是为了方便用户一些操作,特别设计自定义的用户权限管理系统. 在制作权限页面前,首先需要了解权限和用户配置权限的指令,上章讲到权限的添加,删除 ...
- 机器指令翻译成 JavaScript —— No.5 指令变化
上一篇,我们通过内置解释器的方案,解决任意跳转的问题.同时,也提到另一个问题:如果指令发生变化,又该如何应对. 指令自改 如果指令加载到 RAM 中,那就和普通数据一样,也是可以随意修改的.然而,对应 ...
- ARM的栈指令
ARM的指令系统中关于栈指令的内容比较容易引起迷惑,这是因为准确描述一个栈的特点需要两个参数: 栈地址的增长方向:ARM将向高地址增长的栈称为递增栈(Descendent Stack),将向低地址增长 ...
- IL指令详细表
名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...
- Angular学习-指令入门
1.指令的定义 从用户的角度来看,指令就是在应用的模板中使用的自定义HTML标签.指令可以很简单,也可以很复杂.AngularJS的HTML编译器会解析指令,增强模板的功能.也是组件化未来的发展趋势, ...
随机推荐
- LeetCode——remove-duplicates-from-sorted-list-ii
Question Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dist ...
- [PyTorch]PyTorch中模型的参数初始化的几种方法(转)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 本文目录 1. xavier初始化 2. kaiming初始化 3. 实际使用中看到的初始化 3.1 ResNeXt,de ...
- SQLite内存数据库操作类
using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...
- php入门(三)
PHP 面向对象: 在php5中 var就是public的别名. 变量 $this 代表自身的对象. PHP_EOL;为换行符 构造函数+析构函数 <?php class Site { /* 成 ...
- du,df 磁盘管理
du会把指定目录下所有文件.目录.目录下的文件都统计.是建立在文件系统能看到的的确确是有这样一些文件的基础上的.也就是说我们能在文件系统里面看到的文件才会被du统计. df命令可以获取硬盘被占用了多少 ...
- mysql数据库开放远程连接的方法
web与mysql数据库分离开来是一个不错的选择,避免因为大量的数据库查询占用CPU而使web资源不足,同时可以使web服务器的资源尽最大的提供浏览服务,而数据库服务器单独的只处理数据库事物. 适用范 ...
- 搭建selenium + Python环境的总结:
安装Python+Selenium 写博客是一个不错的选择,首先,是担心自己忘掉,其次,可以供大家做一个参考: 其实,这是自己第一次搭建Python环境(之前用了一周的Idle),还是比较容易的吧: ...
- 转载:10+ handy and reusable jQuery code snippets
源地址:http://www.catswhocode.com/blog/10-handy-and-reusable-jquery-code-snippets Smooth scrolling to t ...
- 搞懂分布式技术11:分布式session解决方案与一致性hash
搞懂分布式技术11:分布式session解决方案与一致性hash session一致性架构设计实践 原创: 58沈剑 架构师之路 2017-05-18 一.缘起 什么是session? 服务器为每个用 ...
- VS的 X64下的汇编编译
参考博客 VS编译64位汇编时报错:error C4235: 使用了非标准扩展: 不支持在此结构上使用“_asm”关键字 在用VS2013编译内联汇编时,报如下错误: 错误 5 error ...