Elasticsearch

install es and plugins

elasticsearch-6.4.1 download
plugin:
analysis-smartcn (eg: ./bin/elasticsearch-plugin install analysis-smartcn)
ik

then start es: bin/elasticsearch

surfing

验证分词器是否生效

1
2
3
4
5
6
7
curl --request POST \
--url http://localhost:9200/_analyze \
--header 'Content-Type: application/json' -d \
'{
"analyzer": "pinyin",
"text": "小明"
}'

hibernate-search-elasticsearch

code

config index and mapping

  1. 设置analyzer
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    curl --request PUT \
    --url http://localhost:9200/com.wework.entity.company -d \
    '{
    "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" : 16,
    "lowercase" : true,
    "remove_duplicated_term" : true
    }
    }
    }
    }
    }'
  2. 设置mapping
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    curl --request PUT \
    --url http://localhost:9200/com.wework.entity.company/_mapping/com.wework.entity.Company -d \
    '{
    "properties": {
    "name": {
    "type": "text",
    "analyzer": "ik_smart",
    "fields": {
    "pinyin": {
    "type": "text",
    "store": false,
    "term_vector": "with_offsets",
    "analyzer": "pinyin_analyzer",
    "boost": 10
    },
    "keyword": {
    "type": "keyword",
    "ignore_above": 256
    }
    }
    }
    }
    }'
  3. 查看mapping
    1
    2
    curl --request GET \
    --url http://localhost:9200/com.wework.entity.user/_mapping \
  4. 搜索
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    curl --request POST \
    --url 'http://localhost:9200/com.wework.entity.user/_search?pretty=' -d \
    '{
    "query": {
    "match": {
    "name.pinyin": "xiao明"
    }
    },
    "from": 0,
    "size": 10,
    "sort": ["_score"]
    }'