查看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": "[email protected]",
"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