elasticsearch,Node.js,elasticsearch" /> elasticsearch,Node.js,elasticsearch" />

Node.js 弹性搜索查询,为给定关键字选择最相关的数据

Node.js 弹性搜索查询,为给定关键字选择最相关的数据,node.js,elasticsearch,Node.js,elasticsearch,我使用搜索查询从弹性搜索数据库获取数据。查询如下 GET /v_entity_master/_search { "query": { "bool": { "must": [ { "query_string": { "query": "(*gupta*)", "fields": [ "id","name","mobile" ]

我使用搜索查询从弹性搜索数据库获取数据。查询如下

GET /v_entity_master/_search
{
"query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "(*gupta*)",
            "fields": [
              "id","name","mobile"
            ]
          }
        },
        {
          "query_string": {
            "query": "*7542*",
            "fields": [
              "id","name","mobile"
            ]
          }
        }
      ]
    }
  }
}
此查询返回

{
"id":34501,
"name": "sree gupta",
"mobile":"98775421
},
{
"id":12302,
"name": "gupta",
"mobile":"98775422
}
但我需要的是,给定搜索关键字的精确匹配应该出现在第一个结果中

预计产量为

{
"id":12302,
"name": "gupta",
"mobile":"98775422
},{
"id":34501,
"name": "sree gupta",
"mobile":"98775421
}

请分享你的建议和想法来解决这个问题。提前感谢

首先,你为什么要在id和手机中搜索gupta?领域基于您共享的两个结果,它们是数字字段,那么您的意图是什么

第二个必须条款也有同样的问题。我从未遇到过包含数字值的真名

我也不明白为什么在first-must子句中使用通配符。我假设你想做全文搜索。因此,您可以简单地使用

现在谈谈你的实际问题:

我在测试集群中创建了一个索引,并为您显示为文档的两个响应编制了索引。这是我执行您的查询时的响应:

GET gupta/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "gupta"
          }
        },
        {
          "query_string": {
            "query": "*7542*",
            "fields": ["mobile"]
          }
        }
      ]
    }
  }
}
请注意,两个文档的分数相同。这是因为您在搜索查询中指定了通配符

现在,让我们修改您的查询:

GET gupta/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "gupta"
          }
        },
        {
          "query_string": {
            "query": "*7542*",
            "fields": ["mobile"]
          }
        }
      ]
    }
  }
}
主要区别在于此查询使用匹配查询进行全文搜索。您不需要指定任何通配符,因为您的文本字段是经过分析的

这将返回以下内容:

{
  ...
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.2111092,
    "hits" : [
      {
        "_index" : "gupta",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.2111092,
        "_source" : {
          "id" : 12302,
          "name" : "gupta",
          "mobile" : "98775422"
        }
      },
      {
        "_index" : "gupta",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.160443,
        "_source" : {
          "id" : 34501,
          "name" : "sree gupta",
          "mobile" : "98775421"
        }
      }
    ]
  }
}
现在,由于字段长度标准化,这两个文档的分数不同。如本文所述,在总术语数较少的字段中找到的术语匹配比在术语数较多的字段中找到的匹配更重要


希望我能帮助你。

我的回答你成功了吗?如果是的话,我将非常感激接受我的回答。谢谢