当前位置: 首页 > 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…...

MySQL SQL语句性能优化

MySQL SQL语句性能优化指南 一、查询设计优化1. 避免 SELECT *2. 使用 WHERE 进行条件过滤3. 避免在索引列上使用函数和表达式4. 使用 LIMIT 限制返回行数5. 避免使用子查询6. 优化 JOIN 操作7. 避免全表扫描 二、索引优化1. 使用合适的索引2. 覆盖索引3. 索引选择性4. 多列索引…...

【潜意识Java】期末考试可能考的选择题(附带答案解析)

目录 选择题一:Java 数据类型 选择题二:Java 控制结构 选择题三:面向对象编程 选择题四:Java 集合框架 选择题五:Java 异常处理 选择题六:Java 方法 选择题七:Java 流程控制 选择题八&a…...

修炼之道 --- 其一

序言 大家对面试中的面经八股文是怎样的看法呢,从他的名字 八股文 就可以看出来大家可能并不喜欢他,八股文一般是 死板、浮于表面、不重实际 的特点。但是,我们需要通过辩证的角度来看待一个事情,不能单方面来定性!  …...

【前端】HTML

目录 一、HTML结构 1.1 HTML标签1.2 HTML文件基本结构1.3 快速生成框架 二、HTML常见标签 2.1 注释标签 !-- –2.2 标题标签 h1到h62.3 段落标签 p2.4 换行标签 br2.5 格式化标签2.6 图片标签 img2.7 超链接标签 a 三、表格标签 3.1 常用标签3.2 合并单元格 四、列表标签五、表…...

LabVIEW实现GPS通信

目录 1、GPS通信原理 2、硬件环境部署 3、程序架构 4、前面板设计 5、程序框图设计 6、测试验证 本专栏以LabVIEW为开发平台,讲解物联网通信组网原理与开发方法,覆盖RS232、TCP、MQTT、蓝牙、Wi-Fi、NB-IoT等协议。 结合实际案例,展示如何利用LabVIEW和常用模块实现物联网系…...

【Python 小课堂】第 2 课 Python 基础知识:语句、常量、变量和注释

第 2 课 基础知识:语句、常量/变量和注释 By Yichen Li 2024/12/14 一、内容简介 在本次课中,介绍Python语句、常量/变量以及代码注释的基本概念,一些详细的概念、扩展及用法等细节,留至后续介绍。 二、Python语句 一般来说&…...

基于STM32设计的工地扬尘与噪音实时监测系统(网页)

一、前言 当前项目使用的相关软件工具、传感器源代码工程已经上传到网盘(实时更新项目内容):https://ccnr8sukk85n.feishu.cn/wiki/QjY8weDYHibqRYkFP2qcA9aGnvb?fromfrom_copylink 1.1 项目开发背景 近年来,随着城市化进程的…...

LLM之RAG实战(五十)| FastAPI:构建基于LLM的WEB接口界面

FastAPI是WEB UI接口,随着LLM的蓬勃发展,FastAPI的生态也迎来了新的机遇。本文将围绕FastAPI、OpenAI的API以及FastCRUD,来创建一个个性化的电子邮件写作助手,以展示如何结合这些技术来构建强大的应用程序。 下面我们开始分步骤操…...

JavaScript 中的 Map方法

JavaScript 中的 Map方法 在 JavaScript 中,Map 是一种用于存储键值对的数据结构,相较于传统的对象(Object),Map 提供了更高效的键值对操作方式适合处理需要频繁操作键值对的场景。 1. 创建 Map const map new Map…...

img引入svg如何修改颜色

方法1:通过css中filter:drop-shadow 首先需要一个容纳图标的父盒子(下方实例中的.svg-img),通过css造一个图标的‘影子’(.svg-color中的drop-shadow),然后设置‘影子’的颜色,再把图标本体移出父盒子&…...

自然语言处理基础及应用场景

自然语言处理定义 让计算机理解人所说的文本 语音 Imitation Game 图灵测试 行为主义 鸭子理论 自然语言处理的基本任务 词性标注:区分每个词名词、动词、形容词等词性命名实体的识别:名词的具体指代是哪一类事物共指消解:代词指代的是前面…...

构建centos docker基础镜像

1、介绍 比较老的版本docker镜像,不太好找,可以尝试自己构建 各版本构建基础镜像方法不太一样,方式也不同,自己尝试,本文只介绍了我自己的尝试 2、构建centos5.11 docker镜像 准备iso文件 (1)安…...

etcd命令大全

默认安装自带etcdctl 命令行客户端,分两个版本ETCDCTL_API2和ETCDCTL_API3,两个版本不一样,操作的数据也不相容。 本文以v3 为例。 使用之前需要先设置:export ETCDCTL_API3。 1 etcd查询集群节点列表及状态 标准输出&#xff1…...

Go有限状态机实现和实战

Go有限状态机实现和实战 有限状态机 什么是状态机 有限状态机(Finite State Machine, FSM)是一种用于建模系统行为的计算模型,它包含有限数量的状态,并通过事件或条件实现状态之间的转换。FSM的状态数量是有限的,因此称…...

使用torch模拟 BMM int8量化计算。

使用torch模型BMM int8计算。 模拟:BMM->softmax->BMM 计算流程 import torch import numpy as np torch.manual_seed(777) def int8_quantize_per_token(x: torch.Tensor, axis: int -1, attnsFalse):if x.dtype ! torch.float32:x x.type(torch.float32)…...

vue3的watch一次性监听多个值用法

vue3的watch一次性监听多个值 1、监听单个值 watch(() > route.params.keyword, (newValue, oldValue) > {console.log(监听值变化, newVal, oldVal)state.a newValue});2、监听多个值 watch(() > [route.params.id, route.params.keyword], (newValue, oldValue) &g…...

【one-api和ollama结合使用】

将Ollama接入one-api one-api是一个开源AI中间件服务,可以聚合各家大模型API,比如OpenAI、ChatGLM、文心一言等,聚合后提供统一的OpenAI调用方法。举个例子:ChatGLM和文心一言的API调用方法并不相同,one-api可以对其进…...

Oracle PDB的开启和关闭

[生产环境关闭与开启Oracle PDB] 【运维场景】 在运维Oracle PDB的时候经常要开启和关闭PDB,对关闭和开启PDB的操作要非常熟悉。 【操作方法】 1. PDB的打开与关闭 关闭和开启DB的时候要看DB的警告日志,日志位置(在Oracle用户下查看&…...

十一、动态构建UI元素

装饰器Builder 装饰器BuilderParam <font style"color:rgba(0, 0, 0, 0.9);">BuilderParam</font> 该装饰器用于声明任意UI描述的一个元素&#xff0c;类似slot占位符。 链接 简而言之&#xff1a;就是自定义组件允许外部传递 UI // SonCom 的实现略…...

智能时代的基石:神经网络

智能时代的基石&#xff1a;神经网络 第一节&#xff1a;神经网络简介 课程目标 本节课程旨在全面介绍神经网络的基本概念、结构以及其在历史发展中的重要里程碑。通过深入理解神经网络的工作原理和演变过程&#xff0c;学员将能够掌握神经网络在现实世界中的多种应用&#…...

VScode配置GIT

在Visual Studio Code&#xff08;VSCode&#xff09;中检测不到已安装的Git可以通过以下步骤来解决‌&#xff1a; ‌确认Git是否正确安装‌&#xff1a;首先&#xff0c;确保在计算机上正确安装了Git。可以通过打开命令行窗口并输入git --version来检查是否能够显示Git的版本…...

【CSS】css 如何实现固定宽高比

今天和同事讨论这个问题&#xff0c;一时间还想不到了&#xff0c;于是学习了下&#xff0c;就顺便当个记录吧 要在CSS中实现固定宽高比&#xff0c;有两种主要的方法可以选择。一种是使用新的aspect-ratio属性&#xff0c;另一种是利用padding技巧。随着现代浏览器对aspect-ra…...

使用webrtc-streamer查看实时监控

摄像头配置&#xff08;海康摄像头为例&#xff09; 摄像头视频编码应改成H264格式 webrtc-streamer下载 webrtc-streamer下载地址 下载后解压出来双击运行&#xff0c;端口默认8000 VUE2项目引入文件 在项目静态文件“public”中需引入两个js文件“webrtcstreamer.js”与“…...

ansible部署nginx:1个简单的playbook脚本

文章目录 hosts--ventoryroles执行命令 使用ansible向3台centos7服务器上安装nginx hosts–ventory [rootstand playhook1]# cat /root/HOSTS # /root/HOSTS [webservers] 192.168.196.111 ansible_ssh_passpassword 192.168.196.112 ansible_ssh_passpassword 192.168.196.1…...

Ubuntu安装Gitlab详细图文教程

1、环境准备 1.1、Ubuntu环境 Ubuntu24.04Sever版安装教程 1.2、更新系统 sudo apt update -y sudo apt-get update sudo apt-get upgrade 2、安装Nginx 2.1 安装nginx # 安装 apt install nginx -y 2.2 修改nginx配置⽂件 # 修改nginx配置 vim /etc/nginx/si…...

前端面试准备问题2

1.防抖和节流分别是什么&#xff0c;应用场景 防抖&#xff1a;在事件被触发后&#xff0c;只有在指定的延迟时间内没有再次触发&#xff0c;才执行事件处理函数。 在我的理解中&#xff0c;简单的说就是在一个指定的时间内&#xff0c;仅触发一次&#xff0c;如果有多次重复触…...

uni-app之web-view组件 postMessage 通信【跨端开发系列】

&#x1f517; uniapp 跨端开发系列文章&#xff1a;&#x1f380;&#x1f380;&#x1f380; uni-app 组成和跨端原理 【跨端开发系列】 uni-app 各端差异注意事项 【跨端开发系列】uni-app 离线本地存储方案 【跨端开发系列】uni-app UI库、框架、组件选型指南 【跨端开…...

IntelliJ IDEA 使用技巧与插件推荐

目录 常用使用技巧 1. 使用快捷键提升开发效率 2. 多光标编辑 3. 代码自动补全 4. 使用 Find Action 快速执行操作 5. 集成版本控制系统&#xff08;VCS&#xff09; 6. 快速查看代码文档 推荐插件 1. Lombok Plugin 2. Rainbow Brackets 3. Key Promoter X 4. Chec…...

zookeeper基础命令详解

zookeeper基础命令详解目录 文章目录 zookeeper基础命令详解目录一、列出所有基础命令 一、列出所有基础命令 先启动一个zookeeper客户端连接zookeeper&#xff0c;如果还没有启动zookeeper集群的参考本文启动之后再做后续操作。 https://blog.csdn.net/weixin_42924400/artic…...

2025周易算命网站搭建详细方法+源码选择php环境的配置

以下是一个详细的搭建教程&#xff0c;包括网站分类、环境配置、程序设计和功能实现。 1. 环境准备 1.1 服务器选择 操作系统: Linux&#xff08;推荐使用Ubuntu或CentOS&#xff09;Web服务器: Nginx数据库: MySQLPHP版本: 7.4.x&#xff08;确保小于8.0&#xff09; 1.2 安…...

16:00面试,16:06就出来了,问的问题有点变态。。。

从小厂出来&#xff0c;没想到在另一家公司又寄了。 到这家公司开始上班&#xff0c;加班是每天必不可少的&#xff0c;看在钱给的比较多的份上&#xff0c;就不太计较了。没想到5月一纸通知&#xff0c;所有人不准加班&#xff0c;加班费不仅没有了&#xff0c;薪资还要降40%…...

《杨辉三角》

题目描述 给出 n(1≤n≤20)n(1≤n≤20)&#xff0c;输出杨辉三角的前 nn 行。 如果你不知道什么是杨辉三角&#xff0c;可以观察样例找找规律。 输入格式 无 输出格式 无 输入输出样例 输入 #1复制 6 输出 #1复制 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 C语言…...

2024年03月中国电子学会青少年软件编程(Python)等级考试试卷(五级)答案 + 解析

青少年软件编程(Python)等级考试试卷(五级) 分数:100 题数:38 一、单选题(共25题,共50分) 1. 以下代码的输出结果是?( ) nums = list(range(100, 201)) print...

【游戏设计原理】7 - 加德纳的多元智能理论

虽然多元智能理论是对认知方式的分类&#xff0c;但它也可以为游戏设计提供丰富的思路和策略&#xff0c;帮助设计师创建更具吸引力、包容性和多样性的游戏。通过理解不同玩家的认知方式和优势&#xff0c;我们可以更精准地设计游戏的元素和玩法&#xff0c;使其能够吸引广泛的…...

Jackson @JsonProperty 注解

1. 概述 Jackson 是一个流行的Java库&#xff0c;用于将Java对象转换为JSON格式以及从JSON反序列化回Java对象。一种常见的需求是在序列化为JSON或从JSON反序列化时自定义字段的命名。Jackson 的 JsonProperty 注解正好满足了这一需求。 JsonProperty 注解概览 JsonProperty…...

【数据结构——栈与队列】链栈的基本运算(头歌实践教学平台习题)【合集】

目录&#x1f60b; 任务描述 相关知识 测试说明 我的通关代码: 测试结果&#xff1a; 任务描述 本关任务&#xff1a;编写一个程序实现链栈的基本运算。 相关知识 为了完成本关任务&#xff0c;你需要掌握&#xff1a; 初始化栈、销毁栈、判断栈是否为空、进栈、出栈、取栈…...

数据库乐观锁和悲观锁、redis分布式锁使用场景

前言 最近发现我们同事&#xff0c;但凡需要加锁的地方都用的是分布式锁。而且我们的后台系统&#xff0c;并没有什么并发量&#xff0c;而且还是单体应用。我真的有点怀疑我的同事不太清楚数据乐观锁、悲观锁和redis分布式的使用场景。 请今天就说一下各种锁的应用场景吧。 …...

React的状态管理库-Redux

核心思想&#xff1a;单一数据源、状态是只读的、以及使用纯函数更新状态。 组成部分 Store&#xff08;存储&#xff09; 应用的唯一状态容器&#xff0c;存储整个应用的状态树,使用 createStore() 创建。 getState()&#xff1a;获取当前状态。dispatch(action)&#xff…...

《自制编译器》--青木峰郎 -读书笔记 编译hello

在该书刚开始编译hello.cb时就遇到了问题。 本人用的是wsl&#xff0c;环境如下&#xff0c; 由于是64位&#xff0c;因此根据书中的提示&#xff0c;从git上下载了64位的cb编译器 cbc-64bit 问题一: 通过如下命令编译时,总是报错。 cbc -Wa,"--32" -Wl,"-…...

XSS(跨站攻击)

XSS漏洞&#xff08;跨站脚本&#xff09; 1.XSS 漏洞简介 ​ XSS又叫CSS&#xff08;Cross Site Script&#xff09;跨站脚本攻击是指恶意攻击者往Web页面里插入恶意Script代码&#xff0c;当用户浏览该页之时&#xff0c;嵌入其中Web里面的Script代码会被执行&#xff0c;从…...

华为自反ACL实验

一、实验背景 做这个实验的原因是最近公司里上了三台小程序服务器&#xff0c;由于三台服务器的端口都映射出去了&#xff0c;领导要求A网段的三台服务器不能访问内网B&#xff0c;C网段&#xff0c;同时B、C网段内网用户可以访问A段的94、95、96服务器&#xff1b; 也就是PC4\…...

MyBatisPlus实现多表查询

在MyBatisPlus中实现多表查询&#xff0c;主要有以下几种方法&#xff1a; 使用注解进行多表查询&#xff1a; 你可以在Mapper接口中使用Select注解来编写SQL查询语句&#xff0c;实现多表查询。例如&#xff0c;如果你想根据用户ID查询用户信息和对应的区域名称&#xff0c;可…...

【人工智能-中级】循环神经网络(RNN)与Transformer在自然语言处理中的进阶应用

循环神经网络(RNN)与Transformer在自然语言处理中的进阶应用 自然语言处理(NLP)是人工智能领域中的重要分支,其主要任务是使计算机能够理解、生成和处理人类语言。近年来,循环神经网络(RNN)和Transformer模型已成为NLP领域的两大核心技术。本文将探讨这两类模型在自然…...

ZED相机应用

下载SDK wget https://stereolabs.sfo2.cdn.digitaloceanspaces.com/zedsdk/3.6/ZED_SDK_Ubuntu18_cuda11.5_v3.6.5.run 安装 ./ZED_SDK_Ubuntu18_cuda11.5_v3.6.5.run skip_python 测试 cd /usr/local/zed/tools ls ZED_Calibration ZED_Depth_Viewer ZED_Diagnostic ZED_E…...

大模型呼入机器人如何赋能呼叫中心?(转)

大模型呼入机器人如何赋能呼叫中心&#xff1f;(转) 原作者&#xff1a;开源呼叫中心FreeIPCC 大模型呼入机器人在赋能呼叫中心方面发挥着重要作用&#xff0c;主要体现在以下几个方面&#xff1a; 一、提升服务效率与质量 24小时不间断服务&#xff1a; 大模型呼入机器人能…...

基于Python对xslxslx文件进行操作

利用python操作表格文件 读取xsl格式文件-源码 import xlrd# 读取xls文件中的工作对象 wb xlrd.open_workbook(示例文件/xxx物理学与信息技术学院.xls) print(wb)# 获取所有的工作表名称 sheet_names wb.sheet_names() # print(sheet_names)# 选择要读取的具体工作表对象 s…...

预处理器Stylus的介绍及使用,并同Less、Sass进行对比(简单介绍)

目录 一、安装与配置 安装Node.js&#xff1a; 安装Stylus&#xff1a; 配置Webpack&#xff1a; 二、编写Stylus代码 定义变量&#xff1a; 使用变量&#xff1a; 嵌套语法&#xff1a; 混合&#xff08;Mixins&#xff09;&#xff1a; 函数&#xff1a; 6.关键字参…...

ansible自动化运维(四)jinjia2模板

Jinjia2模板 前面说到playbook组成的时候&#xff0c;有介绍到template模块&#xff0c;而template模块对模板文件进行渲染时&#xff0c;使用的就是jinja2模板引擎&#xff0c;jinja2本身就是基于python的模板引擎&#xff0c;所以下面先来了解一下jinjia2模板的一些用法 基…...

ubuntu系统的docker安装(2)

查看系统版本 lsb_release -asudo systemctl status docker查看docker是否安装成功 docker pull拉取镜像不成功/docker run不成功 可能有多种原因&#xff1a;网络链接不稳定&#xff0c;没有重启docker&#xff0c;可以先将docker源设置为国内镜像源 sudo systemctl rest…...