elasticsearch:"More like this" 与附加约束相结合

elasticsearch: "More like this" combined with additional constraint

我刚碰到 "more like this" functionality/api。是否有可能将 more_like_this 的结果与一些额外的搜索约束相结合?

我有两个有效的 ES 查询:

POST /h/B/_search
{
   "query": {
      "more_like_this": {
         "fields": [
            "desc"
         ],
         "ids": [
            "511111260"
         ],
         "min_term_freq": 1,
         "max_query_terms": 25
      }
   }
}

哪个returns

{
   "took": 16,
   "timed_out": false,
   "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
   },
   "hits": {
      "total": 53,
      "max_score": 3.2860293,
      "hits": [
      ...

这很好,但我需要对单独工作的基础文档的其他字段指定额外的约束:

POST /h/B/_search
{
   "query": {
      "bool": {
         "must": {
            "match": {
               "Kind": "Pen"
            }
         }
      }
   }
}

我很乐意将这两者合二为一,因为查询应说明:"Find a similar items to items labelled with Pen"。我尝试使用嵌套查询进行跟踪,但这给了我一些错误:

POST /h/B/_search
{
   "query": {
      "more_like_this": {
         "fields": [
            "desc"
         ],
         "ids": [
            "511111260"
         ],
         "min_term_freq": 1,
         "max_query_terms": 25
      },
      "nested": {
         "query": {
            "bool": {
               "must": {
                  "match": {
                     "Kind": "Pen"
                  }
               }
            }
         }
      }
   }
}

我尝试了几种组合这两个搜索条件的变体,但到目前为止都没有成功。 如果更有经验的人可以提供一些提示,将不胜感激。

谢谢

bool queries 正是用于此目的。 bool must 基本上等同于 Boolean AND 操作。同样,您可以对 Boolean OR 使用 bool should,对 Boolean NOT 操作使用 bool must_not

POST /h/B/_search
{
   "query": {
      "bool": {
         "must": [
            {
               "more_like_this": {
                  "fields": [
                     "desc"
                  ],
                  "ids": [
                     "511111260"
                  ],
                  "min_term_freq": 1,
                  "max_query_terms": 25
               }
            },
            {
               "match": {
                  "Kind": "Pen"
               }
            }
         ]
      }
   }
}