![](/img/trans.png)
[英]How to remove specific word from a field in Elasticsearch Document with Painless Script
[英]How to specify or target a field from a specific document type in queries or filters in Elasticsearch?
鉴于:
问题:
我想构建一个返回两种类型结果的查询,但“产品”类型的文档允许有标签“X”和“Y”,而“类别”类型的文档只允许有标签“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" ]
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.