ElasticSearch基础篇二
条件查询
GET http://10.192.193.98:9200/shopping/_search?q=title:小米手机
- q:代表查询条件
响应结果
{
"took": 772,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 0.42144203,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "NunonIkBDJDpQI3ST7tm",
"_score": 0.42144203,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "N-nonIkBDJDpQI3SXrt6",
"_score": 0.42144203,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "OOnonIkBDJDpQI3SbLvp",
"_score": 0.42144203,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "OenonIkBDJDpQI3ScbtO",
"_score": 0.42144203,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
🔖 由于在域名后面添加查询条件不是很方便,所以我们一般使用json格式发送查询条件
匹配查询
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"match":{
"category":"小米"
}
}
}
- query:代表查询条件
- match:匹配查询
全量查询
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"match_all":{
}
}
}
- match_all:匹配所有
分页查询
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"match_all":{
}
},
"from":0,
"size":2
}
- from:起始位置 (页码-1)*每页数据条数
- size: 分页大小
字段过滤
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"match_all":{
}
},
"_source":[
"title"
]
}
- _source:设置需要返回的字段
排序
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"match_all":{
}
},
"sort":{
"price":{
"order":"desc"
}
}
}
- sort:设置排序
- order:排序方式
asc
desc
多条件查询
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"category":"小米"
}
},
{
"match":{
"title":"小米手机"
}
}
]
}
}
}
- bool:用于构建复杂查询的查询类型。它允许你组合多个查询条件,bool 查询由三个部分组成
- must:所有的查询条件都必须匹配才会返回文档。
- must_not:所有的查询条件都不能匹配才会返回文档。
- should:至少有一个查询条件匹配,但不是必须的
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"category":"小米"
}
},
{
"match":{
"category":"华为"
}
}
]
}
}
}
范围查询
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"category":"小米"
}
},
{
"match":{
"category":"华为"
}
}
],
"filter":{
"range":{
"price":{
"gt":2598
}
}
}
}
}
}
- filter:一种用于限制搜索结果的查询子句,它用于过滤掉不符合特定条件的文档,而不会对相关性进行计算,过滤器(filter)不会影响搜索结果的相关性得分。相比于查询,过滤器更适合用于筛选文档
- range:设置范围
完全匹配
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"match_phrase":{
"category":"小"
}
}
}
- match_phrase:代表完全匹配,不会对查询条件中的内容进行分词
❗️如果使用match则为全文检索模式,会对查询条件中的内容进行分词,返回包含具有查询条件分词后结果文章来源:https://www.toymoban.com/news/detail-621106.html
高亮查询
GET http://10.192.193.98:9200/shopping/_search
{
"query":{
"match":{
"category":"小米"
}
},
"highlight":{
"fields":{
"category":{}
}
}
}
<em>小</em><em>米</em>
- highlight:设置高亮字段
聚合查询
GET http://10.192.193.98:9200/shopping/_search
{
"aggs":{
"price_group":{//为操作起名price_group
"terms":{
"field":"price"
}
},
"price_avg":{
"avg":{
"field":"price"
}
}
},
"size":0
}
- aggs:聚合操作
- terms:分组
- avg:求平局值
响应结果文章来源地址https://www.toymoban.com/news/detail-621106.html
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 3999.0,
"doc_count": 4
},
{
"key": 1999.0,
"doc_count": 1
},
{
"key": 2599.0,
"doc_count": 1
},
{
"key": 2999.0,
"doc_count": 1
}
]
},
"price_avg": {
"value": 3370.4285714285716
}
}
映射
创建映射
PUT http://10.192.193.98:9200/user/_mapping
{
"properties":{
"name":{
"type":"text",
"index":true
},
"sex":{
"type":"keyword",
"index":true
},
"tel":{
"type":"keyword",
"index":false
}
}
}
- 映射类似于mysql中的表结构约束
- text:文本类型,可以进行全文检索,对查询条件关键字分词匹配
- keyword:关键字,作为一个整体,不会分词
- index:是否可被索引,如果为false则该字段无法被查询
查询映射
GET http://10.192.193.98:9200/user/_mapping
到了这里,关于ElasticSearch基础篇-条件查询与映射的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!