Search code examples
c#elasticsearchnest

Elasticsearch query match in array


id: 1,
name: "Jean Pantalon",
title: null,
subtitle: null,
description: null,
tags: null,
seoUrl: null,
clickCounter: 0,
model: null,
sku: null,
ean: null,
displayPrice: 0,
price: 0,
isActive: true,
isDeleted: false,
productPhotos: null,
productCategories: [
  {
    id: 1,
    productId: 1,
    categoryId: 2,
    category: {
       id: 2,
       name: "Spor",
       topCategoryId: 0,
       subCategories: null
       }
    },
 ]

Hello everyone, elasticseaarch has such a json yield, I want to filter it, for example the name in the productCategories te category Sports ones, how can I write this query

I'm using the c # ta nest library


Solution

  • I don't know about how to do it in your lib, but in elasticsearch:

    productCategories must be mapped with a "nested" data type. Then you will be able to construct a query like this:

    GET /my-index/_search
    {
      "query": {
        "nested": {
          "path": "productCategories",
          "query": {
            "bool": {
              "must": [
                { "match": { "productCategories.category.name": "sport" } }
              ]
            }
          }
        }
      }
    }