【发布时间】:2016-10-25 18:11:50
【问题描述】:
我的问题是关于性能。 我经常使用过滤查询,但我不确定按类型查询的正确方法是什么。
首先,让我们看一下映射:
{
"my_index": {
"mappings": {
"type_Light_Yellow": {
"properties": {
"color_type": {
"properties": {
"color": {
"type": "string",
"index": "not_analyzed"
},
"brightness": {
"type": "string",
"index": "not_analyzed"
}
}
},
"details": {
"properties": {
"FirstName": {
"type": "string",
"index": "not_analyzed"
},
"LastName": {
"type": "string",
"index": "not_analyzed"
},
.
.
.
}
}
}
}
}
}
}
在上面,我们可以看到 浅黄色 类型的一个映射示例。此外,还有更多针对各种类型的映射(颜色。例如:深黄色、浅棕色等...)
请注意color_type 的子字段。
对于 type_Light_Yellow 类型,值始终为:"color": "Yellow", "brightness" : "Light",对于所有其他类型,依此类推。
现在,我的性能问题:我想知道是否有最喜欢的方法来查询我的索引。
例如,让我们搜索 "details.FirstName": "John" 和 "details.LastName": "Doe" 在 type_Light_Yellow 下的所有文档。
当前方法我正在使用:
curl -XPOST 'http://somedomain.com:1234my_index/_search' -d '{
"query":{
"filtered":{
"filter":{
"bool":{
"must":[
{
"term":{
"color_type.color": "Yellow"
}
},
{
"term":{
"color_type.brightness": "Light"
}
},
{
"term":{
"details.FirstName": "John"
}
},
{
"term":{
"details.LastName": "Doe"
}
}
]
}
}
}
}
}'
如上所示,通过定义
"color_type.color": "Yellow" 和 "color_type.brightness": "Light",我正在查询所有索引和引用类型 type_Light_Yellow,因为它只是我正在搜索的文档下的另一个字段。
替代方法是直接在type下查询:
curl -XPOST 'http://somedomain.com:1234my_index/type_Light_Yellow/_search' -d '{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"details.FirstName": "John"
}
},
{
"term": {
"details.LastName": "Doe"
}
}
]
}
}
}
}
}'
请注意第一行:my_index/type_Light_Yellow/_search。
- 从性能方面来说,查询的效率会更高吗?
- 如果我通过代码查询(我正在使用带有 ElasticSearch 包的 Python),会不会是一个不同的答案?
【问题讨论】:
标签: python performance elasticsearch query-performance