美文网首页
ElasticSearch学习(三)--ElasticSearc

ElasticSearch学习(三)--ElasticSearc

作者: 荆辰曦 | 来源:发表于2018-08-08 15:06 被阅读0次

空搜索

最基本的搜索是空搜索,他没有指定任何的查询条件,只返回集群索引中的所有文档在:

curl -i -XGET "localhost:9200/_search"

响应内容类似于这样:

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 1877

{
  "took" : 38,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_score" : 1.0,
        "_source" : {
          "title" : "My first blog entry",
          "text" : "I am starting to get the hang of this...",
          "date" : "2014/01/02"
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "Douglas",
          "last_name" : "Fir",
          "age" : 35,
          "about" : "I like to build cabinets",
          "interests" : [
            "forestry"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Fir",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      },
      {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "X7cGE2UB2XL2Aw5Pme58",
        "_score" : 1.0,
        "_source" : {
          "title" : "My second blog entry",
          "text" : "Still trying this out...",
          "date" : "2014/01/01"
        }
      }
    ]
  }
}

在所有索引的所有类型中搜索

curl -i -XGET "localhost:9200/_search"

在索引gb的所有类型中搜索

curl -i -XGET "localhost:9200/gb/_search"

在索引gb和us的所有类型中搜索

curl -i -XGET "localhost:9200/gb,us/_search"

在以g或u开头的索引的所有类型中搜索

curl -i -XGET "localhost:9200/g*,u*/_search"

在索引gb的user类型中进行搜索

curl -i -XGET "localhost:9200/gb/user/_search"

在索引gb和us的类型为user和tweet中搜索

curl -i -XGET "localhost:9200/gb,us/user,tweet/_search"

在所有索引的类型为user和tweet中搜索

curl -i -XGET "localhost:9200/_all/user,tweet/_search"

分页

和SQL使用LIMIT关键字返回只有一页的结果一样,ElasticSearch接受 fromsize 参数:
**size:每页展示的结果数,默认10;
**from:从0开始跳过的结果数,默认0;
如果你想每页显示5个结果,页码从1到3,那请求如下:

curl -i -XGET "localhost:9200/_search?size=5"
curl -i -XGET "localhost:9200/_search?size=5&from=5"
curl -i -XGET "localhost:9200/_search?size=5&from=10"

简易搜索

有两种搜索模式:一种是"简易版"的查询字符串将所有参数通过查询字符串定义,另一种版本使用JSON完整的表示请求体,这种富搜索语言叫做结构化查询语句(DSL)

查询字符串搜索对于在命令行下运行点对点查询特别有用。例如这个语句查询所有类型为 employee 并在 last_name 字段中为 "Fir"字符的文档:

curl -i -XGET "localhost:9200/_all/employee/_search?q=last_name:Fir"

下一个语句查找last_name字段中包含"Fir"age字段中包含"32"的结果:

curl -i -XGET "localhost:9200/_all/employee/_search?q=+last_name:Fir+age:32"

"+" 前缀表示语句匹配条件必须被满足。类似的 "-" 前缀表示条件必须不被满足。所有条件如果没有 +-表示可选的。

相关文章

网友评论

      本文标题:ElasticSearch学习(三)--ElasticSearc

      本文链接:https://www.haomeiwen.com/subject/mnutbftx.html