美文网首页
Elastic Search 基本操作

Elastic Search 基本操作

作者: 横渡 | 来源:发表于2019-06-13 14:38 被阅读0次

插入

{
    "name": "monkey",
    "country": "China",
    "age": "18",
    "date": "2000-10-01"
}
{
    "name": "杨超越",
    "country": "China",
    "age": "20",
    "date": "1999-10-01"
}

修改

{
    "doc": {
        "name": "齐天大圣"
    }
}
  • 通过脚本修改文档
    修改man类型中id为1的文档
    POST 方法
    URL: http://127.0.0.1:9200/people/man/1/_update
    关键字 script,es支持很多脚本语言,这里使用es内嵌的脚本语言 painless, 在lang关键字中指定脚本语言,在inline关键字中写脚本;ctx._source 指的是要修改文档的上下文。
    json报文:
{
    "script": {
        "lang": "painless",
        "inline": "ctx._source.age = params.age",
        "params" : {
            "age": "500"
        }
    }
}

删除操作

查询

简单查询

GET方法
http://127.0.0.1:9200/book/novel/1
http://{ip}:{port}/<索引>/<类型>/<文档id>

条件查询

  1. 查询所有数据
    POST方法
    http://127.0.0.1:9200/book/_search
    json报文
{
    "query": {
        "match_all": {}
    }
}

我们发现,总共有12条数据,但是hits中默认只返回了10条。

  1. 如何指定返回数据的条数以及从哪里返回呢?from从第几条开始返回,size返回多少条。
{
    "query": {
        "match_all": {}
    },
    "from":1,
    "size":1
}
  1. 指定关键词查询
    我们查询标题里有Elastic的书籍, match_all 改为match, title属性为待查询的关键字。默认的排序是以_score字段倒序排列的
{
    "query":{
        "match": {
            "title": "Elastic Search"
        }
    }
}
  1. 自定义排序
    在sort中自定义排序。
{
    "query":{
        "match_all": {}
    },
    "sort": [
        {
            "publish_date": {
                "order":"desc"
            }
        }
    ],
    "size":15
}

聚合查询

Elastic Search 中的聚合查询和group by类似。聚合查询的关键字是 aggs

  1. 单组聚合
{
    "aggs":{
        "group_by_word_count": { // 这个字段是自己自定义的
            "terms": {
                "field": "word_count" // 按照哪个字段进行聚合查询
            }
        }
    }
}
  1. 多条件聚合
{
    "aggs":{
        "group_by_word_count": {
            "terms": {
                "field": "word_count"
            }
        },
        "group_by_publish_date": {
            "terms": {
                "field": "publish_date"
            }
        }
    }
}
  1. 查询某个字段的统计信息
    stats是一个函数,类似的还有min,max等。
{
    "aggs":{
        "grades_word_count": {
            "stats": {
                "field":"word_count"
            }
        }
    }
}

高级查询

子条件查询

特定字段查询所有特定值

  1. QueryContext
    在查询过程中,除了判断文档是否满足查询条件以外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件匹配的好坏程度。
    常用查询:
  • 全文本查询 针对文本类型数据
    模糊匹配
{
    "query": {
        "match":{
            "title":"Elastic Search入门"
        }
    }
}

查询结果

{
    "took": 19,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 4.3062487,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "2",
                "_score": 4.3062487,
                "_source": {
                    "word_count": 3000,
                    "author": "瓦力",
                    "title": "Elastic Search入门",
                    "publish_date": "2017-03-12"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 1.078649,
                "_source": {
                    "word_count": 3000,
                    "author": "胖子瓦力",
                    "title": "精通Elastic Search",
                    "publish_date": "2017-12-01"
                }
            }
        ]
    }
}

我们分析查询结果可以看出,模糊匹配并不能去匹配整个词语。

习语匹配
习语匹配即完全匹配。
关键字 match_phrase

{
    "query": {
        "match_phrase":{
            "title":"Elastic Search入门"
        }
    }
}

我们更改了关键字后,匹配的结果只剩下一条了:

{
    "took": 22,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 5.384897,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "2",
                "_score": 5.384897,
                "_source": {
                    "word_count": 3000,
                    "author": "瓦力",
                    "title": "Elastic Search入门",
                    "publish_date": "2017-03-12"
                }
            }
        ]
    }
}

多字段匹配查询
关键字 multi_match

{
    "query":{
        "multi_match":{
            "query":"瓦力",
            "fields":["author", "title"]
        }
    }
}
  • 语法查询 根据一定的语法规则进行的查询,支持通配符,范围查询,布尔查询,正则表达式查询。
    经常用在Kibanna中
    关键词 query_string
{
    "query":{
        "query_string":{
            "query":"(瓦力 AND Search) OR Spring Boot"
        }
    }
}

下面是查询结果

{
    "took": 5305,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 2.8850741,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "2",
                "_score": 2.8850741,
                "_source": {
                    "word_count": 3000,
                    "author": "瓦力",
                    "title": "Elastic Search入门",
                    "publish_date": "2017-03-12"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "11",
                "_score": 1.2587122,
                "_source": {
                    "word_count": 28000,
                    "author": "小马哥",
                    "title": "精通Sping Boot编程思想",
                    "publish_date": "2019-01-17"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "12",
                "_score": 1.0346642,
                "_source": {
                    "word_count": 21000,
                    "author": "翟超",
                    "title": "Spring Clound微服务实战",
                    "publish_date": "2018-09-17"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 0.9406741,
                "_source": {
                    "word_count": 15000,
                    "author": "石涛",
                    "title": "Spring Boot2 企业开发实战",
                    "publish_date": "2018-08-01"
                }
            }
        ]
    }
}

语法查询时,指定搜索的列

{
    "query":{
        "query_string":{
            "query":"瓦力 AND Search",
            "fields":["author","title"]
        }
    }
}
  • 字段级别查询 针对结构化数据,如数字、日期等
    针对字段的查询
{
    "query": {
        "term": {
            "word_count": 3000
        }
    }
}

针对author字段查询

{
    "query": {
        "term": {
            "author": "瓦力"
        }
    }
}

针对某个字段范围查询,关键字 range:如word_count大于3000小于15000。

{
    "query":{
        "range":{
            "word_count":{
                "gte": 3000,
                "lte": 15000
            }
        }
    }
}

日期字段的范围查询:

{
    "query":{
        "range":{
            "publish_date":{
                "gte": "2017-01-01",
                "lte": "now"
            }
        }
    }
}

子条件查询

Filter Context
在查询过程中,只判断该文档是否满足条件,只有yes或者No。
而Query Context在查询过程中,还会判断文档匹配的有多好,会有一个score字段来表示。

查询1000字的书籍:

{
    "query":{
        "bool":{
            "filter": {
                "term":{
                    "word_count":1000
                }
            }
        }
    }
}

Filter会做数据过滤,ES会对Filter的结果做缓存,因此相对于query速度会快一些。

复合条件查询

固定分数查询

关键词 constant_score

{
    "query": {
        "constant_score": {
            "filter": {
                "match":{
                    "title":"Elastic"
                }
            }
        }
    }
}

也可以使用boost指定分数

{
    "query": {
        "constant_score": {
            "filter": {
                "match":{
                    "title":"Elastic"
                }
            },
            "boost":2
        }
    }
}

固定分数查询不支持match

布尔查询

表示或的条件
{
    "query":{
        "bool":{
            "should":[{
                "match":{
                    "author":"瓦力"
                }
            },
            {
                "match":{
                    "title":"Elastic"
                }
            }]
        }
    }
}

表达并且的关系

{
    "query":{
        "bool":{
            "must":[{
                "match":{
                    "author":"瓦力"
                }
            },{
                "match":{
                    "title":"Elastic"
                }
            }]
        }
    }
}

添加filter过滤条件

{
    "query":{
        "bool":{
            "must":[{
                "match":{
                    "author":"瓦力"
                }
            },{
                "match":{
                    "title":"Elastic"
                }
            }],
            "filter":[{
                "term":{
                    "word_count":3000
                }
            }]
        }
    }
}

表示非的语义
查询作者不是瓦力的书:

{
    "query":{
        "bool":{
            "must_not":{
                "term":{
                    "author":"瓦力"
                }
            }
        }
    }
}

相关文章

网友评论

      本文标题:Elastic Search 基本操作

      本文链接:https://www.haomeiwen.com/subject/akwjfctx.html