搜索与分析ElasticSearch之一:URL请求

ElasticSearch

一 启动ElasticSearch

点击elasticsearch bin目录下的第一个bat

浏览器输入网址 localhost:9200

二 图形化工具

有两种工具可选elasticsearch-head与kibana

2.1 elasticsearch-head

需要nodejs环境,在elasticsearch-head目录中打开cmd 输入

初次使用需要如下语句:

npm install -g grunt-cli
npm install

启动head:

grunt server

然后访问 http://localhost:9100/,并连接 http://localhost:9200/

如果不能连接,打开elasticsearch中的config/elasticsearch.yml,添加跨域允许

http.cors.enabled: true
http.cors.allow-origin: "*"

2.2 Kibana

下载对应Es版本的Kibana,然后双击bin目录下的kiban.bat
kibana改为中文: kibana.yml末尾增加

 i18n.locale: "zh-CN"

三 增删改查

3.1 增

增操作包含:1增加索引 & 2增加文档

3.1.1 一次性生成索引
PUT http://localhost:9200/blog
{
    "settings": {
        "number_of_shards":3,
        "number_of_replicas":1
    },
    "mappings":{
        "properties":{
            "type":{"type":"keyword"},
            "name":{"type":"text"},
            "country":{"type":"keyword"},
            "age":{"type":"integer"},
            "date":{
                "type":"data",
                "format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
            }
        }
    }
}
3.1.2 先生成空索引,然后在添加在设置mappings

(1)先新建索引

PUT http://ip:port/索引名称
PUT http://localhost:9200/blog
{
    "settings": {
        "number_of_shards":3,
        "number_of_replicas":1
    }
}

(2)在设置_mappings

post http://ip:port/索引名称/_mappings
POST localhost:9200/blog/_mappings
{
	"properties":{
		"id":{
            "type":"long",
            "store":true
		},
        "title":{
            "type":"text",
            "store":true,
            "index":true,
            "analyzer":"standard"
        },
        "content":{
            "type":"text",
            "store":true,
            "index":true,
            "analyzer":"standard"
        }
	}
}
3.1.3 向索引库中添加文档
POST http://ip:port/索引名称/_doc/文档ID(可以不填)
POST http://localhost:9200/blog/_doc/1
{
	"id":1,
    "title":"学习java的全文检索工具",
    "content":"Luence是某某某家伙写的,这个东西真的超级不好用,所有又有人写了ElasticSearch"
}

如果不填写ID,那么将自动生成ID

3.2 删

删除操作主要分:1 删除文档 & 2 删除索引

3.2.1 删除文档
delete http://ip:port/索引名称/_doc/_id
3.2.2 删除索引
delete http://ip:port/索引名称

3.3 改

注:更新\修改的原理是先删除后添加,因为Luence是这个原理

3.3.1 修改文档数据
post http://ip:port/索引名称/_update/_id

{
	"doc":{
		"title":"我去前面探探路"
	}
}

实例:修改索引blog中id为3的文档title为 ”我去前面探探路“

POST http://localhost:9200/blog/_update/3
{
    "doc":{
        "title":"我去前面探探路"
    }
}
3.3.2 修改索引

3.4 查

3.4.1 根据ID查询
GET http://ip:port/索引名称/_doc/_id
GET http://localhost:9200/blog/_doc/3
3.4.2 根据关键词查询

仅仅对输入的内容进行查询

post http://ip:port/索引名称/_search/
{
	"query":{
		"term":{
			"title":"java"
		}
	}
}
3.4.3 queryString查询

根据输入的内容进行分析后查询

post http://ip:port/索引名称/_search/
{
	"query":{
		"query_string":{
			"default_field":"title",
			"query":"java"
		}
	}
}
3.4.4 IK分词器
3.4.4.1分词器效果查看

注意:在7.X版本后分词器写法有更改如果在7.X使用以下写法:

get http://localhost:9200/_analyze?analyzer=standard&text=variously spelled wildcard or wild-card,alse known as at-large berth

返回结果报错为:

{
    "error": {
        "root_cause": [
            {
                "type": "parse_exception",
                "reason": "request body or source parameter is required"
            }
        ],
        "type": "parse_exception",
        "reason": "request body or source parameter is required"
    },
    "status": 400
}

正确写法:

get http://localhost:9200/_analyze
{
	"analyzer":"standard",
	"text":"variously spelled wildcard or wild-card,alse known as at-large berth"
}
3.4.4.2 安装IK插件

1> https://github.com/medcl/elasticsearch-analysis-ik 下载对应版本IK

下载的版本一定要与ES的版本对应,如果不是闲得慌,千万不要尝试最新版的ES和IK

2> 解压后放到plugins目录,重启即可!

3.4.4.3 重新构建以IK为基础的索引
put http://localhost:9200/blog
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "id": {"type": "long", "store": "true","analyzer": "ik_max_word", "search_analyzer": "ik_smart"},
      "title": {"type": "text","store":"true","analyzer": "ik_max_word", "search_analyzer": "ik_smart"},
      "content": {"type": "text","store":"true","analyzer": "ik_max_word", "search_analyzer": "ik_smart"}
    }
  }
}

新建时报错:

{	"type": "access_control_exception",
	"reason": "access denied (\"java.io.FilePermission\" \"D:\\Program%20Files%20(x86)\\programer\\elasticsearch-7.4.0\\plugins\\elasticsearch-analysis-ik-7.4.0\\config\\IKAnalyzer.cfg.xml\" \"read\")"}

原因:elasticsearch安装路径存在空格

更改路径后继续报错:

{
    "error": {
        "root_cause": [
            {
                "type": "mapper_parsing_exception",
                "reason": "Mapping definition for [id] has unsupported parameters:  [analyzer : ik_smart]"
            }
        ],
        "type": "mapper_parsing_exception",
        "reason": "Failed to parse mapping [_doc]: Mapping definition for [id] has unsupported parameters:  [analyzer : ik_smart]",
        "caused_by": {
            "type": "mapper_parsing_exception",
            "reason": "Mapping definition for [id] has unsupported parameters:  [analyzer : ik_smart]"
        }
    },
    "status": 400
}

原因:ID不能用IK,删掉ID那一行的analyzer和search_analyzer即可

3.4.4.3 对查询结果高亮
post http://localhost:9200/blog/_search
{
    "query" : { "match" : { "content" : "校车" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}

结果:

"hits": [
    {
        "_index": "blog",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
            "id": "2",
            "title": "外国人永久居留条例!",
            "content": "美国留给伊拉克的是个烂摊子吗!公安部:各地校车将享最高路权"
        },
        "highlight": {
            "content": [
                "公安部:各地<tag1>校车</tag1>将享最高路权"
            ]
        }
    }
]

四 评分机制

4.1 tfidf算法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您下载了本程序,但是该程序存在问题无法运行,那么您可以选择退款或者寻求我们的帮助(如果找我们帮助的话,是需要追加额外费用的)。另外,您不会使用资源的话(这种情况不支持退款),也可以找我们帮助(需要追加额外费用) 爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值