字词查询(ElasticSearch)不适用于某些字段值

时间:2019-07-10 12:43:49

标签: elasticsearch

这是我的Mapping,我已经在其中声明了必填字段为关键字,但是我的字词查询不适用于category_name和storeName,但对于价格而言却可以正常工作。

 "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
          }
        ]
      }
    }
  }
}

返回如下:

“原因”:{                 “ type”:“ illegal_argument_exception”,                 “ reason”:“默认情况下,文本字段上的字段数据是禁用的。在[category_name]上设置fielddata = true可以通过反转索引来将字段数据加载到内存中。请注意,这可能会占用大量内存。 。”

1 个答案:

答案 0 :(得分:1)

您需要使用.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
          }
        ]
      }
    }
  }
}