簡體   English   中英

Elasticsearch基於2個標准返回文檔,其中一個基於另一個

[英]Elasticsearch to return documents based on 2 criteria where one is based on the other

我有以下格式的文件:

{
   "id": number
   "chefId: number
   "name": String,
   "ingredients": List<String>,
   "isSpecial": boolean
}

這是一份包含5個文件的清單:

{
   "id": 1,
   "chefId": 1,
   "name": "Roasted Potatoes",
   "ingredients": ["Potato", "Onion", "Oil", "Salt"],
   "isSpecial": false
},
{
   "id": 2,
   "chefId": 1,
   "name": "Dauphinoise potatoes",
   "ingredients": ["Potato", "Garlic", "Cream", "Salt"],
   "isSpecial": true
},
{
   "id": 3,
   "chefId": 2,
   "name": "Boiled Potatoes",
   "ingredients": ["Potato", "Salt"],
   "isSpecial": true
},
{
   "id": 4,
   "chefId": 3
   "name": "Mashed Potatoes",
   "ingredients": ["Potato", "Butter", "Milk"],
   "isSpecial": false
},
{
   "id": 5,
   "chefId": 4
   "name": "Hash Browns",
   "ingredients": ["Potato", "Onion", "Egg"],
   "isSpecial": false
}

我將進行搜索,其中“Potatoes”包含在名稱字段中。 像這樣:

{
  "query": {
    "wildcard": {
      "status": {
        "value": "*Potatoes*"
      }
    }
  }
}

但是我還想在返回文檔時添加一些額外的標准:

  • 如果配料中含有洋蔥或牛奶,請退回文件。 因此將返回id為1和4的文檔。 請注意,這意味着我們返回了廚師ID為1和3的文檔。
  • 然后,對於我們尚未獲得具有相同廚師ID的其他文檔的文檔,返回isSpecial標志設置為true的位置。 因此,只返回文檔3。 2將不會被退回,因為我們已經有一個文件,其中廚師ID等於1。

是否有可能在Elasticsearch中進行這種鏈接? 我希望能夠在單個查詢中執行此操作,以便我可以避免向我的(Java)代碼添加邏輯。

您不能在一個elasticsearch查詢中擁有這種邏輯。 您可以使用aggregations / post_filter進行棘手的查詢,以便在一個查詢中獲得所需的所有數據,然后在Java應用程序中對其進行轉換。 但是最好的方法(並且更易於維護)是有兩個查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM