elasticsearch—是否可以从筛选器在elasticsearch中添加数据?

fnatzsnv  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(392)

我有一个ElasticSearch支持的api。根据登录/密码自动应用不同的过滤器。ElasticSearch索引包含:

"organisation.id"
  "organisation.name"
  "organisation.country"
  "shop.id"
  "shop.name"
  "shop.address"
  "creationdatetime"

这将是一个示例过滤器:

{
   "_source":{
    "includes":[
      "organisation.id",
      "organisation.name",
      "shop.id",
      "shop.name"
      "creationdatetime" 
   ],
    "excludes":  [
      "shop.address",
      "organisation.country"
    ]
   },
   "from":"0",
   "size":"500",
   "sort":{"creationdatetime":"asc"},
   "query":{
      "bool":{
         "must":{
            "match":{
               "shop.sharedwith":"client1"
            }
         },   
         "filter":{
            "range":{
               "creationdatetime":{
                  "gte":"2020-01-01"
               }
            }
         }
      }
   }
 }

输出将是

{
"total": 2,
"from": "0",
"size": "10",
"hops": [
    {

        "organisation": [
            {
                "name": "A1",
                "id": "0001-A1"
            }
        ],
        "shop": [
            {
                "name": "A1Shop",
                "id": "0001-0001-A1"
            }
        ]
    }
]

}
我想添加一个“版本”和“过滤器名”的输出。。。来自过滤器本身。正是这样:

{
    "total": 2,
    "from": "0",
    "size": "10",
    "version": "1.0.0.0",      // -------------------------------NEW FIELD
    "filtername": "filter01",  // -------------------------------NEW FIELD
    "hops": [
        {

            "organisation": [
                {
                    "name": "A1",
                    "id": "0001-A1"
                }
            ],
            "shop": [
                {
                    "name": "A1Shop",
                    "id": "0001-0001-A1"
                }
            ]
        }
    ]
}

是否可以从过滤器本身添加这两个额外的输出?

vlurs2pr

vlurs2pr1#

这是不可能的,但是有一个解决方法使用 top_hits 聚合与agg元数据结合使用:

GET _search
{
  "size": 0,                        // no need for the standard hits b/c of our `top_hits`
  "query": {
    "match_all": {}                 // your actual query
  }, 
  "aggs": {
    "my_hits": {
      "top_hits": {
        "size": 10,
        "_source": {
          "includes": [
            "organisation.id",
            "organisation.name",
            "shop.id",
            "shop.name",
            "creationdatetime"
          ],
          "excludes": [
            "shop.address",
            "organisation.country"
          ]
        }
      },
      "meta": {                    // custom key-value pairs
        "version": "1.0.0.0",
        "filtername": "filter01"
      }
    }
  }
}

导致

{
  ...
  "aggregations": {
    "my_hits": {
      "meta": {
        "version": "1.0.0.0",
        "filtername": "filter01"
      },
      "hits": {
        ...                       // the actual docs  
      }
    }
  }
}

尽管命名查询在这里的应用非常松散,但它也值得一看。

相关问题