对搜索文本不分词,直接拿去倒排索引中匹配,你输入的是什么,就去匹配什么。
GET /forum/article/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"userID" : 1
}
}
}
}
}
搜索发帖日期为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"
}
}
}
}
}
}
}
过滤访问在30到60之间的数据
GET /forum/article/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"view_cnt": {
"gt": 30,
"lt": 60
}
}
}
}
}
}
搜索匹配java或者elasticsearch的标题
GET /forum/article/_search
{
"query": {
"match": {
"title": "java elasticsearch"
}
}
}
标题中包含java和elasticsearch
GET /forum/article/_search
{
"query": {
"match": {
"title": {
"query": "java elasticsearch",
"operator": "and"
}
}
}
}
至少匹配其中75%的关键字才会返回结果
GET /forum/article/_search
{
"query": {
"match": {
"title": {
"query": "java elasticsearch spark hadoop",
"minimum_should_match": "75%"
}
}
}
}
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" }}
]
}
}
}
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
}
}
}
most-fields策略,主要是说尽可能返回更多field匹配到某个关键词的doc,优先返回回来
GET /forum/article/_search
{
"query": {
"multi_match": {
"query": "learning courses",
"type": "most_fields",
"fields": [ "sub_title", "sub_title.std" ]
}
}
}
短语匹配与近似匹配
#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
}
}
}
}
GET my_index/my_type/_search
{
"query": {
"prefix": {
"title": {
"value": "C3"
}
}
}
}
GET my_index/my_type/_search
{
"query": {
"wildcard": {
"title": {
"value": "C?K*5"
}
}
}
}
#?:任意字符
#*:0个或任意多个字符
GET /my_index/my_type/_search
{
"query": {
"regexp": {
"title": "C[0-9].+"
}
}
}
#.:一个字符
#+:前面的正则表达式可以出现一次或多次
也可添加slop参数,近似搜索。
GET /my_index/my_type/_search
{
"query": {
"match_phrase_prefix": {
"title": "hello d"
}
}
}
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
}
}
}
}