十九 es的dsl写法

scala

ES权威指南:
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

所有索引,所有type下的所有数据都搜索出来                                              
指定一个index,搜索其下所有type的数据
同时搜索两个index下的数据
按照通配符去匹配多个索引
搜索一个index下指定的type的数据
可以搜索一个index下多个type的数据
搜索多个index下的多个type的数据
搜索多个index下的多个type的数据
可以代表搜索所有index下的指定type的数据
test开头的所有的索引都搜索出来

GET /_search
GET /afa/_search
GET /.kibana_1,test-index2/_search
GET /*1,*2/_search
GET /test-index1/test-index1/_search
GET /index1/type1,type2/_search
GET /index1,index2/type1,type2/_search
GET /_all/type1,type2/_search
GET /test_*/search

创建索引的mapping
获取索引的mapping
删除索引
添加数据

PUT /demo
	{
            "settings": {
                "number_of_shards": 6,
                "index.refresh_interval": "5s"
            },
            "mappings": {
                "demo": {
                    "properties": {
                        "demoId":{
                            "type":"long"
                        },
                        "contentbody": {
                            "type": "text"

                        },
                        "agentStarttime": {
                            "type": "date"
                             ## ,"format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
                        },
                        "applicationName": {
                            "type": "text",
                            "fields": { ##dsl注释 定义精确查找的内部keyword字段
                                "keyword": {
                                    "type": "keyword"
                                }
                            }
                        }
                    }
                }
            }
        }

GET  /company/_mapping

DELETE  /company

PUT /test-index2/test-index2/1
{
  "testfild" : "111"
}


查询所有的商品
查询名称包含yagao的商品,同时按照价格降序排序
指定要查询出来商品的名称和价格就可以
搜索商品名称包含yagao,而且售价大于25元的商品
full-text search(全文检索)
phrase search(短语搜索)
highlight search(高亮搜索结果)

GET /ecommerce/product/_search
{
  "query": {
   "match_all": {
   				}
    }
}

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "name" : "yagao"
        }
    },
    "sort": [
        { "price": "desc" }
    ]
}

GET /ecommerce/product/_search
{
  "query": { 
  "match_all": {
  	} 
  },
  "_source": ["name", "price"]
}

GET /ecommerce/product/_search
{
    "query" : {
        "bool" : {
            "must" : {
                "match" : {
                    "name" : "yagao" 
                }
            },
            "filter" : {
                "range" : {
                    "price" : { "gt" : 25 } 
                }
            }
        }
    }
}

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "producer" : "yagao producer"
        }
    }
}

GET /ecommerce/product/_search
{
    "query" : {
        "match_phrase" : {
            "producer" : "yagao producer"
        }
    }
}

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "producer" : "producer"
        }
    },
    "highlight": {
        "fields" : {
            "producer" : {}
        }
    }
}


计算每个tag下的商品数量
对名称中包含yagao的商品,计算每个tag下的商品数量
先分组,再算每组的平均值,计算每个tag下的商品的平均价格

GET /ecommerce/product/_search
{
  "aggs": {
    "group_by_tags": {
      "terms": { "field": "tags" }
    }
  }
}

GET /ecommerce/product/_search
{
  "size": 0,
  "query": {
    "match": {
      "name": "yagao"
    }
  },
  "aggs": {
    "all_tags": {
      "terms": {
        "field": "tags"
      }
    }
  }
}







查询方式有二种

query string search

query DSL


查询所有的商品

GET /ecommerce/product/_search
{
  "query": { "match_all": {} }
}

GET /ecommerce/product/_search

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "jiajieshi yagao",
          "desc": "youxiao fangzhu",
          "price": 25,
          "producer": "jiajieshi producer",
          "tags": [
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "gaolujie yagao",
          "desc": "gaoxiao meibai",
          "price": 30,
          "producer": "gaolujie producer",
          "tags": [
            "meibai",
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
        }
      }
    ]
  }
}

took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
hits.total:查询结果的数量,3个document
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据



初步图解搜索原理:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值