- 浏览: 150153 次
文章分类
最新评论
-
x_looking:
Client client = new TransportCl ...
ELASTICSEARCH常见问题 -
辣de冷wmyes:
ElasticSearch视频教程百度网盘地址:http:// ...
一、Elasticsearch安装使用教程
实例
http://www.bubuko.com/infodetail-648214.html
GET _search
{
"query": {
"match_all": {}
}
}
PUT /useraudit
POST /useraudit/t_unparticiple_test/_mapping
{
"t_unparticiple_test": {
"properties": {
"uid": {
"type": "long"
},
"country": {
"type": "string"
},
"city": {
"type": "string"
},
"area": {
"type": "string"
},
"address": {
"type": "string"
},
"time": {
"type": "long"
}
}
}
}
POST /useraudit/t_participle_test/_mapping
{
"t_participle_test": {
"properties": {
"uid": {
"type": "long"
},
"country": {
"type": "string"
},
"city": {
"type": "string"
},
"area": {
"type": "string"
},
"address": {
"type": "string",
"include_in_all": true,
"analyzer": "ik",
"term_vector": "with_positions_offsets"
},
"time": {
"type": "long"
}
}
}
}
POST /useraudit/t_participle_index_test/_mapping
{
"t_participle_index_test": {
"properties": {
"uid": {
"type": "long",
"index": "not_analyzed"
},
"country": {
"type": "string",
"index": "not_analyzed"
},
"city": {
"type": "string",
"index": "not_analyzed"
},
"area": {
"type": "string",
"index": "not_analyzed"
},
"address": {
"type": "string",
"include_in_all": true,
"analyzer": "ik",
"term_vector": "with_positions_offsets"
},
"time": {
"type": "long",
"index": "not_analyzed"
}
}
}
}
PUT /useraudit/t_unparticiple_test/1
{
"uid": 200111,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路二段139号"
}
PUT /useraudit/t_unparticiple_test/6
{
"uid": 200111,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "东坡胡公园"
}
PUT /useraudit/t_unparticiple_test/2
{
"uid": 200115,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路西三段123号冒菜店"
}
PUT /useraudit/t_unparticiple_test/3
{
"uid": 362201,
"country": "浙江省",
"city":"杭州市",
"area":"萧山区",
"time": 1441293300,
"address": "宁围镇二桥村顺发康庄4栋1单元902"
}
PUT /useraudit/t_unparticiple_test/4
{
"uid": 362203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "新都工业区文东路852号天幕家纺"
}
PUT /useraudit/t_unparticiple_test/5
{
"uid": 662203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "低塘街道新堰西路45号"
}
PUT /useraudit/t_participle_test/1
{
"uid": 200111,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路二段139号"
}
PUT /useraudit/t_participle_test/2
{
"uid": 200115,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路西三段123号冒菜店"
}
PUT /useraudit/t_participle_test/3
{
"uid": 362201,
"country": "浙江省",
"city":"杭州市",
"area":"萧山区",
"time": 1441293300,
"address": "宁围镇二桥村顺发康庄4栋1单元902"
}
PUT /useraudit/t_participle_test/4
{
"uid": 362203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "新都工业区文东路852号天幕家纺"
}
PUT /useraudit/t_participle_test/5
{
"uid": 662203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "低塘街道新堰西路45号"
}
PUT /useraudit/t_participle_index_test/1
{
"uid": 200111,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路二段139号"
}
PUT /useraudit/t_participle_index_test/2
{
"uid": 200115,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路西三段123号冒菜店"
}
PUT /useraudit/t_participle_index_test/3
{
"uid": 362201,
"country": "浙江省",
"city":"杭州市",
"area":"萧山区",
"time": 1441293300,
"address": "宁围镇二桥村顺发康庄4栋1单元902"
}
PUT /useraudit/t_participle_index_test/4
{
"uid": 362203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "新都工业区文东路852号天幕家纺"
}
PUT /useraudit/t_participle_index_test/5
{
"uid": 662203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "低塘街道新堰西路45号"
}
#精确查询
GET /useraudit/t_unparticiple_test/_search?q=uid:200111
GET /useraudit/t_unparticiple_test/_search
{
"query": {
"match": {
"uid": "200111"
}
}
}
# 字段为strin类型,mappin中未定义取消分词,精确查询不到数据
GET /useraudit/t_unparticiple_test/_search
{
"query": {
"match": {
"city": "衢州市"
}
}
}
GET /useraudit/t_participle_test/_search
{
"query": {
"match": {
"country": "四川省"
}
}
}
# 字段为strin类型,mappin中定义取消分词,精确查询数据
GET /useraudit/t_participle_index_test/_search
{
"query": {
"match": {
"country": "四川省"
}
}
}
#模糊查询
GET /useraudit/t_participle_test/_search
{
"query" : { "term" : { "address" : "红星" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"address" : {}
}
}
}
GET /useraudit/t_participle_index_test/_search
{
"query" : { "term" : { "address" : "红星" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"address" : {}
}
}
}
POST /index/fulltext/_search
{
"query" : { "term" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
二, java客户端调用Elasticsearch集群
1, 精准查询
2, 模糊查询(使用分词)
三, 索引数据的维护
四, 批量插入数据
测试数据量5千万
五, 压力测试并发精准查询和模糊查询
http://www.bubuko.com/infodetail-648214.html
GET _search
{
"query": {
"match_all": {}
}
}
PUT /useraudit
POST /useraudit/t_unparticiple_test/_mapping
{
"t_unparticiple_test": {
"properties": {
"uid": {
"type": "long"
},
"country": {
"type": "string"
},
"city": {
"type": "string"
},
"area": {
"type": "string"
},
"address": {
"type": "string"
},
"time": {
"type": "long"
}
}
}
}
POST /useraudit/t_participle_test/_mapping
{
"t_participle_test": {
"properties": {
"uid": {
"type": "long"
},
"country": {
"type": "string"
},
"city": {
"type": "string"
},
"area": {
"type": "string"
},
"address": {
"type": "string",
"include_in_all": true,
"analyzer": "ik",
"term_vector": "with_positions_offsets"
},
"time": {
"type": "long"
}
}
}
}
POST /useraudit/t_participle_index_test/_mapping
{
"t_participle_index_test": {
"properties": {
"uid": {
"type": "long",
"index": "not_analyzed"
},
"country": {
"type": "string",
"index": "not_analyzed"
},
"city": {
"type": "string",
"index": "not_analyzed"
},
"area": {
"type": "string",
"index": "not_analyzed"
},
"address": {
"type": "string",
"include_in_all": true,
"analyzer": "ik",
"term_vector": "with_positions_offsets"
},
"time": {
"type": "long",
"index": "not_analyzed"
}
}
}
}
PUT /useraudit/t_unparticiple_test/1
{
"uid": 200111,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路二段139号"
}
PUT /useraudit/t_unparticiple_test/6
{
"uid": 200111,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "东坡胡公园"
}
PUT /useraudit/t_unparticiple_test/2
{
"uid": 200115,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路西三段123号冒菜店"
}
PUT /useraudit/t_unparticiple_test/3
{
"uid": 362201,
"country": "浙江省",
"city":"杭州市",
"area":"萧山区",
"time": 1441293300,
"address": "宁围镇二桥村顺发康庄4栋1单元902"
}
PUT /useraudit/t_unparticiple_test/4
{
"uid": 362203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "新都工业区文东路852号天幕家纺"
}
PUT /useraudit/t_unparticiple_test/5
{
"uid": 662203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "低塘街道新堰西路45号"
}
PUT /useraudit/t_participle_test/1
{
"uid": 200111,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路二段139号"
}
PUT /useraudit/t_participle_test/2
{
"uid": 200115,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路西三段123号冒菜店"
}
PUT /useraudit/t_participle_test/3
{
"uid": 362201,
"country": "浙江省",
"city":"杭州市",
"area":"萧山区",
"time": 1441293300,
"address": "宁围镇二桥村顺发康庄4栋1单元902"
}
PUT /useraudit/t_participle_test/4
{
"uid": 362203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "新都工业区文东路852号天幕家纺"
}
PUT /useraudit/t_participle_test/5
{
"uid": 662203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "低塘街道新堰西路45号"
}
PUT /useraudit/t_participle_index_test/1
{
"uid": 200111,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路二段139号"
}
PUT /useraudit/t_participle_index_test/2
{
"uid": 200115,
"country": "四川省",
"city":"眉山市",
"area":"东坡区",
"time": 1441293300,
"address": "红星东路西三段123号冒菜店"
}
PUT /useraudit/t_participle_index_test/3
{
"uid": 362201,
"country": "浙江省",
"city":"杭州市",
"area":"萧山区",
"time": 1441293300,
"address": "宁围镇二桥村顺发康庄4栋1单元902"
}
PUT /useraudit/t_participle_index_test/4
{
"uid": 362203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "新都工业区文东路852号天幕家纺"
}
PUT /useraudit/t_participle_index_test/5
{
"uid": 662203,
"country": "浙江省",
"city":"衢州市",
"area":"常山县",
"time": 1441293300,
"address": "低塘街道新堰西路45号"
}
#精确查询
GET /useraudit/t_unparticiple_test/_search?q=uid:200111
GET /useraudit/t_unparticiple_test/_search
{
"query": {
"match": {
"uid": "200111"
}
}
}
# 字段为strin类型,mappin中未定义取消分词,精确查询不到数据
GET /useraudit/t_unparticiple_test/_search
{
"query": {
"match": {
"city": "衢州市"
}
}
}
GET /useraudit/t_participle_test/_search
{
"query": {
"match": {
"country": "四川省"
}
}
}
# 字段为strin类型,mappin中定义取消分词,精确查询数据
GET /useraudit/t_participle_index_test/_search
{
"query": {
"match": {
"country": "四川省"
}
}
}
#模糊查询
GET /useraudit/t_participle_test/_search
{
"query" : { "term" : { "address" : "红星" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"address" : {}
}
}
}
GET /useraudit/t_participle_index_test/_search
{
"query" : { "term" : { "address" : "红星" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"address" : {}
}
}
}
POST /index/fulltext/_search
{
"query" : { "term" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
二, java客户端调用Elasticsearch集群
1, 精准查询
2, 模糊查询(使用分词)
public class ESQueryClient { static Client CLIENT = null; static { // on startup Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch_useraudit").put("client.transport.sniff", true) .build(); CLIENT = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9301)) .addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9302)) .addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9303)); } public static void main(String[] args) throws ElasticsearchException, IOException { // 通过id查询 qryById(); // 通过某个字段查询,例如uid, country SearchRequestBuilder builder = CLIENT.prepareSearch("useraudit").setTypes("t_participle_test").setSearchType(SearchType.DEFAULT) .setFrom(0).setSize(100); BoolQueryBuilder qb = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder("眉山市").field("city")); builder.setQuery(qb); SearchResponse response = builder.execute().actionGet(); System.out.println("字段为strin类型, mappin中未定义取消分词,精确查询不到数据, 测试结果:"); System.out.println("通过city字段查询:" + response); System.out.println("通过city字段查询信息数量:" + response.getHits().getTotalHits()); // 通过某个字段查询,例如uid, country builder = CLIENT.prepareSearch("useraudit").setTypes("t_participle_index_test").setSearchType(SearchType.DEFAULT).setFrom(0).setSize(100); qb = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder("眉山市").field("city")); builder.setQuery(qb); response = builder.execute().actionGet(); System.out.println("字段为strin类型, mappin中定义取消分词,精确查询数据 , 测试结果:"); System.out.println("通过city字段查询:" + response); System.out.println("通过city字段查询信息数量:" + response.getHits().getTotalHits()); // .should(new QueryStringQueryBuilder("太多").field("city")) CLIENT.close(); } private static void qryById() { GetResponse response = CLIENT.prepareGet("useraudit", "t_participle_test", "1").setOperationThreaded(false).execute().actionGet(); System.out.println("通过id查询:" + response.getSourceAsString()); } }
三, 索引数据的维护
四, 批量插入数据
测试数据量5千万
五, 压力测试并发精准查询和模糊查询
发表评论
-
使用 Scripted Metric Aggregation 遇到的问题
2016-02-19 14:12 2480使用Scripted Metric Aggregation进行 ... -
ElasticSearch性能优化策略
2016-02-17 19:42 3562ElasticSearch性能优化主要分为4个方面的优化。 一 ... -
Elasticsearch 数据建模 - 处理关联关系
2016-02-17 14:40 1078数据建模(Modeling Your Data ... -
数据类型转换错误
2016-02-15 18:28 1633异常: Invalid shift value (64) in ... -
elasticsearch 排序异常,关键字冲突
2016-02-03 12:35 5919org.elasticsearch.action.search ... -
ElasticSearch不同类型下同名字段排序错误
2016-02-03 11:18 1955虽然之前知道elasticsearch不同类型下同名字段要慎用 ... -
ElasticSearch immense term错误
2016-02-02 19:07 2684在使用ElasticSearch的过程中遇到了一个immens ... -
ELASTICSEARCH常见问题
2015-09-22 15:39 32241. ELASTICSEARCH建索引过程中崩溃问题追查 by ... -
routing实例1
2015-09-21 18:51 0package com.eg.part1; import j ... -
routing实例
2015-09-21 18:49 0创建索引: PUT /useraudit_v1 创建索引别名: ... -
elasticsearch java调用实例
2015-09-19 23:55 3576http://outofmemory.cn/code-snip ... -
[维护]Elasticsearch零停机时间更新索引配置或迁移索引
2015-09-18 10:16 799另外一篇文章:http://blog.csdn.net/dm_ ... -
elasticsearch 的mapping定义
2015-09-18 10:16 1212elasticsearch 的mapping 例子一: 订单 ... -
ElasticSearch基础杂烩-配置-索引-优化
2015-09-19 23:56 648http://blog.csdn.net/huwei2003/ ... -
Elasticsearch安装中文分词插件ik
2015-09-18 10:15 2047安装步骤: 1、到github ... -
ElasticSearch的各种服务的URL
2015-09-18 10:15 546前言 elasticsearch 将各种功能、配置、服务都以A ... -
ElasticSearch集群搭建
2015-09-19 23:56 619http://www.linuxidc.com/Linux/2 ... -
elasticsearch 配置
2015-09-18 10:16 715elasticsearch.conf 主要是设置一些java运 ... -
elasticsearch中文分词集成
2015-09-17 09:35 615elasticsearch官方只提供smartcn这个中文分词 ... -
elasticsearch的Mapping定义
2015-09-17 09:35 1423Mapping,就是对索引库中 ...
相关推荐
Elasticsearch(ES)是一种流行的开源全文搜索引擎,它基于Lucene库构建,被广泛用于大数据分析、日志聚合、实时搜索和索引等场景。官方提供的测试数据集是检验Elasticsearch功能、性能和稳定性的关键资源,可以帮助...
本主题聚焦于“Elasticsearch Java代码实现”,将深入探讨如何使用Java API来执行基本的操作,如创建索引、删除索引、更新索引、模糊搜索以及模糊全文搜索和精确查找。 首先,让我们从`ESManager.java`开始,这个类...
1. **节点(Node)**: 每个运行Elasticsearch实例的服务器都是一个节点。节点可以加入集群,共同处理和存储数据。 2. **索引(Index)**: 类似于数据库中的表,是存储文档的地方,可以有多个映射(Mapping)定义不同类型...
3. **搜索功能**:Elasticsearch的搜索功能非常强大,支持全文检索、短语匹配、模糊搜索等多种查询方式。最基本的搜索是使用`_search` API,通过HTTP GET请求发送查询语句。查询语句可以是简单的关键词,也可以是...
了解Elasticsearch的高级查询特性是构建高效搜索应用的关键,包括掌握如何使用Query DSL来精确控制搜索过程,如何使用查询模板来简化复杂查询的编写,以及如何处理过滤器以优化查询性能等。作者在书中强调了过滤器在...
在构建高效的全文搜索引擎时,Elasticsearch(简称ES)是一个常用的选择,而IK分词器则是ES中针对中文处理的重要组件。本知识点将详细介绍ElasticSearch的IK分词器及其在微服务项目中的应用。 首先,Elasticsearch...
**Elasticsearch 概述** Elasticsearch 是一个强大的、分布式的实时搜索和分析引擎,其设计...8. **Java 如何使用 Elasticsearch 进行模糊查询** 这些文章将帮助读者深入理解 Elasticsearch 的使用,并提供实践指导。
节点则是Elasticsearch集群中的单个实例,多个节点可以形成一个集群,共享数据并实现负载均衡。 在1.0.1版本中,Elasticsearch支持RESTful API,这意味着你可以通过HTTP协议来与之交互,发送GET、POST、PUT和DELETE...
在实际应用中,elasticsearch-analysis-pinyin插件不仅可以实现基于拼音的全文搜索,还能支持模糊搜索、首字母搜索等功能,极大地增强了Elasticsearch对中文文本的处理能力。例如,用户可以搜索"shanghai"找到"上海...
### Elasticsearch 8.x Cookbook知识点概览 #### 一、Elasticsearch概述 ...通过180多个精心设计的实例,读者可以迅速掌握Elasticsearch的应用技巧,并将其应用于实际工作中,解决复杂的数据检索与分析问题。
Elasticsearch 还提供了丰富的查询语法,支持布尔查询、短语查询、模糊查询等多种高级搜索功能。 **Mapping 映射** Mapping 是 Elasticsearch 中的元数据,用于定义字段的类型和分析规则。合理的映射设置可以优化...
- **Match Query**:匹配整个字段的查询,支持多种模式,如精确匹配、短语匹配、模糊匹配等。 - **Term Query**:用于查找确切的词项,不受分析影响。 - **Range Query**:筛选满足特定范围的数值字段。 - **Bool ...
- **节点(Node)**: 单个 Elasticsearch 实例。 - **集群(Cluster)**: 多个节点组成的集合,共同管理数据并提供高可用性和容错性。 - **分片(Shards)**: 每个索引被分成多个分片,这些分片可以分布在集群中的不同节点...
每个服务器上的 ElasticSearch 实例称为一个节点。 - **分片(Shard)**:当单个节点的计算能力和硬件限制不足以应对大量数据时,可以将数据切分成更小的部分,每个部分称为一个分片。分片实质上是 Apache Lucene ...
通过扩展,Flask可以支持各种数据库、缓存、邮件服务等,而`flask-elasticsearch`就是这样一个扩展,用于连接和操作Elasticsearch实例。 Elasticsearch是一个分布式、RESTful风格的搜索和分析引擎,特别适合处理...
- **搜索操作**:支持全文检索、精确匹配、范围查询、模糊查询等多种查询方式,并提供高亮显示、过滤、排序和聚合等功能。 - **聚合(Aggregations)**:允许对数据进行复杂的统计分析,如平均值、总和、最大值、...
1. 分词:通过Elasticsearch的分析器将用户输入的查询字符串分解为关键词,进行精确匹配或模糊匹配。 2. 高亮显示:在搜索结果中高亮显示匹配的部分,提高用户体验。 3. 排序和评分:根据相关性、日期、自定义权重等...
1. **了解集群结构**: Elasticsearch 集群由一个或多个节点组成,每个节点都是运行 Elasticsearch 的服务器实例。集群中有一个主节点负责协调集群状态和其他节点的工作负载分配。 2. **创建集群**: 首先安装 ...
2. **分布式搜索**:在大型应用中,可以利用Lucene的分布式搜索方案,如Solr或Elasticsearch,以处理大量数据。 3. **缓存策略**:合理配置缓存,如二级缓存,可以减少对数据库的直接访问,提高系统响应速度。 ...