我试图弄清楚如何用elasticsearch查询对象中的特定字段。
我的索引文档如下所示:
{
name : 'thename'
meta : {
title : {value: 'foo is the title'}
headline: {value : 'bar is the headline'}
}
}
例如,我将如何针对
meta.title.value
创建查询?实际支持吗?
我可以在不指定键的情况下查询这些值:
{
query: 'foo'
}
并且我得到了正确的结果,但是如果我只想在meta对象中搜索该特定键,就无法解决该问题。
更具体地说,如果这有任何区别,我将
mongoose
与mongoosastic
一起使用。这是我在Elasticsearch上映射的文档:
"mappings": {
"asset": {
"properties": {
"kind": {
"type": "string"
},
"meta": {
"properties": {
"description": {
"properties": {
"value": {
"type": "string"
}
}
},
"headline": {
"properties": {
"value": {
"type": "string"
}
}
},
"visible": {
"type": "boolean"
}
}
}
}
}
}
最佳答案
通过特定字段查询的示例:
{
"query": {
"match" : {
"name" : "thename"
}
}
}
在上面的示例中,“名称”是您要查询的字段的名称。
对于嵌套数据(例如,meta.title),可以查看我在此主题https://stackoverflow.com/a/25203970/3917476中发布的“如何查询嵌套字段”一节或查看“嵌套查询” 和 “嵌套类型” 文档:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#query-dsl-nested-query。
但是,我发现您应该阅读ElasticSearch文档,因为按特定字段进行查询是最基本的事情(IMHO)之一。
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-queries.html
您可以创建许多类型的查询。尝试遍历它们中的大多数(首先,我建议您“匹配”,“术语”,“ bool(boolean) ”,“嵌套”,“模糊”和“通配符”)创建小示例。
编辑1
我对您有一个建议:不要创建名称为“value”的字段,否则我们将不得不创建无用的嵌套查询。
这是您的映射建议:
"mappings": {
"asset": {
"properties": {
"kind": {
"type": "string"
},
"meta": {
"properties": {
"description": {
"type": "string"
},
"headline": {
"type": "string"
},
"visible": {
"type": "boolean"
}
}
}
}
}
}
通过此映射,可以使用以下查询:
{
"query": {
"nested": {
"path": "meta",
"query": {
"term": {
"description": "foo"
}
}
}
}
}
否则,您可以对现有映射使用此查询:
{
"query": {
"nested": {
"path": "meta",
"query": {
"nested": {
"path": "description",
"query": {
"term": {
"value": "foo"
}
}
}
}
}
}
}
关于elasticsearch - 如何使用elasticsearch查询对象中的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25211947/