首页 > 解决方案 > 在弹性搜索中存储布尔值:优化?

问题描述

我有 json 文档,其中包含以下条目:

......

{

"Fieldname" : "booked",

"Fieldvalue" : "yes"

}

...

在 json 文档中,有很多这样的字段,其中布尔值是使用 Fieldname 和 Fieldvalue 间接提及的:本质上它表示booked=true。在将 json 存储到 elasticsearch 之前转换它会更有效吗?即用以下内容替换上述内容:

{

"booked" : true

}

? 搜索用例是我想在添加另一个json之前弄清楚系统中是否已经存在类似的json。

标签: jsonelasticsearchboolean

解决方案


是的,后一种是存储搜索目的的更清洁的方式。假设您想从索引中获取所有预订的属性,那么您可以轻松地这样做,而不是使用额外的FieldnameFieldvalue

GET /properties/_search
{
  "size": 10, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "country_code.keyword": "US"
          }
        },
        {
          "match": {
            "booked": true
          }
        },
        {
          "range": {
            "usd_price": {
              "gte": 50,
              "lte": 100000
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "ranking": {
        "order": "desc"
      }
    }
  ],
  "_source": [
    "property_id",
    "property_name",
    "country",
    "country_code",
    "state",
    "state_abbr",
    "city"

  ]
}

推荐阅读