布尔AND与Elasticsearch中的完全匹配

时间:2017-03-16 10:46:56

标签: elasticsearch

在我们的Elasticsearch产品系列中,我们有一系列哈希,称为“营养素”。数据的部分示例是:

"_source": {
  "quantity": "150.0",
  "id": 1001,
  "barcode": "7610809001066",
  "nutrients": [
    {
      "per_hundred": "1010.0",
      "name_fr": "Énergie",
      "per_portion": "758.0",
      "name_de": "Energie",
      "per_day": "9.0",
      "name_it": "Energia",
      "name_en": "Energy"
    },
    {
      "per_hundred": "242.0",
      "name_fr": "Énergie (kCal)",
      "per_portion": "181.0",
      "name_de": "Energie (kCal)",
      "per_day": "9.0",
      "name_it": "Energia (kCal)",
      "name_en": "Energy (kCal)"
    },
    {
      "per_hundred": "18.0",
      "name_fr": "Matières grasses",
      "per_portion": "13.5",
      "name_de": "Fett",
      "per_day": "19.0",
      "name_it": "Grassi",
      "name_en": "Fat"
    },

在搜索中,我们尝试根据营养素数组中包含的两个字段的完全匹配来恢复产品。我发现的是条件似乎是OR而不是AND。

这两次尝试都是:

   "query": {
      "bool": {
          "must": [
              { "match": { "nutrients.name_fr": "Énergie" } },
              { "match": { "nutrients.per_hundred": "242.0" } }
          ]
        }
    }
  }

  "query": {
    "filtered": {
      "filter": {
        "and": [
          { "term": { "nutrients.name_fr": "Énergie" } },
          { "term": { "nutrients.per_hundred": "242.0" } }
        ]
      }
    }
  }

这两个实际上都带回了Énergie和242.0的条目,但也匹配不同的name_fr,例如:

    {
      "per_hundred": "242.0",
      "name_fr": "Acide folique",
      "per_portion": "96.0",
      "name_de": "Folsäure",
      "per_day": "48.0",
      "name_it": "Acido folico",
      "name_en": "Folic acid"
    },

它们也匹配非完全匹配,即:当我们想要仅匹配“Énergie”时,匹配“Énergie(kCal)”

1 个答案:

答案 0 :(得分:1)

关于你的第一个问题: 你必须使营养素字段嵌套,这样你就可以查询其中的每个对象Elasticsearch Nested Objects