asc*_*d00 2 elasticsearch mongoosastic
我想弄清楚如何使用elasticsearch 查询对象中的特定字段。我的索引文档如下所示:
{
name : 'thename'
meta : {
title : {value: 'foo is the title'}
headline: {value : 'bar is the headline'}
}
}
Run Code Online (Sandbox Code Playgroud)
例如,我将如何创建针对 的查询meta.title.value?
这实际上支持吗?
我可以查询这些值而无需指定键,例如:
{
query: 'foo'
}
Run Code Online (Sandbox Code Playgroud)
我得到了正确的结果,但如果我只想搜索元对象中的特定键,我不知道该怎么做。更具体地说,如果这有什么区别,
我正在使用mongoosewith 。mongoosastic
这是我在elasticsearch上的文档映射:
"mappings": {
"asset": {
"properties": {
"kind": {
"type": "string"
},
"meta": {
"properties": {
"description": {
"properties": {
"value": {
"type": "string"
}
}
},
"headline": {
"properties": {
"value": {
"type": "string"
}
}
},
"visible": {
"type": "boolean"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
特定字段查询示例:
{
"query": {
"match" : {
"name" : "thename"
}
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,“name”是您要查询的字段的名称。
对于嵌套数据(例如meta.title)的情况,您可以查看我在本主题中发布的“如何查询嵌套字段”部分/sf/answers/1764277931/或检查“嵌套查询”和“嵌套类型”文档: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
您可以创建多种类型的查询。尝试通过创建小示例来浏览其中的大多数(首先,我建议您“匹配”、“术语”、“布尔”、“嵌套”、“模糊”和“通配符”)。
编辑1
我给你一个建议:不要创建名为“value”的字段,否则我们将不得不创建无用的嵌套查询。
这是对您的映射的建议:
"mappings": {
"asset": {
"properties": {
"kind": {
"type": "string"
},
"meta": {
"properties": {
"description": {
"type": "string"
},
"headline": {
"type": "string"
},
"visible": {
"type": "boolean"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过此映射,您可以使用以下查询:
{
"query": {
"nested": {
"path": "meta",
"query": {
"term": {
"description": "foo"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
否则,您可以将此查询用于现有映射:
{
"query": {
"nested": {
"path": "meta",
"query": {
"nested": {
"path": "description",
"query": {
"term": {
"value": "foo"
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1958 次 |
| 最近记录: |