elasticsearch查询

Posted lilied

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch查询相关的知识,希望对你有一定的参考价值。


#注意 以下字段 均默认text类型   
# match查询
GET article2/info/_search
{
  "query":{
    "match":{
      "title":"mysql-MMM"
    }
  },
  "from": 0,
  "size": 3
}


# term查询 (属于精确查找) 
GET article2/info/_search
{
  "query":{
    "term": {
      "title":"MySQL-MMM"
    }
  }
}


# terms查询 #满足一个词就能查询出来
GET article2/info/_search
{
  "query":{
    "terms":{
      "title":["linux", "振兴"]
    }
  }
}


# match_all查询
GET article2/info/_search
{
  "query":{
    "match_all":{}
  },
  "from": 1,
  "size": 2
}



# match_phrase查询 #可以设置关键词之间距离大于几  
GET article2/info/_search
{
  "query":{
    "match_phrase": {
      "title": {
        "query":"mysql集群",
        "slop": 2
      }
    }
  }
}


# multi_match查询
GET article2/info/_search
{
  "query":{
    "multi_match": {
      "query": "mysql",
      "fields": ["title^3","content"]
    }
  }
}


# 指定返回字段
GET article2/info/_search
{
  "stored_fields": ["content"],
  "query":{
    "match": {
      "title": "mysql"
    }
  }
}


# sort排序
GET article2/info/_search
{
  "query":{
    "match": {
      "title": "mysql"
    }
  },
  "sort": [{
      "_score": {
        "order": "desc"
      }
  }]
}




# 范围查询 
GET article2/info/_search
{
  "query":{
    "range":{
      "publish_date": {
          "gte":"2018/05/18",
          "lte":"now",
          "boost":1.0
      }
    }
  }
}


# wildcard查询
GET article2/info/_search
{
  "query":{
    "wildcard": {
      "title": {
        "value": "pyth*n",
        "boost": 2
      }
    }
  }
}


# bool查询  filtered被替换
# bool包括must,should,must_not,filter 4种
# bool:{
#   "must":[],  
#   "should":[],
#   "must_not":[],
#   "filter":[],
# }


# 添加模拟数据
POST test2/info/_bulk
{"index":{"_id":1}}
{"salary":100, "title":"Python"}
{"index":{"_id":2}}
{"salary":80, "title":"mysql"}
{"index":{"_id":3}}
{"salary":40, "title":"linux"}
{"index":{"_id":4}}
{"salary":90, "title":"java"}
{"index":{"_id":5}}
{"salary":50, "title":"ruby"}

# 最简单的filter查询 不参与打分 
# select * from info where salary=100;
GET test2/info/_search
{ 
  "query":{
    "bool":{
      "must":[
        {
          "match_all":{}
        }
      ],
      "filter":[
        {
          "term":{
            "salary":100
          }
        }  
      ]
    }
  }
}

# select * from info where title=Python;
GET test2/info/_search
{ 
  "query":{
    "bool":{
      "must":[
        {
          "match_all":{}
        }
      ],
      "filter":[
        {
          "term":{
            "title":"python"
          }
        }  
      ]
    }
  }
}
# 用term无结果,因为大写分词时会成小写,精确查找(filter)是找不到
# 要么换成match, 要么改成小写python

# 查看分词器
GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "Python网络"
}
# 论证 大写分词时会成小写






# bool的组合过滤查询
# select * from info where (salary=70 OR title=Python) AND (salary!=30)
GET test2/info/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "term":{
            "salary":70
          }
        },
        {
          "term":{
            "title":"python"
          }
        }
      ],
      "must_not":[
        {
          "term":{
            "salary":30
          }
        }  
      ]
    }
  }
}


# 为空和不为空查询 
# 模拟数据 
POST test2/info2/_bulk
{"index":{"_id":1}}
{"tags":"lily"}
{"index":{"_id":2}}
{"tags":"jack"}
{"index":{"_id":3}}
{"tags":null}
{"index":{"_id":4}}
{"tags":"lucy"}
{"index":{"_id":5}}
{"tags":null}
{"index":{"_id":6}}
{"tags":"queen"}


# 查询不为空的tags 
GET test2/info2/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "exists":{
            "field":"tags"
          }
        }
      ]
    }
  } 
}


# 查询空tags(不查询存在的tags ) 
GET test2/info2/_search
{
  "query":{
    "bool":{
      "must_not": [
        {
          "exists":{
            "field":"tags"
          }
        }
      ]
    }
  } 
}








以上是关于elasticsearch查询的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch笔记九之优化

ElasticSearch--请求体查询

Elasticsearch--搜索

小烨收藏ElasticSearch权威指南-请求体查询

Elasticsearch:运用 Elasticsearch 查找类似文档:more_like_this

Elasticsearch:运用 Elasticsearch 查找类似文档:more_like_this