Elasticsearch笔记(5)

143 阅读3分钟

这是我参与8月更文挑战的第15天,活动详情查看:8月更文挑战

备注:继Elasticsearch笔记(4)

元数据

  • _index:文档数据所属那个索引,理解为数据库的某张表即可。
  • _type:文档数据属于哪个类型,新版本使用_doc
  • _id:文档数据的唯一标识,类似数据库中某张表的主键。可以自动生成或者手动指定。
  • _score:查询相关度,是否契合用户匹配,分数越高用户的搜索体验越高。
  • _version:版本号。
  • _source:文档数据,json格式。

定制结果集

# GET /{索引名}/_doc/{索引id}?_source=id,name
# GET /{索引名}/_doc/_search?_source=id,name

返回结果定制化了 只返回了id和name
{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "index_jacquesh",
                "_type": "_doc",
                "_id": "1002",
                "_score": 1.0,
                "_source": {
                    "name": "黄某人",
                    "id": 1002
                }
            }
        ]
    }
}

判断文档是否存在

# HEAD /{索引名}/_doc/{索引id}

如果不存在

image-20200904175312823.png

如果存在

image-20200904175403726.png

文档乐观锁控制 if_seq_no与if_primary_term

观察操作
  • 插入新数据

    # POST /index_jacquesh/_doc
    {
        "id": 1010,
        "name": "jacquesh1010",
        "desc": "jacquesh1010!",
        "create_date": "2019-12-24"
    }
    # 此时 _version 为 1
    
  • 修改数据

    #POST    /index_jacquesh/_doc/{_id}/_update
    {
        "doc": {
            "name": "updatejacquesh1010"
        }
    }
    # 此时 _version 为 2
    
  • 模拟两个客户端操作同一个文档数据,_version都携带为一样的数值

    • # 操作1
      #POST    /index_jacquesh/_doc/{_id}/_update?if_seq_no={数值}&if_primary_term={数值}
      {
          "doc": {
              "name": "修改1"
          }
      }
      
      # 操作2
      # POST    /index_jacquesh/_doc/{_id}/_update?if_seq_no={数值}&if_primary_term={数值}
      {
          "doc": {
              "name": "修改2"
          }
      }
      
版本元数据
  • _seq_no:文档版本号,作用同_version(相当于学生编号,每个班级的班主任为学生分配编号,效率要比学校教务处分配来的更加高效,管理起来更方便)
  • _primary_term:文档所在位置(相当于班级)

分词与内置分词器

什么是分词?

把文本转换为一个个的单词,分词称之为analysis。es默认只对英文语句做分词,中文不支持,每个中文字都会被拆分为独立的个体。

  • 英文分词:I am coder
  • 中文分词:我是程序员
POST /_analyze
{
    "analyzer": "standard",
    "text": "text文本"
}
POST /index_jacquesh/_analyze
{
     "analyzer": "standard",
     "field": "name",
     "text": "text文本"
}

es内置分词器

  • standard:默认分词,单词会被拆分,大小会转换为小写。

  • simple:按照非字母分词。大写转为小写。

  • whitespace:按照空格分词。忽略大小写。

  • stop:去除无意义单词,比如the/a/an/is

  • keyword:不做分词。把整个文本作为一个单独的关键词。

    {
        "analyzer": "standard",
        "text": "My name is Peter Parker,I am a Super Hero. I don't like the Criminals."
    }
    

IK中文分词器

Github:github.com/medcl/elast…

  1. zip解压:
 unzip xxx.zip -d ik
  1. 将解压的ik文件夹移动到es的plugins下
mv   ik  elasticsearch/plugins/

测试中文分词效果


# POST /_analyze
{
    "analyzer": "ik_max_word",
    "text": "上下班车流量很大"
}
#返回结果
{
    "tokens": [
        {
            "token": "上下班",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "上下",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "下班",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "班车",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 3
        },
        {
            "token": "车流量",
            "start_offset": 3,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 4
        },
        {
            "token": "车流",
            "start_offset": 3,
            "end_offset": 5,
            "type": "CN_WORD",
            "position": 5
        },
        {
            "token": "流量",
            "start_offset": 4,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 6
        },
        {
            "token": "很大",
            "start_offset": 6,
            "end_offset": 8,
            "type": "CN_WORD",
            "position": 7
        }
    ]
}
# 可以看出经过了ik分词器的处理
自定义中文词库
  1. 在{es}/plugins/ik/config下,创建:
vim custom.dic
  1. 并且添加内容:
黄某人
某人
真帅
  1. 配置自定义扩展词典
vim {es}/plugins/ik/config/IKAnalyzer.cfg.xml

<entry key="ext_dict">custom.dic</entry>
  1. 测试

    {
        "analyzer": "ik_max_word",
        "text": "黄某人真帅"
     }
    #返回结果
    {
        "tokens": [
            {
                "token": "黄某人",
                "start_offset": 0,
                "end_offset": 3,
                "type": "CN_WORD",
                "position": 0
            },
            {
                "token": "某人",
                "start_offset": 1,
                "end_offset": 3,
                "type": "CN_WORD",
                "position": 1
            },
            {
                "token": "真帅",
                "start_offset": 3,
                "end_offset": 5,
                "type": "CN_WORD",
                "position": 2
            }
        ]
    }