1.query sting search
GET /ecummerce/product/_search
1.took :耗费了几毫秒
2.timed_out 是否超时 这里没有
3._shards :数据拆成了一份,所以对于搜索请求,会打到所有primary shard(或者是它的某个replica shard 也可以)
4.hits.total 查询结果的数量 3个document
5.hits.max_score :score 的含义 就是document对于一个search的相关的匹配分数,越相关,就越匹配 分数越高。
6.hits.hits:包含了匹配搜索的document的详细的数据
{
"took" : 438,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "jiajieshi fangzhu",
"price" : 25,
"product" : "jiajieshi producter",
"tags" : [
"fengzhu"
]
}
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhizu",
"price" : 40,
"product" : "zhonghua producter",
"tags" : [
"qingxin"
]
}
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"product" : "gaolujie producter",
"tags" : [
"fengzhu",
"meibai"
]
}
}
]
}
}
query string search r 由来search参数都是以http请求的query string来附带的
2.查询商品名称中包含yagao的商品,而且按照售价降序排序:GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhizu",
"price" : 40,
"product" : "zhonghua producter",
"tags" : [
"qingxin"
]
},
"sort" : [
40
]
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"product" : "gaolujie producter",
"tags" : [
"fengzhu",
"meibai"
]
},
"sort" : [
30
]
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "jiajieshi fangzhu",
"price" : 25,
"product" : "jiajieshi producter",
"tags" : [
"fengzhu"
]
},
"sort" : [
25
]
}
]
}
}
query DSL
http equest body :请求体 可以用json的格式来构建查询语法,比较方便,可以构建各种复杂的语法。
查询所有的商品:
GET /ecummerce/product/_search
{
"query":{
"match_all":{}
}
}
GET /ecummerce/product/_search
{
"query":{
"match_all":{}
}
}
G
查询商品名称中包含yagao的商品,而且按照售价降序排序ET /ecummerce/product/_search
{
"query":{
"match":{
"name":"yagao"
}
},
"sort":[
{"price":"desc"}
]
}
3.分页查询商品,总共3条商品,假设每页就显示1条商品,现在显示第2页,所以就查询出来第2 个商品
from:从第几个商品开始
size: 每页大小
GET /ecummerce/product/_search
{
"query":{
"match_all":{}
},
"from":1,
"size":1
}
查询结果:
{
"took" : 32,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhizu",
"price" : 40,
"product" : "zhonghua producter",
"tags" : [
"qingxin"
]
}
}
]
}
}
4.指定要查询出来商品的名称和价格就可以
GET /ecummerce/product/_search
{
"query":{
"match_all":{}
},
"_source":["name","price"]
}
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"price" : 25,
"name" : "jiajieshi yagao"
}
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"price" : 40,
"name" : "zhonghua yagao"
}
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"price" : 30,
"name" : "gaolujie yagao"
}
}
]
}
}
3.query filter
查询商品名称包含yagao,而且售价大干25元的商品
GET /ecummerce/product/_search
{
"query":{
"bool": {
"must": {
"match": {
"name": "yagao"
}
},
"filter":{
"range":{
"price":{
"gt":25
}
}
}
}
}
}
运行结果:
{
"took" : 35,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.13353139,
"hits" : [
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "3",
"_score" : 0.13353139,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhizu",
"price" : 40,
"product" : "zhonghua producter",
"tags" : [
"qingxin"
]
}
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "1",
"_score" : 0.13353139,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"product" : "gaolujie producter",
"tags" : [
"fengzhu",
"meibai"
]
}
}
]
}
}
4.ful-text search (全文检索)
GET /ecummerce/product/_search
{
"query":{
"match": {
"product": "yagao producter"
}
}
}
producter字段会先被拆解,建立倒排索引
运行结果:
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 0.110377684,
"hits" : [
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "2",
"_score" : 0.110377684,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "jiajieshi fangzhu",
"price" : 25,
"product" : "jiajieshi producter",
"tags" : [
"fengzhu"
]
}
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "3",
"_score" : 0.110377684,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhizu",
"price" : 40,
"product" : "zhonghua producter",
"tags" : [
"qingxin"
]
}
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "1",
"_score" : 0.110377684,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"product" : "gaolujie producter",
"tags" : [
"fengzhu",
"meibai"
]
}
},
{
"_index" : "ecummerce",
"_type" : "product",
"_id" : "5",
"_score" : 0.09271725,
"_source" : {
"name" : "special yagao",
"desc" : "special meibai",
"price" : 50,
"product" : "special gaolujie producter",
"tags" : [
"meibai"
]
}
}
]
}
}
5.highight search(高亮搜索结果)
6.聚合
1.第一个分析需求,计算每个tag下的商品数量
GET /ecummerce/product/_search
{
"aggs":{
"group_by_tages":{
"terms": {
"field": "tags"
}
}
}
}
PUT /ecummerce/_mapping/product
{
"properties":{
"tags":{
"type":"text",
"fielddata":true
}
}
}
第二个 聚合分析的需求:对名称中包含yagao的商品,计算每个tag下的商品数量
GET /ecummerce/product/_search
{
"query":{
"match": {
"name": "yagao"
}
},
"aggs":{
"group_by_tages":{
"terms": {
"field": "tags"
}
}
}
}
第三个聚合分析的需求,先分组,再算每组的平均值,计算每个tag下的商品的平均价格。
GET /ecummerce/product/_search
{
"size":0,
"aggs":{
"group_by_tages":{
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}