elasticsearch中文分词和拼音混合搜索

一、安装elasticsearch

  1. 在elasticsearch目录下分别创建data和tmp文件夹

  1. vi /etc/profile

加入配置

export ES_TMPDIR=/mnt/elasticsearch-7.17.7/tmp
export ES_JAVA_OPTS="-Djna.tmpdir=/mnt/elasticsearch-7.17.7/tmp"
  1. vi /etc/sysctl.conf

加入配置

vm.max_map_count = 655360
  1. vi elasticsearch.yml

加入配置

node.name: node-1
path.data: /mnt/elasticsearch-7.17.7/data
path.logs: /mnt/elasticsearch-7.17.7/logs
network.host: 0.0.0.0
http.port: 19200
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
cluster.initial_master_nodes: ["node-1"]
  1. 在elasticsearch下的plugins目录下解压分词插件和拼音插件

unzip elasticsearch-analysis-ik-7.17.7.zip -d ik
unzip elasticsearch-analysis-pinyin-7.17.7.zip -d pinyin
  1. 创建es用户

groupadd es
useradd es -g es -p es3050
chown -R es:es /mnt/elasticsearch-7.17.7
  1. 设置elastic密码

./elasticsearch-setup-passwords interactive

  1. 启动elasticsearch

./elasticsearch -d

二、使用elasticsearch搜索

  1. 创建索引

curl --user elastic:123456  -H "Content-Type: application/json"  -XPUT "http://localhost:19200/hello?pretty" -d'
{
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "ik_pinyin_analyzer": {
                        "type": "custom",
                        "tokenizer": "ik_max_word",
                        "filter": ["my_pinyin"]
                    }
                },
                "filter": {
                    "my_pinyin": {
                        "type": "pinyin",
                        "keep_separate_first_letter": false,
                        "keep_full_pinyin": true,
                        "keep_original": false,
                        "limit_first_letter_length": 10,
                        "lowercase": true,
                        "remove_duplicated_term": true
                    }
                }
            }
        }
    }
}'
  1. 创建索引映射

curl --user elastic:123456  -H "Content-Type: application/json"  -XPUT "http://localhost:19200/hello/_mapping?pretty" -d'
{
    "properties":{
        "id":{
            "type":"long"
        },
        "name":{
            "type":"text",
            "index":true,
            "analyzer":"ik_pinyin_analyzer"
        },
        "address":{
            "type":"text",
            "index":true,
            "analyzer":"ik_max_word"
        }
    }
}'
  1. 导入数据

curl --user elastic:123456  -H "Content-Type: application/json"  -XPUT "http://localhost:19200/hello/_create/1?pretty" -d'
{
    "name":"张三丰",
    "address":"武当山"
}'
  1. 查询数据

curl --user elastic:123456  -H "Content-Type: application/json"  -XPOST "http://localhost:19200/hello/_search?pretty" -d'
{
    "query":{
        "match":{
            "name":"张三"
        }
    }
}'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ronshi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值