ElasticSearch筛选器嵌套对象

0mkxixxg  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(185)

我有一个带有嵌套对象的索引,其中包含两个属性,即scope eIDCategory Name。以下是索引的Map部分

"mappedCategories" : {
      "type" : "nested",
      "properties": {
        "scopeId": {"type":"long"},
        "categoryName": {"type":"text",
          "analyzer" : "productSearchAnalyzer",
          "search_analyzer" : "productSearchQueryAnalyzer"}
      }

    }

包含嵌套mappdCategories对象的示例文档如下所示:

POST productsearchna_2/_doc/1
{
          "categoryName" : "Operating Systems",
          "contexts" : [
            0
          ],
          "countryCode" : "US",
          "id" : "10076327-1",
          "languageCode" : "EN",
          "localeId" : 1,
          "mfgpartno" : "test123",
          "manufacturerName" : "Hewlett Packard Enterprise",
          "productDescription" : "HPE Microsoft Windows 2000 Datacenter Server - Complete Product - Complete Product - 1 Server - Standard",
          "productId" : 10076327,
          "skus" : [ 
            {"sku": "43233004",
              "skuName": "UNSPSC"},
            {"sku": "43233049",
              "skuName": "SP Richards"},
            {"sku": "43234949",
              "skuName": "Ingram Micro"}
          ],
          "mappedCategories" : [ 
            {"scopeId": 3228552,
              "categoryName": "Laminate Bookcases"},
            {"scopeId": 3228553,
              "categoryName": "Bookcases"},
            {"scopeId": 3228554,
              "categoryName": "Laptop"}
          ]
 }

我想在Scope ID:3228553上筛选Category Name“LAP”,即我的查询应该返回0个匹配项,因为Laptop被Map到Scope ID 3228554。但我的以下查询返回1个作用域ID为3228554的匹配项

POST productsearchna_2/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "mappedCategories",
            "query": {
              "term": {
                "mappedCategories.categoryName": "lap"
              }
            },
            "inner_hits": {}
          }
        }
      ],
      "filter": [
        {
          "nested": {
            "path": "mappedCategories",
            "query": {
              "term": {
                "mappedCategories.scopeId": {
                  "value": 3228552
                }
              }
            }
          }
        }
      ]
    }
  },
  "_source": ["mappedCategories.categoryName", "productId"]
}

以下是查询结果的一部分:

"inner_hits" : {
          "mappedCategories" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.5586993,
              "hits" : [
                {
                  "_index" : "productsearchna_2",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "mappedCategories",
                    "offset" : 2
                  },
                  "_score" : 1.5586993,
                  "_source" : {
                    "scopeId" : 3228554,
                    "categoryName" : "Laptop"
                  }
                }
              ]
            }
          }

我希望我的查询返回零个匹配项,如果我搜索范围为3228552的“book”,我希望我的查询返回两个匹配项,一个是书架,另一个是层压书架类别名称。请帮帮忙。

ybzsozfc

ybzsozfc1#

这个查询解决了部分问题,但是当搜索Scope ID为3228552的book“时,它将只得到1个结果。

GET idx_test/_search?filter_path=hits.hits.inner_hits
{
  "query": {
    "nested": {
      "path": "mappedCategories",
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "mappedCategories.scopeId": {
                  "value": 3228553
                }
              }
            }
          ],
          "must": [
            {
              "match": {
                "mappedCategories.categoryName": "laptop"
              }
            }
          ]
        }
      },
      "inner_hits": {}
    }
  }
}

相关问题