elasticsearch 分词

2022-05-25 20:45:33 浏览数 (1)

安装中文、拼音分词

代码语言:javascript复制
https://github.com/medcl/elasticsearch-analysis-ik
https://github.com/medcl/elasticsearch-analysis-pinyin

下载和elasticsearch对应的版本,解压后移到plugins目录

代码语言:javascript复制
root@57d58faf9b1e:/usr/share/elasticsearch/plugins# ls
ik  pinyin

重启elasticsearch使生效

测试一下

代码语言:javascript复制
默认分词
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/_analyze?pretty' -d'
{
  "analyzer": "standard",
  "text":"22强烈推荐11"
}'

ik中文分词
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/_analyze?pretty' -d'
{
  "analyzer": "ik_max_word",
  "text":"22强烈推荐11"
}'

拼音分词
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/_analyze?pretty' -d'
{
  "analyzer": "pinyin",
  "text":"22强烈推荐11"
}'

创建索引article,内容如下

代码语言:javascript复制
{
  "settings": {
    "index":{
      "number_of_shards": "1",
      "number_of_replicas": "0",
      "analysis" : {
        "analyzer" : {
          "default" : {
            "tokenizer" : "ik_max_word"
          },
          "pinyin_analyzer" : {
            "tokenizer" : "my_pinyin"
          }
        },
        "tokenizer" : {
          "my_pinyin" : {
            "keep_separate_first_letter" : "false",
            "lowercase" : "true",
            "type" : "pinyin",
            "limit_first_letter_length" : "16",
            "keep_original" : "true",
            "keep_full_pinyin" : "true"
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer":"ik_max_word",
        "fields" : {
          "pinyin" : {
            "type" : "text",
            "term_vector" : "with_positions_offsets",
            "analyzer" : "pinyin_analyzer",
            "boost" : 10.0
          }
        }
      },
      "content": {
        "type": "text",
        "analyzer":"ik_max_word"
      },
      "create_time": {
        "type": "long"
      },
      "id": {
        "type": "long"
      },
      "update_time": {
        "type": "long"
      }
    }
  }
}

php

导入数据后,就可以测试了

代码语言:javascript复制
    public function search($keyword, $page=1, $max=10) {
        $params = [
            'index' => 'article',
            'body' => [
                'query' => ['multi_match' => ['query' => $keyword, 'fields'=>['title', 'title.pinyin', 'content']]],
                '_source'=>['id', 'title', 'content', 'create_time'],
                'highlight'=>['fields'=>['title'=>new stdClass(), 'content'=>new stdClass()]],

                "sort"=>['_doc'],
                'from'=>($page-1)*$max,//from, size相当于sql的limit
                'size'=>$max,
            ]
        ];
        return $this->cache()->search($params);
    }

0 人点赞