文档地址
elasticsearch2
elasticsearch高级检索
网址
官网 https://www.elastic.co/
java client
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/indexing.html
Java REST Client (deprecated)
https :
Java Transport Client (deprecated)
https :
参考文档地址
https :
https :
jdk兼容性
https :
操作系统兼容性
https :
ELK的自身兼容性
https :
es下载地址
https :
https :
es字段数据类型
https :
数据库使用排行查看
https :
搜索引擎排行查看
https :
kibana下载地址
https :
参考文档
https://blog.csdn.net/u011863024/article/details/115721328
查询参数
track_total_hits 可以查询大于1万的数据
单机部署
1、下载:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0
2、创建es启动用户
useradd es 新增 es 用户
passwd es 为 es 用户设置密码
userdel -r es 如果错了,可以删除再加
chown -R es:es /data/rbc/es 文件夹所有者
3、修改配置文件/config/elasticsearch.yml,加入如下配置
cluster.name: elasticsearch 集群名称
node.name: node-1 几点名称
network.host: linu01 服务器ip
http.port: 9200 访问端口
cluster.initial_master_nodes: ["node-1" ] 主节点名称
path.data: ./data
path.logs: ./logs
4、修改/etc/security/limits.conf
es soft nofile 65536 es表示es用户
es hard nofile 65536
5、修改/etc/security/limits.d/20-nproc.conf
es soft nofile 65536
es hard nofile 65536
* hard nproc 4096 操作系统级别对每个用户创建的进程数的限制, * 代表Linux所有用户名称
6、修改/etc/sysctl.conf
vm.max_map_count=655360 增加此内容,一个进程可以拥有的VMA(虚拟内存区域)的数量,默认值为 65536
sysctl -p 重新加载
7、修改jvm.options配置
-Xms4g
-Xmx4g
8、启动
bin/elasticsearch 控制台启动
bin/elasticsearch -d 后台启动
9、停止es
pkill -F pid
10、访问
http://IP:9200/
集群部署
1 、下载:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0
2 、创建es启动用户
useradd es 新增 es 用户
passwd es 为 es 用户设置密码
userdel -r es 如果错了,可以删除再加
chown -R es:es /data/rbc/es 文件夹所有者
3 、修改配置文件/config/elasticsearch.yml,加入如下配置
cluster.name: cluster-es 自定义集群名称,所有的ES必须是同一个名称,否则会视为不同集群
node.name: node-1 节点名称,每个节点的名称不能重复
network.host: linu01 节点所绑定的IP地址,每个节点的地址不能重复
node.master: true 是不是有资格主节点
node.data: true 是不是数据节点
http.port: 9200 对外访问
transport.tcp.port: 9300 es各节点之间通信
cluster.initial_master_nodes: ["node-1" ]
discovery.seed_hosts: ["linux1:9300" ,"linux2:9300" ,"linux3:9300" ]
path.data: ./data
path.logs: ./logs
gateway.recover_after_nodes: 2
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
cluster.routing.allocation.cluster_concurrent_rebalance: 16 集群内同时启动的数据任务个数,默认是2个
cluster.routing.allocation.node_concurrent_recoveries: 16 添加或删除节点及负载均衡时并发恢复的线程个数,默认4个
cluster.routing.allocation.node_initial_primaries_recoveries: 16 初始化数据恢复时,并发恢复线程的个数,默认4个
head 插件需要这打开这两个配置,跨域时使用
http.cors.allow-origin: "*"
http.cors.enabled: true
4 、修改配置文件/etc/security/limits.conf
es soft nofile 65536
es hard nofile 65536
5 、修改配置文件/etc/security/limits.d/20-nproc.conf
es soft nofile 65536
es hard nofile 65536
* hard nproc 4096 操作系统级别对每个用户创建的进程数的限制, * 代表Linux所有用户名称
6 、修改配置文件/etc/sysctl.conf
vm.max_map_count=655360 增加此内容,一个进程可以拥有的VMA(虚拟内存区域)的数量,默认值为 65536
sysctl -p 重新加载
7 、修改jvm.options配置
-Xms4g
-Xmx4g
8 、分别在不同节点上启动
bin/elasticsearch 控制台启动
bin/elasticsearch -d -p pid 后台启动
将进程ID记录到pid文件
9 、停止es
pkill -F pid
10 、测试集群
http://IP:9200/_cat/nodes
设置es密码,使用的Xpack
https://www.cnblogs.com/djd66/p/16828001.html
https://www.cnblogs.com/zhangmingcheng/p/14572877.html
https://www.cnblogs.com/cjzzz/p/16133347.html
https://www.cnblogs.com/h--d/p/13128635.html
X-Pack是Elastic Stack扩展功能,提供安全性,警报,监视,报告,机器学习和许多其他功能。 ES7.0+之后,默认情况下,当安装Elasticsearch时,会安装X-Pack,无需单独再安装。
自6.8以及7.1+版本之后,基础级安全永久免费。
1、创建一个证书颁发机构
bin/elasticsearch-certutil ca
2、在节点生成证书和私钥
./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
3、把生成的文件elastic-certificates.p12放到config下,并发送到其它集群节点
4、修改配置并启动
xpack.security.enabled: true 开机xpack安全认证,默认为false
xpack.security.transport.ssl.enabled: true 必须开启,节点间ssl通信
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
5、ES需是启动状态,在集群的其中一个节点设置密码即可,设置完之后,数据会自动同步到其他节点(后面加入的ES节点,也会自动同步)。
bin/elasticsearch-setup-passwords interactive
--elastic 超级用户账号:拥有 superuser 角色,是内置的超级用户。
--kibana 账号:拥有 kibana_system 角色,用户 kibana 用来连接 elasticsearch 并与之通信。
--logstash_system 账号:拥有 logstash_system 角色。用户 Logstash 在 Elasticsearch 中存储监控信息时使用。
--beats_system账号:拥有 beats_system 角色。用户 Beats 在 Elasticsearch 中存储监控信息时使用。
6、重置密码时,在高版本有重置密码命令,在低版本的只能删除数据文件及文件elasticsearch.keystore
ELK操作
配置es-head跨域可访问
http.cors.enabled: true
http.cors.allow-origin: "*"
让外网可访问主要修改文件 config/elasticsearch.yml,添加配置
network.host: 0.0.0.0
bootstrap.system_call_filter: false
cluster.initial_master_nodes: ["node-1" ]
es启动
bin/elasticsearch -d
或 bin/elasticsearch -d -p pid
或 bin/elasticsearch &
es停止
cat pid|xargs kill
es访问
http://localhost:9200
kibana启动、停止
bin/kibana
kill -9 pid
kibana访问
http://localhost:5601
Kibana启动报错 kibana server is not ready yet
1.kibana与es版本不兼容
2.kibana中配置的es地址elasticsearch.hosts不对
3.es禁止跨域访问
4.服务器开启了防火墙
5.es磁盘空间已使用90%
通过get _cat/ 可以获取ElasticSearch的当前的很多信息!
GET _cat/indices
GET _cat/aliases
GET _cat/allocation
GET _cat/count
GET _cat/fielddata
GET _cat/health
GET _cat/indices
GET _cat/master
GET _cat/nodeattrs
GET _cat/nodes
GET _cat/pending_tasks
GET _cat/plugins
GET _cat/recovery
GET _cat/repositories
GET _cat/segments
GET _cat/shards
GET _cat/snapshots
GET _cat/tasks
GET _cat/templates
GET _cat/thread_pool
查询多个索引数据时
POST indexName1,indexName2/_search
匹配所有字段规则
匹配字段不加时默认是全部,或者加上*也是表示全部 fields":[" *"],
索引的CRUD
当给索引字段指定分词器时,字段数据会根据指定的分词器创建倒排索引,在搜索时,如果搜索字段未指定分词器,默认使用索引字段指定的分词器
分词器
GET _analyze
{
"analyzer" : "standard" ,
"text" : "中华人民共和国"
}
分词器
GET _analyze
{
"analyzer" :"ik_max_word" ,
"text" :"中华人民共和国"
}
查询索引
GET test_index
GET test_index/_mapping
GET _cat/indices?v
查看指定字段mapping
GET test_index/_mappings
删除索引
DELETE test_index
添加空mapping索引
PUT test_index
添加指定mapping索引
PUT test_index
{
"mappings" : {
"properties" : {
"name" : {
"type" : "text" ,
"analyzer" : "ik_max_word" ,
"fields" : {
"keyword" : {
"type" : "keyword" ,
"ignore_above" : 256
}
}
}
}
}
}
根据id 查询
GET test_index/_doc/1
查询所用
GET test_index/_search
带参数查询
GET test_index/_search?q=name:小红
分页并排序
GET test_index/_search?from=0&size=2&sort =age:asc
post方法直接搜索
POST test_index/_search
post方法多字段查询
POST test_index/_search
{
"query" : {
"multi_match" : {
"query" : "订单" ,
"fields" : ["name1" , "name2" ]
}
}
}
post方法复杂搜索
POST test_index/_search
{
"_source" : ["name" , "age" ],
"query" : {
"match" : {
"name" : {
"query" : "小红" ,
"analyzer" : "ik_max_word"
}
}
},
"sort" : [
{
"age" : {
"order" : "desc"
}
}
],
"from" : 0,
"size" : 10
}
添加数据自动生成id
POST test_index/_doc
{
"name" :"小红同学" ,
"age" :22
}
添加数据指定id
POST test_index/_doc/1
{
"name" :"小红同学" ,
"age" :22
}
删除数据
DELETE test_index/_doc/1
更新数据(会丢失数据)
PUT test_index/_doc/1
{
"name" : "小红2"
}
更新数据(不会丢失)
POST test_index/_update/1
{
"doc" : {
"name" : "小红2"
}
}
推荐搜索相关
PUT product_suggest
{
"mappings" : {
"properties" : {
"fileName" : {
"type" : "completion" ,
"analyzer" :"ik_max_word"
}
}
}
}
PUT product_suggest
{
"mappings" : {
"properties" : {
"fileName" : {
"type" : "text" ,
"analyzer" : "ik_max_word" ,
"fields" : {
"keyword" : {
"type" : "keyword" ,
"ignore_above" : 256
},
"suggest" : {
"type" : "completion" ,
"analyzer" : "ik_max_word"
}
}
}
}
}
}
POST product_suggest/_search
{
"suggest" :{
"my_suggest" :{
"prefix" :"中国" ,
"completion" :{
"field" :"fieldName.suggest" ,
"size" :10,
"skip_duplicates" :true
}
}
}
}
搜索和查询 Query DSL(Domain Specific Language)特定领域语言
示例代码
DELETE product
PUT /product/_doc/1
{
"name" : "xiaomi phone" ,
"desc" : "shouji zhong de zhandouji" ,
"date" : "2021-06-01" ,
"price" : 3999,
"tags" : [ "xingjiabi" , "fashao" , "buka" ]
}
PUT /product/_doc/2
{
"name" : "xiaomi nfc phone" ,
"desc" : "zhichi quangongneng nfc,shouji zhong de jianjiji" ,
"date" : "2021-06-02" ,
"price" : 4999,
"tags" : [ "xingjiabi" , "fashao" , "gongjiaoka" ]
}
PUT /product/_doc/3
{
"name" : "nfc phone" ,
"desc" : "shouji zhong de hongzhaji" ,
"date" : "2021-06-03" ,
"price" : 2999,
"tags" : [ "xingjiabi" , "fashao" , "menjinka" ]
}
PUT /product/_doc/4
{
"name" : "xiaomi erji" ,
"desc" : "erji zhong de huangmenji" ,
"date" : "2021-04-15" ,
"price" : 999,
"tags" : [ "low" , "bufangshui" , "yinzhicha" ]
}
PUT /product/_doc/5
{
"name" : "hongmi erji" ,
"desc" : "erji zhong de kendeji 2021-06-01" ,
"date" : "2021-04-16" ,
"price" : 399,
"tags" : [ "lowbee" , "xuhangduan" , "zhiliangx" ]
}
GET product/_search
GET product/_search
{
"_source" : false ,
"query" : {
"match_all" : {}
}
}
PUT /product2/_doc/1
{
"owner" :{
"name" :"zhangsan" ,
"sex" :"男" ,
"age" :18
},
"name" : "hongmi erji" ,
"desc" : "erji zhong de kendeji" ,
"price" : 399,
"tags" : [
"lowbee" ,
"xuhangduan" ,
"zhiliangx"
]
}
PUT product2
{
"mappings" : {
"_source" : {
"includes" : [
"name" ,
"price"
],
"excludes" : [
"desc" ,
"tags"
]
}
}
}
DELETE product2
GET product2/_search
{
"_source" : ["owner.name" ,"owner.sex" ],
"query" : {
"match_all" : {}
}
}
GET product2/_search
GET product2/_search
{
"_source" : {
"includes" : [
"owner.*" ,
"name"
],
"excludes" : [
"name" ,
"desc" ,
"price"
]
},
"query" : {
"match_all" : {}
}
}
GET _search
GET product/_search
{
"query" : {
"match" : {
"name" : "xiaomi nfc phone"
}
}
}
GET product/_search
{
"query" : {
"match_all" : {}
}
}
GET product/_search
{
"query" : {
"multi_match" : {
"query" : "phone huangmenji" ,
"fields" : ["name" ,"desc" ]
}
}
}
GET product/_mapping
GET product/_search
{
"query" : {
"match_phrase" : {
"name" : "xiaomi nfc"
}
}
}
GET _analyze
{
"analyzer" : "standard" ,
"text" : "xiaomi nfc phone"
}
GET product/_search
{
"query" : {
"match" : {
"name" : "xiaomi phone"
}
}
}
GET product/_search
{
"query" : {
"term" : {
"name" : "xiaomi phone"
}
}
}
GET product/_search
{
"query" : {
"term" : {
"name" : "xiaomi phone"
}
}
}
GET product/_search
{
"query" : {
"match_phrase" : {
"name" : "xiaomi phone"
}
}
}
GET product/_mapping
GET product/_search
{
"query" : {
"term" : {
"name" : "xiaomi phone"
}
}
}
GET product/_search
{
"query" : {
"term" : {
"name.keyword" : "xiaomi phone"
}
}
}
GET product/_search
{
"query" : {
"terms" : {
"tags" : [ "lowbee" , "gongjiaoka" ],
"boost" : 1.0
}
}
}
GET /_search
{
"query" : {
"range" : {
"age" : {
"gte" : 10,
"lte" : 20,
"boost" : 2.0
}
}
}
}
GET product/_search
GET product/_search
{
"query" : {
"range" : {
"date" : {
"gte" : "2021-04-15" ,
"lt" : "2021-04-16"
}
}
}
}
GET product/_search
{
"query" : {
"range" : {
"date" : {
"gte" : "now-1d/d" ,
"lt" : "now/d"
}
}
}
}
GET product/_search
{
"query" : {
"range" : {
"date" : {
"time_zone" : "+08:00" ,
"gte" : "2021-04-15T08:00:00" ,
"lt" : "now"
}
}
}
}
GET product/_search
GET product/_search
{
"query" : {
"match" : {
"name" : "phone"
}
}
}
GET product/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"name" : "phone"
}
},
"boost" : 1.2
}
}
}
GET product/_search
{
"query" : {
"bool" : {
"filter" : {
"term" : {
"name" : "phone"
}
}
}
}
}
GET product/_search
{
"query" : {
"bool" : {
"filter" : [
{
"match" : {
"name" : "xiaomi phone"
}
},
{
"match_phrase" : {
"desc" : "shouji zhong"
}
}
]
}
}
}
GET product/_search
{
"query" : {
"bool" : {
"must_not" : [
{
"match" : {
"name" : "xiaomi nfc"
}
},
{
"range" : {
"price" : {
"gte" : "500"
}
}
}
]
}
}
}
GET product/_search
{
"query" : {
"bool" : {
"should" : [
{
"match_phrase" : {
"name" : "xiaomi nfc"
}
},
{
"range" : {
"price" : {
"lte" : "500"
}
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"must" : [
{
"match" : {
"name" : "xiaomi"
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"filter" : [
{
"range" : {
"price" : {
"lte" : "1000"
}
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"filter" : [
{
"range" : {
"price" : {
"lte" : "1000"
}
}
}
],
"must" : [
{
"match" : {
"name" : "xiaomi"
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"must" : [
{
"match" : {
"name" : "xiaomi"
}
},
{
"range" : {
"price" : {
"lte" : "1000"
}
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"filter" : [
{
"range" : {
"price" : {
"lte" : "10000"
}
}
}
],
"should" : [
{
"match_phrase" : {
"name" : "nfc phone"
}
},
{
"match" : {
"name" : "erji"
}
},
{
"bool" : {
"must" : [
{
"range" : {
"price" : {
"gte" : 900,
"lte" : 3000
}
}
}
]
}
}
],
"minimum_should_match" : 2
}
}
}
GET product/_search
{
"query" : {
"bool" : {
"filter" : [
{
"match" : {
"name" : "xiaomi phone"
}
},
{
"match_phrase" : {
"desc" : "shouji zhong"
}
}
]
}
}
}
GET product/_search
{
"query" : {
"bool" : {
"must_not" : [
{
"match" : {
"name" : "xiaomi nfc"
}
},
{
"range" : {
"price" : {
"gte" : "500"
}
}
}
]
}
}
}
GET product/_search
{
"query" : {
"bool" : {
"should" : [
{
"match_phrase" : {
"name" : "xiaomi nfc"
}
},
{
"range" : {
"price" : {
"lte" : "500"
}
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"must" : [
{
"match" : {
"name" : "xiaomi"
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"filter" : [
{
"range" : {
"price" : {
"lte" : "1000"
}
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"filter" : [
{
"range" : {
"price" : {
"lte" : "1000"
}
}
}
],
"must" : [
{
"match" : {
"name" : "xiaomi"
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"must" : [
{
"match" : {
"name" : "xiaomi"
}
},
{
"range" : {
"price" : {
"lte" : "1000"
}
}
}
]
}
}
}
GET product/_search
{
"_source" : false ,
"query" : {
"bool" : {
"filter" : [
{
"range" : {
"price" : {
"lte" : "10000"
}
}
}
],
"should" : [
{
"match_phrase" : {
"name" : "nfc phone"
}
},
{
"match" : {
"name" : "erji"
}
},
{
"bool" : {
"must" : [
{
"range" : {
"price" : {
"gte" : 900,
"lte" : 3000
}
}
}
]
}
}
],
"minimum_should_match" : 2
}
}
}
1."_source" : "false" ,
2."_source" : "obj.*" ,
3."_source" : [ "obj1.*" , "obj2.*" ],
4."_source" : {"includes" : [ "obj1.*" , "obj2.*" ], "excludes" : [ "*.description" ]}
GET _analyze
{
"analyzer" : "standard" ,
"text" : "xiaomi nfc phone"
}
POST test_index/_search
{
"query" : {
查询所用数据等价于直接查询 GET test_index/_search
"match_all" : {}
}
}
POST test_index/_search
{
"query" : {
name分词后搜索匹配
"match" : {
"name" : "小红 小明"
}
}
}
POST test_index/_search
{
"query" : {
多字段查询匹配相同内容
"multi_match" : {
"query" : "phone huangmenji" ,
"fields" : ["name" ,"desc" ]
}
}
}
POST test_index/_search
{
"query" : {
短语搜索,查询内容"xiaomi nfc" 不会被分词查询
"match_phrase" : {
"name" : "xiaomi nfc"
}
}
}
1.match_phrase 会将检索关键词分词, match_phrase的分词结果必须在被检索字段的分词中都包含,而且顺序必须相同,而且默认必须都是连续的;
2.term搜索不会将搜索词分词,直接匹配搜索字段的分词,只要有一个分词匹配即命中;
1.term是对于搜索词不分词;
2.keyword是字段数据类型,是对于source data中的字段值不分词;
POST test_index/_search
{
"query" : {
term搜索不会将搜索词分词,直接匹配搜索字段的分词,只要有一个分词匹配即命中;
"term" : {
"name" : "xiaomi phone"
}
}
}
POST test_index/_search
{
"query" : {
当字段类型为数组时,可以使用terms查询,数据数组中存在查询数组中任意一个即命中
"terms" : {
"tags" : [ "lowbee" , "gongjiaoka" ],
"boost" : 1.0
}
}
}
POST test_index/_search
{
"query" : {
"range" : {
"age" : {
"gte" : 10,
"lte" : 20,
"boost" : 2.0
}
}
}
}
POST test_index/_search
{
"query" : {
"range" : {
"date" : {
"gte" : "2021-04-15" ,
"lt" : "2021-04-16"
}
}
}
}
POST test_index/_search
{
"query" : {
"range" : {
"date" : {
"time_zone" : "+08:00" ,
"gte" : "2021-04-15T08:00:00" ,
"lt" : "now"
}
}
}
}
POST test_index/_search
{
"query" : {
"match" : {
"name" : "phone"
}
}
}
1.query倾向于“当前文档和查询的语句的相关度”,而filter倾向于“当前文档和查询的条件是不是相符”;
2.在查询过程中,query是要对查询的每个结果计算相关性得分的,而filter不会;
3.filter有相应的缓存机制,可以提高查询效率;
POST test_index/_search
{
"query" : {
表示不以评分查询,查询出的数据评分是一样的
"constant_score" : {
筛选数据条件,具体用处时在这里筛选数据后再用query查询
"filter" : {
"term" : {
"name" : "phone"
}
},
"boost" : 1.2
}
}
}
POST test_index/_search
{
"query" : {
"bool" : {
"filter" : {
"term" : {
"name" : "phone"
}
}
}
}
}
must:必须满足子句出现在匹配的文档中,并将有助于得分。
filter:过滤器 不计算相关度分数,cache☆子句(查询)必须出现在匹配的文档中。但是不像 must查询的分数将被忽略。Filter子句在filter上下文中执行,这意味着计分被忽略,并且子句被考虑用于缓存。
should:可能满足or子句(查询)应出现在匹配的文档中。
must_not:必须不满足 不计算相关度分数 not子句(查询)不得出现在匹配的文档中。子句在过滤器上下文中执行,这意味着计分被忽略,并且子句被视为用于缓存。由于忽略计分,0因此将返回所有文档的分数。
POST test_index/_search
{
"query" :{
"bool" :{
"must" :[
{
"match" :{
"name" :"xiaomi phone"
}
},
{
"match_phrase" :{
"desc" :"shouji zhong"
}
}
]
}
}
}
POST test_index/_search
{
"query" : {
"bool" : {
"filter" : [
{
"match" : {
"name" : "xiaomi phone"
}
},
{
"match_phrase" : {
"desc" : "shouji zhong"
}
}
]
}
}
}
POST test_index/_search
{
"query" : {
"bool" : {
"must_not" : [
{
"match" : {
"name" : "xiaomi nfc"
}
},
{
"range" : {
"price" : {
"gte" : "500"
}
}
}
]
}
}
}
POST test_index/_search
{
"query" : {
"bool" : {
"should" : [
{
"match_phrase" : {
"name" : "xiaomi nfc"
}
},
{
"range" : {
"price" : {
"lte" : "500"
}
}
}
]
}
}
}
POST test_index/_search
{
"_source" : false ,
"query" : {
"bool" : {
"must" : [
{
查询匹配xiaomi的
"match" : {
"name" : "xiaomi"
}
}
]
}
}
}
POST test_index/_search
{
"_source" : false ,
"query" : {
"bool" : {
过滤price小于等于1000的
"filter" : [
{
"range" : {
"price" : {
"lte" : "1000"
}
}
}
]
}
}
}
POST test_index/_search
{
"_source" : false ,
"query" : {
"bool" : {
先过滤查询数据
"filter" : [
{
"range" : {
"price" : {
"lte" : "1000"
}
}
}
],
再匹配查询数据
"must" : [
{
"match" : {
"name" : "xiaomi"
}
}
]
}
}
}
POST test_index/_search
{
"_source" : false ,
"query" : {
"bool" : {
"must" : [
{
"match" : {
"name" : "xiaomi"
}
},
{
"range" : {
"price" : {
"lte" : "1000"
}
}
}
]
}
}
}
POST test_index/_search
{
"_source" : false ,
"query" : {
"bool" : {
查询价格小于10000
"filter" : [
{
"range" : {
"price" : {
"lte" : "10000"
}
}
}
],
should等同于or
"should" : [
{
"match_phrase" : {
"name" : "nfc phone"
}
},
{
"match" : {
"name" : "erji"
}
},
{
"bool" : {
"must" : [
{
"range" : {
"price" : {
"gte" : 900,
"lte" : 3000
}
}
}
]
}
}
],
"minimum_should_match" : 2
}
}
}
参考文章地址
文章1
文章2
文章3
文章4
文章5
文章6
文章7
es-head安装
//head插件依赖node.js
下载node地址:https://nodejs.org/en/download
node版本查看:node -v
//安装grunt
npm install -g grunt-cli
grunt -version
1.修改/etc/elasticsearch/elasticsearch.yml,增加下面跨域访问参数,这样head 插件可以访问es,修改后,重启elasticsearch
http.cors.enabled: true
http.cors.allow-origin: "*"
2.下载并安装、启动
git clone git://github.com/mobz/elasticsearch-head.git
或 直接下载https://github.com/mobz/elasticsearch-head
cd elasticsearch-head
npm install -g grunt-cli
npm install
grunt server
npm run start
3.默认监听地址是localhost,修改监控地址 vi head /Gruntfile.js,
文件,添加hostname:'*'
4.修改head 的链接地址 vi head /_site/app.js
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri" ) || "http://127.0.0.1:9200" ;
5.访问
http://localhost:9100
java示例 es7.4
ES条件查询
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from (0 );
sourceBuilder.size(1 );
sourceBuilder.timeout(TimeValue.timeValueMillis(5000 ));
sourceBuilder.query(QueryBuilders.termQuery("tagId" , tagId));
MapSearchResult pageResult = this .search(new SearchRequest(new String[]{indexTags}, sourceBuilder));
主键查询
GetResponse r = client.get (new GetRequest(index, id), RequestOptions.DEFAULT);
System.out .println(r.getSourceAsString());
批量主键获取
try {
MultiGetRequest multiGetRequest = new MultiGetRequest();
multiGetRequest.add (index, "1" );
multiGetRequest.add (index, "2" );
MultiGetResponse multiResponse = client.mget(multiGetRequest, RequestOptions.DEFAULT);
MultiGetItemResponse[] itemResponses = multiResponse.getResponses();
for (MultiGetItemResponse itemResponse : itemResponses) {
GetResponse response = itemResponse.getResponse();
System.out .println(response.getId() +":" +response.getSource());
}
} catch (IOException e) {
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)