Elasticsearchで、マルチレベルのネストされたオブジェクトから複数のフィールドの文字列を検索するにはどうすればよいですか?

ビマラ

Elasticsearch 6には、次のようなネストされたオブジェクトを含むデータがあります。

{
    "brands" : 
    [
        {
            "brand_name" : "xyz",
            "products" : 
            [
               {
                   "title" : "test",
                   "mrp" : 100,
                   "sp" : 90,
                   "status" : 1
               },
               {
                   "title" : "test1",
                   "mrp" : 50,
                   "sp" : 45,
                   "status" : 1
                }
            ]
        },
        {
            "brand_name" : "aaa",
            "products" : 
            [
                {
                    "title" : "xyz",
                    "mrp" : 100,
                    "sp" : 90,
                    "status" : 1
                },
                {
                    "title" : "abc",
                    "mrp" : 50,
                    "sp" : 45,
                    "status" : 1
                }
            ]
        }
    ]
}

フィールドbrand_nameまたはフィールドタイトルのいずれかから検索したい。そして、すべての結果を同じinner_hitsで返したいです。

例:検索文字列を「xyz」と入力すると、両方のブランドオブジェクトと対応する製品オブジェクトが返されます。検索文字列を「test」として入力すると、最初の製品オブジェクトのみを含む最初のブランド配列のみが返されます。

どうすればこれを達成できますか。何か案は?

私は次のようなネストされたパスクエリを試しました:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "brands",
            "query": {
              "bool": {
                "should": [
                  {
                    "term": {
                      "brands.brand_name": "xyz"
                    }
                  },
                  {
                    "term": {
                      "brands.brand_name.keyword": "aaa"
                    }
                  },
                  {
                    "nested": {
                      "path": "brands.products",
                      "query": {
                        "bool": {
                          "should": [
                            {
                              "match": {
                                "brands.products.title": "xyz"
                              }
                            }
                          ]
                        }
                      },
                      "inner_hits": {}
                    }
                  }
                ]
              }
            },
            "inner_hits": {}
          }
        }
      ]
    }
  }
}

ただし、このクエリは、ブランドごとおよび製品ごとに複数の配列オブジェクトを含む複数のinner_hits応答で返されます。

文字列と一致するすべてのブランド名が1つの配列の下にリストされ、すべての製品が同じinner_hitsの下の別の配列の下にリストされるような応答が必要です。

Opster ES Ninja Nishant

一致が発生した場所、つまりbrands.brand_nameまたはbrands.products.title基づいて内部ヒットを異なるものにしたいので、独立したネストされたクエリとして、ブランド名と製品タイトルの2つのクエリを作成できます。これらのクエリshouldは、boolクエリの内部句である必要があります。ネストされた各クエリにはinner_hits、次のように独自のクエリが必要です。

{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "brands",
            "inner_hits": {},
            "query": {
              "term": {
                "brands.brand_name.keyword": "test"
              }
            }
          }
        },
        {
          "nested": {
            "path": "brands.products",
            "inner_hits": {},
            "query": {
              "term": {
                "brands.products.title": "test"
              }
            }
          }
        }
      ]
    }
  },
  "_source": false
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ