Elasticserch学习之搜索(二)

测试需要用到的数据:

PUT /us/user/1?pretty=1
{

   "email" : "john@smith.com",

   "name" : "John Smith",

   "username" : "@john"

}

PUT /gb/user/2?pretty=1
{

   "email" : "mary@jones.com",

   "name" : "Mary Jones",

   "username" : "@mary"

}

PUT /gb1/tweet/3?pretty=1
{

   "date" : "2014-09-13",

   "name" : "Mary Jones",

   "tweet" : "Elasticsearch means full text search has never been so easy",

   "user_id" : 2

}

PUT /us1/tweet/4?pretty=1
{

   "date" : "2014-09-14",

   "name" : "John Smith",

   "tweet" : "@mary it is not just text, it does everything",

   "user_id" : 1

}

PUT /gb1/tweet/5?pretty=1
{

   "date" : "2014-09-15",

   "name" : "Mary Jones",

   "tweet" : "However did I manage before Elasticsearch?",

   "user_id" : 2

}

PUT /us1/tweet/6?pretty=1
{

   "date" : "2014-09-16",

   "name" : "John Smith",

   "tweet" : "The Elasticsearch API is really easy to use",

   "user_id" : 1

}

PUT /gb1/tweet/7?pretty=1
{

   "date" : "2014-09-17",

   "name" : "Mary Jones",

   "tweet" : "The Query DSL is really powerful and flexible",

   "user_id" : 2

}

PUT /us1/tweet/8?pretty=1
{

   "date" : "2014-09-18",

   "name" : "John Smith",

   "user_id" : 1

}

PUT /gb1/tweet/9?pretty=1
{

   "date" : "2014-09-19",

   "name" : "Mary Jones",

   "tweet" : "Geo-location aggregations are really cool",

   "user_id" : 2

}

PUT /us1/tweet/10?pretty=1
{

   "date" : "2014-09-20",

   "name" : "John Smith",

   "tweet" : "Elasticsearch surely is one of the hottest new NoSQL products",

   "user_id" : 1

}

PUT /gb1/tweet/11?pretty=1
{

   "date" : "2014-09-21",

   "name" : "Mary Jones",

   "tweet" : "Elasticsearch is built for the cloud, easy to scale",

   "user_id" : 2

}

PUT /us1/tweet/12?pretty=1
{

   "date" : "2014-09-22",

   "name" : "John Smith",

   "tweet" : "Elasticsearch and I have left the honeymoon stage, and I still love her.",

   "user_id" : 1

}

PUT /gb1/tweet/13?pretty=1
{

   "date" : "2014-09-23",

   "name" : "Mary Jones",

   "tweet" : "So yes, I am an Elasticsearch fanboy",

   "user_id" : 2

}

PUT /us1/tweet/14?pretty=1
{

   "date" : "2014-09-24",

   "name" : "John Smith",

   "tweet" : "How many more cheesy tweets do I have to write?",

   "user_id" : 1

}

空搜索:

GET /_search

结果:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 34,
    "successful" : 34,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 28,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : ".kibana_1",
        "_type" : "doc",
        "_id" : "space:default",
        "_score" : 1.0,
        "_source" : {
          "space" : {
            "name" : "Default",
            "description" : "This is your default space!",
            "color" : "#00bfb3",
            "_reserved" : true
          },
          "type" : "space",
          "updated_at" : "2019-03-11T01:39:09.342Z"
        }
      },
      {
        "_index" : ".kibana_1",
        "_type" : "doc",
        "_id" : "config:6.6.1",
        "_score" : 1.0,
        "_source" : {
          "config" : {
            "buildNum" : 19513
          },
          "type" : "config",
          "updated_at" : "2019-03-11T01:39:15.676Z"
        }
      },
      {
        "_index" : ".kibana_1",
        "_type" : "doc",
        "_id" : "telemetry:telemetry",
        "_score" : 1.0,
        "_source" : {
          "telemetry" : {
            "enabled" : false
          },
          "type" : "telemetry",
          "updated_at" : "2019-03-11T05:17:03.176Z"
        }
      },
      {
        "_index" : "us1",
        "_type" : "tweet",
        "_id" : "14",
        "_score" : 1.0,
        "_source" : {
          "date" : "2014-09-24",
          "name" : "John Smith",
          "tweet" : "How many more cheesy tweets do I have to write?",
          "user_id" : 1
        }
      },
      {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "bf7xa2kBUi3i0pMAwG44",
        "_score" : 1.0,
        "_source" : {
          "title" : "My first blog entry",
          "text" : "Just trying this out...",
          "date" : "2014/01/01"
        }
      },
      {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_score" : 1.0,
        "_source" : {
          "title" : "But we can update it"
        }
      },
      {
        "_index" : "gb1",
        "_type" : "tweet",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "date" : "2014-09-15",
          "name" : "Mary Jones",
          "tweet" : "However did I manage before Elasticsearch?",
          "user_id" : 2
        }
      },
      {
        "_index" : "gb1",
        "_type" : "tweet",
        "_id" : "9",
        "_score" : 1.0,
        "_source" : {
          "date" : "2014-09-19",
          "name" : "Mary Jones",
          "tweet" : "Geo-location aggregations are really cool",
          "user_id" : 2
        }
      },
      {
        "_index" : "us1",
        "_type" : "tweet",
        "_id" : "8",
        "_score" : 1.0,
        "_source" : {
          "date" : "2014-09-18",
          "name" : "John Smith",
          "user_id" : 1
        }
      },
      {
        "_index" : "us1",
        "_type" : "tweet",
        "_id" : "10",
        "_score" : 1.0,
        "_source" : {
          "date" : "2014-09-20",
          "name" : "John Smith",
          "tweet" : "Elasticsearch surely is one of the hottest new NoSQL products",
          "user_id" : 1
        }
      }
    ]
  }
}

hits

  • 响应中最重要的部分是hits,它包含了total字段来表示匹配到的文档总数,hits数组还包含了匹配到的前10条数据。
  • hits 数组中的每个结果都包含_index_type和文档的_id字段,被加入到_source字段中这意味着在搜索结果中我们将可以直接使用全部文档。这不像其他搜索引擎只返回文档ID,需要你单独去获取文档。
  • 每个节点都有一个_score字段,这是相关性得分(relevance score),它衡量了文档与查询的匹配程度。默认的,返回的结果中关联性最大的文档排在首位;这意味着,它是按照_score降序排列的。这种情况下,我们没有指定任何查询,所以所有文档的相关性是一样的,因此所有结果的_score都是取得一个中间值1。
  • max_score指的是所有文档匹配查询中_score的最大值。

took

  • took告诉我们整个搜索请求花费的毫秒数。

shards

  • _shards节点告诉我们参与查询的分片数(total字段),有多少是成功的(successful字段),有多少的是失败的(failed字段)。

timeout

  • time_out值告诉我们查询超时与否。一般的,搜索请求不会超时。如果响应速度比完整的结果更重要,你可以定义timeout参数为10或者10ms(10毫秒),或者1s(1秒)。
GET /_search?timeout=10ms

Elasticsearch将返回在请求超时前收集到的结果。

 

posted on 2019-03-12 15:39  mentiantian  阅读(151)  评论(0编辑  收藏  举报