如何在弹性搜索中匹配每个数组项的多个字段

时间:2019-05-21 20:03:44

标签: json elasticsearch

我正在尝试创建一个弹性搜索查询来匹配数组内部对象的多个字段。

例如,我要查询的Elastic Search结构类似于以下内容:

"hits": [
            {
                "_index": "titles",
                "_type": "title",
                ...
                "_source": {
                    ...
                    "genres": [
                        {
                            "code": "adventure",
                            "priority": 1
                        },
                        {
                            "code": "action",
                            "priority": 2
                        },
                        {
                            "code": "horror",
                            "priority": 3
                        }
                    ],
                    ...
                },
                ...
        ]

我想做的是匹配具有特定类型/优先级配对的标题。例如,我试图将所有标题都与code=actionpriority=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
                                    }
                            }}
                        ]
                    }                    
                },
                ...
            }

有什么方法可以构成查询,以使标题与同时包含priority=1code=action的单一流派相匹配?

1 个答案:

答案 0 :(得分:3)

我已经解决了您的问题。我添加了以下映射

PUT titles 
{
  "mappings": {
    "title": {
      "properties": {
        "author": {
          "type": "text"
        },
        "genres": {
          "type": "nested"
        }
      }
    }
  }
}

然后我将值添加到索引。这就是插入的内容

  "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的标题。