zoukankan      html  css  js  c++  java
  • ElasticSearch利用IK实现全文搜索

    要做到中文全文检索还需要按照中文分词库 ,这里就使用 IK来设置

    安装中文分词库
    相关命令:
    
    whereis elasticsearch  找到目录
    进入 到/usr/elasticsearch/bin 
    执行  ./elasticsearch-plugin插件命令 安装插件
    
    ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.1/elasticsearch-analysis-ik-6.6.1.zip
    
    service elasticsearch restart 重启服务
    
    安装好之后可以看到 plugin 下有 analysis-ik 
    
    接下来按照官方的步骤往下面走
    
    1、创建一个索引
    
    curl -XPUT http://localhost:9200/myfulltext
    
    2、创建
    
    curl -XPOST http://localhost:9200/liyouming/liyoumingtext/_mapping -H 'Content-Type:application/json' -d'
    {
            "properties": {
                "mytext": {  
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                }
            }
    
    }'
    这里要注意 content 不能冲突 ,分析器字段要定义
    Mapper for [content] conflicts with existing mapping in other types:
    [mapper [content] has different [analyzer]]
    Mapper for[content]与其他类型的现有映射冲突:[mapper[content]有不同的[分析器]

    这里我们还是通过WebAPI来测试

    首先创建我们的索引

    OK后创建 全文检索相关设置 设置字段、分析器配置    ik_smart  、ik_max_word

     分别添加如下数据

    {
    "url":"liyouming/liyoumingtext",
    "param":{"mytext":"深夜还在写代码的人只有黎又铭"}
    }
    {
    "url":"liyouming/liyoumingtext",
    "param":{"mytext":"中午黎又铭在操场上打篮球"}
    }
    {
    "url":"liyouming/liyoumingtext",
    "param":{"mytext":"黎又铭早上吃了一碗面"} 
    }

     查询下所有数据可以看到

    {
      "took": 8,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 3.0561461,
        "hits": [
          {
            "_index": "liyouming",
            "_type": "liyoumingtext",
            "_id": "c7eQAWoB0Mh7sqcTGY-K",
            "_score": 3.0561461,
            "_source": {
              "mytext": "中午黎又铭在操场上打篮球"
            }
          },
          {
            "_index": "liyouming",
            "_type": "liyoumingtext",
            "_id": "dLeQAWoB0Mh7sqcTdo9b",
            "_score": 2.1251993,
            "_source": {
              "mytext": "深夜还在写代码的人只有黎又铭"
            }
          },
          {
            "_index": "liyouming",
            "_type": "liyoumingtext",
            "_id": "crePAWoB0Mh7sqcTzY-2",
            "_score": 0.8630463,
            "_source": {
              "mytext": "黎又铭早上吃了一碗面"
            }
          }
        ]
      }
    }

    检索下篮球并高亮文本内容可以看到下面的结果 <tag1>篮球</tag1> 已经被高亮标签处理

    {
      "took": 8,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1.0187154,
        "hits": [
          {
            "_index": "liyouming",
            "_type": "liyoumingtext",
            "_id": "c7eQAWoB0Mh7sqcTGY-K",
            "_score": 1.0187154,
            "_source": {
              "mytext": "中午黎又铭在操场上打篮球"
            },
            "highlight": {
              "mytext": [
                "中午黎又铭在操场上打<tag1>篮球</tag1>"
              ]
            }
          }
        ]
      }
    }
  • 相关阅读:
    struts2 之 Action的创建方式
    struts2 之 struts2数据处理
    SuperMap for WebGL 9D 加载平面坐标系三维场景
    SuperMap-WebGL-坐标系及转换说明
    SuperMap -WebGL 实现地球的背景透明并显示自定义图片
    转载: ssh连接上华为云Linux服务器,一会就自动断开
    Arcgis瓦片--js客户端加载
    Arcgis瓦片--数据获取
    转载:Linux服务器Cache占用过多内存导致系统内存不足最终java应用程序崩溃解决方案
    转载-浏览器优化中的2-5-8原则
  • 原文地址:https://www.cnblogs.com/liyouming/p/10678738.html
Copyright © 2011-2022 走看看