您当前的位置: 首页 > 

Dongguo丶

暂无认证

  • 4浏览

    0关注

    472博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

36手动建立和修改mapping以及定制string类型数据是否分词

Dongguo丶 发布时间:2021-11-08 07:23:06 ,浏览量:4

1、如何建立索引

index的值只有三种(no,not_analyzed,analyzed)

https://www.elastic.co/guide/en/elasticsearch/reference/5.6/mapping-index.html

analyzed:添加到索引并分词 not_analyzed:添加到索引不分词 no:不添加到索引

2、修改mapping

只能创建index时手动建立mapping,或者新增field mapping,但是不能update field mapping

由于之前已经自动创建了website的mapping,mapping创建后不能修改已经存在filed mapping

1删除index
DELETE website

响应结果

{
  "acknowledged": true
}
2创建index并手动建立mapping

设置publisher_id:not_analyzed添加到索引但不分词

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": "not_analyzed"
        }
      }
    }
  }
}

响应结果

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "website"
}
3修改其中一个filed的mapping
PUT /website
{
  "mappings": {
    "article": {
      "properties": {
        "author_id": {
          "type": "text"
        }
      }
    }
  }
}

响应结果

{
  "error": {
    "root_cause": [
      {
        "type": "index_already_exists_exception",
        "reason": "index [website/PS4-kwtUQO2JdcreQS9X5Q] already exists",
        "index_uuid": "PS4-kwtUQO2JdcreQS9X5Q",
        "index": "website"
      }
    ],
    "type": "index_already_exists_exception",
    "reason": "index [website/PS4-kwtUQO2JdcreQS9X5Q] already exists",
    "index_uuid": "PS4-kwtUQO2JdcreQS9X5Q",
    "index": "website"
  },
  "status": 400
}
4新增一个filed并设置mapping
PUT /website/_mapping/article
{
  "properties" : {
    "tags" : {
      "type" :    "string"
    }
  }
}

响应结果

{
  "acknowledged": true
}
5再创建一个not_analyzed不分词的filed
PUT /website/_mapping/article
{
  "properties" : {
    "new_field" : {
      "type" :    "string",
      "index":    "not_analyzed"
    }
  }
}

响应结果

{
  "acknowledged": true
}
6查看mapping
GET  /website/_mapping/article

响应结果

{
  "website": {
    "mappings": {
      "article": {
        "properties": {
          "author_id": {
            "type": "long"
          },
          "content": {
            "type": "text"
          },
          "new_field": {
            "type": "keyword"
          },
          "post_date": {
            "type": "date"
          },
          "publisher_id": {
            "type": "text"
          },
          "tags": {
            "type": "text"
          },
          "title": {
            "type": "text",
            "analyzer": "english"
          }
        }
      }
    }
  }
}

“content”: { “type”: “text”} text是会分词的

“new_field”: {“type”: “keyword”} keyword是不会分词的

3、测试mapping

测试不同的filed mapping类型是否分词

1"content": { “type”: “text”}

测试结果为分词

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

响应结果

{
  "tokens": [
    {
      "token": "my",
      "start_offset": 0,
      "end_offset": 2,
      "type": "",
      "position": 0
    },
    {
      "token": "dogs",
      "start_offset": 3,
      "end_offset": 7,
      "type": "",
      "position": 1
    }
  ]
}

2"title": { “type”: “new_field”, “analyzer”: “not_analyzed” }

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

响应结果

{
  "tokens": [
    {
      "token": "my dogs",
      "start_offset": 0,
      "end_offset": 7,
      "type": "word",
      "position": 0
    }
  ]
}
关注
打赏
1638062488
查看更多评论
立即登录/注册

微信扫码登录

0.2239s