这可能是一个用例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