给出:
问题:
我想建立一个查询,返回两种类型的结果,但是类型为“product”的文档允许使用标签“X”和“Y”,类型为“category”的文档只能使用标签“Z” '。我该如何实现?看来我不能使用product.tags和category.tags,因为ES会查找文档的product / category字段,这不是我想要的。
注意:
虽然对于上面的示例可能有某种解决方法,但我正在寻找一种在编写查询时定位或指定特定文档类型的字段的通用方法。我基本上想对查询中使用的字段名称进行“命名空间”,因此仅考虑要使用的类型的文档。
最佳答案
我认为字段别名将是您的最佳选择,但这是不可能的。
相反,您可以使用“copy_to”,但它可能会影响索引大小:
DELETE /test
PUT /test
{
"mappings": {
"product" : {
"properties": {
"tags": { "type": "string", "copy_to": "ptags" },
"ptags": { "type": "string" }
}
},
"category" : {
"properties": {
"tags": { "type": "string", "copy_to": "ctags" },
"ctags": { "type": "string" }
}
}
}
}
PUT /test/product/1
{ "tags":"X" }
PUT /test/product/2
{ "tags":"Y" }
PUT /test/category/1
{ "tags":"Z" }
您可以查询一个或多个字段:
GET /test/product,category/_search
{
"query": {
"term": {
"ptags": {
"value": "x"
}
}
}
}
GET /test/product,category/_search
{
"query": {
"multi_match": {
"query": "x",
"fields": [ "ctags", "ptags" ]
}
}
}
关于elasticsearch - 如何在Elasticsearch的查询或过滤器中从特定文档类型指定或定位字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36911358/