如何从节点js中的弹性搜索中获取数据?

how to fetch data from elastic search in node js?

我正在使用带有弹性搜索数据库的 NODE JS。 我正在使用这个包 https://www.npmjs.com/package/@elastic/elasticsearch 我的弹性搜索数据库中有这个集合

[
  {
    "_index": "products",
    "_id": "wZRh3n8Bs9qQzO6fvTTS",
    "_score": 1.0,
    "_source": {
      "title": "laptop issues",
      "description": "laptop have issue present in according"
    }
  },
  {
    "_index": "products",
    "_id": "wpRh3n8Bs9qQzO6fvzQM",
    "_score": 1.0,
    "_source": {
      "title": "buy mobile",
      "description": "mobile is in Rs 250"
    }
  },
  {
    "_index": "products",
    "_id": "w5Rh3n8Bs9qQzO6fvzTz",
    "_score": 1.0,
    "_source": {
      "title": "laptop payment",
      "description": "laptop payment is given in any way"
    }
  }
]

现在我打算从弹性数据库中获取数据。当我通过 "LAP" 或 "lap" 时。它给我空白数组或 [] 数组为什么? “lap”存在于所有对象中

我就是这样做的

 const result= await client.search({
      index: 'products',
      query: {
        match_phrase: {
            description: "lap"
        }
      }  

我哪里做错了。我需要 lap 关键字存在的所有结果

匹配查询无效,因为您正在尝试搜索 laptop 个术语的部分字符。

您可以使用 Prefix Query 作为单个术语,如下所示:

{
  "query": {
    "prefix": {
      "title": {
        "value": "lap"
      }
    }
  }
}

如果你想搜索词组那么你可以使用Phrase Prefix Query:

{
  "query": {
    "match_phrase_prefix": {
      "title": "lap"
    }
  }
}

如果您只想匹配查询中的部分单词,则可以使用 match query 并将 operator 设置为 or

POST querycheck/_search
{
  "query": {
    "match": {
      "title": {
        "query": "i have issues",
        "operator": "or"
      }
    }
  }
}