1、Elasticsearch搜索方式介绍

Elasticsearch官方提供了两中检索方式:一种是通过 URL 参数进行搜索,另一种是通过 DSL(Domain Specified Language) 进行搜索。官方更推荐使用第二种方式, 第二种方式是基于传递JSON作为请求体(request body)格式与Elasticsearch进行交互,这种方式更强大,更简洁。

备注:DSL是什么?请移步:http://www.mybatis.cn/archives/1229.html

2、Elasticsearch搜索数据实战操作:采用URL参数的形式

首先,准备测试数据:

curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/myindex' -d '
{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    }
}
'

curl -X PUT "localhost:9200/myindex/_doc/1?pretty" -H 'Content-Type: application/json' -d'{
    "id":1,
    "title":"网站公告1",
    "content":"大家注意了,网站核心社群正式启动!",
    "date":"2020-05-12"
}'

curl -X PUT "localhost:9200/myindex/_doc/2?pretty" -H 'Content-Type: application/json' -d'{
    "id":2,
    "title":"网站公告2",
    "content":"各位读者,大家好:祝大家元旦快乐!",
    "date":"2021-01-01"
}'

执行搜索操作:

[root@root ~]# curl -X GET -H 'Content-Type: application/json'  "localhost:9200/myindex/_search?q=*&sort=date:asc&pretty"

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "id" : 1,
          "title" : "网站公告1",
          "content" : "大家注意了,网站核心社群正式启动!",
          "date" : "2020-05-12"
        },
        "sort" : [
          1589241600000
        ]
      },
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "id" : 2,
          "title" : "网站公告2",
          "content" : "各位读者,大家好:祝大家元旦快乐!",
          "date" : "2021-01-01"
        },
        "sort" : [
          1609459200000
        ]
      }
    ]
  }
}

3、Elasticsearch搜索数据实战操作:采用DSL方式

[root@root ~]# curl -X POST -H 'Content-Type: application/json'  "localhost:9200/myindex/_search?pretty" -d '{
     "query": {"match_all": {}},
     "sort": [
         {
             "date": {
                 "order": "asc"
             }
         }
     ]
 }'

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "id" : 1,
          "title" : "网站公告1",
          "content" : "大家注意了,网站核心社群正式启动!",
          "date" : "2020-05-12"
        },
        "sort" : [
          1589241600000
        ]
      },
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "id" : 2,
          "title" : "网站公告2",
          "content" : "各位读者,大家好:祝大家元旦快乐!",
          "date" : "2021-01-01"
        },
        "sort" : [
          1609459200000
        ]
      }
    ]
  }
}

标签: none

[网站公告]-[2024年兼职介绍]


添加新评论