电商项目商品搜索模块 - ES&ik分词器安装

26145249_pLMS.jpg

洋鼹鼠-满足国人一切海外需求!

洋鼹鼠PC端搜索商品

com.ningpai.site.goods.controller.GoodsSiteController#esearchProduct

boss后台把所有的商品,在es中生成索引
boss->店铺-》店铺商品-》店铺商品列表-》生成索引

com.ningpai.goods.controller.GoodsElasticSearchController#insertLasticGoods

 

es官网

https://www.elastic.co/products/elasticsearch
 

官网方式下载es

官网->Download->

Downloads:

注:1、如果想要以前的版本:111715_T4Cv_2475326.png

2、下载之后,解压,运行 bin/elasticsearch (or bin\elasticsearch.bat on Windows)就行了。

3、Run curl http://localhost:9200/ or Invoke-RestMethod http://localhost:9200 with PowerShell

在浏览器中打开http://localhost:9200/如果启动成功,就可以看到:

112259_Km80_2475326.png

 

ElasticSearch-Kopf插件

Kopf是一个ElasticSearch的管理工具,它也提供了对ES集群操作的API。

26114213_d6SI.png

安装

:5.0之后的es安装包中没有bin/plugin命令,5.0版本之前的es,可以使用此命令安装kopf插件

./elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf/{branch|version}

open http://localhost:9200/_plugin/kopf
版本支持

因为我们项目使用的es是1.7.1版本的,所以我安装kopf插件选择v1.6.1

-----------------------------------------------------------
    elasticsearch   | version branch    |   latest version
-----------------------------------------------------------
    0.90.X          |   0.90            |   v0.90
-----------------------------------------------------------
    1.X             |   1.0             |   v1.6.1
-----------------------------------------------------------
    2.X             |   2.0             |   v2.1.1  
-----------------------------------------------------------   

 

 

ik分词器

项目中用到了ik分词器。ik分词器官网

1、下载和编译

1)手动安装
git clone https://github.com/medcl/elasticsearch-analysis-ik
cd elasticsearch-analysis-ik
​​​​​​​git tag
git checkout {tag version}
mvn clean
mvn compile
mvn package
2)使用 elasticsearch-plugin 命令去安装 ( version > v5.5.1 )
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.1/elasticsearch-analysis-ik-5.6.1.zip

2、添加ik的配置到elasticsearch-HOME/config/elasticsearch.yml

cat elasticsearch-analysis-ik/config/elasticsearch.yml >> elasticsearch-HOME/config/elasticsearch.yml

注释:经过实际安装测试,此方法配置ik会报错:
org.elasticsearch.index.mapper.MapperParsingException: Analyzer [ik_max_word] not found for field [content]

修改方法:这里把下面配置内容保存到elasticsearch-HOME/config/elasticsearch.yml后重启,可以解决此问题。
(下面这些配置内容是洋鼹鼠测试环境es配置文件中复制出来的)

#添加ik分词
index:
  analysis:                   
    analyzer:      
      ik:
          #alias: [ik_analyzer]
          type: org.elasticsearch.index.analysis.IkAnalyzerProvider
      ik_max_word:
          type: ik
          use_smart: false
      ik_smart:
          type: ik
          use_smart: true
index.analysis.analyzer.default.type : "ik"

3、把ik配置文件复制到elasticsearch-HOME/config/

mv elasticsearch-analysis-ik/config/ik/ elasticsearch-HOME/config/

4、restart elasticsearch

./bin/elasticsearch

 

验证Quick Example

1.create a index

curl -XPUT http://localhost:9200/index

2.create a mapping

curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
        }
    
}'

注释:

1)ik官网上例子上type:text,会报错:把"type":"text"改成"type":"string"

org.elasticsearch.index.mapper.MapperParsingException: No handler for type [text] declared on field [content]

3.index some docs

curl -XPOST http://localhost:9200/index/fulltext/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/index/fulltext/2 -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/index/fulltext/3 -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -XPOST http://localhost:9200/index/fulltext/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'

4.query with highlighting

curl -XPOST http://localhost:9200/index/fulltext/_search  -d'
{
    "query" : { "match" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
'

Result

{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2,
        "hits": [
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "4",
                "_score": 2,
                "_source": {
                    "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
                },
                "highlight": {
                    "content": [
                        "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
                    ]
                }
            },
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "3",
                "_score": 2,
                "_source": {
                    "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
                },
                "highlight": {
                    "content": [
                        "均每天扣1艘<tag1>中国</tag1>渔船 "
                    ]
                }
            }
        ]
    }
}

 

参考:

Elasticsearch官网

API | Elasticsearch权威指南(中文版)

ElasticSearch 2 (6) - 插件安装Head、Kopf与Bigdesk

elasticsearch-analysis-ik

 

 

有什么问题,可以在下面留言。看到后会及时回复。

 

 

转载于:https://my.oschina.net/anxiaole/blog/1543508

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值