[英]Is it better to query by type in ElasticSearch?
我的问题是关于表现 。 我经常使用过滤查询,我不确定按类型查询的正确方法是什么。
首先,让我们看一下映射 :
{
"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"
type_Light_Yellow
"color": "Yellow", "brightness" : "Light"
等所有其他类型。
现在,我的性能问题:我想知道是否有一种最喜欢的查询索引的方法。
例如,让我们在类型type_Light_Yellow
下搜索"details.FirstName": "John"
和"details.LastName": "Doe"
所有文档。
我正在使用的当前方法 :
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
因为它只是我文档下的另一个字段搜索。
另一种方法是直接在类型下查询:
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中的类型通过向文档添加_type属性来工作,每次搜索特定类型时,它都会自动按_type属性进行过滤。 因此,性能方面不应该有太大差异。 类型是抽象而不是实际数据。 我的意思是,跨多个文档类型的字段在整个索引上被展平,即一种类型的字段也占用其他类型的字段上的空间,即使它们没有被索引(想象它与null占用的方式相同)空间)。
但重要的是要记住过滤顺序会影响性能。您必须一次性排除尽可能多的文档。 因此,如果您认为最好不要先按类型进行过滤,那么首选过滤方式更为可取。 否则,如果订购相同,我认为不会有太大差别。
由于Python API也在默认设置中通过http查询,因此使用Python不应影响性能。
在这种情况下,虽然在_type元字段和颜色字段中都捕获了颜色,但在某种程度上是数据重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.