【问题标题】:ElasticSearch source filtering array of objectsElasticSearch 源过滤对象数组
【发布时间】:2019-08-10 03:31:01
【问题描述】:

这是一份文件

{
  "Id": "1",
  "Name": "Thing",
  "Prices": [
    {"CompanyId": "1", "Price": "11.11"},
    {"CompanyId": "2", "Price": "12.12"},
    {"CompanyId": "3", "Price": "13.13"}

这里是关联的 ElasticSearch 架构:

"Prices" : {
    "type" : "nested",
    "properties" : {
        "CompanyId": {
            "type" : "integer"
        },
        "Price" : {
            "type" : "scaled_float",
            "scaling_factor" : 100
        }
    }
}

如果用户购买CompantId = 3,那么供应商不希望他们能够看到CompanyId = 1 的优惠价格。

因此我需要使用源过滤器来删除所有CompanyId 不是3 的价格。

我发现这行得通。

"_source":{
  "excludes": ["Prices.companyId.CompanyId"]
}

但我不明白如何或为什么。 它不可能工作,因为在整个 ElasticSearch 搜索 JSON 中的任何地方都没有提到所需的 CompanyId

添加完整的搜索 JSON:

{
   "query":{
      "bool":{
         "must":[
            {
               "match_all":{

               }
            }
         ],
         "filter":{
            "match":{
               "PurchasingViews":6060
            }
         }
      }
   },
   "size":20,
   "aggs":{
      "CompanyName.raw":{
         "terms":{
            "field":"CompanyName.raw",
            "size":20,
            "order":{
               "_count":"desc"
            }
         }
      }
   },
   "_source":{
      "excludes":[
         "PurchasingViews",
         "ContractFilters",
         "SearchField*",
         "Keywords*",
         "Menus*",
         "Prices.companyId.CompanyId"
      ]
   }
}

结果:

{
   "took":224,
   "timed_out":false,
   "_shards":{
      "total":5,
      "successful":5,
      "skipped":0,
      "failed":0
   },
   "hits":{
      "total":1173525,
      "max_score":1.0,
      "hits":[
         {
            "_index":"products_purchasing",
            "_type":"product_purchasing",
            "_id":"12787114",
            "_score":1.0,
            "_source":{
               "CompanyName":"...",
               "Prices":[
                  {
                     "CompanyId":1474,
                     "Price":697.3
                  }
               ],
               "CompanyId":571057,
               "PartNumber":"...",
               "LongDescription_en":"...",
               "Name_en":"...",
               "DescriptionSnippet_en":"...",
               "ProductId":9605985,
               "Id":12787114
            }
         }
      ]
   },
   "aggregations":{
      "CompanyName.raw":{
         "doc_count_error_upper_bound":84,
         "sum_other_doc_count":21078,
         "buckets":[
            {
               "key":"...",
               "doc_count":534039
            }
         ]
      }
   }
}

【问题讨论】:

  • 你能显示你发送的查询和你看到的结果吗?
  • 你能把所有这些都添加到你的问题中吗,因为它更清晰,谢谢
  • 刚刚发现过滤器巧合地将结果减少到只有一个价格的文档。所以问题仍然存在:我如何编写一个源过滤器来删除 CompanyId 不是 3 的所有价格。

标签: elasticsearch elasticsearch-query


【解决方案1】:

https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

我相信您使用“嵌套”类型放置映射的方式已经创建了您所质疑的引用。

另外 - 我建议将查询设置为仅查找 3 而不是“排除”除 3 之外的所有内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多