如何访问elasticsearch嵌套对象

时间:2016-03-04 02:04:22

标签: elasticsearch

我试图让我的脑袋围绕嵌套查询,但我不能让它工作。 ES中有2个项目看起来像这样

 {
"_index": "catalog",
"_type": "products",
"_source": {
   "product": {
      "ean": "abc",
      "features": {
         "Product Type": "DVD player",
      },
      "color": "Black",
      "manufacturer": "Sony",
      "sitedetails": [
         {
            "name": "amazon.com",
            "sku": "zzz",
            "url": "http://www.amazon.com/dp/zzz"
         }
      ],
      "category": "Portable DVD Players"
   }
 }
},
{
"_index": "catalog",
"_type": "products",
"_source": {
   "product": {
      "ean": "def",
      "features": {
         "Product Type": "MP3 player",
      },
      "color": "Black",
      "manufacturer": "LG",
      "sitedetails": [
         {
            "name": "amazon.com",
            "sku": "aaa",
            "url": "http://www.amazon.com/dp/aaa"
         }
      ],
      "category": "MP3 Players"
   }
}

}

2个问题:

  • 获得sku = zzz的卷曲是什么?
  • 在搜索"玩家"这两个项目的卷曲是什么?
  • TNX!

    2 个答案:

    答案 0 :(得分:1)

    Heyy兄弟,让我们做一些魔术。 首先,您需要一个包含嵌套对象的映射,如此

    curl -XPUT "http://192.168.99.100:9200/catalog" -d'
    {
          "mappings": {
             "products": {
                "properties": {
                   "product": {
                      "type": "nested",
                      "properties": {
                          "features": {
                            "type":"nested"  
                          },
                         "sitedetails": {
                            "type": "nested"
                         }
                      }
                   }
                }
             }
       }
    }'
    

    之后,让我们插入您的数据(将您的产品类型更改为product_type)

     curl -XPOST "http://192.168.99.100:9200/catalog/products" -d'
    {
        "product": {
          "ean": "abc",
          "features": {
             "product_type": "DVD player"
          },
          "color": "Black",
          "manufacturer": "Sony",
          "sitedetails": [
             {
                "name": "amazon.com",
                "sku": "zzz",
                "url": "http://www.amazon.com/dp/zzz"
             }
          ],
          "category": "Portable DVD Players"
       }
    }'
    

    现在,让我们进行查询

    curl -XPOST "http://192.168.99.100:9200/catalog/products/_search" -d'
    {
       "query": {
          "bool": {
             "must": [
                {
                   "nested": {
                      "path": "product.features",
                      "query": {
                         "match": {
                            "product.features.product_type": "player"
                         }
                      }
                   }
                },
                {
                   "nested": {
                      "path": "product.sitedetails",
                      "query": {
                         "match": {
                            "product.sitedetails.sku": "zzz"
                         }
                      }
                   }
                }
             ]
          }
       }
    }'
    

    响应将是:

      "hits": {
          "total": 1,
          "max_score": 1.4054651,
          "hits": [
             {
                "_index": "catalog",
                "_type": "products",
                "_id": "AVM_fcYgvVoSi3OfqPTX",
                "_score": 1.4054651,
                "_source": {
                   "product": {
                      "ean": "abc",
                      "features": {
                         "Product Type": "DVD player"
                      },
                      "color": "Black",
                      "manufacturer": "Sony",
                      "sitedetails": [
                         {
                            "name": "amazon.com",
                            "sku": "zzz",
                            "url": "http://www.amazon.com/dp/zzz"
                         }
                      ],
                      "category": "Portable DVD Players"
                   }
                }
             }
          ]
       }
    

    希望有所帮助:D

    答案 1 :(得分:-2)

    使用:

    1. curl 'http://localhost:9200/catalog/products/_search?q=sku:"zzz"&pretty=true'
    2. curl 'http://localhost:9200/catalog/products/_search?q=sku:*&pretty=true'。就像我的想法一样,你想在sku中获取数据:" zzz"和sku:" aaa"。
    3. 的Referer: http://joelabrahamsson.com/elasticsearch-101/

      http://www.elasticsearchtutorial.com/elasticsearch-in-5-minutes.html