本文档为您介绍使用阿里云Elasticsearch(简称ES)服务搜索数据的方法,包括全文搜索和按查询条件搜索。

全文搜索

Kibana控制台中,执行以下命令搜索描述内容包含每天收益到账消息推送的所有产品。

GET /product_info/products/_search
{
  "query": {
    "match": {
      "describe": "每天收益到账消息推送"
    }
  }
}

搜索成功后,返回结果如下。

{
  "took" : 21,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 1.3968885,
    "hits" : [
      {
        "_index" : "product_info",
        "_type" : "products",
        "_id" : "WLvWYXAB8Rql5AUxLqUU",
        "_score" : 1.3968885,
        "_source" : {
          "productName" : "西部通宝",
          "annual_rate" : "3.1100%",
          "describe" : "90天定投产品,最低10000起投,每天收益到账消息推送"
        }
      },
      {
        "_index" : "product_info",
        "_type" : "products",
        "_id" : "WrvWYXAB8Rql5AUxLqUU",
        "_score" : 1.3968885,
        "_source" : {
          "productName" : "5G设备采购月月盈",
          "annual_rate" : "3.1200%",
          "describe" : "90天定投产品,最低12000起投,每天收益到账消息推送"
        }
      },
      {
        "_index" : "product_info",
        "_type" : "products",
        "_id" : "WbvWYXAB8Rql5AUxLqUU",
        "_score" : 1.3547361,
        "_source" : {
          "productName" : "安详畜牧产业",
          "annual_rate" : "3.3500%",
          "describe" : "270天定投产品,最低40000起投,每天收益立即到账消息推送"
        }
      },
      {
        "_index" : "product_info",
        "_type" : "products",
        "_id" : "W7vWYXAB8Rql5AUxLqUU",
        "_score" : 1.1507283,
        "_source" : {
          "productName" : "新能源动力理财",
          "annual rate" : "3.0100%",
          "describe" : "30天定投产品推荐,最低8000起投,每天收益会消息推送"
        }
      },
      {
        "_index" : "product_info",
        "_type" : "products",
        "_id" : "XLvWYXAB8Rql5AUxLqUU",
        "_score" : 0.5753642,
        "_source" : {
          "productName" : "微贷赚",
          "annual_rate" : "2.7500%",
          "describe" : "热门短期产品,3天短期,无须任何手续费用,最低500起投,通过短信提示获取收益消息"
        }
      },
      {
        "_index" : "product_info",
        "_type" : "products",
        "_id" : "V7vWYXAB8Rql5AUxLqUU",
        "_score" : 0.31854028,
        "_source" : {
          "productName" : "大健康天天理财",
          "annual_rate" : "3.2200%",
          "describe" : "180天定期理财,最低20000起投,收益稳定,可以自助选择消息推送"
        }
      }
    ]
  }
}

阿里云ES支持通过索引创建分词器进行数据搜索,同时也支持评分排序。在上文的返回结果中,前两条商品信息中都出现了每天收益到账消息推送,后两条商品信息中只出现了关键词消息推送,所以越靠前的搜索结果的匹配度越高,分数也越高。

按查询条件搜索

Kibana控制台中,执行以下命令搜索年化率在3.0000%到3.1300%之间的产品。

GET /product_info/products/_search
{
  "query": {
    "range": {
      "annual_rate": {
        "gte": "3.0000%",
        "lte": "3.1300%"
      }
    }
  }
}

执行成功后,返回结果如下。

{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "product_info",
        "_type" : "products",
        "_id" : "WLvWYXAB8Rql5AUxLqUU",
        "_score" : 1.0,
        "_source" : {
          "productName" : "西部通宝",
          "annual_rate" : "3.1100%",
          "describe" : "90天定投产品,最低10000起投,每天收益到账消息推送"
        }
      },
      {
        "_index" : "product_info",
        "_type" : "products",
        "_id" : "WrvWYXAB8Rql5AUxLqUU",
        "_score" : 1.0,
        "_source" : {
          "productName" : "5G设备采购月月盈",
          "annual_rate" : "3.1200%",
          "describe" : "90天定投产品,最低12000起投,每天收益到账消息推送"
        }
      }
    ]
  }
}

ES会根据条件搜索到符合预期的产品,同时以降序排列展示。

更多查询方法请参见Query DSL