2

我想弄清楚如何使用elasticsearch查询对象中的特定字段。我的索引文档如下所示:

{
    name : 'thename'
    meta : {
        title : {value: 'foo is the title'}
        headline: {value : 'bar is the headline'}
    }
}

例如,我将如何创建一个查询meta.title.value
这真的支持吗?
我可以在不指定键的情况下查询这些值:

{
    query: 'foo'
}

我得到了正确的结果,但如果我只想在元对象中搜索那个特定的键,我不知道该怎么做。
更具体地说,我正在使用mongoosewithmongoosastic如果这有什么不同。

这是我在elasticsearch上的文档映射:

"mappings": {
    "asset": {
        "properties": {
            "kind": {
                "type": "string"
            },
            "meta": {
                "properties": {
                    "description": {
                        "properties": {
                            "value": {
                                "type": "string"
                            }
                        }
                    },
                    "headline": {
                        "properties": {
                            "value": {
                                "type": "string"
                            }
                        }
                    },
                    "visible": {
                        "type": "boolean"
                    }
                }
            }
        }
    }
}
4

2 回答 2

4

特定字段查询示例:

{
    "query": {
        "match" : {
            "name" : "thename"
        }
    }    
}

在上面的示例中,“name”是您要查询的字段的名称。

对于嵌套数据(例如 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 文档,因为按特定字段查询是最基本的事情之一(恕我直言)。 http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-queries.html

您可以创建多种类型的查询。尝试浏览其中的大多数(首先,我建议您使用“match”、“term”、“bool”、“nested”、“fuzzy”和“wildcard”)创建小示例。


编辑 1

我给你一个建议:不要创建名称为“值”的字段,否则我们将不得不创建无用的嵌套查询。

这是对您的映射的建议:

"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"
                        }
                    }
                }
            }
        }
    }
}
于 2014-08-09T16:45:21.077 回答
-1

使用 mongodb 你会想要做这样的事情:

询问:{'meta.title.value': 'foo'}

您显然必须指定一个密钥。如果您想查看多个字段,可以使用 $or 运算符

询问:{$or: [{'meta.title.value': 'foo'}, {'meta.headline.value': 'foo'}]}

这将返回具有fooinmeta.title.value或 in值的每个文档meta.headline.value

于 2014-08-08T21:24:52.143 回答