Elasticsearch PHP:[text]中VALUE_STRING的未知密钥

时间:2018-05-02 10:28:45

标签: php elasticsearch elasticsearch-api

我正在使用Elasticsearch-PHP [6.0]
我有一个索引products,其中我有一个产品名称实施短语建议。

索引设置

{
  "products": {
    "aliases": {},
    "mappings": {
      "surat": {
        "properties": {
          "suggest": {
            "type": "completion",
            "analyzer": "simple",
            "preserve_separators": true,
            "preserve_position_increments": true,
            "max_input_length": 50
          },
          "title": {
            "type": "text",
            "fields": {
              "reverse": {
                "type": "text",
                "analyzer": "reverse"
              },
              "trigram": {
                "type": "text",
                "analyzer": "trigram"
              }
            }
          }
        }
      }
    },
    "settings": {
      "index": {
        "number_of_shards": "5",
        "provided_name": "products",
        "creation_date": "1525244538694",
        "analysis": {
          "filter": {
            "shingle": {
              "max_shingle_size": "3",
              "min_shingle_size": "2",
              "type": "shingle"
            }
          },
          "analyzer": {
            "reverse": {
              "filter": [
                "standard",
                "reverse"
              ],
              "type": "custom",
              "tokenizer": "standard"
            },
            "trigram": {
              "filter": [
                "standard",
                "shingle"
              ],
              "type": "custom",
              "tokenizer": "standard"
            }
          }
        },
        "number_of_replicas": "1",
        "uuid": "ljVUaIOURjqFOyQ0CXZmow",
        "version": {
          "created": "6020499"
        } 
      }
    }
  } 
}  

查询参数

$params = [
    'index' => 'products',
    'body' => [
        "text" => fortune,
        "simple_phrase" => [
            "phrase" => [
                "field" => "title.trigram",
                "size" => 1,
                "direct_generator" => [ 
                    [
                        "field" => "title.trigram",
                        "suggest_mode" => "always"
                    ],
                    [
                        "field" => "title.reverse",
                        "suggest_mode" => "always",
                        "pre_filter" => "reverse",
                        "post_filter" => "reverse"
                    ]
                ]
            ]
        ]
    ]
];  
$response = $client->search($params);

错误

{"error":{"root_cause":[{"type":"parsing_exception","reason":"Unknown key for a VALUE_STRING in [text].","line":1,"col":9}],"type":"parsing_exception","reason":"Unknown key for a VALUE_STRING in [text].","line":1,"col":9},"status":400}  

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您只是错过了suggest关键字:

$params = [
    'index' => 'products',
    'body' => [
      "suggest" => [                               <-- add this
        "text" => fortune,
        "simple_phrase" => [
            "phrase" => [
                "field" => "title.trigram",
                "size" => 1,
                "direct_generator" => [ 
                    [
                        "field" => "title.trigram",
                        "suggest_mode" => "always"
                    ],
                    [
                        "field" => "title.reverse",
                        "suggest_mode" => "always",
                        "pre_filter" => "reverse",
                        "post_filter" => "reverse"
                    ]
                ]
            ]
        ]
      ]                                            <-- and this
    ]
];  
$response = $client->search($params);