博客
关于我
关于文档的基本操作
阅读量:618 次
发布时间:2019-03-13

本文共 2998 字,大约阅读时间需要 9 分钟。

Elasticsearch 文档操作指南

文档操作基础

数据添加

在Elasticsearch中添加文档可以通过PUT请求实现。以下是添加用户数据的示例:

PUT /latte2/user/1
{
"name": "latte",
"age": 23,
"desc": "干",
"tags": ["技术","海贼王"]
}

同样,可以添加其他用户数据:

PUT /latte2/user/2
{
"name": "张三",
"age": 28,
"desc": "法外狂徒",
"tags": ["旅游", "渣男", "交友"]
}

获取数据

使用GET请求可以获取文档数据。以下是获取单个用户数据的示例:

GET latte2/user/1

如果需要搜索特定关键字,可以使用q参数:

GET latte2/user/_search?q=name:latte

文档更新

PUT请求可以用于文档更新,但需要注意以下几点:

  • 覆盖原有字段:如果字段没有被更新,原有字段会被丢弃。
  • 推荐使用POST:对于更新操作,建议使用POST请求,尤其是当只需要更新部分字段时。
  • 以下是使用POST更新文档的示例:

    POST latte2/user/1/_update
    {
    "name": "new_name"
    }

    数据搜索

    简单搜索

    使用GET请求加上_search路径即可实现简单搜索:

    GET latte2/user/_search
    {
    "query": {
    "match": {
    "name": "latte2"
    }
    }
    }

    复杂操作搜索

    支持多种操作,如排序、分页、高亮查询等。以下是分页和排序的示例:

    GET latte2/user/_search
    {
    "query": {
    "match": {
    "name": "latte2"
    }
    },
    "sort": [
    {
    "age": "asc"
    }
    ],
    "from": 0,
    "size": 2
    }

    高级搜索

    布尔查询

    • must(与,and):所有条件都必须满足
    • should(或,or):至少满足一个条件
    • must_not(非,not):所有条件都不满足

    以下是多条件查询的示例:

    GET latte2/user/_search
    {
    "query": {
    "bool": {
    "must": [
    {
    "match": {
    "name": "latte2"
    }
    },
    {
    "match": {
    "age": 23
    }
    }
    ]
    }
    }
    }

    过滤条件

    可以通过filter子查询进行过滤。以下是高于某个年龄的过滤查询:

    GET latte2/user/_search
    {
    "query": {
    "bool": {
    "must": [
    {
    "match": {
    "name": "latte2"
    }
    }
    ],
    "filter": [
    {
    "range": {
    "age": {
    "gt": 27
    }
    }
    }
    ]
    }
    }
    }

    分词与精准匹配

    • **text**类型:支持分词,会自动解析文本
    • **keyword**类型:不支持分词,精准匹配

    以下是创建映射关系的示例:

    PUT /latte3/mappings
    {
    "properties": {
    "name": {
    "type": "text"
    },
    "desc": {
    "type": "keyword"
    }
    }
    }

    多值匹配

    可以通过term子查询进行多值匹配:

    GET latte3/_search
    {
    "query": {
    "bool": {
    "should": [
    {
    "term": {
    "t1": "22"
    }
    },
    {
    "term": {
    "t1": "33"
    }
    }
    ]
    }
    }
    }

    高亮查询

    可以通过highlight参数实现高亮显示:

    GET latte2/user/_search
    {
    "query": {
    "match": {
    "name": "latte2"
    }
    },
    "highlight": {
    "pre_tags": "

    ",

    "post_tags": "

    ",
    "fields": {
    "name": {}
    }
    }
    }

    Elasticsearch 操作注意事项

    • 分词器text类型会进行分词处理,而keyword类型则不会
    • 倒排索引term查询直接利用倒排索引进行精准匹配
    • 数据类型:确保查询字段与数据类型匹配
    • 默认映射:Elasticsearch会自动为字段创建映射,建议根据需求进行定制

    通过以上操作,您可以对Elasticsearch进行基本的文档管理和搜索操作。

    转载地址:http://yzkaz.baihongyu.com/

    你可能感兴趣的文章
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>