我目前正在使用Elasticsearch,并且正在尝试构建一个搜索查询,以根据文档类型在不同的字段中查找关键字。
Example:
Index Name: index1
Document types:
- doc1 (_id, name, size, color, weight)
- doc2 (_id, name, duration, length, width, height)
现在,我想查询
index1
中的doc1
和doc2
类型的文档,但是在doc1
中,我只想在name
,size
和weight
列中搜索,而在doc2
中,我想在name
,length
和height
列中搜索。如何在单个查询中实现?
最佳答案
您可以按类型和名称引用这些字段,并使用 bool(boolean) 查询。下面是一个基本示例。
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"doc1.name": {
"value": "2"
}
}
},
{
"term": {
"doc1.size": {
"value": "23"
}
}
},
{
"term": {
"doc1.weight": {
"value": "52"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"doc2.name": {
"value": "2"
}
}
},
{
"term": {
"doc2.length": {
"value": "23"
}
}
},
{
"term": {
"doc2.height": {
"value": "52"
}
}
}
]
}
}
]
}
}
}
关于elasticsearch - 按文档类型查询特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21757535/