如何在弹性搜索中匹配每个数组项的多个字段 [英] How to match on multiple fields per array item in elastic search
问题描述
我正在尝试创建一个弹性搜索查询,以匹配数组中对象内部的多个字段.
I am trying to create an elastic search query to match multiple fields inside of an object inside of an array.
例如,我要查询的Elastic Search结构类似于以下内容:
For example, the Elastic Search structure I am querying against is similar to the following:
"hits": [
{
"_index": "titles",
"_type": "title",
...
"_source": {
...
"genres": [
{
"code": "adventure",
"priority": 1
},
{
"code": "action",
"priority": 2
},
{
"code": "horror",
"priority": 3
}
],
...
},
...
]
我想做的是匹配具有特定流派/优先级配对的标题.例如,我试图将所有标题与 code = action
和 priority = 1
匹配,但是我的查询返回的结果过多.由于类别列表包含具有 code = action
的类别和与 priority = 1
相匹配的另一种类别,因此在此示例中点击了上面的标题.我的查询类似于以下内容:
And what I am trying to do is match on titles with specific genre/priority pairings. For example, I am trying to match all titles with code=action
and priority=1
, but my query is returning too many results. The above title is hit during this example due to the fact that the genre list contains both a genre with code=action
AND another genre that matches priority=1
. My query is similar to the following:
"query": {
"bool": {
"filter": [
{
"bool": {
"must":[
{"term": {
"genres.code": {
"value": "action",
"boost": 1.0
}
}},
{"term": {
"genres.priority": {
"value": 1,
"boost": 1.0
}
}}
]
}
},
...
}
有什么方法可以构成查询,以使标题与同时包含 priority = 1
和 code = action
的单一流派相匹配?
Is there any way to form the query in order to match a title with a single genre containing both priority=1
AND code=action
?
推荐答案
我已经重新创建了您的问题.我添加了以下映射
I have recreated your problem. I added the following mapping
PUT titles
{
"mappings": {
"title": {
"properties": {
"author": {
"type": "text"
},
"genres": {
"type": "nested"
}
}
}
}
}
然后我将值添加到索引中.这就是插入的内容
Then I added values to the index. This was what was inserted
"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
}
]
}
}
]
}
我的查询是:
GET titles/title/_search
{
"query": {
"nested": {
"path": "genres",
"query": {
"bool": {
"must": [
{
"term": {
"genres.code": {
"value": "horror"
}
}
},
{
"term": {
"genres.priority": {
"value": 1
}
}
}
]
}
}
}
}
}
查询返回
"_source": {
"author": "Author 1",
"genres": [
{
"code": "adventure",
"priority": 2
},
{
"code": "action",
"priority": 3
},
{
"code": "horror",
"priority": 1
}
]
}
此标题是唯一具有代码='恐怖'和优先级= 1的标题.
This title is the only one that has code = 'horror' and priority = 1.
这篇关于如何在弹性搜索中匹配每个数组项的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!