试试搜索一下吧

同义词查询如何区分权重

Elasticsearch | 作者 WangChuanfu | 发布于2020年12月17日 | 阅读数:1663

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 潮流 保温杯"
}
}
]
}
}
 
现在数据库里商品名称有杯子和保温杯,比如我想按照杯子去查询,如何将杯子的商品展示在前面呢?
 
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自: ly1991

这里给出了不错的解决方案:
 
https://stackoverflow.com/ques ... earch
 
方案的核心:
1、定义nested 类型;
2、指定特定字段,并且加上权限weight字段。
 
不妨一试,期待再次交流/。

CurryQin

赞同来自:

你配置的双向关联,去掉双向关联,配置成单向关联根据你的需求

WangChuanfu - 深入学习es

赞同来自:

PUT /synonym_test/
{
  "settings": {
    "analysis": {
      "analyzer": {
        "no_synonyms": {
          "type": "custom",
          "tokenizer": "ik_smart"
        },
        "synonyms": {
         "type": "custom",
          "tokenizer": "ik_smart",
          "filter": [
            "synonym"
          ]
        }
      },
      "filter": {
        "synonym": {
          "type": "synonym",
          "synonyms_path": "analysis/synonym.txt"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "syn": {
        "type": "text",
        "analyzer": "synonyms"
      },
      "no_syn": {
        "type": "text",
        "analyzer": "no_synonyms"
      }
    }
  }
}






POST synonym_test/_doc/1
{
  "syn": "江苏潮流杯子价格实惠",
  "no_syn": "江苏潮流杯子价格实惠"
}
POST synonym_test/_doc/2
{
  "syn": "乌鲁木齐潮流杯子样式绝美",
  "no_syn": "乌鲁木齐潮流杯子样式绝美"
}
POST synonym_test/_doc/3
{
  
  "syn": "Momscook 潮流 保温杯",
  "no_syn": "Momscook 潮流 保温杯"
}

POST synonym_test/_doc/4
{

   "syn": "上海潮流保温杯好用",
  "no_syn": "上海潮流保温杯好用"
}


GET /synonym_test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "syn": {
              "query": "杯子",
                "boost": 10
            }
          }
        },
        {
          "match": {
            "no_syn": "杯子"
          }
        }
        
      ]
    }
  }
}



GET /synonym_test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "syn": {
              "query": "保温杯",
                "boost": 10
            }
          }
        },
        {
          "match": {
            "no_syn": "保温杯"
          }
        }
        
      ]
    }
  }
}

要回复问题请先登录注册