elasticsearch:"更像这样"加上额外的约束

jak*_*sky 4 elasticsearch

我只是碰到了"更像这样"的功能/ 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
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

哪个回报

{
   "took": 16,
   "timed_out": false,
   "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
   },
   "hits": {
      "total": 53,
      "max_score": 3.2860293,
      "hits": [
      ...
Run Code Online (Sandbox Code Playgroud)

哪个没问题,但是我需要在底层文档的其他字段中指定附加约束,它单独工作:

POST /h/B/_search
{
   "query": {
      "bool": {
         "must": {
            "match": {
               "Kind": "Pen"
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我希望将这两者结合起来,因为查询应该说明:"查找类似于用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"
                  }
               }
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了几种变体来结合这两个搜索标准,但到目前为止没有运气.如果有经验的人可以提供一些非常值得赞赏的提示.

谢谢

bit*_*kar 8

bool查询完全用于此目的.A bool must基本上等同于Boolean AND操作.同样,您可以使用bool shouldfor Boolean ORbool must_notfor Boolean 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"
               }
            }
         ]
      }
   }
}
Run Code Online (Sandbox Code Playgroud)