Python 在弹性搜索中禁用对某些字段的搜索
我只想在一个字段“XYZ”中启用搜索。我正在使用下面的代码,但它给了我错误。你能帮我吗Python 在弹性搜索中禁用对某些字段的搜索,python,amazon-web-services,
elasticsearch,Python,Amazon Web Services,
elasticsearch,我只想在一个字段“XYZ”中启用搜索。我正在使用下面的代码,但它给了我错误。你能帮我吗 { "dynamic": "false", "properties": { "ABC": { "type": "text", "index": false, "store": true
{
"dynamic": "false",
"properties": {
"ABC": {
"type": "text",
"index": false,
"store": true
},
"PQR": {
"type": "text",
"index": false,
"store": true
},
"XYZ": {
"type": "text"
}
}
}
另外,我希望字段“ABC”和“PQR”存储在弹性搜索中,但我也希望禁用对这些字段的搜索。您可以使用
启用的设置,该设置只能应用于顶层
将定义和映射到对象字段会导致Elasticsearch跳过
完全解析字段的内容。JSON仍然可以是
从_source字段检索,但不可搜索或存储
以任何其他方式:
索引映射:
{
"mappings": {
"properties": {
"ABC": {
"enabled":false
},
"PQR": {
"enabled":false
},
"XYZ": {
"type": "text"
}
}
}
}
{
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
{
"query": {
"match": {
"XYZ": {
"query": "a"
}
}
}
}
"hits": [
{
"_index": "stof_64113718",
"_type": "_doc",
"_id": "1",
"_score": 0.13353139,
"_source": {
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
}
]
索引数据:
{
"mappings": {
"properties": {
"ABC": {
"enabled":false
},
"PQR": {
"enabled":false
},
"XYZ": {
"type": "text"
}
}
}
}
{
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
{
"query": {
"match": {
"XYZ": {
"query": "a"
}
}
}
}
"hits": [
{
"_index": "stof_64113718",
"_type": "_doc",
"_id": "1",
"_score": 0.13353139,
"_source": {
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
}
]
搜索查询:
在PQR
字段中搜索时,没有搜索结果
{
"query": {
"match": {
"PQR": {
"query": "c"
}
}
}
}
搜索结果:
{
"mappings": {
"properties": {
"ABC": {
"enabled":false
},
"PQR": {
"enabled":false
},
"XYZ": {
"type": "text"
}
}
}
}
{
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
{
"query": {
"match": {
"XYZ": {
"query": "a"
}
}
}
}
"hits": [
{
"_index": "stof_64113718",
"_type": "_doc",
"_id": "1",
"_score": 0.13353139,
"_source": {
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
}
]
搜索查询:
{
"mappings": {
"properties": {
"ABC": {
"enabled":false
},
"PQR": {
"enabled":false
},
"XYZ": {
"type": "text"
}
}
}
}
{
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
{
"query": {
"match": {
"XYZ": {
"query": "a"
}
}
}
}
"hits": [
{
"_index": "stof_64113718",
"_type": "_doc",
"_id": "1",
"_score": 0.13353139,
"_source": {
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
}
]
搜索结果:
{
"mappings": {
"properties": {
"ABC": {
"enabled":false
},
"PQR": {
"enabled":false
},
"XYZ": {
"type": "text"
}
}
}
}
{
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
{
"query": {
"match": {
"XYZ": {
"query": "a"
}
}
}
}
"hits": [
{
"_index": "stof_64113718",
"_type": "_doc",
"_id": "1",
"_score": 0.13353139,
"_source": {
"ABC": "b",
"PQR": "c",
"XYZ": "a"
}
}
]
看起来您遇到了以下异常: “原因”:“创建查询失败:无法在字段[PQR]上搜索,因为” 它没有索引。” 为了搜索它,它的正确as需要被索引,这由设置为false的index param控制
您当前的设置是正确的,您需要从搜索查询中排除未编入索引的字段,这是许多应用程序遵循的一种非常常见的模式,否则,您可以遵循Bhavya的答案,但这些字段将作为
\u source
的一部分存储,并确保未禁用(默认情况下已启用).我认为您没有正确使用语法:
PUT indexName
{
"mappings": { // <=============== This is missing
"dynamic": false,
"properties": {
"ABC": {
"type": "text", //<============= If type is object, use 'enabled' instead of 'index'
"index": false,
"store": true
},
"PQR": {
"type": "text",
"index": false,
"store": true
},
"XYZ": {
"type": "text"
}
}
}
}