【问题标题】:Terms query(ElasticSearch) not working for some field values条款查询(ElasticSearch)不适用于某些字段值
【发布时间】:2019-07-10 12:43:49
【问题描述】:

这是我的映射,我已经将我的必填字段声明为关键字,但我的条款查询不适用于 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", “原因”:“默认情况下,在文本字段上禁用字段数据。在 [category_name] 上设置 fielddata=true 以便通过反转倒排索引将字段数据加载到内存中。请注意,这可能会占用大量内存。或者使用关键字字段代替。”

【问题讨论】:

    标签: elasticsearch


    【解决方案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
              }
            ]
          }
        }
      }
    }
    

    【讨论】:

    • 感谢@Val,你拯救了我的一天 :)
    猜你喜欢
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2020-10-21
    • 2022-01-09
    • 2019-03-06
    • 1970-01-01
    相关资源
    最近更新 更多