es搜索命令

es常用命令

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,搜索文本最多可以纠正几个字母去跟数据进行匹配,默认如果不设置,就是2

GET /my_index/my_type/_search
{
“query”: {
“fuzzy”: {
“text”: {
“value”: “surprize”,
“fuzziness”: 2
}
}
}
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值