1. 程式人生 > >ElasticSearch33:初識搜尋引擎_手動建立和修改mapping以及定製sting型別資料是否分詞

ElasticSearch33:初識搜尋引擎_手動建立和修改mapping以及定製sting型別資料是否分詞

1.如何建立索引
string型別的欄位,是否進行分詞設定:
  • 需要進行分詞 analyzed
  • 不進行分詞 not_analyzed
  • 不能進行搜尋 no

2.新增或修改mapping

只能在建立index時手動建立mapping,或者新增field mapping,但是不能update field mapping

1)新建mapping

例子:

PUT /website
{
  "mappings": {
    "article":{
      "properties": {
        "content":{
          "type": "text",
          "analyzer":"english"
        },
        "post_date":{
          "type":"date"
        },
        "title":{
          "type":"text"
        },
        "publisher_id":{
          "type": "text",
          "index":"not_analyzed"
        }
      }
    }
  }
}
執行結果


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

2)如果對該index的type中修改已有欄位的mapping,那麼就會報錯

例子:

PUT /website
{
  "mappings": {
    "article":{
      "properties": {
        "publisher_id":{
          "type": "text",
          "index":"not_analyzed"
        }
      }
    }
  }
}

執行結果
{
  "error": {
    "root_cause": [
      {
        "type": "index_already_exists_exception",
        "reason": "index [website/nJoZIdmbSk-llLCT-bLdDg] already exists",
        "index_uuid": "nJoZIdmbSk-llLCT-bLdDg",
        "index": "website"
      }
    ],
    "type": "index_already_exists_exception",
    "reason": "index [website/nJoZIdmbSk-llLCT-bLdDg] already exists",
    "index_uuid": "nJoZIdmbSk-llLCT-bLdDg",
    "index": "website"
  },
  "status": 400
}

3)新增一個type中的欄位

例子:

PUT /website/_mapping/article
{
  "properties": {
    "tags":{
      "type":"text"
    }
  }
}

執行結果:
{
  "acknowledged": true
}


2)修改mapping,修改的時候只能新增新的field,不能修改原來的field
如果我們想要修改已有的field的mapping屬性,那麼就會報錯


3.測試mapping
1)測試分詞的型別

GET /website/_analyze
{
    "field":"title",
    "text":"a dog"
}

因為使用的是standard analyzer
執行結果:

{
  "tokens": [
    {
      "token": "a",
      "start_offset": 0,
      "end_offset": 1,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "dog",
      "start_offset": 2,
      "end_offset": 5,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}
2)測試不進行分詞的型別
新增一個欄位,定義為string型別,並設定成not_analyzed,這樣就不會進行分詞
PUT /website/_mapping/article
{
  "properties": {
    "new_field":{
      "type":"string",
      "index":"not_analyzed"
    }
  }
}

檢視website/article的mapping,可以看到new_field的type是keyword,表示不進行分詞操作。
{
  "website": {
    "mappings": {
      "article": {
        "properties": {
          "content": {
            "type": "text",
            "analyzer": "english"
          },
          "new_field": {
            "type": "keyword"
          },
          "post_date": {
            "type": "date"
          },
          "publisher_id": {
            "type": "text"
          },
          "tags": {
            "type": "text"
          },
          "title": {
            "type": "text"
          }
        }
      }
    }
  }
}

測試該欄位的分詞:
GET /website/_analyze
{
  "field": "new_field",
  "text": "a dog"
}
執行結果:因為new_field不能進行分詞,所以不支援analyse操作(analysis請求只支援可分詞欄位)
{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[5JcZFTo][127.0.0.1:9300][indices:admin/analyze[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Can't process field [new_field], Analysis requests are only supported on tokenized fields"
  },
  "status": 400
}