弹性搜索聚合字段不起作用..?

时间:2016-12-26 11:01:01

标签: python elasticsearch

我使用elasticsearch python API索引elasticsearch索引上的JSON对象。

我无法在文档后面使用多令牌字段获得预期结果。

我在身体映射代码下方提供:

body={"mappings":{
        "my_doc": {
            "properties": {
                "id": {
                    "properties": {
                        "name": {"type": "string"},
                        "value": {"type": "keyword"}
                    }
                },
                "name": {
                    "properties": {
                        "first": {"type": "text"},
                        "last": {"type": "text"}
                    }
                },
                "location": {
                    "properties": {
                        "city": {"type": "text"},
                        "state": {
                                "type": "text",
                                "fielddata": True,
                                "fields": {
                                    "raw": {
                                        "type": "string",
                                        "index": "not_analyzed"
                                    }
                                }
                        },
                        "street": {"type": "text"},
                        "postcode": {"type": "text"}
                    }
                }
            }
        }
    }
}

当我使用此aqg查询时:

curl -XGET localhost:9200/my_index/_search -d '{
"size": 0,
"aggs": {
    "locations": {
        "terms": {
            "field": "location.state"
        }
    }
  }
}'

我获得了代币,好的 但是,当我使用原始字段时:

curl -XGET localhost:9200/my_index/_search -d '{
"size": 0,
"aggs": {
    "locations": {
        "terms": {
            "field": "location.state.raw"
        }
    }
  }
}'

我没有获得桶。

有什么建议吗?

Lax的建议后的最终映射:

body={"mappings":{
            "my_doc": {
                "properties": {
                    "id": {
                        "properties": {
                            "name": {"type": "string"},
                            "value": {"type": "keyword"}
                        }
                    },
                    "name": {
                        "properties": {
                            "first": {"type": "text"},
                            "last": {"type": "text"}
                        }
                    },
                    "location": {
                        "properties": {
                            "city": {"type": "text"},
                            "state": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword"
                                        }
                                    }
                            },
                            "street": {"type": "text"},
                            "postcode": {"type": "text"}
                        }
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

在该映射中删除

"index": "not_analyzed

它会起作用