【问题标题】:write the query to get both Top 10 and Exclude & Include编写查询以获得 Top 10 和 Exclude & Include
【发布时间】:2021-01-04 17:58:55
【问题描述】:

我必须使用 3 个字段(答案、问题;关键字、来源)

  1. 我的要求是我需要排除字段 answer = "UNHANDLED"

  2. 我需要字段 question.keyword

  3. 我需要包含字段 source = "sonax"

我使用以下查询来获取输出。但是在应用字段 answer = "UNHANDLED" 后,我仍然在数据中获得未处理的记录。

{
  "query": {
    "bool": {
      "must_not": {
        "term": {
          "answer": "UNHANDLED"
        }
      },
      "must": {
        "term": {
          "source": "sonax"
        }
      }
    }
  },
  "aggs": {
    "top_tags": {
      "terms": {
        "field": "question.keyword"
      },
      "aggs": {
        "top_faq_hits": {
          "top_hits": {
            "_source": {
              "includes": [
                "answer"
              ]
            },
            "size": 1
          }
        }
      }
    }
  }
}

问候, 帕布

【问题讨论】:

  • 能否分享一些示例索引数据和预期的搜索结果?

标签: elasticsearch elastic-stack elasticsearch-5


【解决方案1】:

添加一个包含索引数据、搜索查询和搜索结果的工作示例

索引数据:

{
  "question": "c",
  "answer": "UNHANDLED",
  "source": "sonax"
}
{
  "question": "b",
  "answer": "c",
  "source": "titan"
}
{
  "question": "b",
  "answer": "q",
  "source": "sonax"
}
{
  "question": "d",
  "answer": "a",
  "source": "volvo"
}

搜索查询:

{
  "query": {
    "bool": {
      "must_not": {
        "term": {
          "answer.keyword": "UNHANDLED"
        }
      },
      "must": {
        "term": {
          "source.keyword": "sonax"
        }
      }
    }
  },
  "aggs": {
    "top_tags": {
      "terms": {
        "field": "question.keyword"
      },
      "aggs": {
        "top_faq_hits": {
          "top_hits": {
            "_source": {
              "includes": [
                "source"
              ]
            },
            "size": 1
          }
        }
      }
    }
  }
}

搜索结果:

"aggregations": {
    "top_tags": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "b",
          "doc_count": 1,
          "top_faq_hits": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.6931471,
              "hits": [
                {
                  "_index": "65567523",
                  "_type": "_doc",
                  "_id": "3",
                  "_score": 0.6931471,
                  "_source": {
                    "source": "sonax"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }

【讨论】:

  • @Prabhudas8703 请仔细阅读答案,如果这能解决您的问题,请告诉我?
  • @Prabhudas8703 很高兴能帮到你?
猜你喜欢
  • 1970-01-01
  • 2021-09-06
  • 2014-02-04
  • 2016-06-07
  • 2014-06-30
  • 2019-04-25
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多