当前位置: 首页 > news >正文

谷粒商城—分布式高级①.md

1. ELASTICSEARCH

1、安装elastic search

dokcer中安装elastic search

(1)下载ealastic search和kibana

docker pull elasticsearch:7.6.2
docker pull kibana:7.6.2

(2)配置

mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data
echo "http.host: 0.0.0.0" >/mydata/elasticsearch/config/elasticsearch.yml
chmod -R 777 /mydata/elasticsearch/

(3)启动Elastic search

docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e  "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v  /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.6.2 

设置开机启动elasticsearch

docker update elasticsearch --restart=always

(4)启动kibana:

docker run --name kibana -e ELASTICSEARCH_HOSTS=http://172.17.0.3:9200 -p 5601:5601 -d kibana:7.6.2

设置开机启动kibana

docker update kibana  --restart=always

(5)测试

查看elasticsearch版本信息: http://#:9200/

{"name": "0adeb7852e00","cluster_name": "elasticsearch","cluster_uuid": "9gglpP0HTfyOTRAaSe2rIg","version": {"number": "7.6.2","build_flavor": "default","build_type": "docker","build_hash": "ef48eb35cf30adf4db14086e8aabd07ef6fb113f","build_date": "2020-03-26T06:34:37.794943Z","build_snapshot": false,"lucene_version": "8.4.0","minimum_wire_compatibility_version": "6.8.0","minimum_index_compatibility_version": "6.0.0-beta1"},"tagline": "You Know, for Search"
}

显示elasticsearch 节点信息http://#:9200/_cat/nodes ,

127.0.0.1 76 95 1 0.26 1.40 1.22 dilm * 0adeb7852e00

访问Kibana: http://#:5601/app/kibana

在这里插入图片描述

2、初步检索

1)_CAT

(1)GET/cat/nodes:查看所有节点

如:http://#:9200/_cat/nodes :

127.0.0.1 61 91 11 0.08 0.49 0.87 dilm * 0adeb7852e00

注:*表示集群中的主节点

(2)GET/cat/health:查看es健康状况

如: http://#:9200/_cat/health

1588332616 11:30:16 elasticsearch green 1 1 3 3 0 0 0 0 - 100.0%

注:green表示健康值正常

(3)GET/cat/master:查看主节点

如: http://#:9200/_cat/master

vfpgxbusTC6-W3C2Np31EQ 127.0.0.1 127.0.0.1 0adeb7852e00

(4)GET/_cat/indicies:查看所有索引 ,等价于mysql数据库的show databases;

如: http://#:9200/_cat/indices

green open .kibana_task_manager_1   KWLtjcKRRuaV9so_v15WYg 1 0 2 0 39.8kb 39.8kb
green open .apm-agent-configuration cuwCpJ5ER0OYsSgAJ7bVYA 1 0 0 0   283b   283b
green open .kibana_1                PqK_LdUYRpWMy4fK0tMSPw 1 0 7 0 31.2kb 31.2kb
2)索引一个文档

保存一个数据,保存在哪个索引的哪个类型下,指定用那个唯一标识
PUT customer/external/1;在customer索引下的external类型下保存1号数据为

PUT customer/external/1
{"name":"John Doe"
}

PUT和POST都可以
POST新增。如果不指定id,会自动生成id。指定id就会修改这个数据,并新增版本号;
PUT可以新增也可以修改。PUT必须指定id;由于PUT需要指定id,我们一般用来做修改操作,不指定id会报错。

下面是在postman中的测试数据:
在这里插入图片描述

创建数据成功后,显示201 created表示插入记录成功。

{"_index": "customer","_type": "external","_id": "1","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 0,"_primary_term": 1
}

这些返回的JSON串的含义;这些带有下划线开头的,称为元数据,反映了当前的基本信息。

“_index”: “customer” 表明该数据在哪个数据库下;

“_type”: “external” 表明该数据在哪个类型下;

“_id”: “1” 表明被保存数据的id;

“_version”: 1, 被保存数据的版本

“result”: “created” 这里是创建了一条数据,如果重新put一条数据,则该状态会变为updated,并且版本号也会发生变化。

下面选用POST方式:

添加数据的时候,不指定ID,会自动的生成id,并且类型是新增:

image-20200501195619925

再次使用POST插入数据,仍然是新增的:

image-20200501195732492

添加数据的时候,指定ID,会使用该id,并且类型是新增:

image-20200501200048361

再次使用POST插入数据,类型为updated

image-20200501200132199
3)查看文档

GET /customer/external/1

http://#:9200/customer/external/1

{"_index": "customer",//在哪个索引"_type": "external",//在哪个类型"_id": "1",//记录id"_version": 3,//版本号"_seq_no": 6,//并发控制字段,每次更新都会+1,用来做乐观锁"_primary_term": 1,//同上,主分片重新分配,如重启,就会变化"found": true,"_source": {"name": "John Doe"}
}

通过“if_seq_no=1&if_primary_term=1 ”,当序列号匹配的时候,才进行修改,否则不修改。

实例:将id=1的数据更新为name=1,然后再次更新为name=2,起始_seq_no=6,_primary_term=1

(1)将name更新为1

http://#:9200/customer/external/1?if_seq_no=6&if_primary_term=1

image-20200501212224983

(2)将name更新为2,更新过程中使用seq_no=6

http://#:9200/customer/external/1?if_seq_no=6&if_primary_term=1

image-20200501213047499

出现更新错误。

(3)查询新的数据

http://#:9200/customer/external/1

在这里插入图片描述

能够看到_seq_no变为7。

(4)再次更新,更新成功

http://#:9200/customer/external/1?if_seq_no=7&if_primary_term=1

image-20200501213130001
4)更新文档

在这里插入图片描述

在这里插入图片描述

(1)POST更新文档,带有_update

http://#:9200/customer/external/1/_update

在这里插入图片描述

如果再次执行更新,则不执行任何操作,序列号也不发生变化

在这里插入图片描述

POST更新方式,会对比原来的数据,和原来的相同,则不执行任何操作(version和_seq_no)都不变。

(2)POST更新文档,不带_update

在这里插入图片描述

在更新过程中,重复执行更新操作,数据也能够更新成功,不会和原来的数据进行对比。

5)删除文档或索引
DELETE customer/external/1
DELETE customer

注:elasticsearch并没有提供删除类型的操作,只提供了删除索引和文档的操作。

实例:删除id=1的数据,删除后继续查询

image-20200501220559094

实例:删除整个costomer索引数据

删除前,所有的索引

green  open .kibana_task_manager_1   KWLtjcKRRuaV9so_v15WYg 1 0 2 0 39.8kb 39.8kb
green  open .apm-agent-configuration cuwCpJ5ER0OYsSgAJ7bVYA 1 0 0 0   283b   283b
green  open .kibana_1                PqK_LdUYRpWMy4fK0tMSPw 1 0 7 0 31.2kb 31.2kb
yellow open customer                 nzDYCdnvQjSsapJrAIT8Zw 1 1 4 0  4.4kb  4.4kb

删除“ customer ”索引

在这里插入图片描述

删除后,所有的索引

green  open .kibana_task_manager_1   KWLtjcKRRuaV9so_v15WYg 1 0 2 0 39.8kb 39.8kb
green  open .apm-agent-configuration cuwCpJ5ER0OYsSgAJ7bVYA 1 0 0 0   283b   283b
green  open .kibana_1                PqK_LdUYRpWMy4fK0tMSPw 1 0 7 0 31.2kb 31.2kb
6)eleasticsearch的批量操作——bulk

语法格式:

{action:{metadata}}\n
{request body  }\n{action:{metadata}}\n
{request body  }\n

这里的批量操作,当发生某一条执行发生失败时,其他的数据仍然能够接着执行,也就是说彼此之间是独立的。

bulk api以此按顺序执行所有的action(动作)。如果一个单个的动作因任何原因失败,它将继续处理它后面剩余的动作。当bulk api返回时,它将提供每个动作的状态(与发送的顺序相同),所以您可以检查是否一个指定的动作是否失败了。

实例1: 执行多条数据

POST customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"John Doe"}
{"index":{"_id":"2"}}
{"name":"John Doe"}

执行结果

#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{"took" : 491,"errors" : false,"items" : [{"index" : {"_index" : "customer","_type" : "external","_id" : "1","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1,"status" : 201}},{"index" : {"_index" : "customer","_type" : "external","_id" : "2","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 1,"_primary_term" : 1,"status" : 201}}]
}

实例2:对于整个索引执行批量操作

POST /_bulk
{"delete":{"_index":"website","_type":"blog","_id":"123"}}
{"create":{"_index":"website","_type":"blog","_id":"123"}}
{"title":"my first blog post"}
{"index":{"_index":"website","_type":"blog"}}
{"title":"my second blog post"}
{"update":{"_index":"website","_type":"blog","_id":"123"}}
{"doc":{"title":"my updated blog post"}}

运行结果:

#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{"took" : 608,"errors" : false,"items" : [{"delete" : {"_index" : "website","_type" : "blog","_id" : "123","_version" : 1,"result" : "not_found","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1,"status" : 404}},{"create" : {"_index" : "website","_type" : "blog","_id" : "123","_version" : 2,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 1,"_primary_term" : 1,"status" : 201}},{"index" : {"_index" : "website","_type" : "blog","_id" : "MCOs0HEBHYK_MJXUyYIz","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 2,"_primary_term" : 1,"status" : 201}},{"update" : {"_index" : "website","_type" : "blog","_id" : "123","_version" : 3,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 3,"_primary_term" : 1,"status" : 200}}]
}
7)样本测试数据

准备了一份顾客银行账户信息的虚构的JSON文档样本。每个文档都有下列的schema(模式)。

{"account_number": 1,"balance": 39225,"firstname": "Amber","lastname": "Duke","age": 32,"gender": "M","address": "880 Holmes Lane","employer": "Pyrami","email": "amberduke@pyrami.com","city": "Brogan","state": "IL"
}

https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json ,导入测试数据,

POST bank/account/_bulk

3、检索

1)search Api

ES支持两种基本方式检索;

  • 通过REST request uri 发送搜索参数 (uri +检索参数);
  • 通过REST request body 来发送它们(uri+请求体);

信息检索

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

uri+请求体进行检索

GET /bank/_search
{"query": { "match_all": {} },"sort": [{ "account_number": "asc" },{"balance":"desc"}]
}

HTTP客户端工具(),get请求不能够携带请求体,

GET bank/_search?q=*&sort=account_number:asc

返回结果:

{"took" : 235,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1000,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "bank","_type" : "account","_id" : "0","_score" : null,"_source" : {"account_number" : 0,"balance" : 16623,"firstname" : "Bradshaw","lastname" : "Mckenzie","age" : 29,"gender" : "F","address" : "244 Columbus Place","employer" : "Euron","email" : "bradshawmckenzie@euron.com","city" : "Hobucken","state" : "CO"},"sort" : [0]},{"_index" : "bank","_type" : "account","_id" : "1","_score" : null,"_source" : {"account_number" : 1,"balance" : 39225,"firstname" : "Amber","lastname" : "Duke","age" : 32,"gender" : "M","address" : "880 Holmes Lane","employer" : "Pyrami","email" : "amberduke@pyrami.com","city" : "Brogan","state" : "IL"},"sort" : [1]},{"_index" : "bank","_type" : "account","_id" : "2","_score" : null,"_source" : {"account_number" : 2,"balance" : 28838,"firstname" : "Roberta","lastname" : "Bender","age" : 22,"gender" : "F","address" : "560 Kingsway Place","employer" : "Chillium","email" : "robertabender@chillium.com","city" : "Bennett","state" : "LA"},"sort" : [2]},{"_index" : "bank","_type" : "account","_id" : "3","_score" : null,"_source" : {"account_number" : 3,"balance" : 44947,"firstname" : "Levine","lastname" : "Burks","age" : 26,"gender" : "F","address" : "328 Wilson Avenue","employer" : "Amtap","email" : "levineburks@amtap.com","city" : "Cochranville","state" : "HI"},"sort" : [3]},{"_index" : "bank","_type" : "account","_id" : "4","_score" : null,"_source" : {"account_number" : 4,"balance" : 27658,"firstname" : "Rodriquez","lastname" : "Flores","age" : 31,"gender" : "F","address" : "986 Wyckoff Avenue","employer" : "Tourmania","email" : "rodriquezflores@tourmania.com","city" : "Eastvale","state" : "HI"},"sort" : [4]},{"_index" : "bank","_type" : "account","_id" : "5","_score" : null,"_source" : {"account_number" : 5,"balance" : 29342,"firstname" : "Leola","lastname" : "Stewart","age" : 30,"gender" : "F","address" : "311 Elm Place","employer" : "Diginetic","email" : "leolastewart@diginetic.com","city" : "Fairview","state" : "NJ"},"sort" : [5]},{"_index" : "bank","_type" : "account","_id" : "6","_score" : null,"_source" : {"account_number" : 6,"balance" : 5686,"firstname" : "Hattie","lastname" : "Bond","age" : 36,"gender" : "M","address" : "671 Bristol Street","employer" : "Netagy","email" : "hattiebond@netagy.com","city" : "Dante","state" : "TN"},"sort" : [6]},{"_index" : "bank","_type" : "account","_id" : "7","_score" : null,"_source" : {"account_number" : 7,"balance" : 39121,"firstname" : "Levy","lastname" : "Richard","age" : 22,"gender" : "M","address" : "820 Logan Street","employer" : "Teraprene","email" : "levyrichard@teraprene.com","city" : "Shrewsbury","state" : "MO"},"sort" : [7]},{"_index" : "bank","_type" : "account","_id" : "8","_score" : null,"_source" : {"account_number" : 8,"balance" : 48868,"firstname" : "Jan","lastname" : "Burns","age" : 35,"gender" : "M","address" : "699 Visitation Place","employer" : "Glasstep","email" : "janburns@glasstep.com","city" : "Wakulla","state" : "AZ"},"sort" : [8]},{"_index" : "bank","_type" : "account","_id" : "9","_score" : null,"_source" : {"account_number" : 9,"balance" : 24776,"firstname" : "Opal","lastname" : "Meadows","age" : 39,"gender" : "M","address" : "963 Neptune Avenue","employer" : "Cedward","email" : "opalmeadows@cedward.com","city" : "Olney","state" : "OH"},"sort" : [9]}]}
}

(1)只有6条数据,这是因为存在分页查询;

使用fromsize可以指定查询

GET /bank/_search
{"query": { "match_all": {} },"sort": [{ "account_number": "asc" },{"balance":"desc"}],"from": 20,"size": 10
}

(2)详细的字段信息,参照: https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-search.html

The response also provides the following information about the search request:

  • took – how long it took Elasticsearch to run the query, in milliseconds
  • timed_out – whether or not the search request timed out
  • _shards – how many shards were searched and a breakdown of how many shards succeeded, failed, or were skipped.
  • max_score – the score of the most relevant document found
  • hits.total.value - how many matching documents were found
  • hits.sort - the document’s sort position (when not sorting by relevance score)
  • hits._score - the document’s relevance score (not applicable when using match_all)
2)Query DSL
(1)基本语法格式

Elasticsearch提供了一个可以执行查询的Json风格的DSL。这个被称为Query DSL,该查询语言非常全面。

一个查询语句的典型结构

QUERY_NAME:{ARGUMENT:VALUE,ARGUMENT:VALUE,...
}

如果针对于某个字段,那么它的结构如下:

{QUERY_NAME:{FIELD_NAME:{ARGUMENT:VALUE,ARGUMENT:VALUE,...}   }
}
GET bank/_search
{"query": {"match_all": {}},"from": 0,"size": 5,"sort": [{"account_number": {"order": "desc"}}]
}

query定义如何查询;

  • match_all查询类型【代表查询所有的所有】,es中可以在query中组合非常多的查询类型完成复杂查询;
  • 除了query参数之外,我们可也传递其他的参数以改变查询结果,如sort,size;
  • from+size限定,完成分页功能;
  • sort排序,多字段排序,会在前序字段相等时后续字段内部排序,否则以前序为准;
(2)返回部分字段
GET bank/_search
{"query": {"match_all": {}},"from": 0,"size": 5,"sort": [{"account_number": {"order": "desc"}}],"_source": ["balance","firstname"]}

查询结果:

{"took" : 18,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1000,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "bank","_type" : "account","_id" : "999","_score" : null,"_source" : {"firstname" : "Dorothy","balance" : 6087},"sort" : [999]},{"_index" : "bank","_type" : "account","_id" : "998","_score" : null,"_source" : {"firstname" : "Letha","balance" : 16869},"sort" : [998]},{"_index" : "bank","_type" : "account","_id" : "997","_score" : null,"_source" : {"firstname" : "Combs","balance" : 25311},"sort" : [997]},{"_index" : "bank","_type" : "account","_id" : "996","_score" : null,"_source" : {"firstname" : "Andrews","balance" : 17541},"sort" : [996]},{"_index" : "bank","_type" : "account","_id" : "995","_score" : null,"_source" : {"firstname" : "Phelps","balance" : 21153},"sort" : [995]}]}
}
(3)match匹配查询
  • 基本类型(非字符串),精确控制
GET bank/_search
{"query": {"match": {"account_number": "20"}}
}

match返回account_number=20的数据。

查询结果:

{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "bank","_type" : "account","_id" : "20","_score" : 1.0,"_source" : {"account_number" : 20,"balance" : 16418,"firstname" : "Elinor","lastname" : "Ratliff","age" : 36,"gender" : "M","address" : "282 Kings Place","employer" : "Scentric","email" : "elinorratliff@scentric.com","city" : "Ribera","state" : "WA"}}]}
}
  • 字符串,全文检索
GET bank/_search
{"query": {"match": {"address": "kings"}}
}

全文检索,最终会按照评分进行排序,会对检索条件进行分词匹配。

查询结果:

{"took" : 30,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2,"relation" : "eq"},"max_score" : 5.990829,"hits" : [{"_index" : "bank","_type" : "account","_id" : "20","_score" : 5.990829,"_source" : {"account_number" : 20,"balance" : 16418,"firstname" : "Elinor","lastname" : "Ratliff","age" : 36,"gender" : "M","address" : "282 Kings Place","employer" : "Scentric","email" : "elinorratliff@scentric.com","city" : "Ribera","state" : "WA"}},{"_index" : "bank","_type" : "account","_id" : "722","_score" : 5.990829,"_source" : {"account_number" : 722,"balance" : 27256,"firstname" : "Roberts","lastname" : "Beasley","age" : 34,"gender" : "F","address" : "305 Kings Hwy","employer" : "Quintity","email" : "robertsbeasley@quintity.com","city" : "Hayden","state" : "PA"}}]}
}
(4) match_phrase [短句匹配]

将需要匹配的值当成一整个单词(不分词)进行检索

GET bank/_search
{"query": {"match_phrase": {"address": "mill road"}}
}

查处address中包含mill_road的所有记录,并给出相关性得分

查看结果:

{"took" : 32,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 8.926605,"hits" : [{"_index" : "bank","_type" : "account","_id" : "970","_score" : 8.926605,"_source" : {"account_number" : 970,"balance" : 19648,"firstname" : "Forbes","lastname" : "Wallace","age" : 28,"gender" : "M","address" : "990 Mill Road","employer" : "Pheast","email" : "forbeswallace@pheast.com","city" : "Lopezo","state" : "AK"}}]}
}

match_phrase和Match的区别,观察如下实例:

GET bank/_search
{"query": {"match_phrase": {"address": "990 Mill"}}
}

查询结果:

{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 10.806405,"hits" : [{"_index" : "bank","_type" : "account","_id" : "970","_score" : 10.806405,"_source" : {"account_number" : 970,"balance" : 19648,"firstname" : "Forbes","lastname" : "Wallace","age" : 28,"gender" : "M","address" : "990 Mill Road","employer" : "Pheast","email" : "forbeswallace@pheast.com","city" : "Lopezo","state" : "AK"}}]}
}

使用match的keyword

GET bank/_search
{"query": {"match": {"address.keyword": "990 Mill"}}
}

查询结果,一条也未匹配到

{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 0,"relation" : "eq"},"max_score" : null,"hits" : [ ]}
}

修改匹配条件为“990 Mill Road”

GET bank/_search
{"query": {"match": {"address.keyword": "990 Mill Road"}}
}

查询出一条数据

{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 6.5032897,"hits" : [{"_index" : "bank","_type" : "account","_id" : "970","_score" : 6.5032897,"_source" : {"account_number" : 970,"balance" : 19648,"firstname" : "Forbes","lastname" : "Wallace","age" : 28,"gender" : "M","address" : "990 Mill Road","employer" : "Pheast","email" : "forbeswallace@pheast.com","city" : "Lopezo","state" : "AK"}}]}
}

文本字段的匹配,使用keyword,匹配的条件就是要显示字段的全部值,要进行精确匹配的。

match_phrase是做短语匹配,只要文本中包含匹配条件,就能匹配到。

(5)multi_math【多字段匹配】
GET bank/_search
{"query": {"multi_match": {"query": "mill","fields": ["state","address"]}}
}

state或者address中包含mill,并且在查询过程中,会对于查询条件进行分词。

查询结果:

{"took" : 28,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : 5.4032025,"hits" : [{"_index" : "bank","_type" : "account","_id" : "970","_score" : 5.4032025,"_source" : {"account_number" : 970,"balance" : 19648,"firstname" : "Forbes","lastname" : "Wallace","age" : 28,"gender" : "M","address" : "990 Mill Road","employer" : "Pheast","email" : "forbeswallace@pheast.com","city" : "Lopezo","state" : "AK"}},{"_index" : "bank","_type" : "account","_id" : "136","_score" : 5.4032025,"_source" : {"account_number" : 136,"balance" : 45801,"firstname" : "Winnie","lastname" : "Holland","age" : 38,"gender" : "M","address" : "198 Mill Lane","employer" : "Neteria","email" : "winnieholland@neteria.com","city" : "Urie","state" : "IL"}},{"_index" : "bank","_type" : "account","_id" : "345","_score" : 5.4032025,"_source" : {"account_number" : 345,"balance" : 9812,"firstname" : "Parker","lastname" : "Hines","age" : 38,"gender" : "M","address" : "715 Mill Avenue","employer" : "Baluba","email" : "parkerhines@baluba.com","city" : "Blackgum","state" : "KY"}},{"_index" : "bank","_type" : "account","_id" : "472","_score" : 5.4032025,"_source" : {"account_number" : 472,"balance" : 25571,"firstname" : "Lee","lastname" : "Long","age" : 32,"gender" : "F","address" : "288 Mill Street","employer" : "Comverges","email" : "leelong@comverges.com","city" : "Movico","state" : "MT"}}]}
}
(6)bool用来做复合查询

复合语句可以合并,任何其他查询语句,包括符合语句。这也就意味着,复合语句之间
可以互相嵌套,可以表达非常复杂的逻辑。

must:必须达到must所列举的所有条件

GET bank/_search
{"query":{"bool":{"must":[{"match":{"address":"mill"}},{"match":{"gender":"M"}}]}}
}

must_not,必须不匹配must_not所列举的所有条件。

should,应该满足should所列举的条件。

实例:查询gender=m,并且address=mill的数据

GET bank/_search
{"query": {"bool": {"must": [{"match": {"gender": "M"}},{"match": {"address": "mill"}}]}}
}

查询结果:

{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 6.0824604,"hits" : [{"_index" : "bank","_type" : "account","_id" : "970","_score" : 6.0824604,"_source" : {"account_number" : 970,"balance" : 19648,"firstname" : "Forbes","lastname" : "Wallace","age" : 28,"gender" : "M","address" : "990 Mill Road","employer" : "Pheast","email" : "forbeswallace@pheast.com","city" : "Lopezo","state" : "AK"}},{"_index" : "bank","_type" : "account","_id" : "136","_score" : 6.0824604,"_source" : {"account_number" : 136,"balance" : 45801,"firstname" : "Winnie","lastname" : "Holland","age" : 38,"gender" : "M","address" : "198 Mill Lane","employer" : "Neteria","email" : "winnieholland@neteria.com","city" : "Urie","state" : "IL"}},{"_index" : "bank","_type" : "account","_id" : "345","_score" : 6.0824604,"_source" : {"account_number" : 345,"balance" : 9812,"firstname" : "Parker","lastname" : "Hines","age" : 38,"gender" : "M","address" : "715 Mill Avenue","employer" : "Baluba","email" : "parkerhines@baluba.com","city" : "Blackgum","state" : "KY"}}]}
}

must_not:必须不是指定的情况

实例:查询gender=m,并且address=mill的数据,但是age不等于38的


GET bank/_search
{"query": {"bool": {"must": [{"match": {"gender": "M"}},{"match": {"address": "mill"}}],"must_not": [{"match": {"age": "38"}}]}}

查询结果:

{"took" : 4,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 6.0824604,"hits" : [{"_index" : "bank","_type" : "account","_id" : "970","_score" : 6.0824604,"_source" : {"account_number" : 970,"balance" : 19648,"firstname" : "Forbes","lastname" : "Wallace","age" : 28,"gender" : "M","address" : "990 Mill Road","employer" : "Pheast","email" : "forbeswallace@pheast.com","city" : "Lopezo",

相关文章:

谷粒商城—分布式高级①.md

1. ELASTICSEARCH 1、安装elastic search dokcer中安装elastic search (1)下载ealastic search和kibana docker pull elasticsearch:7.6.2 docker pull kibana:7.6.2(2)配置 mkdir -p /mydata/elasticsearch/config mkdir -p /mydata/elasticsearch/data echo "h…...

Unity开发配置不足,卡顿崩溃怎么办?

在游戏开发和虚拟现实等领域,Unity 软件以其强大的功能和广泛的适用性成为了众多开发者的首选。然而,要充分发挥 Unity 的性能,一台高性能的电脑设备是必不可少的。今天,我要向大家介绍川翔云电脑,它为 Unity 开发者提…...

在 Linux 上以 All-in-One 模式安装 kubernetes v1.22.12 kubesphere v3.4.1

KubeSphere4.1安装文档 在 Kubernetes 上快速安装 KubeSphere 在 Linux 上以 All-in-One 模式安装 kubernetes v1.22.12 kubesphere v3.4.1 官方文档:在 Linux 上以 All-in-One 模式安装 KubeSphere 下载文件 KubeKey git地址Releases kubesphere/kubekey 或 …...

网络安全自学是一项需要耐心和恒心的任务

网络安全自学是一项需要耐心和恒心的任务,但只要你按照正确的学习路线图去努力,就能够逐步掌握这一领域的知识和技能。下面是一份详细的学习路线图,它将帮助你从零基础开始,逐步成为网络安全领域的专家。 第一阶段:基…...

Python+OpenCV系列:图像的几何变换

Python OpenCV 系列:图像的几何变换 引言 在图像处理领域,几何变换是一个非常重要的操作,它可以改变图像的位置、大小、方向或形状。在计算机视觉中,这些操作对于图像预处理、特征提取和图像增强至关重要。本文将介绍如何利用 …...

第P1周:Pytorch实现mnist手写数字识别

🍨 本文为🔗365天深度学习训练营 中的学习记录博客🍖 原作者:K同学啊 目标 1. 实现pytorch环境配置 2. 实现mnist手写数字识别 3. 自己写几个数字识别试试具体实现 (一)环境 语言环境:Python…...

使用EventLog Analyzer进行Apache日志监控和日志分析

一、什么是Apache日志分析 Apache日志分析是网站管理和维护的重要部分,通过分析Apache服务器生成的日志文件,可以了解网站的访问情况、识别潜在的安全问题、优化网站性能等。 二、Apache日志类型 Apache日志主要有两种类型:访问日志&a…...

PaddleOCR模型ch_PP-OCRv3文本检测模型研究(二)颈部网络

上节研究了PaddleOCR文本检测v3模型的骨干网,本文接着研究其颈部网络。 文章目录 研究起点残注层颈部网络代码实验小结 研究起点 摘取开源yml配置文件,摘取网络架构Architecture中颈部网络的配置如下 Neck:name: RSEFPNout_channels: 96shortcut: True可…...

360极速浏览器不支持看PDF

360安全浏览器采用的是基于IE内核和Chrome内核的双核浏览器。360极速浏览器是源自Chromium开源项目的浏览器,不但完美融合了IE内核引擎,而且实现了双核引擎的无缝切换。因此在速度上,360极速浏览器的极速体验感更佳。 展示自己的时候要在有优…...

【深度学习】深刻理解ViT

ViT(Vision Transformer)是谷歌研究团队于2020年提出的一种新型图像识别模型,首次将Transformer架构成功应用于计算机视觉任务中。Transformer最初应用于自然语言处理(如BERT和GPT),而ViT展示了其在视觉任务…...

解决vue2中更新列表数据,页面dom没有重新渲染的问题

在 Vue 2 中,直接修改数组的某个项可能不会触发视图的更新。这是因为 Vue 不能检测到数组的索引变化或对象属性的直接赋值。为了确保 Vue 能够正确地响应数据变化,你可以使用以下几种方法: 1. 使用 Vue.set() 使用 Vue.set() 方法可以确保 …...

vscode通过ssh连接远程服务器(实习心得)

一、连接ssh服务器 1.打开Visual Studio Code,进入拓展市场(CtrlShiftX),下载拓展Remote - SSH 2. 点击远程资源管理器选项卡,并选择远程(隧道/SSH)类别 3. 点击ssh配置:输入你的账号主机ip地址 4.在弹出的选择配置文件中&#xf…...

知识图谱9:知识图谱的展示

1、知识图谱的展示有很多工具 Neo4j Browser - - - - 浏览器版本 Neo4j Desktop - - - - 桌面版本 graphX - - - - 可以集成到Neo4j Desktop Neo4j 提供的 Neo4j Bloom 是用户友好的可视化工具,适合非技术用户直观地浏览图数据。Cypher 是其核心查询语言&#x…...

leetcode 面试经典 150 题:验证回文串

链接验证回文串题序号125类型字符串解题方法双指针法难度简单 题目 如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后,短语正着读和反着读都一样。则可以认为该短语是一个 回文串 。 字母和数字都属于字母数字字符。 给你一个字符串 s&#xf…...

【0363】Postgres内核 从 XLogReaderState readBuf 解析 XLOG Record( 8 )

上一篇: 【0362】Postgres内核 XLogReaderState readBuf 有完整 XLOG page header 信息 ? ( 7 ) 直接相关: 【0341】Postgres内核 读取单个 xlog page (2 - 2 ) 文章目录 1. readBuf 获取 page header 大小1.1 XLOG record 跨 page ?1.2 获取 XLOG Record 的 长度(xl…...

docker tdengine windows快速体验

#拉取镜像 docker pull tdengine/tdengine:2.6.0.34#容器运行 docker run -d --name td2.6 --restartalways -p 6030:6030 -p 6041:6041 -p 6043:6043 -p 6044-6049:6044-6049 -p 6044-6045:6044-6045/udp -p 6060:6060 tdengine/tdengine:2.6.0.34#容器数据持久化到本地 #/va…...

详解RabbitMQ在Ubuntu上的安装

​​​​​​​ 目录 Ubuntu 环境安装 安装Erlang 查看Erlang版本 退出命令 ​编辑安装RabbitMQ 确认安装结果 安装RabbitMQ管理界面 启动服务 查看服务状态 通过IP:port访问 添加管理员用户 给用户添加权限 再次访问 Ubuntu 环境安装 安装Erlang RabbitMq需要…...

Python的3D可视化库【vedo】2-2 (plotter模块) 访问绘制器信息、操作渲染器

文章目录 4 Plotter类的方法4.1 访问Plotter信息4.1.1 实例信息4.1.2 演员对象列表 4.2 渲染器操作4.2.1 选择渲染器4.2.2 更新渲染场景 4.3 控制渲染效果4.3.1 渲染窗格的背景色4.3.2 深度剥离效果4.3.3 隐藏线框的线条4.3.4 改为平行投影模式4.3.5 添加阴影4.3.6 环境光遮蔽4…...

【vue2】文本自动省略组件,支持单行和多行省略,超出显示tooltip

代码见文末 vue3实现 最开始就用的vue3实现,如下 Vue3实现方式 vue2开发和使用文档 组件功能 TooltipText 是一个文字展示组件,具有以下功能: 文本显示:支持单行和多行文本显示。自动判断溢出:判断文本是否溢出…...

网络安全产品之认识防病毒软件

随着计算机技术的不断发展,防病毒软件已成为企业和个人计算机系统中不可或缺的一部分。防病毒软件是网络安全产品中的一种,主要用于检测、清除计算机病毒,以及预防病毒的传播。本文我们一起来认识一下防病毒软件。 一、什么是计算机病毒 计算…...

游戏引擎学习第42天

仓库: https://gitee.com/mrxiao_com/2d_game 简介 目前我们正在研究的内容是如何构建一个基本的游戏引擎。我们将深入了解游戏开发的每一个环节,从最基础的技术实现到高级的游戏编程。 角色移动代码 我们主要讨论的是角色的移动代码。我一直希望能够使用一些基…...

区块链智能合约( solidity) 安全编程

引言:本文由天玄链开源开发者提供,欢迎报名公益天玄链训练营 https://blockchain.163.com/trainingCamp 一、重入和竞态 重入和竞态在solidity 编程安全中会多次提及,历史上也造成了重大的损失。 1.1 问题分析 竞态的描述不严格&#xf…...

GUNS搭建

一、准备工作 源码下载: 链接: https://pan.baidu.com/s/1bJZzAzGJRt-NxtIQ82KlBw 提取码: criq 官方文档 二、导入代码 1、导入后端IDE 导入完成需要,需要修改yml文件中的数据库配置,改成自己的。 2、导入前端IDE 我是用npm安装的yarn npm…...

【ETCD】【源码阅读】stepWithWaitOption方法解析

在分布式系统中,ETCD 作为一个强一致性、高可用的 key-value 存储系统,广泛应用于服务发现、配置管理等场景。ETCD 在内部采用了 Raft 协议来保证集群的一致性,而日志预提案(log proposal)是 Raft 协议中至关重要的一部…...

redis 怎么样查看list

在 Redis 中,可以通过以下方法查看列表的内容或属性: 1. 查看列表中的所有元素 使用 LRANGE 命令: LRANGE key start endkey 是列表的名称。start 是起始索引,0 表示第一个元素。end 是结束索引,-1 表示最后一个元素…...

E: 无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),是否有其他进程正占用它?

我们在使用Ubuntu系统时经常性使用sudo apt install命令安装所需要的软件库,偶尔会出现如下问题: E: 无法获得锁 /var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用) E: 无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),是否有其…...

创建型设计模式

一、设计模式介绍 1.设计模式是什么 设计模式是指在软件开发中,经过验证的,用于解决在特定环境下,重复出现的,特定问题的解决方案; 2.设计模式怎么来的? 满足设计原则后,慢慢迭代出来的。 3.设…...

仿iOS日历、飞书日历、Google日历的日模式

仿iOS日历、飞书日历、Google日历的日模式,24H内事件可自由上下拖动、自由拉伸。 以下是效果图: 具体实现比较简单,代码如下: import android.content.Context; import android.graphics.Canvas; import android.graphics.Color;…...

vuedraggable

官方文档:https://www.npmjs.com/package/vuedraggable 中文文档:http://www.itxst.com/vue-draggable/tutorial.html 案例下载地址: https://github.com/SortableJS/Vue.Draggable.git vuedraggablehttps://sortablejs.github.io/Vue.Dr…...

新手从事直播软件源码开发搭建经验与技巧

如果从YY或六间房的PC秀场直播间系统软件算起,直播软件已经在国内风云了至少10年了,站在用户角度提到直播系统大家基本都知道核心功能有开直播刷礼物等,那么如果站在直播软件源码开发搭建的技术角度去看呢?是不是要从需求调研分析…...

移动应用开发女生学难不难/百度seo软件首选帝搜软件

人类都很难驾驭的漂移板,却被加州大学伯克利分校的双足机器人Cassie轻松实现,直行、转弯、高速行驶都不在话下。IEEE Spectrum对机器人设计团队进行了专访,为我们揭开Cassie背后的技术。机器人现在也可以驾驭漂移板了!新智元之前报道过媲美波…...

做网站的意义/制作网站的软件

Android权限说明 Android系统是运行在Linux内核上的,Android与Linux分别有自己的一套严格的安全及权限机制, Android系统权限相关的内容, (一)linux文件系统上的权限 -rwxr-x--x system system 4156 2012-06…...

网站开发协助方案/大连网站建设费用

引入引入jqueVirevol ai is a collaborative visual shopping app. We run a remote-first lean team, have been for 2 years. To do our magic, we swim with a lot of data.Virevol ai是一款协作式视觉购物应用程序。 我们拥有一支偏远的精益团队,已有2年以上的经…...

福州做网站哪家最好/广告宣传网站

这周五,就是昨天,部门走了3个人。他们是“被离职”的,从被通知到走人,只过了一周左右。一开始经理和他们谈的时候,说有没有赔偿不能确定,然后到周四左右,上面的人可能是等急了,才说年…...

如何做可以微信转发的网站/wap网站html5

2019独角兽企业重金招聘Python工程师标准>>> 在前几篇中,我们搭建好了Go语言的开发环境,从今天开始就正式进入Go语言的编程学习了。 一、Go语言程序基本结构与外部包的导入 首先来看Go语言程序的基本结构: 以上就是最基本的结构&a…...

吉林市教做网站/网站怎样优化文章关键词

门窗报警器主要利用开关型霍尔传感器具有磁感应强度达到一定强度时输出电压才会发生电平转换的特点,我们进行方案设定磁场靠近时霍尔输出低电平不报警,反之磁场离开时霍尔输出高电平为报警。因为门磁开关报警器为了方便安装使用采用电池供电,…...