在elasticsearch中搜索嵌套构面的值

时间:2014-03-07 13:34:47

标签: elasticsearch nested facet elastica

这代表我的映射:

{
    "name": {"type": "string", "include_in_all": true},
    "properties": {
    "type": "nested",
    "properties": {
        "name": {"type": "string"},
        "value": {"type": "string"}
    }
}

如何使用facetted搜索'properties.value'的值?这是一个示例文档:

{
    "name": "Testproduct",
    "properties": [{
        "name": "Color",
        "value": "Green"
    }, {
        "name": "Size",
        "value": "M"
    }]
}

我想通过“Color”属性构建一个facetted列表(仅限)。所以结果应该是这样的:

Red: 7 times
Green: 5 times
Blue: 1 times

这是我到目前为止所尝试的:

{
    "size": 1000,
    "query": {
        "query_string": {
            "query": " ... ",
            "default_operator": "AND"
        }
    },
    "facets": {
        "resolution": {
            "nested": "properties",
            "facet_filter": {
                "term": {
                    "name": "Color"
                }
            },
            "terms_stats": {
                "key_field": "name",
                "value_field": "value"
            }
        }
    }
}

如果我执行此搜索查询,我会收到以下回复:

FacetPhaseExecutionException[Facet [resolution]: value_field [value] isn't a number field, but a string];

我几乎不理解答案,但我不知道我的概念是做错了什么。

1 个答案:

答案 0 :(得分:1)

那是因为您使用的是terms_statsterm_stats适用于numbers。您应该使用terms。请尝试此查询:

{
  "size": 1000,
  "query": {
    "query_string": {
      "query": " ... ",
      "default_operator": "AND"
    }
  },
  "facets": {
    "resolution": {
      "nested": "properties",
      "facet_filter": {
        "term": {
          "name": "Color"
        }
      },
      "terms": {
        "field": "properties.value"
      }
    }
  }
}