Elasticsearch相关CRUD的DSL语句

查看ES种的所有索引

GET _cat/indices
根据索引查询,默认查询前10条,  
ES默认查询最大数据为10000条,涉及分布式数据库的深度分页问题
如果分页查询10000条数据,需要每个分片查询,然后所有分片数据累加,3个分片就是30000条数据,
然后协调节点才会重新排序找到前10000条,所以就会查询大量数据
GET accounts/_search
查询ID=2的数据
GET accounts/_doc/2
返回数据字段过滤

一:创建索引时,指定映射关系,过滤返回的数据字段,但mapping不可变,一般不考虑这种方式
includes:查询结果只返回当前配置的字段
excludes:查询结果不返回配置的字段
PUT test_index
{
  "mappings": {
    "_source": {
      "includes":["id"],
      "excludes":["name"]
    }
  }
}

二:在查询时过滤:
GET test_index/_search
{
  "_source": {
    "includes": "name",
    "excludes": "id"
  }
}
也可以直接写返回的字段

GET test_index/_search
{
  "_source": ["name", "id"]
}
query查询中 match和match_phrase的区别, 语句查询时match_phrase有着更高的准确度
一:match查询
GET test_index/_search
{
  "query": {
    "match": {
      "name": "小米"
    }
  }
}

match查询结果:
"hits" : [	
  {
    "_index" : "test_index",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 1.5408251,
    "_source" : {
      "id" : 2,
      "name" : "小米手机"
    }
  },
  {
    "_index" : "test_index",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 0.42221838,
    "_source" : {
      "id" : 3,
      "name" : "小米"
    }
  }
]

二:match_phrase查询
GET test_index/_search
{
  "query": {
    "match_phrase": {
      "name": "小米手机"
    }
  }
}
Result:
match_phrase查询结果
"hits" : [
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.5408251,
        "_source" : {
          "id" : 2,
          "name" : "小米手机"
        }
      }
    ]
新增一条ID=2数据,如果已有ID=2的数据则会报错,没有才会新增
也可以用 [POST] API
PUT accounts/_create/2
{
  "account_number": 2,
  "balance": 100,
  "firstname": "li",
  "lastname": "si",
  "desc":"哈哈"

}
新增一条数据,无论是否已有,直接覆盖
也可以用【POST】API
PUT accounts/_doc/2
{
  "account_number": 2,
  "balance": 100,
  "firstname": "li",
  "lastname": "si",
  "desc":"哈哈",
  "age": 32,
  "gender": "M",
  "address": "880 Holmes Lane",
  "employer": "Pyrami",
  "email": "amberduke@pyrami.com",
  "city": "Brogan",
  "state": "IL"
}
POST 和 PUT的区别:POST可以不指定文档ID,回自动生成类似UUID的ID

一:POST 自动生成ID
POST test_index/_doc
{
  "id":3,
  "desc":"aaa"
}

二:  POST修改指定字段,原数据没有的字段会新增, PUT无此API修改方法
POST test_index/_update/3
{
  "doc": {
    "id": 3,
    "name": "china"
  }
}

ES查询score评分用的算法TF-DF,  Okapi-BM25

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值