【问题标题】:Matching documents where multiple fields match in an array of objects匹配对象数组中多个字段匹配的文档
【发布时间】:2021-08-12 15:57:22
【问题描述】:

我在 ES 的文档中有一个对象数组

我只想匹配对象(数组内)内的 2 个字段匹配的文档。

我的问题是这个。假设我有 3 个文档(删除了外部文档),其中有一组对象,例如

                       // Doc 1
                        "questionAnswers": [
               
                        {
                            "question": "First yes no question",
                            "answer": "Yes"
                        },
                        {
                            "question": "Second yes no question",
                            "answer": "No"
                        }
                    ]


                       // Doc 2
                        "questionAnswers": [
               
                        {
                            "question": "First yes no question",
                            "answer": "No"
                        },
                        {
                            "question": "Second yes no question",
                            "answer": "No"
                        }
                    ]


                       // Doc 3
                        "questionAnswers": [
               
                        {
                            "question": "First yes no question",
                            "answer": "No"
                        },
                        {
                            "question": "Second yes no question",
                            "answer": "Yes"
                        }
                    ]

我的查询是

{
"from": 0,
"size": 25,
"query": {
    "bool": {
        "filter": [
            {
                "match": {
                    "questionAnswers.question.keyword": "First yes no question"
                }
            },
            {
                    "match": {
                        "questionAnswers.answer": "Yes"
                    }
                
            }
        ]
    }
}

}

我目前正在获取文档 1 和 3 的匹配项。而我只希望第一个文档匹配 where question = First yes no question and answer = Yes

如何做到这一点?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您需要将questionAnswers字段定义为nested type,以便可以单独查询其中的每个数组对象,并且可以使用inner_hits在结果中仅获取匹配的文档。

    添加一个带有索引映射、搜索查询和搜索结果的工作示例

    索引映射:

    {
      "mappings": {
        "properties": {
          "questionAnswers": {
            "type": "nested"
          }
        }
      }
    }
    

    搜索查询:

    {
      "query": {
        "nested": {
          "path": "questionAnswers",
          "query": {
            "bool": {
              "filter": [
                {
                  "match": {
                    "questionAnswers.question.keyword": "First yes no question"
                  }
                },
                {
                  "match": {
                    "questionAnswers.answer": "Yes"
                  }
                }
              ]
            }
          },
          "score_mode": "avg",
          "inner_hits": {}
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "68760699",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.0,
            "_source": {
              "questionAnswers": [
                {
                  "question": "First yes no question",
                  "answer": "Yes"
                },
                {
                  "question": "Second yes no question",
                  "answer": "No"
                }
              ]
            },
            "inner_hits": {
              "questionAnswers": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 0.0,
                  "hits": [
                    {
                      "_index": "68760699",
                      "_type": "_doc",
                      "_id": "1",
                      "_nested": {
                        "field": "questionAnswers",
                        "offset": 0
                      },
                      "_score": 0.0,
                      "_source": {
                        "question": "First yes no question",           //note this
                        "answer": "Yes"
                      }
                    }
                  ]
                }
              }
            }
          }
        ]
    

    【讨论】:

    • @David Billings 请仔细阅读答案,如果这解决了您的问题,请告诉我?
    • 感谢您的帮助,是的,这可以满足我的需要!
    猜你喜欢
    • 2014-09-25
    • 2018-05-29
    • 2018-02-12
    • 2015-01-20
    • 2021-07-26
    • 2022-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多