Change the search order based on word index

南楼画角 提交于 2020-07-09 12:20:34

问题


Is there any way to add weight to the terms found at the beginning of the document? For e.g. I have 3 documents.

Medicine XXX
Sulpher This medicine contains sulpher and should be taken only after consultation with your doctor.

Medicine YYY
contains: sulpher Not recommended by most physicians

Medicine ZZZ
This medicine works like sulpher but does not contain sulpher at all.

The document XXX should be listed at the top for the search term "Sulpher" because that is the first word in that document. It is OK if YYY is listed at the top because that is same as XXX. But ZZZ should always be the last. In other words terms found at "left" should have higher priority than the terms found on "right" side of the document.


回答1:


You could boost by the lowercase-normalized term position:

PUT sulphur
{
  "settings": {
    "analysis": {
      "normalizer": {
        "keyword_lowercase": {
          "type": "custom",
          "filter": ["lowercase"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "normalizer": "keyword_lowercase"
          }
        }
      }
    }
  }
}

POST sulphur/_doc
{"text":"This medicine works like sulpher but does not contain sulpher at all."}
POST sulphur/_doc
{"text":"contains: sulpher Not recommended by most physicians"}
POST sulphur/_doc
{"text":"Sulpher This medicine contains sulpher and should be taken only after consultation with your doctor."}

and then

GET sulphur/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "function_score": {
            "query": {
              "match": {
                "text": "sulpher"
              }
            },
            "script_score": {
              "script": """
                def pos = doc['text.keyword'].value.indexOf('sulpher');
                return Math.exp((2.0/(pos+1)))
              """
            },
            "boost_mode": "replace"
          }
        }
      ]
    }
  }
}

yielding

[
  {
    "_index":"sulphur",
    "_type":"_doc",
    "_id":"sf5S2nEBW-D5QnrWODvB",
    "_score":7.389056,
    "_source":{
      "text":"Sulpher This medicine contains sulpher and should be taken only after consultation with your doctor."
    }
  },
  {
    "_index":"sulphur",
    "_type":"_doc",
    "_id":"sP5S2nEBW-D5QnrWNjtw",
    "_score":1.1993961,
    "_source":{
      "text":"contains: sulpher Not recommended by most physicians"
    }
  },
  {
    "_index":"sulphur",
    "_type":"_doc",
    "_id":"r_5S2nEBW-D5QnrWNDuw",
    "_score":1.079959,
    "_source":{
      "text":"This medicine works like sulpher but does not contain sulpher at all."
    }
  }
]


来源:https://stackoverflow.com/questions/61568717/change-the-search-order-based-on-word-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!