【问题标题】:elasticsearch: "More like this" combined with additional constraintelasticsearch:“更像这样”结合额外的约束
【发布时间】:2015-03-23 17:06:00
【问题描述】:

我刚刚碰到“更像这样”的功能/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
      }
   }
}

返回

{
   "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"
            }
         }
      }
   }
}

我很想将这两者合二为一,因为查询应说明:“查找与用 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"
                  }
               }
            }
         }
      }
   }
}

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

谢谢

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    bool queries 正是用于此目的。 bool must 基本上等同于 Boolean AND 操作。同样,您可以将bool should 用于Boolean ORbool must_not 用于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"
                   }
                }
             ]
          }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      相关资源
      最近更新 更多