Elasticsearch - 仅返回自动完成的特定字段

时间:2014-09-11 22:31:09

标签: jquery ajax elasticsearch

我有自动完成设置w / jquery ajax并且只想返回特定字段,而不是整个响应。我有:

curl -X POST 'localhost:9200/music/_suggest?pretty' -d '{
    "song-suggest" : {
        "text" : "n",
        "completion" : {
            "fields" : ["suggest"],
            "field" : "suggest"
        }
    }
}'

我收到错误:

{
   "_shards":{
      "total":5,
      "successful":0,
      "failed":5,
      "failures":[
         {
            "index":"music",
            "shard":3,
            "reason":"BroadcastShardOperationFailedException[[music][3] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[[suggest] does not support [fields]]; "
         },
         {
            "index":"music",
            "shard":4,
            "reason":"BroadcastShardOperationFailedException[[music][4] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[[suggest] does not support [fields]]; "
         },
         {
            "index":"music",
            "shard":1,
            "reason":"BroadcastShardOperationFailedException[[music][1] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[[suggest] does not support [fields]]; "
         },
         {
            "index":"music",
            "shard":2,
            "reason":"BroadcastShardOperationFailedException[[music][2] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[[suggest] does not support [fields]]; "
         },
         {
            "index":"music",
            "shard":0,
            "reason":"BroadcastShardOperationFailedException[[music][0] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[[suggest] does not support [fields]]; "
         }
      ]
   }
}

我显然做错了什么,无法弄清楚如何正确地得到结果。 (我还应该提到我是弹性搜索的新手。)

编辑:我在nginx后面运行了弹性搜索,所以如果在nginx中有一个解决方案只能获得"建议"可接受的领域。

编辑#2:我的映射

{
   "song":{
      "properties":{
         "suggest":{
            "type":"completion"
         }
      }
   }
}

编辑#3:

退出"字段"参数给出:

{
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "s": [
    {
      "text": "Kate",
      "offset": 0,
      "length": 4,
      "options": [
        {
          "text": "Kate Bush",
          "score": 1.0
        },
        {
          "text": "Kate Middleton",
          "score": 1.0
        }
      ]
    }
  ]
}

最终用户不需要查看" _shards"也不是原始查询。理想情况下,返回类似:

[
  {
    "text": "Kate Bush"
  },
  {
    "text": "Kate Middleton"
  }
]

编辑#4:

Victor的建议会返回以下错误:

{
  "_shards": {
    "total": 5,
    "successful": 0,
    "failed": 5,
    "failures": [
      {
        "index": "music",
        "shard": 1,
        "reason": "BroadcastShardOperationFailedException[[music][1] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[Suggester[query_string] not supported]; "
      },
      {
        "index": "music",
        "shard": 0,
        "reason": "BroadcastShardOperationFailedException[[music][0] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[Suggester[query_string] not supported]; "
      },
      {
        "index": "music",
        "shard": 2,
        "reason": "BroadcastShardOperationFailedException[[music][2] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[Suggester[query_string] not supported]; "
      },
      {
        "index": "music",
        "shard": 3,
        "reason": "BroadcastShardOperationFailedException[[music][3] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[Suggester[query_string] not supported]; "
      },
      {
        "index": "music",
        "shard": 4,
        "reason": "BroadcastShardOperationFailedException[[music][4] ]; nested: ElasticsearchException[failed to execute suggest]; nested: ElasticsearchIllegalArgumentException[Suggester[query_string] not supported]; "
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

  1. 要使自动完成工作,您可以使用“前缀”搜索与

    之类的通配符
    {
        //query with wildcard to perform prefix search
        "query":{"query_string" : {
            "default_field" : "my_field",
            "query" : "search_prefix*"
        }},   
        //return field "my_field" and not the whole document  
        "fields":["my_field"]
    }
    
  2. Elasticsearch旨在返回这个奇怪的对象,并且可以通过服务器端代码来获取实际的自动完成数组,该代码请求弹性搜索并为您转换结果; b)只使用您感兴趣的字段,而不是查看其他字段。