這可能是一個用例Dis Max Query
生成的通過其 子查詢產生的文檔的聯合查詢,並且分數具有最大分數的文檔作爲由任何子查詢產生的每個文檔 ,再加上一條打破 增加額外的匹配子查詢。
因此,您需要將您的答案分數作爲完全匹配並給予最高提升。你必須爲此使用自定義分析器。這會是你的映射:
PUT /test
{
"settings": {
"analysis": {
"analyzer": {
"my_keyword": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"asciifolding",
"lowercase"
]
}
}
}
},
"mappings": {
"question": {
"properties": {
"title": {
"type": "string"
},
"answer": {
"type": "object",
"properties": {
"text": {
"type": "string",
"analyzer": "my_keyword",
"fields": {
"stemmed": {
"type": "string",
"analyzer": "standard"
}
}
}
}
}
}
}
}
}
您的測試數據:
PUT /test/question/1
{
"title": "title nr1",
"answer": [
{
"text": "yes correct."
}
]
}
PUT /test/question/2
{
"title": "title nr2",
"answer": [
{
"text": "yes correct"
}
]
}
現在,當你使用這樣的查詢查詢"yes correct."
:
POST /test/_search
{
"query": {
"dis_max": {
"tie_breaker": 0.7,
"boost": 1.2,
"queries": [
{
"match": {
"answer.text": {
"query": "yes correct.",
"type": "phrase"
}
}
},
{
"match": {
"answer.text.stemmed": {
"query": "yes correct.",
"operator": "and"
}
}
}
]
}
}
}
你得到這樣的輸出:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.37919715,
"hits": [
{
"_index": "test",
"_type": "question",
"_id": "1",
"_score": 0.37919715,
"_source": {
"title": "title nr1",
"answer": [
{
"text": "yes correct."
}
]
}
},
{
"_index": "test",
"_type": "question",
"_id": "2",
"_score": 0.11261705,
"_source": {
"title": "title nr2",
"answer": [
{
"text": "yes correct"
}
]
}
}
]
}
}
如果y OU運行同樣的查詢,而尾隨點,然後成爲"yes correct"
,你得到這樣的結果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.37919715,
"hits": [
{
"_index": "test",
"_type": "question",
"_id": "2",
"_score": 0.37919715,
"_source": {
"title": "title nr2",
"answer": [
{
"text": "yes correct"
}
]
}
},
{
"_index": "test",
"_type": "question",
"_id": "1",
"_score": 0.11261705,
"_source": {
"title": "title nr1",
"answer": [
{
"text": "yes correct."
}
]
}
}
]
}
}
希望這是你在找什麼。
順便說一句,我建議在執行文本搜索時總是使用Match查詢。從資料爲準:
比較QUERY_STRING /場
匹配家庭查詢 不會通過「查詢解析」的過程走的。它不支持 字段名稱前綴,通配符或其他「高級」 功能。由於這個原因,它失敗的機率很小/非 存在,它提供了一個很好的行爲,當它涉及到 分析和運行該文本作爲查詢行爲(這通常是什麼 文本搜索框)。另外,phrase_prefix類型可以提供一個很棒的「你輸入」行爲來自動加載搜索結果。
請注意''fields'「應該是」default_field「,否則查詢將不起作用。兩人在我的最後都得到了完全相同的分數。你能展示你正在基於自己的樣本文件嗎? – Val
對不起,我想我的代碼中有一個錯字。使用'fields'對我有用,並將其更改爲'default_field'不會改變匹配分數。我也沒有意識到分數是完全一樣的。 * oops * – user5243421
我的不好,抱歉,''fields「'當然需要一些咖啡:)' – Val