中文,拼音分词使用练习记录

#删除索引

DELETE tt

#设置全局属性

PUT tt
{
  "settings": {
        "analysis": {
            "analyzer": {
                "ik_smart_pinyin": {
                    "type": "custom",
                    "tokenizer": "ik_smart",
                    "filter": ["my_pinyin"]
                },
                "ik_max_word_pinyin": {
                    "type": "custom",
                    "tokenizer": "ik_max_word",
                    "filter": ["my_pinyin"]
                }
            },
            "filter": {
                "my_pinyin": {
        "keep_joined_full_pinyin": "true",
        "lowercase": "true",
        "none_chinese_pinyin_tokenize": "false",
        "keep_none_chinese_in_joined_full_pinyin": "true",
        "keep_original": "false",
        "remove_duplicated_term": "true",
        "keep_first_letter": "false",
        "keep_separate_first_letter": "false",
        "type": "pinyin",
        "limit_first_letter_length": "16",
        "keep_full_pinyin": "false"
        
                }
            }
        }
  }
    
}

#测试全局属性

 GET tt/_analyze
    {
      "text":"翻哩",
      "analyzer":"ik_max_word_pinyin"
    }

GET tt/_analyze
{
  "text":": 2g",
  "analyzer":"ik_max_word_pinyin"
}

#新建映射

POST tt/entry/_mapping
{
  "entry": {
    "properties": {
      "author": {
        "type": "keyword",
        "store": true
      },
      "content": {
        "type": "text",
        "store": true,
        "analyzer": "ik_max_word_pinyin"
      },
      "createTime": {
        "type": "date",
        "store": true,
        "format": "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd"
      },
      "fondsId": {
        "type": "keyword",
        "store": true
      },
      "mgtLevel": {
        "type": "keyword",
        "store": true
      },
      "nodeId": {
        "type": "keyword",
        "store": true
      },
      "other": {
        "type": "text",
        "store": true
      },
      "tag": {
        "type": "text",
        "store": true
      },
      "title": {
        "type": "text",
        "store": true,
        "analyzer": "ik_max_word_pinyin"
      },
      "updateTime": {
        "type": "date",
        "store": true,
        "format": "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd"
      },
      "view": {
        "type": "text",
        "store": true
      }
    }
  }
    
}

#查看全局设置

GET tt/_settings

#写值

PUT tt/entry/1
{
  "id":106996,
  "title":"431101199607116477(431101199607116477)",
  "content":"43110s",
  "fondsId":299
}

#测试另一种映射

POST tt/entry/_mapping
{
  "entry":{
    "properties":{
      "id":{
        "type":"keyword",
        "store":true
      },
      "name":{
        "type":"text",
        "store":true,
        "analyzer":"ik_smart_pinyin"
      },
      "content":{
        "type":"text",
        "store":true,
        "analyzer":"ik_smart"
      }
    }
  }
}

#查看映射

GET tt/_mapping

#删除索引库

DELETE test

#创建索引库

PUT test

#配置全局映射:默认+动态模板

PUT _template/global_template
{
  "template": "*",
  "settings": {"number_of_shards": 1},
  "mappings": {
    "_default_": {
      "_all":{
        "enabled":false
      },
      "dynamic_templates":[
        {
          "string_as_text":{
            "match_mapping_type":"string",
            "match":"*_text",
            "mapping":{
              "type":"text",
              "analyzer":"ik_max_word",
              "search_analyzer":"ik_max_word",
              "fields":{
                "row":{
                  "type":"keyword",
                  "ignore_above":256
                }
              }
            }
          }
        },{
          "string_as_keyword":{
            "match_mapping_type":"string",
            "mapping":{
              "type":"keyword"
            }
          }
        }
      ]
    }
  }
}

#删除类型映射

DELETE test/_mapping/user

#做类型映射

POST test/_mapping/user
{
  "user":{
    "properties":{
      "id":{
        "type":"long"
      },
      "userAll":{
        "type":"text",
        "analyzer":"ik_smart"
      },
      "name":{
        "type":"keyword"
      },
      "info":{
        "type":"text",
        "analyzer":"ik_smart",
        "search_analyzer":"ik_smart",
        "copy_to":"userAll"
      }
    }
  }
}

#查看类型映射

GET test/_mapping/user

#添加数据

PUT test/user/1
{
  "id":"1",
  "name":"张三丰",
  "info":"张三丰,名君宝(又名全一),字符元,道号三丰。"
}

#根据索引查看

GET test/user/1

#查询

GET test/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "info": "君宝"
          }
        }
      ]
    }
  }
}

#细粒度分词

POST _analyze
{
  "analyzer": "ik_smart",
  "text": "ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开..."
}

#最大拆分分词

POST _analyze
{
  "analyzer": "ik_max_word",
  "text": "ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开..."
}

#删除索引库

DELETE test

#创建索引库

PUT test

#配置全局映射:默认+动态模板

PUT _template/global_template
{
  "template": "*",
  "settings": {"number_of_shards": 1},
  "mappings": {
    "_default_": {
      "_all":{
        "enabled":false
      },
      "dynamic_templates":[
        {
          "string_as_text":{
            "match_mapping_type":"string",
            "match":"*_text",
            "mapping":{
              "type":"text",
              "analyzer":"ik_max_word",
              "search_analyzer":"ik_max_word",
              "fields":{
                "row":{
                  "type":"keyword",
                  "ignore_above":256
                }
              }
            }
          }
        },{
          "string_as_keyword":{
            "match_mapping_type":"string",
            "mapping":{
              "type":"keyword"
            }
          }
        }
      ]
    }
  }
}

#拼音测试

DELETE twitter

PUT twitter
{
	"index": {
		"analysis": {
			"analyzer": {
				"ik_pinyin_analyzer": {
					"type": "custom",
					"tokenizer": "ik_smart",
					"filter": ["my_pinyin",
					"word_delimiter"]
				}
			},
			"filter": {
				"my_pinyin": {
					"type": "pinyin",
					"first_letter": "prefix",
					"padding_char": " "
				}
			}
		}
	}
}

PUT twitter/doc/_mapping
{
	"doc": {
		"properties": {
		    "type": { "type": "keyword" }, 
        "name": { "type": "text" },
        "user_name": { "type": "keyword" },
        "email": { "type": "keyword" },
        "content": { 
          "type": "keyword",
          "analyzer":"ik_smart",
          "fields": {
  					"pinyin": {
  						"type": "text",
  						"store": false,
  						"term_vector": "with_positions_offsets",
  						"analyzer": "ik_pinyin_analyzer",
  						"boost": 10
  					}
  				}
        },
        "tweeted_at": { "type": "date" }
		}
	}
}

PUT twitter/doc/user-kimchy
{
  "type": "user", 
  "name": "Shay Banon",
  "user_name": "kimchy",
  "email": "shay@kimchy.com"
}

PUT twitter/doc/tweet-4
{
  "type": "tweet", 
  "user_name": "kimchy",
  "tweeted_at": "2017-10-24T09:00:00Z",
  "content": "明星生孩子压力大?高圆圆钟丽缇心态不一,婚后生活大不同 "
}

GET twitter/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "content": "gaoyuanyuan"
        }
      },
      "filter": {
        "match": {
          "type": "tweet" 
        }
      }
    }
  }
}

#做类型映射

POST test/_mapping/user
{
  "user":{
    "properties":{
      "id":{
        "type":"long"
      },
      "userAll":{
        "type":"text",
        "analyzer":"ik_smart",
        "fields": {
					"pinyin": {
						"type": "text",
						"store": false,
						"term_vector": "with_positions_offsets",
						"analyzer": "ik_pinyin_analyzer",
						"boost": 10
					}
				}
      },
      "name":{
        "type":"keyword"
      },
      "info":{
        "type":"text",
        "analyzer":"ik_smart",
        "search_analyzer":"ik_smart",
        "copy_to":"userAll"
      }
    }
  }
}

#查看类型映射

GET test/_mapping/user

#添加数据

PUT test/user/5
{
  "id":"5",
  "name":"张三丰",
  "info":"名君宝,字符元。三"
}

#根据索引查看

GET test/user/1

#查询

GET test/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "info": "三丰"
          }
        }
      ]
    }
  }
}

#细粒度分词

POST _analyze
{
  "analyzer": "pinyin",
  "text": "张三丰,名君宝(又名全一),字符元,道号三丰。"
}

#最大拆分分词

POST _analyze
{
  "analyzer": "ik_max_word",
  "text": "ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开..."
}

GET _search

#拼音分词

POST /_analyze
{
    "analyzer":"pinyin",
    "text":"中华人民共和国国歌"
}

#配置全局映射:默认+动态模板

PUT _template/global_pinyin_template
{
	"index": {
		"analysis": {
			"analyzer": {
				"pinyin_analyzer": {
					"tokenizer": "my_pinyin"
				}
			},
			"tokenizer": {
				"my_pinyin": {
					"type": "pinyin",
					"keep_separate_first_letter": false,
					"keep_full_pinyin": true,
					"keep_original": true,
					"limit_first_letter_length": 10,
					"lowercase": true,
					"remove_duplicated_term": true
				}
			}
		}
	}
}

#删除 index

DELETE index_name

#创建一个 index_name 的 index

PUT index_name
{
	"index": {
		"analysis": {
			"analyzer": {
				"ik_pinyin_analyzer": {
					"type": "custom",
					"tokenizer": "ik_smart",
					"filter": ["my_pinyin",
					"word_delimiter"]
				}
			},
			"filter": {
				"my_pinyin": {
					"type": "pinyin",
					"first_letter": "prefix",
					"padding_char": " "
				}
			}
		}
	}
}

#修改 type 的 mapping

PUT index_name/app/_mapping
{
	"app": {
		"properties": {
			"ProductCName": {
				"type": "keyword",
				"fields": {
					"pinyin": {
						"type": "text",
						"store": false,
						"term_vector": "with_positions_offsets",
						"analyzer": "ik_pinyin_analyzer",
						"boost": 10
					}
				}
			},
			"ProductEName": {
				"type": "text",
				"analyzer": "ik_smart"
			},
			"Description": {
				"type": "text",
				"analyzer": "ik_smart"
			}
		}
	}
}

#创建测试数据

PUT index_name/app/1
{
  "ProductCName":"口红世家",
  "ProductEName":"Red History",
  "Description":"口红真是很棒的东西呢"
}

#查询

GET index_name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "ProductCName.pinyin": "kou"
          }
        }
      ]
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值