这是我的映射,我已经将必填字段声明为关键字,但我的术语查询不适用于category_name和storeName,但适用于price。
"mappings": {
"properties" : {
"firebaseId":{
"type":"text"
},
"name" : {
"type" : "text",
"analyzer" : "synonym"
},
"name_auto" : {
"type": "text",
"fields": {
"edgengram": {
"type": "text",
"analyzer": "edge_ngram_analyzer",
"search_analyzer": "edge_ngram_search_analyzer"
},
"completion": {
"type": "completion"
}
}
},
"category_name" : {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"storeName" : {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"sku" : {
"type" : "text"
},
"price" : {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"magento_id" : {
"type" : "text"
},
"seller_id" : {
"type" : "text"
},
"square_item_id" : {
"type" : "text"
},
"square_variation_id" : {
"type" : "text"
},
"typeId" : {
"type" : "text"
}
}
}
}
}
以下是我的疑问:
{
"size": 0,
"aggs": {
"Category Filter": {
"terms": {
"field": "category_name",
"size": 10
}
},
"Store Filter": {
"terms": {
"field": "storeName",
"size": 10
}
},
"Price Filter": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 50
},
{
"from": 50,
"to": 100
},
{
"from": 100,
"to": 200
}
]
}
}
}
}
其返回如下:
“reason”:{”type“:”illegal_argument_exception“,”reason“:”默认情况下在文本字段上禁用Fielddata。在[category_name]上设置Fielddata=true,以便通过取消反转索引将字段数据加载到内存中。请注意,这可能会占用大量内存。或者改用关键字字段。“
您需要使用.keyword
子字段,如下所示:
{
"size": 0,
"aggs": {
"Category Filter": {
"terms": {
"field": "category_name.keyword", <-- change this
"size": 10
}
},
"Store Filter": {
"terms": {
"field": "storeName.keyword", <-- change this
"size": 10
}
},
"Price Filter": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 50
},
{
"from": 50,
"to": 100
},
{
"from": 100,
"to": 200
}
]
}
}
}
}