嵌套字段的Elasticsearch完成建议上下文

时间:2016-02-28 13:24:05

标签: elasticsearch completion

我正在开发具有完成功能的简单搜索应用。 我需要以某种方式保护这些建议,所以我想出最简单的方法是将上下文添加到完成建议器。我的问题是我不知道如何在嵌套字段中使用建议上下文。

这就是我的映射的样子,非常简单,只有3个字段,一个是嵌套的。

curl-XPUT'http: //localhost: 9200/cr/_mapping/agreement_index'-d'{
    "agreement_index": {
        "properties": {
            "agreement_name": {
                "type": "string",
                "fields": {
                    "suggest": {
                        "type": "completion",
                        "analyzer": "simple",
                        "payloads": false,
                        "preserve_separators": true,
                        "preserve_position_increments": true,
                        "max_input_length": 50,
                        "context": {
                            "permitted": {
                                "type": "category",
                                "path": "permitted",
                                "default": []
                            }
                        }
                    }
                }
            },
            "permitted": {
                "type": "integer"
            },
            "team": {
                "type": "nested",
                "dynamic": "false",
                "properties": {
                    "email": {
                        "type": "string",
                        "fields": {
                            "raw": {
                                "type": "string",
                                "index": "not_analyzed"
                            },
                            "suggest": {
                                "type": "completion",
                                "analyzer": "simple",
                                "payloads": false,
                                "preserve_separators": true,
                                "preserve_position_increments": true,
                                "max_input_length": 50,
                                "context": {
                                    "permitted": {
                                        "type": "category",
                                        "path": "permitted",
                                        "default": []
                                    }
                                }
                            }
                        }
                    },
                    "name": {
                        "type": "string",
                        "fields": {
                            "raw": {
                                "type": "string",
                                "index": "not_analyzed"
                            },
                            "suggest": {
                                "type": "completion",
                                "analyzer": "simple",
                                "payloads": false,
                                "preserve_separators": true,
                                "preserve_position_increments": true,
                                "max_input_length": 50,
                                "context": {
                                    "permitted": {
                                        "type": "category",
                                        "path": "permitted",
                                        "default": []
                                    }
                                }
                            }
                        }
                    },
                    "permitted": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}'

在索引此类文档时:

curl-XPUT'http: //localhost: 9200/cr/agreement_index/1'-d'{
    "agreement_name": "QWERTY",
    "team": [{
        "name": "Tomasz Sobkowiak",
        "permitted": ["2"],
        "email": "tsobkowiak@fake.com"
    }],
    "permitted": ["2"]
}'

我得到以下错误:

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"one or more prefixes needed"}],"type":"illegal_argument_exception","reason":"one or more prefixes needed"},"status":400}

从嵌套字段中的完成建议中删除上下文后,一切正常。 所以我的问题是,我如何在嵌套字段中使用上下文建议,路径指向外部文档中的字段?这样的事情甚至可能吗?

1 个答案:

答案 0 :(得分:3)

问题在于您的映射。默认不能为空。您需要在context suggester的映射中至少分配一个默认值。

    "context": {
        "permitted": {
            "type": "category",
            "path": "permitted",
            "default": [] // <-- defaults can not be empty, provide at least one default integer value
        }
}
  

使用默认字段的值,如果没有具体的话   为特定背景提供。请注意,上下文由at定义   至少一个值。

此外,在您尝试编制索引的文档中,您在string中使用permitted,而它被映射为Integer

"permitted": ["2"] // <-- change this to "permitted":[2]