关于弹性搜索嵌套过滤器

时间:2014-08-21 05:46:18

标签: elasticsearch spring-data-elasticsearch elasticsearch-java-api

我有以下映射,

{
  "wms": {
    "mappings": {
      "item": {
        "properties": {
          "barcode": {
            "type": "string"
          },
          "comments": {
            "type": "string"
          },
          "id": {
            "type": "long"
          },
          "sku": {
            "type": "nested",
            "properties": {
              "brandName": {
                "type": "string"
              },
              "code": {
                "type": "string"
              },
              "id": {
                "type": "long"
              }
            }
          }
        }
      }
    }
  }
}

及以下数据。

{
  "_index": "wms",
  "_type": "item",
  "_id": "100006868381",
  "_score": 1.0,
  "_source": {
    "id": 100006868381,
    "createdBy": null,
    "createdOn": null,
    "lastModifiedOn": null,
    "barcode": "100006868381",
    "sku": {
      "id": 396829,
      "createdBy": null,
      "createdOn": null,
      "lastModifiedOn": null,
      "name": null,
      "code": "KIRAHDBG00598",
      "lastUser": null,
      "adminDisabled": null,
      "description": null,
      "vendorArticleNo": null,
      "vendorArticleName": null,
      "size": null,
      "brandId": null,
      "brandName": "KIARA",
      "brandCode": null,
      "articleTypeId": null,
      "articleTypeName": null,
      "articleTypeCode": null,
      "remarks": null,
      "jitSourced": null,
      "gtin": null,
      "enabled": null,
      "version": null
    },
    "quality": null,
    "itemStatus": null,
    "warehouseId": null,
    "enabled": null,
    "poId": null,
    "poBarcode": null,
    "comments": "Created for PO: RNSI233380213-00Thu Aug 21 10:40:36 IST 2014",
    "orderId": null,
    "bin": null,
    "lotId": null,
    "lotBarcode": null,
    "poSkuId": null,
    "grnSkuId": null,
    "grnBarcode": null,
    "inwardedOn": null,
    "rejectReason": null,
    "rejectReasonDescription": null,
    "cartonBarcode": null
  }
}

我使用spring-elasticsearch数据但没有存储库。我的意思是使用模板本身。 当我搜索条形码时,它工作正常。当我在sku.id

上运行嵌套查询时,一切似乎都没问题
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "nested": {
          "filter": {
            "term": {
              "sku.id": "396829"
            }
          },
          "path": "sku"
        }
      }
    }
  }
}

但是当我在sku.code

上搜索时,事情无效
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "nested": {
          "filter": {
            "term": {
              "sku.code": "KIRAHDBG00598"
            }
          },
          "path": "sku"
        }
      }
    }
  }
}

我正在使用以下代码进行搜索。

filterBuilder = FilterBuilders.termFilter(name, value);
if (name.contains(".")) {
    String firstPart = name.split("\\.")[0];
    return FilterBuilders.nestedFilter(firstPart, filterBuilder);
}
Pageable pageable = new PageRequest(0, 20);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filterBuilder))
        .withPageable(pageable)
        .build();
Page<E> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, this.entryClass);

你能说出我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

您的字段“sku.code” 已分析术语过滤器需要未分析字段。 这就是为什么它不起作用的原因。

对于你的长场,这没问题,因为数字字段不会遇到这个问题。

我建议您使用匹配查询(将匹配查询放在过滤器中没有问题),这适用于已分析的字段。

所以,这应该适合你。

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "nested": {
          "filter": {
            "query": {
              "match": {
                "code": "KIRAHDBG00598"
              }
            }
          },
          "path": "sku"
        }
      }
    }
  }
}

如果您不熟悉已分析未分析字段,建议您使用匹配 term <测试小示例/ strong>查询。