首页 > 解决方案 > 将 QS 查询字符串转换为弹性云查询

问题描述

有什么方法可以将使用qs npm的查询字符串转换为弹性云查询字符串?哪个支持所有类型的嵌套查询参数,大于、小于、范围之间的值所有类型的查询?

任何执行此操作的 npm 模块、git hub 代码或代码片段?示例:对于我的 api,我支持 qs npm 模块支持的所有类型的查询参数,其中我的搜索 api 是从弹性云中获取的。

所以如果我得到查询

?title=Resurgence&causes=General&location.city=Mysuru&latitude=12.9803047&longitude=77.62949349999997&distance=50000&pageNumber=0&pageSize=10& organisation._id=5c36dd152edb5028fd7655a4

然后查询字符串将由 qs npm 模块转换为 json 对象,并给出如下的 json 对象

{"title":"Resurgence","causes":"General","location.city":"Mysuru","latitude":"12.9803047","longitude":"77.62949349999997","distance":"50000","pageNumber":"0","pageSize":"10","organisation._id":"5c36dd152edb5028fd7655a4"}

在弹性搜索中搜索时,我想将其转换为弹性搜索查询对象,如下所示。所以我只是在寻找一个 npm 模块,它将 qs 模块的查询字符串 json 转换为弹性云查询体?

{
  "index": "researchArticles",
  "from": 0,
  "size": 10,
  "body": {
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "status": "PUBLISHED"
            }
          },
          {
            "match": {
              "title": {
                "query": "Resurgence",
                "operator": "or",
                "prefix_length": 1
              }
            }
          },
          {
            "match": {
              "causes": {
                "query": "General",
                "operator": "or",
                "prefix_length": 1
              }
            }
          },
          {
            "match": {
              "location.city": {
                "query": "Bengaluru",
                "operator": "or",
                "prefix_length": 1
              }
            }
          },
          {
            "match": {
              "pageNumber": {
                "query": "0",
                "operator": "or",
                "prefix_length": 1
              }
            }
          },
          {
            "match": {
              "pageSize": {
                "query": "10",
                "operator": "or",
                "prefix_length": 1
              }
            }
          },
          {
            "match": {
              "organisation._id": {
                "query": "5c36dd152edb5028fd7655a4",
                "operator": "or",
                "prefix_length": 1
              }
            }
          }
        ],
        "should": {
          "range": {
            "startDate": {
              "gte": "now"
            }
          }
        },
        "filter": {
          "geo_distance": {
            "distance": "50000",
            "location.geoSpatial": {
              "lat": "12.9803047",
              "lon": "77.62949349999997"
            }
          }
        }
      }
    },
    "sort": [
      {
        "_geo_distance": {
          "location.geoSpatial": "12.9803047,77.62949349999997",
          "order": "asc",
          "unit": "m"
        }
      }
    ]
  }
}

标签: node.jselasticsearchquery-string

解决方案


推荐阅读