欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

初识搜索引擎_手动建立和修改mapping 及 定制string类型数据是否分词

程序员文章站 2024-03-02 19:44:52
...

1.如何建立索引

PUT website
{
  "mappings": {
    "article":{
      "properties": {
        "author_id":{
          "type": "long"
        },
        "title":{
          "type": "text",
          "analyzer": "english"
        },
        "content":{
          "type": "text"
        },
        "post_date":{
          "type": "date"
        },
        "publisher_id":{
          "type": "text",
          "index": false 
        }
      }
    }
  }
}

运行结果

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "website"
}

2.修改mapping
只能创建index时手动建立mapping,或者新增field mapping,但是不能update field mapping
(1)update field mapping

PUT /website
{
  "mappings": {
    "article": {
      "properties": {
        "author_id": {
          "type": "text"
        }
      }
    }
  }
}

运行结果

{
  "error": {
    "root_cause": [
      {
        "type": "resource_already_exists_exception",
        "reason": "index [website/ReGvtNLiQnSFZ-LGEd_ymA] already exists",
        "index_uuid": "ReGvtNLiQnSFZ-LGEd_ymA",
        "index": "website"
      }
    ],
    "type": "resource_already_exists_exception",
    "reason": "index [website/ReGvtNLiQnSFZ-LGEd_ymA] already exists",
    "index_uuid": "ReGvtNLiQnSFZ-LGEd_ymA",
    "index": "website"
  },
  "status": 400
}

(2)新增field mapping

PUT website/_mapping/article
{
  "properties": {
    "new_field":{
      "type": "keyword",
      "index": false
     
    }
  }
}

运行结果

{
  "acknowledged": true
}

3.测试mapping

GET website/_analyze
{
  "field": "new_field",
  "text": "my-dogs"
}

运行结果

{
  "tokens": [
    {
      "token": "my-dogs",
      "start_offset": 0,
      "end_offset": 7,
      "type": "word",
      "position": 0
    }
  ]
}
GET website/_analyze
{
  "field": "content",
  "text": "my dog"
}

运行结果

{
  "tokens": [
    {
      "token": "my",
      "start_offset": 0,
      "end_offset": 2,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "dog",
      "start_offset": 3,
      "end_offset": 6,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}