es常用命令
文章目录
- es常用命令
- 1.搜索
- 1.1term filter/query搜索
- 1.2基于bool组合多个filter搜索
- 1.3range范围过滤
- 1.4全文检索
- 1.4.1或
- 1.4.2且
- 1.4.3百分比控制搜素精度
- 1.5dis_max查询
- 1.6tie_breaker 优化dis_max查询
- 1.7mult_match结合most field查询
- 1.8phrase matching与proximity match匹配
- 1.9前缀搜索
- 1.10通配符搜索
- 1.11正则搜索
- 1.12match_phrase_predix搜索
- 1.13fuzzy模糊搜索
1.搜索
1.1term filter/query搜索
对搜索文本不分词,直接拿去倒排索引中匹配,你输入的是什么,就去匹配什么。
GET /forum/article/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"userID" : 1
}
}
}
}
}
1.2基于bool组合多个filter搜索
搜索发帖日期为2017-01-01,或者帖子ID为XHDK-A-1293-#fJ3的帖子,同时要求帖子的发帖日期绝对不为2017-01-02
GET /forum/article/_search
{
"query": {
"constant_score": {
"filter": {
"bool": {
"should": [
{"term": { "postDate": "2017-01-01" }},
{"term": {"articleID": "XHDK-A-1293-#fJ3"}}
],
"must_not": {
"term": {
"postDate": "2017-01-02"
}
}
}
}
}
}
}
1.3range范围过滤
过滤访问在30到60之间的数据
GET /forum/article/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"view_cnt": {
"gt": 30,
"lt": 60
}
}
}
}
}
}
1.4全文检索
1.4.1或
搜索匹配java或者elasticsearch的标题
GET /forum/article/_search
{
"query": {
"match": {
"title": "java elasticsearch"
}
}
}
1.4.2且
标题中包含java和elasticsearch
GET /forum/article/_search
{
"query": {
"match": {
"title": {
"query": "java elasticsearch",
"operator": "and"
}
}
}
}
1.4.3百分比控制搜素精度
至少匹配其中75%的关键字才会返回结果
GET /forum/article/_search
{
"query": {
"match": {
"title": {
"query": "java elasticsearch spark hadoop",
"minimum_should_match": "75%"
}
}
}
}
1.5dis_max查询
dis_max是best fields查询策略,即某一个field中匹配到了尽可能多的关键词配排在前面。dis_max只取某一个query最大的分数,完全不考虑其他query的分数.
GET /forum/article/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "java solution" }},
{ "match": { "content": "java solution" }}
]
}
}
}
1.6tie_breaker 优化dis_max查询
tie_breaker参数的意义,在于说,将其他query的分数,乘以tie_breaker,然后综合与最高分数的那个query的分数,综合在一起进行计算。
除了取最高分以外,还会考虑其他的query的分数
GET /forum/article/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "java beginner" }},
{ "match": { "body": "java beginner" }}
],
"tie_breaker": 0.3
}
}
}
1.7mult_match结合most field查询
most-fields策略,主要是说尽可能返回更多field匹配到某个关键词的doc,优先返回回来
GET /forum/article/_search
{
"query": {
"multi_match": {
"query": "learning courses",
"type": "most_fields",
"fields": [ "sub_title", "sub_title.std" ]
}
}
}
1.8phrase matching与proximity match匹配
短语匹配与近似匹配
#match_phrase语法
#只有包含java spark这个短语的doc才返回
GET /forum/article/_search
{
"query": {
"match_phrase": {
"content": "java spark"
}
}
}
#近似匹配
#加入slop参数,搜索文本中的几个term,要经过几次移动才能与一个document匹配,这个移动的次数,就是slop
GET /forum/article/_search
{
"query": {
"match_phrase": {
"title": {
"query": "java spark",
"slop": 1
}
}
}
}
1.9前缀搜索
GET my_index/my_type/_search
{
"query": {
"prefix": {
"title": {
"value": "C3"
}
}
}
}
1.10通配符搜索
GET my_index/my_type/_search
{
"query": {
"wildcard": {
"title": {
"value": "C?K*5"
}
}
}
}
#?:任意字符
#*:0个或任意多个字符
1.11正则搜索
GET /my_index/my_type/_search
{
"query": {
"regexp": {
"title": "C[0-9].+"
}
}
}
#.:一个字符
#+:前面的正则表达式可以出现一次或多次
1.12match_phrase_predix搜索
也可添加slop参数,近似搜索。
GET /my_index/my_type/_search
{
"query": {
"match_phrase_prefix": {
"title": "hello d"
}
}
}
1.13fuzzy模糊搜索
fuzziness,搜索文本最多可以纠正几个字母去跟数据进行匹配,默认如果不设置,就是2
GET /my_index/my_type/_search
{
"query": {
"fuzzy": {
"text": {
"value": "surprize",
"fuzziness": 2
}
}
}
}efix": {
“title”: “hello d”
}
}
}### 1.13fuzzy模糊搜索
> fuzziness,搜索文本最多可以纠正几个字母去跟数据进行匹配,默认如果不设置,就是2GET /my_index/my_type/_search
{
“query”: {
“fuzzy”: {
“text”: {
“value”: “surprize”,
“fuzziness”: 2
}
}
}
}