ElasticSearch _suggest查询可以返回多个字段吗?

时间:2014-08-06 16:36:01

标签: elasticsearch foselasticabundle

我有一个索引的映射,我正在进行建议/自动完成查找和响应。

"mappings": {
    "listing": {
        "_source": {
            "enabled": false
        },
        "dynamic": false,
        "properties": {
            "_all": {
                "enabled": false
            },
            "listingTitle": {
                "type": "completion",
                "index_analyzer": "str_index_analyzer",
                "search_analyzer": "str_search_analyzer"
            },
            "address": {
                "dynamic": false,
                "properties": {
                    "city": {
                        "type": "completion",
                        "index_analyzer": "str_index_analyzer",
                        "search_analyzer": "str_search_analyzer"
                    },
                    "stateOrProvince": {
                        "type": "completion",
                        "index_analyzer": "str_index_analyzer",
                        "search_analyzer": "str_search_analyzer"
                    }
                }
            }
        }
    }
}

这是请求的端点和数据:site.dev:9200/listingsuggest/_suggest

{
  "result": {
    "text": "Minn",
    "completion": {
      "field": "address.city"
    }
  }
}

所以这适用于查找匹配以Minn开头的城市的文档。我遇到的问题是我还想返回匹配" Minn"的每个文档的stateOrProvince字段值。例如,我的结果集返回以下匹配的单词:

Minneapolis
Minnetonka

我需要它做的是返回:

Minneapolis, MN
Minnetonka, MN

目前的完整回复:

{
    _shards: {
        total: 1
        successful: 1
        failed: 0
    }
    result: [{
        text: Minn
        offset: 0
        length: 4
        options: [{
            text: Minnetonka
            score: 1
        } {
            text: Minneapolis
            score: 1
        }]
    }]
}

如果可能,需要完整回复:

{
    _shards: {
        total: 1
        successful: 1
        failed: 0
    }
    result: [{
        text: Minn
        offset: 0
        length: 4
        options: [{
            text: Minnetonka, MN
            score: 1
        } {
            text: Minneapolis, MN
            score: 1
        }]
    }]
}

这可能是该响应的某种变化吗?

1 个答案:

答案 0 :(得分:0)

是的,这是可能的!试试这个:

PUT /listingsuggest
{
   "mappings": {
      "listing": {
         "_source": {
            "enabled": false
         },
         "dynamic": false,
         "properties": {
            "_all": {
               "enabled": false
            },
            "listingTitle": {
               "type": "completion",
               "index_analyzer": "standard",
               "search_analyzer": "standard"
            },
            "address": {
               "dynamic": false,
               "properties": {
                  "city": {
                     "type": "completion",
                     "index_analyzer": "standard",
                     "search_analyzer": "standard"
                  }
               }
            }
         }
      }
   }
}

您不需要address.stateOrProvince

发布一些数据:

POST /listingsuggest/listing
{
    "listingTitle" : "title1",
    "address" : {
        "city" : {
            "input" : ["Minneapolis"],
            "output" : "Minneapolis, MN"
        }
    }
}

POST /listingsuggest/listing
{
    "listingTitle" : "title2",
    "address" : {
        "city" : {
            "input": ["Minnetonka"],
            "output" : "Minnetonka, MN"
        }
    }
}

并使用此查询:

POST /listingsuggest/_suggest
{
  "listing" : {
    "text" : "Minn",
    "completion" : {
      "field" : "address.city"
    }
  }
}

它返回:

   "listing": [
      {
         "options": [
            {
               "text": "Minneapolis, MN",
               "score": 1
            },
            {
               "text": "Minnetonka, MN",
               "score": 1
            }
         ]
      }
   ]
相关问题