Bra*_*rad 3 json elasticsearch
我正在尝试创建一个弹性搜索查询来匹配数组内对象内的多个字段。
例如,我正在查询的 Elastic Search 结构类似于以下内容:
"hits": [
{
"_index": "titles",
"_type": "title",
...
"_source": {
...
"genres": [
{
"code": "adventure",
"priority": 1
},
{
"code": "action",
"priority": 2
},
{
"code": "horror",
"priority": 3
}
],
...
},
...
]
Run Code Online (Sandbox Code Playgroud)
我想做的是匹配具有特定类型/优先级配对的标题。例如,我尝试将所有标题与code=actionand匹配priority=1,但我的查询返回太多结果。在此示例中,由于流派列表包含与 匹配的流派以及code=action另一个与 匹配的流派,因此出现了上述标题priority=1。我的查询类似于以下内容:
"query": {
"bool": {
"filter": [
{
"bool": {
"must":[
{"term": {
"genres.code": {
"value": "action",
"boost": 1.0
}
}},
{"term": {
"genres.priority": {
"value": 1,
"boost": 1.0
}
}}
]
}
},
...
}
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以形成查询,以便将标题与包含priority=1AND的单个流派相匹配code=action?
我已经重现了你的问题。我添加了以下映射
PUT titles
{
"mappings": {
"title": {
"properties": {
"author": {
"type": "text"
},
"genres": {
"type": "nested"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将值添加到索引中。这是插入的内容
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "titles",
"_type": "title",
"_id": "2",
"_score": 1,
"_source": {
"author": "Author 1",
"genres": [
{
"code": "adventure",
"priority": 2
},
{
"code": "action",
"priority": 3
},
{
"code": "horror",
"priority": 1
}
]
}
},
{
"_index": "titles",
"_type": "title",
"_id": "1",
"_score": 1,
"_source": {
"author": "Author 2",
"genres": [
{
"code": "adventure",
"priority": 3
},
{
"code": "action",
"priority": 1
},
{
"code": "horror",
"priority": 2
}
]
}
},
{
"_index": "titles",
"_type": "title",
"_id": "3",
"_score": 1,
"_source": {
"author": "Author 3",
"genres": [
{
"code": "adventure",
"priority": 3
},
{
"code": "action",
"priority": 1
},
{
"code": "horror",
"priority": 2
}
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的查询是:
GET titles/title/_search
{
"query": {
"nested": {
"path": "genres",
"query": {
"bool": {
"must": [
{
"term": {
"genres.code": {
"value": "horror"
}
}
},
{
"term": {
"genres.priority": {
"value": 1
}
}
}
]
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
查询返回
"_source": {
"author": "Author 1",
"genres": [
{
"code": "adventure",
"priority": 2
},
{
"code": "action",
"priority": 3
},
{
"code": "horror",
"priority": 1
}
]
}
Run Code Online (Sandbox Code Playgroud)
该游戏是唯一一个代码 = '恐怖' 且优先级 = 1 的游戏。
| 归档时间: |
|
| 查看次数: |
3837 次 |
| 最近记录: |