elasticsearch - 搜索数组中的哈希

标签 elasticsearch

我在ES中有一个像这样的对象:

{
  _index: products_development_20141007185853021
  _type: product
  _id: 5039
  _version: 1
  _score: 1
  _source: {
    name: Le Moelleux - Gâ teau tout Chocolat
    quantity: 500
    quantity_unit: gram
    store_ids: [
      503
      504
      505
    ]
    name_suggest: Le Moelleux - Gâ teau tout Chocolat
    store_prices: [{
      id: 503
      price: 2.65
    } {
      id: 504
      price: 2.65
    } {
      id: 505
      price: 2.65
    } ]
    product_categories: [{
      id: 109
      name: Viennoiserie
      parent: {
        id: 105
        name: Pain,
        viennoiserie,
        biscotte
        parent_id: 92
        tags: [
          pains et viennoiseries
          biscotte
          tartine
          biscottes tartines
          boulangerie
          pâ tisseries moelleuses
        ]
      }
    }]
    product_brand: {
      id: 1134
      name: KER CADELAC
      type: null
    }
    store_company: {
      id: 4
      name: Chronodrive
    }
    categories_and_ancestors: [{
      id: 109
    } {
      id: 105
    } {
      id: 92
    }]
  }
}

使用此映射:
mappings: {
  product: {
    properties: {
      item_count: {
        type: integer
      }
      name_suggest: {
        search_analyzer: whitespace_analyzer
        index_analyzer: nGram_analyzer
        type: string
      }
      store_company: {
        properties: {
          name: {
            type: string
          }
          id: {
            type: long
          }
        }
      }
      quantity_unit: {
        index: not_analyzed
        type: string
      }
      quantity: {
        type: double
      }
      store_ids: {
        type: string
      }
      store_prices: {
        properties: {
          price: {
            type: double
          }
          id: {
            type: integer
          }
        }
      }
      categories_and_ancestors: {
        properties: {
          id: {
            type: integer
          }
        }
      }
      product_categories: {
        properties: {
          parent: {
            properties: {
              parent_id: {
                type: long
              }
              name: {
                type: string
              }
              id: {
                type: long
              }
              tags: {
                type: string
              }
            }
          }
          name: {
            type: string
          }
          id: {
            type: integer
          }
        }
      }
      name: {
        analyzer: product_analyzer
        type: string
      }
      product_brand: {
        properties: {
          name: {
            type: string
            fields: {
              raw: {
                index: not_analyzed
                type: string
              }
            }
          }
          id: {
            type: integer
          }
          type: {
            type: string
          }
        }
      }
    }
  }
}

我如何发出请求或筛选以获取store_prices没有的所有文档:
{
  id: 503
  price: 0
}

我的意思是完整的对象。我想在ES中翻译此查询:
select from products where store_prices does not include { id: 503, price: 0 }

谢谢

最佳答案

如果您希望元组{id:503,price:0}不包含在该元组数组中(并匹配整个元组,而不是一个元组的“id”和另一个元组的“price”),则不能。您需要嵌套的对象。对此的最好解释是here, in the documentation

为此,您需要以下商店价格映射:

"store_prices": {
  "type": "nested", 
  "properties": {
    "price": {
      "type": "double"
    },
    "id": {
      "type": "integer"
    }
  }
}

要查询(过滤)上述映射,您可以使用以下方法:
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "nested": {
                "path": "store_prices",
                "query": {
                  "bool": {
                    "must": [
                      { "match": {"store_prices.id": "503"}},
                      { "match": {"store_prices.price": "2.65"}}
                    ]
                  }}}}
          ]}}}
  }
}

关于elasticsearch - 搜索数组中的哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26254220/

相关文章:

java - ElasticSearch 全文搜索

elasticsearch - Elasticsearch 中实时数据的最佳设置

docker - Kibana 没有连接到 elasticsearch docker

elasticsearch - 排序时,Elasticsearch 5会做什么?

elasticsearch - 如何将pattern_replace char过滤器与同义词过滤器结合使用(跳过同义词?)

elasticsearch - 如何使用Elasticsearch 5.5.1为文件建立索引

node.js - Elasticsearch 中的连接池

elasticsearch - Elasticsearch :如何 'OR'两个不同的嵌套查询?

elasticsearch - 如何找到距输入位置一定范围内的位置?

mongodb - 从Elasticsearch河进口中排除mongodb字段