ElasticSearch | 单字符串 | 多字段 | multi_match

multi_match | 三种场景

最佳字段 | Best Field
  • 当字段之间相互竞争,又相互关联,例如 title 和 body 这样的字段;
  • 评分来自最匹配字段;
多数字段 | most_fields
  • 处理英文内容时,一种常见的手段是,在主字段上采用英文分词器(English Analyzer)抽取词干,加入同义词,以匹配更多的文档;
  • 相同的文本,加入子字段中,采用标准分词器(Standard Analyzer),以提供更加精准的匹配;
  • 其他字段作为匹配文档提高相关度的信号;
  • 匹配字段越多则越好;
混合字段 | Cross Field
  • 对于某些实体,例如人名,地址,图书信息,需要在多个字段中确定信息,单个字段只能作为整体的一部分,希望在任何这些列出的字段中找到尽可能多的词;

多数字段 | most_fields | 举个栗子

创建索引 titles
DELETE /titles
PUT /titles
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "english"
      }
    }
  }
}
写入文档
POST titles/_bulk
{ "index": { "_id": 1 }}
{ "title": "My dog barks" }
{ "index": { "_id": 2 }}
{ "title": "I see a lot of barking dogs on the road " }
使用 match 查询
  • id 为 1 的算分更高;
  • 使用 english analyzer 的效果:文档在索引进 ElasticSearch 的时候被分词成英文原形,关键词也分词成英文原形;
  • id 为 1 的文档的 Term 更少,所以评分更高;
GET titles/_search
{
  "query": {
    "match": {
      "title": "barking dogs"
    }
  }
}
重新创建索引 titles
  • 为 title 字段增加子字段 std,std 的分词器是 standard,standard 分词器并不会对词干做任何提取;
DELETE /titles
PUT /titles
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "english",
        "fields": {"std": {"type": "text","analyzer": "standard"}}
      }
    }
  }
}
写入文档
POST titles/_bulk
{ "index": { "_id": 1 }}
{ "title": "My dog barks" }
{ "index": { "_id": 2 }}
{ "title": "I see a lot of barking dogs on the road " }
使用 multi_match 查询
  • "type": "most_fields" 可以将 title 和 title.std 的算分做个叠加;
  • 最匹配的结果显示在第一位了;
GET /titles/_search
{
   "query": {
        "multi_match": {
            "query":  "barking dogs",
            "type":   "most_fields",
            "fields": [ "title", "title.std" ]
        }
    }
}

混合字段 | cross_field | 举个栗子

创建索引 | 写入文档
PUT /address/_doc/1
{
  "street":"5 Poland Street",
  "city":"London",
  "country":"United Kingdom",
  "postcode":"W1V 3DG"
}
使用 multi_match 查询
  • 查到了;
  • 感觉是把所有的字段的值码成一排了,然后在搜索关键词;
POST address/_search
{
  "query":{
    "multi_match":{
      "query":"Poland Street W1V",
      "type":"cross_fields",
      "operator":"and",
      "fields":["street","city","country","postcode"]
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值