热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ElasticSearch6.x学习笔记:15.检索入门

15.1准备数据[es@node1~]$viwebsite.json[es@node1~]$catwebsite.json{index:{_index:web

15.1 准备数据

[es@node1 ~]$ vi  website.json 
[es@node1 ~]$ cat website.json
{ "index":{ "_index": "website", "_type": "blog", "_id": "1" }}
{ "title": "Ambari源码编译","author":"程裕强","postdate":"2016-12-21","abstract":"CentOS7.x下的Ambari2.4源码编译","url":"http://url.cn/53788351"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "2" }}
{ "title": "watchman源码编译","author":"程裕强","postdate":"2016-12-23","abstract":"CentOS7.x的watchman源码编译","url":"http://url.cn/53844169"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "3" }}
{ "title": "CentOS升级gcc","author":"程裕强","postdate":"2016-12-25","abstract":"CentOS升级gcc","url":"http://url.cn/53868915"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "4" }}
{ "title": "vmware复制虚拟机","author":"程裕强","postdate":"2016-12-29","abstract":"vmware复制虚拟机","url":"http://url.cn/53946664"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "5" }}
{ "title": "libstdc++.so.6","author":"程裕强","postdate":"2016-12-30","abstract":"libstdc++.so.6问题解决","url":"http://url.cn/53946911"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "6" }}
{ "title": "CentOS更换国内yum源","author":"程裕强","postdate":"2016-12-30","abstract":"CentOS更换国内yum源","url":"http://url.cn/53946911"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "7" }}
{ "title": "搭建Ember开发环境","author":"程裕强","postdate":"2016-12-30","abstract":"CentOS下搭建Ember开发环境","url":"http://url.cn/53947507"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "8" }}
{ "title": "es高亮","author":"程裕强","postdate":"2017-01-03","abstract":"Elasticsearch查询关键字高亮","url":"http://url/53991802"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "9" }}
{ "title": "to be or not to be","author":"somebody","postdate":"2018-01-03","abstract":"to be or not to be,that is the question","url":"http://url/63991802"}
[es@node1 ~]$

15.2 创建索引

PUT website
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 5
},
"mappings": {
"blog":{
"properties": {
"title":{
"type":"text",
"analyzer": "ik_max_word"
},
"author":{
"type":"text"
},
"postdate":{
"type":"date",
"format": "yyyy-MM-dd"
},
"abstract":{
"type":"text",
"analyzer": "ik_max_word"
},
"url":{
"type":"text"
}
}
}
}
}

15.3 批量导入

[es@node1 ~]$ curl -XPOST "http://node1:9200/_bulk?pretty" -H "Content-Type: application/json;charset=UTF-8" --data-binary @website.json
{
"took" : 28,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "4",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "5",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "6",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "7",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "8",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "9",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 201
}
}
]
}
[es@node1 ~]$

15.4 term查询

GET website/_search
{
"query": {
"term": {
"title": "vmware"
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
}
,
"hits": {
"total": 1,
"max_score": 0.9227539,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "4",
"_score": 0.9227539,
"_source": {
"title": "vmware复制虚拟机",
"author": "程裕强",
"postdate": "2016-12-29",
"abstract": "vmware复制虚拟机",
"url": "http://url/53946664"
}
}
]
}
}

15.5 分页

GET website/_search
{
"from":0,
"size":3,
"query": {
"match_all": {}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
}
,
"hits": {
"total": 8,
"max_score": 1,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "5",
"_score": 1,
"_source": {
"title": "libstdc++.so.6",
"author": "程裕强",
"postdate": "2016-12-30",
"abstract": "libstdc++.so.6问题解决",
"url": "http://url/53946911"
}
},
{
"_index": "website",
"_type": "blog",
"_id": "8",
"_score": 1,
"_source": {
"title": "es高亮",
"author": "程裕强",
"postdate": "2017-01-03",
"abstract": "Elasticsearch查询关键字高亮",
"url": "http://url/53991802"
}
},
{
"_index": "website",
"_type": "blog",
"_id": "2",
"_score": 1,
"_source": {
"title": "watchman源码编译",
"author": "程裕强",
"postdate": "2016-12-23",
"abstract": "CentOS7.x的watchman源码编译",
"url": "http://url.cn/53844169"
}
}
]
}
}

15.6 过滤字段

GET website/_search
{
"_source": ["title","author"],
"query": {
"term": {
"title": "centos"
}
}
}
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
}
,
"hits": {
"total": 2,
"max_score": 0.9227539,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "6",
"_score": 0.9227539,
"_source": {
"author": "程裕强",
"title": "CentOS更换国内yum源"
}
},
{
"_index": "website",
"_type": "blog",
"_id": "3",
"_score": 0.2876821,
"_source": {
"author": "程裕强",
"title": "CentOS升级gcc"
}
}
]
}
}

15.7 显示version

GET website/_search
{
"_source": ["title"],
"version": true,
"query": {
"term": {
"title": "centos"
}
}
}
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
}
,
"hits": {
"total": 2,
"max_score": 0.9227539,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "6",
"_version": 1,
"_score": 0.9227539,
"_source": {
"title": "CentOS更换国内yum源"
}
},
{
"_index": "website",
"_type": "blog",
"_id": "3",
"_version": 1,
"_score": 0.2876821,
"_source": {
"title": "CentOS升级gcc"
}
}
]
}
}

15.8 评分过滤

GET website/_search
{
"min_score":"0.5",
"query": {
"term": {
"title": "centos"
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
}
,
"hits": {
"total": 1,
"max_score": 0.9227539,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "6",
"_score": 0.9227539,
"_source": {
"title": "CentOS更换国内yum源",
"author": "程裕强",
"postdate": "2016-12-30",
"abstract": "CentOS更换国内yum源",
"url": "http://url/53946911"
}
}
]
}
}

15.9 高亮关键字

GET website/_search
{
"query": {
"term": {
"title": "centos"
}
},
"highlight": {
"fields": {
"title": {}
}
}
}
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
}
,
"hits": {
"total": 2,
"max_score": 0.9227539,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "6",
"_score": 0.9227539,
"_source": {
"title": "CentOS更换国内yum源",
"author": "程裕强",
"postdate": "2016-12-30",
"abstract": "CentOS更换国内yum源",
"url": "http://url/53946911"
}
,
"highlight": {
"title": [
"CentOS更换国内yum源"
]
}
},
{
"_index": "website",
"_type": "blog",
"_id": "3",
"_score": 0.2876821,
"_source": {
"title": "CentOS升级gcc",
"author": "程裕强",
"postdate": "2016-12-25",
"abstract": "CentOS升级gcc",
"url": "http://url/53868915"
}
,
"highlight": {
"title": [
"CentOS升级gcc"
]
}
}
]
}
}

推荐阅读
  • 本文由编程笔记#小编为大家整理,主要介绍了markdown[软件代理设置]相关的知识,希望对你有一定的参考价值。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • PDO MySQL
    PDOMySQL如果文章有成千上万篇,该怎样保存?数据保存有多种方式,比如单机文件、单机数据库(SQLite)、网络数据库(MySQL、MariaDB)等等。根据项目来选择,做We ... [详细]
  • 本文讨论了在使用PHP cURL发送POST请求时,请求体在node.js中没有定义的问题。作者尝试了多种解决方案,但仍然无法解决该问题。同时提供了当前PHP代码示例。 ... [详细]
  • 原文地址http://balau82.wordpress.com/2010/02/28/hello-world-for-bare-metal-arm-using-qemu/最开始时 ... [详细]
  • php7 curl_init(),php7.3curl_init获取301、302跳转后的数据
    最近在做一个蜘蛛项目,发现在抓取数据时,有时会碰到301的页面,原本写的curl_init函数php7-远程获取api接口或网页内容&#x ... [详细]
  • maven阿里云镜像一路繁花似锦绣前程
    重点:找到maven引用的settings.xml配置文件,将以下代码复制至<mirrors><mirrors&a ... [详细]
  • 表单代码 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了如何在使用emacs时去掉ubuntu的alt键默认功能,并提供了相应的操作步骤和注意事项。 ... [详细]
  • 本文详细介绍了使用C#实现Word模版打印的方案。包括添加COM引用、新建Word操作类、开启Word进程、加载模版文件等步骤。通过该方案可以实现C#对Word文档的打印功能。 ... [详细]
  • Summarize function is doing alignment without timezone ?
    Hi.Imtryingtogetsummarizefrom00:00otfirstdayofthismonthametric, ... [详细]
author-avatar
爱是一道菜lzb
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有