2、elasticsearch_course_如何进行搜索

在 Elasticsearch 中的搜索中,有两类搜索:

  • queries 检索
    • 全文检索
  • aggregations 聚合
    • 数据统计和分析

我们有时也可以结合 query 及 aggregation一起使用,比如我们可以先对文档进行搜索然后在进行 aggregation:
内容介绍

  • /twitter/_search
  • /twitter/_count
  • /twitter/_search?filter_path=hits.hits._source.city
  • /twitter/_settings
  • /_aliases

已经建立了 twitter 的index(索引)

1. 指定索引全文档检索, 相当于sql的全表查询

GET   /twitter/_search

2. 不指定索引,默认查找所有的index,默认返回默认个数的10个

GET /_search
指定查找index的数量
GET /_search?size=20 

3. 多个索引查询

GET /twitter,blog/_search

4. 通配符和 排除

POST /index*,-index3/_search

5. 结果分析!!

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,  	#检索的文档条数
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "twitter",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "user" : "makuo",
          "uid" : 1,
          "city" : "shandong",
          "province" : "Guangdong",
          "country" : "China",
          "name" : "makuo"
        }
      },
      {
        "_index" : "twitter",
        "_type" : "_doc",
        "_id" : "eJEIdX0BPy_Vj1s24c-0",
        "_score" : 1.0,
        "_source" : {
          "name" : "makuo",
          "age" : 18,
          "gender" : "male"
        }
      }
    ]
  }
}

6. 控制展示的字段 ,和select id,name from mysql.user; 相似

GET twitter/_search?filter_path=hits.hits._source.city
{
  "query": {
    "match": {
      "city": "shandong"
    }
  }
}
结果!!
{
  "_index" : "twitter",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 20,
  "_seq_no" : 21,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "city" : "shandong",
    "name" : "makuo"
  }
}

7. 在过滤的基础上统计 index(索引)里有多少条相关的文档 ,类似select count(*) from t1 where name>18;

GET  /twitter/_count
{
  "query": {
    "match": {
      "city": "北京"
    }
}
}

8.查看 index配置

GET twitter/_settings
结果 !!
{
  "twitter" : {
    "settings" : {
      "index" : {
        "creation_date" : "1638336296507",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "5bIbHWfUQKK52hW_PYmKSA",
        "version" : {
          "created" : "7090399"
        },
        "provided_name" : "twitter"
      }
    }
  }
}
从这里我们可以看到我们的 twitter 索引有多少个 shards 及多少个 replicas。我们也可以通过如下的接口来设置:
PUT twitter
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}
一旦我们把 number_of_shards 定下来了,我们就不可以修改了,除非把 index 删除,并重新 index 它。这是因为每个文档存储到哪一个 shard 是和 number_of_shards这 个数值有关的。一旦这个数值发生改变,那么之后寻找那个文档所在的 shard 就会不准确

9.修改索引的 mapping

Elasticsearch 号称是 schemaless,在实际所得应用中,每一个 index 都有一个相应的 mapping。这个 mapping 在我们生产第一个文档时已经生产。它是对每个输入的字段进行自动的识别从而判断它们的数据类型。我们可以这么理解 schemaless:

不需要事先定义一个相应的 mapping 才可以生产文档。字段类型是动态进行识别的。这和传统的数据库是不一样的
如果有动态加入新的字段,mapping 也可以自动进行调整并识别新加入的字段

自动识别字段有一个问题,那就是有的字段可能识别并不精确,比如对于我们例子中的位置信息。那么我们需要对这个字段进行修改。
我们可以通过如下的命令来查询目前的 index 的 mapping:

GET twitter/_mapping

查询数据

match query 或者可以理解为 query match

GET twitter/_search
{
  "query": {
    "match": {
      "city": "shandong"
    }
  }
}
结果!!
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,  
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,  #匹配条件的文档的总数量
      "relation" : "eq"
    },
    "max_score" : 0.2876821,  #关联结果
    "hits" : [
      {
        "_index" : "twitter",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,   #关联结果
        "_source" : {
          "user" : "makuo",
          "uid" : 1,
          "city" : "shandong",
          "province" : "Guangdong",
          "country" : "China",
          "name" : "makuo"
        }
      }
    ]
  }
}
也可以使用这样的

posted @ 2021-12-01 16:29  mk-备忘  阅读(54)  评论(0)    收藏  举报