zoukankan      html  css  js  c++  java
  • elasticsearch 同义词配置搜索

    
    

    同义词的配置如下:

    
    

    杯子,保温杯

    
    


    PUT tongyiciv2
    {
    "settings": {
    "analysis": {
    "filter": {
    "word_sync": {
    "type": "synonym",
    "synonyms_path": "analysis/synonym.txt"
    }
    },
    "analyzer": {
    "ik_sync_smart": {
    "filter": [
    "word_sync"
    ],
    "type": "custom",
    "tokenizer": "ik_smart"
    }
    }
    }
    },
    "mappings": {
    "properties": {
    "id": {
    "type": "long"

    },
    "goodsName": {
    "type": "text",
    "analyzer": "ik_sync_smart",
    "search_analyzer": "ik_sync_smart"
    },
    "goodsContent": {
    "type": "text",
    "analyzer": "ik_sync_smart",
    "search_analyzer": "ik_sync_smart"
    }
    }
    }
    }

    
    
    
    
    

    GET tongyiciv2/_search

    
    

    POST tongyiciv2/_doc/1
    {
    "id":1,
    "goodsName":"江苏潮流杯子加个实惠"
    }
    POST tongyiciv2/_doc/2
    {
    "id":2,
    "goodsName":"乌鲁木齐潮流杯子样式绝美"
    }
    POST tongyiciv2/_doc/3
    {
    "id":3,
    "goodsName":"Momscook 潮流 保温杯"
    }

    
    

    POST tongyiciv2/_doc/5
    {
    "id":4,
    "goodsName":"上海潮流保温杯好用"
    }

    
    


    GET tongyiciv2/_analyze
    {
    "text": "保温杯",
    "analyzer": "ik_sync_smart"
    }

    
    


    GET tongyiciv2/_search
    {
    "query": {
    "match": {
    "goodsName": "杯子"
    }
    }
    }
    GET tongyiciv2/_search
    {
    "query": {
    "match": {
    "goodsName": "保温杯"
    }
    }
    }

     

    
    
    ============用"保温杯"分词后的结果===============
    GET tongyiciv2/_analyze { "text": "保温杯", "analyzer": "ik_sync_smart" }

    {

    "tokens" : [
    {
    "token" : "保温杯",
    "start_offset" : 0,
    "end_offset" : 3,
    "type" : "CN_WORD",
    "position" : 0
    },
    {
    "token" : "杯子",
    "start_offset" : 0,
    "end_offset" : 3,
    "type" : "SYNONYM",
    "position" : 0
    }
    ]
    }

    
    
    ============用"杯子"分词后的结果===============

    GET tongyiciv2/_analyze
    {
    "text": "杯子",
    "analyzer": "ik_sync_smart"
    }

    {
    "tokens" : [
    {
    "token" : "杯子",
    "start_offset" : 0,
    "end_offset" : 2,
    "type" : "CN_WORD",
    "position" : 0
    },
    {
    "token" : "保温杯",
    "start_offset" : 0,
    "end_offset" : 2,
    "type" : "SYNONYM",
    "position" : 0
    }
    ]
    }

    
    ===========用"杯子"查询=================
    
    GET tongyiciv2/_search
    {
      "query": {
        "match": {
          "goodsName": "杯子"
        }
      }
    }

    ===========用"保温杯"查询================= GET tongyiciv2
    /_search { "query": { "match": { "goodsName": "保温杯" } } }
    查询的结果是一样的:

    {
    "took" : 7,
    "timed_out" : false,
    "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
    },
    "hits" : {
    "total" : {
    "value" : 4,
    "relation" : "eq"
    },
    "max_score" : 0.41501677,
    "hits" : [
    {
    "_index" : "tongyiciv2",
    "_type" : "_doc",
    "_id" : "PFBLa3YBxN1KAZdSYRfw",
    "_score" : 0.41501677,
    "_source" : {
    "id" : 1,
    "goodsName" : "江苏潮流杯子加个实惠"
    }
    },
    {
    "_index" : "tongyiciv2",
    "_type" : "_doc",
    "_id" : "R8Iba3YBNdF4r2WqF8xZ",
    "_score" : 0.2876821,
    "_source" : {
    "id" : 4,
    "goodsName" : "上海潮流保温杯好用"
    }
    },
    {
    "_index" : "tongyiciv2",
    "_type" : "_doc",
    "_id" : "PVBLa3YBxN1KAZdSwRdi",
    "_score" : 0.24309544,
    "_source" : {
    "id" : 2,
    "goodsName" : "乌鲁木齐潮流杯子样式绝美"
    }
    },
    {
    "_index" : "tongyiciv2",
    "_type" : "_doc",
    "_id" : "RsIba3YBNdF4r2WqEcyq",
    "_score" : 0.21110919,
    "_source" : {
    "id" : 3,
    "goodsName" : "Momscook 潮流 保温杯"
    }
    }
    ]
    }
    }

     

    注意:

    我是在本地测试,在配置或者删除同义词时要重启es 服务器 bin/.elasticsearch

    同义词配置有两种:

    1,a=>b: 通俗的来讲,就是尽管用户输入的是a,但是es在查询的是会转成b去搜索,比如 保温杯=>杯子,用户输入的是"保温杯",但是es会用"杯子"去做搜索

    2,a,b:通俗的来讲,就是不管用户输入的是a还是b,es在查询的是用a,或者b搜索.比如 保温杯,杯子,用户输入的是"保温杯",es会用"杯子"去做搜索,也会用"保温杯"搜索

     
    无为而治
  • 相关阅读:
    专职DBA-MySQL体系结构与基本管理
    JSON
    MIME类型
    文件上传下载
    response常用的方法
    2020.11.27小记
    HTTP请求状态码
    1561. Maximum Number of Coins You Can Get
    1558. Minimum Numbers of Function Calls to Make Target Array
    1557. Minimum Number of Vertices to Reach All Nodes
  • 原文地址:https://www.cnblogs.com/wangchuanfu/p/14148662.html
Copyright © 2011-2022 走看看