问题描述
我正在尝试使用query_string编写查询以检索嵌套对象的数据查询.
I am trying to write a query using query_string to retrieve data querying by nested objects.
我要执行的查询示例如下:
An example of query I would like to do is this one:
{
"query": {
"query_string": {
"query": "a.id:2"
}
}
}
其中"a"是嵌套对象,"id"是"a"的字段.
Where "a" is a nested object, and "id" is a field of "a".
我知道我可以使用嵌套查询成功地执行此任务,编写如下查询:
I know I can successfully perform this task using using a nested query, writing a query like:
{
"nested": {
"path": "a"
"query_string": {
"query": "a.id:2"
}
}
}
但是,我想避免这种情况.我不想自己弄清楚用户正在搜索嵌套字段并修改查询.我尝试使用"fields"参数,但它似乎不适用于嵌套对象.
However, I would like to avoid it. I don't want to figure out by myself that the user is searching for a nested field and modify the query.I tried to use the "fields" parameter, but it looks like it doesn't work with nested objects.
是否可以使用"query_string"查询直接编写此查询?有可能获得什么语义?(例如,如果我写"a.id:2 AND a.b:10",我是在匹配同一对象还是不同对象中的两个字段?)
Is it possible to write this query directly using "query_string" queries?What semantic is it possible to obtain? (for instance, if I write "a.id:2 AND a.b:10" I am matching the two fields in the same object or in different objects?)
推荐答案
我进行了更多研究,发现可以通过在映射中将include_in_parent设置为true来实现.
I was doing more research and found this can be achieved by setting the include_in_parent setting to true in the mapping.
现在您应该可以进行
{
"query": {
"query_string": {
"query": "fields.fieldvalue:sometext"
}
}
}
例如:-
"mappings": {
"documentmetadatavalue": {
"properties": {
"id": {
"type": "string"
},
"fields": {
"type": "nested",
"include_in_parent": true,
"properties": {
"fieldId": {"type": "string"},
"fieldvalue": {"type": "string"}
}
}
}
}
}
让我知道是否有帮助.
这篇关于Elasticsearch query_string嵌套查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!