映射:

   {
  "s_q_s" : {
    "mappings" : {
      "properties" : {
        "f1" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "f2" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "f3" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
文件:
    {
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "s_q_s",
        "_type" : "_doc",
        "_id" : "yArwuXQBrACLjhbhLKPa",
        "_score" : 1.0,
        "_source" : {
          "f1" : "major",
          "f2" : "general",
          "f3" : "ram"
        }
      },
      {
        "_index" : "s_q_s",
        "_type" : "_doc",
        "_id" : "yQrwuXQBrACLjhbhT6OJ",
        "_score" : 1.0,
        "_source" : {
          "f1" : "general",
          "f2" : "major",
          "f3" : "ram"
        }
      },
      {
        "_index" : "s_q_s",
        "_type" : "_doc",
        "_id" : "ygrxuXQBrACLjhbhi6Op",
        "_score" : 1.0,
        "_source" : {
          "f1" : "general",
          "f2" : "major major major",
          "f3" : "ram"
        }
      },
      {
        "_index" : "s_q_s",
        "_type" : "_doc",
        "_id" : "ywrxuXQBrACLjhbhuKME",
        "_score" : 1.0,
        "_source" : {
          "f1" : "general",
          "f2" : "major major",
          "f3" : "ram"
        }
      }
    ]
  }
}
查询:
GET s_q_s/_search
{
  "query": {
    "simple_query_string": {
      "query": "major",
      "fields": ["f1","f2^2"] //<===== f2 is twice important that f1
    }
  }
}
结果:
 "hits" : [
      {
        "_index" : "s_q_s",
        "_type" : "_doc",
        "_id" : "yArwuXQBrACLjhbhLKPa",
        "_score" : 1.0,
        "_source" : {
          "f1" : "major",
          "f2" : "general",
          "f3" : "ram"
        }
      },
      {
        "_index" : "s_q_s",
        "_type" : "_doc",
        "_id" : "yQrwuXQBrACLjhbhT6OJ",
        "_score" : 1.0,
        "_source" : {
          "f1" : "general",
          "f2" : "major",
          "f3" : "ram"
        }
      },
      {
        "_index" : "s_q_s",
        "_type" : "_doc",
        "_id" : "ygrxuXQBrACLjhbhi6Op",
        "_score" : 1.0,
        "_source" : {
          "f1" : "general",
          "f2" : "major major major",
          "f3" : "ram"
        }
      },
      {
        "_index" : "s_q_s",
        "_type" : "_doc",
        "_id" : "ywrxuXQBrACLjhbhuKME",
        "_score" : 1.0,
        "_source" : {
          "f1" : "general",
          "f2" : "major major",
          "f3" : "ram"
        }
      }
    ]
文件摘录:

问题:
当我在查询中定义f2的重要性是f1的两倍时,为什么在f1中具有“major”的文档排在顶部而不是一次在f2中具有“major”的文档出现在顶部?

最佳答案


不带增强功能的搜索查询:

{
    "query": {
        "simple_query_string": {
            "fields": [
                "f1",
                "f2"
            ],
            "query": "major"
        }
    }
}
搜索结果:
"hits": [
      {
        "_index": "stof_64023501",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.2039728,  <-- note this
        "_source": {
          "f1": "major",
          "f2": "general",
          "f3": "ram"
        }
      },
      {
        "_index": "stof_64023501",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.48608798,   <-- note this
        "_source": {
          "f1": "general",
          "f2": "major major major",
          "f3": "ram"
        }
      }
]
带有增强2的搜索查询:f2字段上的匹配权重是f1字段上的匹配权重的两倍,但是f1字段的得分仍然比f2的得分高
您可以在上一个搜索查询中看到,匹配f2字段的分数为0.48608798,现在,由于应用了2增强,该分数已变为0.48608798 * 2 = 0.97217596
但是f2字段的分数也小于f1字段的分数,因为0.97217596 <1.2039728
{
    "query": {
        "simple_query_string": {
            "fields": [
                "f1",
                "f2^2"
            ],
            "query": "major"
        }
    }
}
搜索结果:
"hits": [
      {
        "_index": "stof_64023501",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.2039728,   <-- note this
        "_source": {
          "f1": "major",
          "f2": "general",
          "f3": "ram"
        }
      },
      {
        "_index": "stof_64023501",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.97217596,   <-- note this
        "_source": {
          "f1": "general",
          "f2": "major major major",
          "f3": "ram"
        }
      }
]
搜索查询:
现在,使用参数f2提高3字段的值,您可以看到分数的显着变化。因此,现在可以根据您的要求准确得出结果。
{
    "query": {
        "query_string": {
            "fields": [
                "f1",
                "f2^3"
            ],
            "query": "major"
        }
    }
}
搜索结果:
"hits": [
      {
        "_index": "stof_64023501",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.4582639,
        "_source": {
          "f1": "general",
          "f2": "major major major",
          "f3": "ram"
        }
      },
      {
        "_index": "stof_64023501",
        "_type": "_doc",
        "_id": "4",
        "_score": 1.4144535,
        "_source": {
          "f1": "general",
          "f2": "major major",
          "f3": "ram"
        }
      },
      {
        "_index": "stof_64023501",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.2975104,
        "_source": {
          "f1": "general",
          "f2": "major",
          "f3": "ram"
        }
      },
      {
        "_index": "stof_64023501",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.2039728,
        "_source": {
          "f1": "major",
          "f2": "general",
          "f3": "ram"
        }
      }
    ]

关于elasticsearch - ElasticSearch | SimpleQueryString | FieldBoosting无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64023501/

10-16 11:32