首页 > 解决方案 > ElasticSearch - MoreLikeThis - 条件提升

问题描述

我想根据出版年份设置不同的“提升条款”,例如:

“boost_term”:10.0 到 2015 年之后生产

“boost_term”:5.0 至 2010 年至 2015 年间生产

“boost_term”:3.0 至 2010 年至 2005 年间生产

等等..

Current code:

res = es.search(body={
    "query": { 
        "dis_max": {
            "queries": [
                {
                "more_like_this" : {
                    "fields": [
                        "article.name",
                        "article.year"
                        ],
                        "like" : {
                            "_index" : "test-index",
                            "_type" : "researcher",
                            "_id" : "idResearcher,
                        },
                        "min_term_freq" : 1,
                        "min_doc_freq": 1,
                        "boost_terms": 5.0
                        }
                       },
                    ]
                 }
                }
                })

标签: pythonelasticsearch

解决方案


尝试类似:

{
   "query": {
      "bool": {
         "must": [
            {
               "more_like_this": {
                  "fields": [
                    "article.name",
                    "article.year"
                  ],
                  "like" : {
                        "_index" : "test-index",
                        "_type" : "researcher",
                        "_id" : "idResearcher",
                    },
                  "min_term_freq": 1,
                  "min_doc_freq": 1
          }
        }
      ],
      "should": [
        {
          "range": {
        "producedYear" : {
            "gte" : "2015",
            "boost" : 10.0
        }
          }
        },
        {
          "range": {
        "producedYear" : {
            "gte" : "2010",
            "lt" : "2015"
            "boost" : 10.0
        }
          }
        },{
          "range": {
        "producedYear" : {
            "gte" : "2005",
            "lt" : "2010"
            "boost" : 3.0
        }
          }
        }

      ]
    }
  }
}

推荐阅读