Suggester之Context Suggester

概念

completion suggester会考虑索引中的所有文档,但有些情况下我们希望在复合一定的过滤条件的范围内获得suggest。例如,您想建议由某些艺术家的歌曲标题,或者您要根据其类型来提升某些歌曲标题的权重。

为了实现过滤或增强suggest,您可以在配置completion字段的mapping时添加上context mappings。可以为completion字段定义多个上context mappings。每个context mappings都有唯一的name和type。有两种type:category 和 geo。上下文映射在字段映射中的contexts参数下配置。

创建索引mapping

创建索引mapping有两种方式:

  1. 定义context名称和类型,在index的时候必须提供这个context的内容
PUT test1
{
  "mappings":{
    "properties": {
      "suggest": {
        "type": "completion",
        "contexts": [
          {
          "name": "place_type",
          "type": "category"
          },
          {
          "name": "location",
          "type": "geo",
          "precision": 4
          }
        ]
      }
    }
  }
}
  1. 定义了place_type context的内容从path映射的值中读取, 在index的时候不用再提供这个context的内容了,只需要提供cat的内容
PUT test2
{
    "mappings": {
        "properties" : {
            "suggest" : {
                "type" : "completion",
                "contexts": [
                    { 
                        "name": "place_type",
                        "type": "category",
                        "path": "cat"  
                    },
                    { 
                        "name": "location",
                        "type": "geo",
                        "precision": 4,
                        "path": "loc"  
                    }
                ]
            },
            "loc": {
                "type": "geo_point"
            }
        }
    }
}

插入数据

PUT test1/_doc/1
{
    "suggest": {
        "input": ["timmy's", "starbucks", "dunkin donuts"],
        "contexts": {
            "place_type": ["cafe", "food"] 
        }
    }
}

PUT test2/_doc/1
{
    "suggest": ["timmy's", "starbucks", "dunkin donuts"],
    "cat": ["cafe", "food"] 
}

查询数据

POST test1/_search?pretty
{
    "suggest": {
        "place_suggestion" : {
            "prefix" : "tim",
            "completion" : {
                "field" : "suggest",
                "size": 10,
                "contexts": {
                    "place_type": [ "cafe", "restaurants" ]
                }
            }
        }
    }
}

OR

POST test1/_search?pretty
{
    "suggest": {
        "place_suggestion" : {
            "prefix" : "tim",
            "completion" : {
                "field" : "suggest",
                "size": 10,
                "contexts": {
                    "place_type": [ 
                        { "context" : "cafe" },
                        { "context" : "restaurants", "boost": 2 }
                     ]
                }
            }
        }
    }
}

返回结果

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "suggest": {
        "place_suggestion": [
            {
                "text": "tim",
                "offset": 0,
                "length": 3,
                "options": [
                    {
                        "text": "timmy's",
                        "_index": "place",
                        "_type": "_doc",
                        "_id": "1",
                        "_score": 1,
                        "_source": {
                            "suggest": {
                                "input": [
                                    "timmy's",
                                    "starbucks",
                                    "dunkin donuts"
                                ],
                                "contexts": {
                                    "place_type": [
                                        "cafe",
                                        "food"
                                    ]
                                }
                            }
                        },
                        "contexts": {
                            "place_type": [
                                "cafe"
                            ]
                        }
                    }
                ]
            }
        ]
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

走进IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值