弹性查询全文搜索

时间:2018-11-20 17:31:26

标签: elasticsearch

使用弹性POST请求创建索引(会有多个记录)

POST /rewards/deals/_bulk
{ "index": {}}
{
    "deal_id":25, 
    "title": "Deal 1 Title", 
    "deal_subtitle":"Deal 1 subtitle - Amazing deal 20% off",
    "header_title":"Never before deal", 
    "header_subtitle":"This is available as an amazing deal you can use.", 
    "categories":[
    {"categoryId":32, "categoryName":"Treats!"},
    {"categoryId":31, "categoryName":"Travel & Transportation"}],
    "stores_list": [{"storeId":524, "storeName":"Awesome store", "geoX":"", "geoY":"", "postalCode":"417392"}], 
    "merchant_name": "Merchant Name", 
    "id": 1, 
    "merchant_id": 137, 
    "startEnd":"2018-11-16 00:00:00", 
    "endDate":"2018-11-30 00:00:00"
} 

编写查询以执行全文搜索

GET /rewards/deals/_search
{
  "query": {
    "multi_match": {
      "query": "treat",
      "fields": ["title^10", "deal_subtitle^10", "header_subtitle^5", "header_title^5", "categories.categoryName^3", "stores_list.storeName^3", "stores_list.postalCode^3", "merchant_name^10"]
    }
  }
}

但是我没有得到任何有关“治疗”的数据,而且category.categoryName中也提供了treat(源自treats)。有人可以帮忙吗? 当我搜索“治疗”时,会得到答复。如何获得以搜索关键字开头的单词的响应。

1 个答案:

答案 0 :(得分:0)

只需在现有查询中添加“类型”即可使其工作

GET /rewards/deals/_search
{
  "query": {
    "multi_match": {
      "query": "treat",
      "type":"phrase_prefix",
      "fields": ["title^10", "deal_subtitle^10", "header_subtitle^5", "header_title^5", "categories.categoryName^3", "stores_list.storeName^3", "stores_list.postalCode^3", "merchant_name^10"]
    }
  }
}