1

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

"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
        }]
    }]
}

这是可能的,还是该响应的某种变体?

4

1 回答 1

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
            }
         ]
      }
   ]
于 2014-12-09T14:59:40.937 回答