Elasticsearch 入门到精通-Elasticsearch数据查询

 创建索引

PUT blog
{
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "text"
      },
      "type": {
        "type": "keyword"
      },
      "score": {
        "type": "double"
      }
    }
  }
}

写入测试数据

POST blog/_bulk
{"index":{"_index":"blog","_id":"1"}}
{"id":"1","name":"什么样的人最容易犯罪?","content":"脸上有马赛克的人","type":"脑筋急转弯","score":96}
{"index":{"_index":"blog","_id":"2"}}
{"id":"2","name":"生蚝熟了之后还是生蚝吗?","content":"还是生蚝","type":"科普","score":96}
{"index":{"_index":"blog","_id":"3"}}
{"id":"3","name":"生蚝熟了之后还是生蚝吗?","content":"还是生蚝","type":"科普","score":96}
{"index":{"_index":"blog","_id":"4"}}
{"id":"4","name":"如果猪肾虚的话,吃猪腰子还补吗?","content":"会更虚吧","type":"科普","score":96}
{"index":{"_index":"blog","_id":"5"}}
{"id":"5","name":"如果猪肾虚的话,吃猪腰子还补吗?","content":"第七次","type":"科普","score":96}

一、精准查询term


term是代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇

1、term单值


字段只有一个值时候,用term关键词查询

查询 id 值为 1 的记录

GET blog/_search
{
  "query": {
    "term": {
      "id": {
        "value": "1"
      }
    }
  }
}

进一步优化查询,因为是精准查询,不需要查询进行评分计算,只希望对文档进行包括或排除的计算,所以我们会使用 constant_score 查询以非评分模式来执行 term 查询并以一作为统一评分。推荐如下查询

GET blog/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "id": "1"
        }
      },
      "boost": 1.2
    }
  }
}

2、terms多值

字段有一多个值时候,用terms关键词查询,后跟数组

GET blog/_search
{
  "query": {
    "terms": {
      "id": [
        "1",
        "2"
      ]
    }
  }
}

constant_score 以非评分模式查询,推荐如下查询

GET blog/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "id": [
            "1",
            "4"
          ]
        }
      },
      "boost": 1.2
    }
  }
}

3、term多个字段

GET blog/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "id": {
              "value": "1"
            }
          }
        },
        {
          "term": {
            "type": {
              "value": "科普"
            }
          }
        }
      ]
    }
  }
}

 二、匹配查询match


match和term的区别是,match查询的时候,elasticsearch会根据你给定的字段提供合适的分析器,而term查询不会有分析器分析的过程,match查询相当于模糊匹配,只包含其中一部分关键词就行

1、match


进行full text search或者exact value(非string字段或not_analyzed的字段),进行匹配

GET blog/_search
{
  "query": {
    "term": {
      "type": {
        "value": "科普"
      }
    }
  },
  "sort": [
    {
      "score": {
        "order": "desc"
      }
    }
  ]
}


2、match_all


{ "match_all": {}} 匹配所有的, 当不给查询条件时,默认全查。

GET blog/_search
{
  "query": {
    "match_all": {}
  }
}

3、multi_match


同时对查询的关键词,多个字段同时进行匹配,即多个字段是AND的关系

GET blog/_search
{
  "query": {
    "multi_match": {
      "query": "科技",
      "fields": ["name","content"]
    }
  }
}

 同时field还支持更为丰富的查询


4、match_phrase


 match_phrase查询分析文本,并从分析文本中创建短语查询。
类似 match 查询, match_phrase 查询首先将查询字符串解析成一个词项列表,然后对这些词项进行搜索,但只保留那些包含 全部 搜索词项,且 位置与搜索词项相同的文档

如下,查询 quick brown、quick brown fox、 brown fox可以查询到,quick fox 查询不到

{  
      "query": {  
          "match_phrase": {  
              "title": "quick brown fox"  
          }  
      }  
}  
 如下, 查询 a,b,啊和b之间隔3个字符可以查询到,隔不是3个查询不到 

{
    "query":{
        "match_phrase" :{
            "query":"a,b",
            "slop":3
        }
    }
}


三、bool查询


bool查询包含四种操作符,分别是must,should,must_not,query。它们均是一种数组,数组里面是对应的判断条件

must: 必须匹配,与and等价。贡献算分

must_not:必须不匹配,与not等价,常过滤子句用,但不贡献算分

should: 选择性匹配,至少满足一条,与 OR 等价。贡献算分

filter: 过滤子句,必须匹配,但不贡献算分

GET blog/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "id": {
              "value": "1"
            }
          }
        },
        {
          "term": {
            "type": {
              "value": "科普"
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "name": "生蚝"
          }
        }
      ]
    }
  }
}

 四、filter查询


过滤器,会查询对结果进行缓存,不会计算相关度,避免计算分值,执行速度非常快。

如下, 查询出 type 为  科普 类型的数据

GET blog/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "type": "科普"
          }
        }
      ]
    }
  }
}

filter也常和range范围查询一起结合使用,range范围可供组合的选项

gt : 大于

lt : 小于

gte : 大于等于

lte :小于等于

如下,查询merchant_id值为2501下的交易数据

GET blog/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "type": "科普"
          }
        },
        {
          "range": {
            "score": {
              "gte": 10,
              "lte": 120
            }
          }
        }
      ]
    }
  }
}

如下查询,must下匹配,filter进行过滤,range定义范围

GET blog/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "生蚝"
          }
        }
      ]
    }
  },
  "post_filter": {
    "bool": {
      "filter": [
        {
          "term": {
            "type": "科普"
          }
        },
        {
          "range": {
            "score": {
              "gte": 10,
              "lte": 120
            }
          }
        }
      ]
    }
  }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值