居然是你

Java连接到ElasticSearch 5.2 报错

Elasticsearch李远 回复了问题 • 7 人关注 • 6 个回复 • 16090 次浏览 • 2018-06-20 15:28 • 来自相关话题

elasticsearch-jdbc导入oracle出问题

Elasticsearchjutem 回复了问题 • 2 人关注 • 1 个回复 • 3230 次浏览 • 2017-02-08 16:10 • 来自相关话题

payload信息如何建立索引

Elasticsearchmedcl 回复了问题 • 2 人关注 • 1 个回复 • 3733 次浏览 • 2017-04-17 09:52 • 来自相关话题

集群索引数建议?

Elasticsearchrebornhuang 回复了问题 • 5 人关注 • 2 个回复 • 7433 次浏览 • 2019-12-25 15:55 • 来自相关话题

elasticsearch query 语法和字段分词问题

Elasticsearchkennywu76 回复了问题 • 3 人关注 • 1 个回复 • 4383 次浏览 • 2017-02-07 16:28 • 来自相关话题

我使用了es5.1.1版本,安装了x-pack,启用了密码验证,现在我安装head插件,怎么设置head密码配置,使得head可以访问es

Elasticsearchmedcl 回复了问题 • 3 人关注 • 1 个回复 • 15283 次浏览 • 2017-02-23 15:18 • 来自相关话题

Elasticsearch 5.x 字段折叠的使用

Elasticsearchmedcl 发表了文章 • 27 个评论 • 47262 次浏览 • 2017-02-03 18:45 • 来自相关话题

199aon3omgg1vjpg.jpg

 在 Elasticsearch 5.x 有一个字段折叠(Field Collapsing,#22337)的功能非常有意思,在这里分享一下,
 
字段折叠是一个很有历史的需求了,可以看这个 issue,编号#256,最初是2010年7月提的issue,也是讨论最多的帖子之一(240+评论),熬了6年才支持的特性,你说牛不牛,哈哈。
 
目测该特性将于5.3发布,尝鲜地址:Elasticsearch-5.3.0-SNAPSHOT,文档地址:search-request-collapse
 
So,什么是字段折叠,可以理解就是按特定字段进行合并去重,比如我们有一个菜谱搜索,我希望按菜谱的“菜系”字段进行折叠,即返回结果每个菜系都返回一个结果,也就是按菜系去重,我搜索关键字“鱼”,要去返回的结果里面各种菜系都有,有湘菜,有粤菜,有中餐,有西餐,别全是湘菜,就是这个意思,通过按特定字段折叠之后,来丰富搜索结果的多样性。
 
说到这里,有人肯定会想到,使用 term agg+ top hits agg 来实现啊,这种组合两种聚和的方式可以实现上面的功能,不过也有一些局限性,比如,不能分页,#4915;结果不够精确(top term+top hits,es 的聚合实现选择了牺牲精度来提高速度);数据量大的情况下,聚合比较慢,影响搜索体验。
 
而新的的字段折叠的方式是怎么实现的的呢,有这些要点:
  1. 折叠+取 inner_hits 分两阶段执行(组合聚合的方式只有一个阶段),所以 top hits 永远是精确的。
  2. 字段折叠只在 top hits 层执行,不需要每次都在完整的结果集上对为每个折叠主键计算实际的 doc values 值,只对 top hits 这小部分数据操作就可以,和 term agg 相比要节省很多内存。
  3. 因为只在 top hits 上进行折叠,所以相比组合聚合的方式,速度要快很多。
  4. 折叠 top docs 不需要使用全局序列(global ordinals)来转换 string,相比 agg 这也节省了很多内存。
  5. 分页成为可能,和常规搜索一样,具有相同的局限,先获取 from+size 的内容,再合并。
  6. search_after 和 scroll 暂未实现,不过具备可行性。
  7.  折叠只影响搜索结果,不影响聚合,搜索结果的 total 是所有的命中纪录数,去重的结果数未知(无法计算)。

 
下面来看看具体的例子,就知道怎么回事了,使用起来很简单。
  • 先准备索引和数据,这里以菜谱为例,name:菜谱名,type 为菜系,rating 为用户的累积平均评分

DELETE recipes
PUT recipes
POST recipes/type/_mapping
{
"properties": {
"name":{
"type": "text"
},
"rating":{
"type": "float"
},"type":{
"type": "keyword"
}
}
}
POST recipes/type/
{
"name":"清蒸鱼头","rating":1,"type":"湘菜"
}

POST recipes/type/
{
"name":"剁椒鱼头","rating":2,"type":"湘菜"
}

POST recipes/type/
{
"name":"红烧鲫鱼","rating":3,"type":"湘菜"
}

POST recipes/type/
{
"name":"鲫鱼汤(辣)","rating":3,"type":"湘菜"
}

POST recipes/type/
{
"name":"鲫鱼汤(微辣)","rating":4,"type":"湘菜"
}

POST recipes/type/
{
"name":"鲫鱼汤(变态辣)","rating":5,"type":"湘菜"
}

POST recipes/type/
{
"name":"广式鲫鱼汤","rating":5,"type":"粤菜"
}

POST recipes/type/
{
"name":"鱼香肉丝","rating":2,"type":"川菜"
}

POST recipes/type/
{
"name":"奶油鲍鱼汤","rating":2,"type":"西菜"

  • 现在我们看看普通的查询效果是怎么样的,搜索关键字带“鱼”的菜,返回3条数据

POST recipes/type/_search
{
"query": {"match": {
"name": "鱼"
}},"size": 3
全是湘菜,我的天,最近上火不想吃辣,这个第一页的结果对我来说就是垃圾,如下:
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": 0.26742277,
    "hits": [
      {
        "_index": "recipes",
        "_type": "type",
        "_id": "AVoESHYF_OA-dG63Txsd",
        "_score": 0.26742277,
        "_source": {
          "name": "鲫鱼汤(变态辣)",
          "rating": 5,
          "type": "湘菜"
        }
      },
      {
        "_index": "recipes",
        "_type": "type",
        "_id": "AVoESHXO_OA-dG63Txsa",
        "_score": 0.19100356,
        "_source": {
          "name": "红烧鲫鱼",
          "rating": 3,
          "type": "湘菜"
        }
      },
      {
        "_index": "recipes",
        "_type": "type",
        "_id": "AVoESHWy_OA-dG63TxsZ",
        "_score": 0.19100356,
        "_source": {
          "name": "剁椒鱼头",
          "rating": 2,
          "type": "湘菜"
        }
      }
    ]
  }
}
我们再看看,这次我想加个评分排序,大家都喜欢的是那些,看看有没有喜欢吃的,执行查询:
POST recipes/type/_search
{
"query": {"match": {
"name": "鱼"
}},"sort": [
{
"rating": {
"order": "desc"
}
}
],"size": 3
结果稍微好点了,不过3个里面2个是湘菜,还是有点不合适,结果如下:
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": null,
    "hits": [
      {
        "_index": "recipes",
        "_type": "type",
        "_id": "AVoESHYF_OA-dG63Txsd",
        "_score": null,
        "_source": {
          "name": "鲫鱼汤(变态辣)",
          "rating": 5,
          "type": "湘菜"
        },
        "sort": [
          5
        ]
      },
      {
        "_index": "recipes",
        "_type": "type",
        "_id": "AVoESHYW_OA-dG63Txse",
        "_score": null,
        "_source": {
          "name": "广式鲫鱼汤",
          "rating": 5,
          "type": "粤菜"
        },
        "sort": [
          5
        ]
      },
      {
        "_index": "recipes",
        "_type": "type",
        "_id": "AVoESHX7_OA-dG63Txsc",
        "_score": null,
        "_source": {
          "name": "鲫鱼汤(微辣)",
          "rating": 4,
          "type": "湘菜"
        },
        "sort": [
          4
        ]
      }
    ]
  }
}
现在我知道了,我要看看其他菜系,这家不是还有西餐、广东菜等各种菜系的么,来来,帮我每个菜系来一个菜看看,换 terms agg 先得到唯一的 term 的 bucket,再组合 top_hits agg,返回按评分排序的第一个 top hits,有点复杂,没关系,看下面的查询就知道了:
GET recipes/type/_search
{
"query": {
"match": {
"name": "鱼"
}
},
"sort": [
{
"rating": {
"order": "desc"
}
}
],"aggs": {
"type": {
"terms": {
"field": "type",
"size": 10
},"aggs": {
"rated": {
"top_hits": {
"sort": [{
"rating": {"order": "desc"}
}],
"size": 1
}
}
}
}
},
"size": 0,
"from": 0
看下面的结果,虽然 json 结构有点复杂,不过总算是我们想要的结果了,湘菜、粤菜、川菜、西菜都出来了,每样一个,不重样:
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "type": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "湘菜",
          "doc_count": 6,
          "rated": {
            "hits": {
              "total": 6,
              "max_score": null,
              "hits": [
                {
                  "_index": "recipes",
                  "_type": "type",
                  "_id": "AVoESHYF_OA-dG63Txsd",
                  "_score": null,
                  "_source": {
                    "name": "鲫鱼汤(变态辣)",
                    "rating": 5,
                    "type": "湘菜"
                  },
                  "sort": [
                    5
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "川菜",
          "doc_count": 1,
          "rated": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "recipes",
                  "_type": "type",
                  "_id": "AVoESHYr_OA-dG63Txsf",
                  "_score": null,
                  "_source": {
                    "name": "鱼香肉丝",
                    "rating": 2,
                    "type": "川菜"
                  },
                  "sort": [
                    2
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "粤菜",
          "doc_count": 1,
          "rated": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "recipes",
                  "_type": "type",
                  "_id": "AVoESHYW_OA-dG63Txse",
                  "_score": null,
                  "_source": {
                    "name": "广式鲫鱼汤",
                    "rating": 5,
                    "type": "粤菜"
                  },
                  "sort": [
                    5
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "西菜",
          "doc_count": 1,
          "rated": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "recipes",
                  "_type": "type",
                  "_id": "AVoESHY3_OA-dG63Txsg",
                  "_score": null,
                  "_source": {
                    "name": "奶油鲍鱼汤",
                    "rating": 2,
                    "type": "西菜"
                  },
                  "sort": [
                    2
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}
上面的实现方法,前面已经说了,可以做,有局限性,那看看新的字段折叠法如何做到呢,查询如下,加一个 collapse 参数,指定对那个字段去重就行了,这里当然对菜系“type”字段进行去重了:
GET recipes/type/_search
{
"query": {
"match": {
"name": "鱼"
}
},
"collapse": {
"field": "type"
},
"size": 3,
"from": 0
}
结果很理想嘛,命中结果还是熟悉的那个味道(和查询结果长的一样嘛),如下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoDNlRJ_OA-dG63TxpW",
"_score": 0.018980097,
"_source": {
"name": "鲫鱼汤(微辣)",
"rating": 4,
"type": "湘菜"
},
"fields": {
"type": [
"湘菜"
]
}
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoDNlRk_OA-dG63TxpZ",
"_score": 0.013813315,
"_source": {
"name": "鱼香肉丝",
"rating": 2,
"type": "川菜"
},
"fields": {
"type": [
"川菜"
]
}
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoDNlRb_OA-dG63TxpY",
"_score": 0.0125863515,
"_source": {
"name": "广式鲫鱼汤",
"rating": 5,
"type": "粤菜"
},
"fields": {
"type": [
"粤菜"
]
}
}
]
}
}
我再试试翻页,把 from 改一下,现在返回了3条数据,from 改成3,新的查询如下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoDNlRw_OA-dG63Txpa",
"_score": 0.012546891,
"_source": {
"name": "奶油鲍鱼汤",
"rating": 2,
"type": "西菜"
},
"fields": {
"type": [
"西菜"
]
}
}
]
}
}
上面的结果只有一条了,去重之后本来就只有4条数据,上面的工作正常,每个菜系只有一个菜啊,那我不乐意了,帮我每个菜系里面多返回几条,我好选菜啊,加上参数 inner_hits 来控制返回的条数,这里返回2条,按 rating 也排个序,新的查询构造如下:
GET recipes/type/_search
{
"query": {
"match": {
"name": "鱼"
}
},
"collapse": {
"field": "type",
"inner_hits": {
"name": "top_rated",
"size": 2,
"sort": [
{
"rating": "desc"
}
]
}
},
"sort": [
{
"rating": {
"order": "desc"
}
}
],
"size": 2,
"from": 0
}
查询结果如下,完美:
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": null,
    "hits": [
      {
        "_index": "recipes",
        "_type": "type",
        "_id": "AVoESHYF_OA-dG63Txsd",
        "_score": null,
        "_source": {
          "name": "鲫鱼汤(变态辣)",
          "rating": 5,
          "type": "湘菜"
        },
        "fields": {
          "type": [
            "湘菜"
          ]
        },
        "sort": [
          5
        ],
        "inner_hits": {
          "top_rated": {
            "hits": {
              "total": 6,
              "max_score": null,
              "hits": [
                {
                  "_index": "recipes",
                  "_type": "type",
                  "_id": "AVoESHYF_OA-dG63Txsd",
                  "_score": null,
                  "_source": {
                    "name": "鲫鱼汤(变态辣)",
                    "rating": 5,
                    "type": "湘菜"
                  },
                  "sort": [
                    5
                  ]
                },
                {
                  "_index": "recipes",
                  "_type": "type",
                  "_id": "AVoESHX7_OA-dG63Txsc",
                  "_score": null,
                  "_source": {
                    "name": "鲫鱼汤(微辣)",
                    "rating": 4,
                    "type": "湘菜"
                  },
                  "sort": [
                    4
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "recipes",
        "_type": "type",
        "_id": "AVoESHYW_OA-dG63Txse",
        "_score": null,
        "_source": {
          "name": "广式鲫鱼汤",
          "rating": 5,
          "type": "粤菜"
        },
        "fields": {
          "type": [
            "粤菜"
          ]
        },
        "sort": [
          5
        ],
        "inner_hits": {
          "top_rated": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "recipes",
                  "_type": "type",
                  "_id": "AVoESHYW_OA-dG63Txse",
                  "_score": null,
                  "_source": {
                    "name": "广式鲫鱼汤",
                    "rating": 5,
                    "type": "粤菜"
                  },
                  "sort": [
                    5
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}
好了,字段折叠介绍就到这里。

ES5.1中put _mapping的问题

Elasticsearchrockybean 回复了问题 • 2 人关注 • 1 个回复 • 2356 次浏览 • 2017-02-04 18:17 • 来自相关话题

elasticsearch严重问题

Elasticsearchwangjueying 回复了问题 • 2 人关注 • 1 个回复 • 4132 次浏览 • 2017-02-03 15:06 • 来自相关话题

Elastic Stack 5.2.0 发布

资讯动态medcl 发表了文章 • 1 个评论 • 6979 次浏览 • 2017-02-03 13:26 • 来自相关话题

本周三,Elastic 发布了 ElasticStack 的全新版本5.2.0,包含了很多激动人心的特性,让我们 一起来看看吧,
(Elastic Stack 包括 Elasticsearch、Logstash、Kibana、Beats):
 Elasticsearch 5.2.0 主要亮点:
  1. 新增数字和日期范围字段类型,非常方便对范围类型进行交并集的查询,比如你的数据是日历类型的数据,每天都有一些会议信息,会议的开始和结束时间都不同,你想看本周那天有空,可以使用 range fields 很方便的进行查询。了解更多
  2. 新增分片分配解释 API,会告知分片失败的具体原因,如分片损坏、磁盘写满还是配置错误,进行快速运维,以前只能根据经验到处寻找可能是什么问题,费事费力,了解更多
  3. 对 Keyword 类型标准化,在5.0将 string 类型拆成了 text 和 keyword两种类型,text 支持分词,keyword 则不分词默认支持 doc values,不过有时候你还需要对 keyword 类型进行一些标准化处理,如都转成小写,现在可以使用 normalizers 参数来指定标准化的 filter。
  4. Terms 聚合的分区,terms 聚合默认返回10个 term,以前如果你需要返回全部的 term 列表是不可能的任务(基于内存压力),现在你可以通过将请求分区,以多次请求的方式来取回这些数据,了解更多
  5. 字段折叠,在搜索时可以按某个字段的值进行折叠,在每个折叠的值内进行排序选取 topN,了解更多

 
相关链接:
Download Elasticsearch 5.2.0
Elasticsearch 5.2.0 release notes
Elasticsearch 5.2.0 breaking changes
X-Pack 5.2.0 release notes
 
Logstash 5.2.0 主要亮点:
  1. 新的监控 UI,现在 X-Pack 也能监控 logstash 了,X-Pack 基础免费版就包括,如下图:
    MonitoringUI-rel-blog.png
    Screen_Shot_2017-01-09_at_16.51_.49_.png
  2. 更多的监控 API,新增3个类型的统计信息:cgroup、持久化队列和 output新增持续时间字段。
  3. 离线插件管理,在很多没有公网的部署环境,都需要离线安装,以前的离线安装不够完善,尽管大部分插件没问题,但是还是存在个别插件的依赖链下载不完整的问题,为了解决这个问题,我们基本上重新设计了整个工作流程使用了新的方式来打包插件和他所有的依赖,了解更多

 
相关连接:
Download Logstash 5.2.0
Logstash 5.2.0 release notes
 
Kibana 5.2.0 主要亮点:
  1. 支持 Elasticsearch Tribe 节点,在“admin”集群的基础上,引入了新的“data”集群,“data”集群可以理解成 Kibana 后面的数据来源,可以是 tribe 节点,而“admin”集群是存放 kibana 可视化信息“。kibana”索引的地方,默认 data 和 admin 集群都是在同一个集群,且不能是 tribe 节点,目前还有一些细节正在处理,如 console 还不能很好的工作。
  2. 增加新的可视化类型:热点图(heatmap),可以很方便的按区间和按时间来显示数据的范围分布,如下图:
    Heatmap_Linechart.png
  3. 开始进行国际化的支持,感谢 IBM 团队的努力,目前已经提供了基础的框架支持,虽然是万里长征的第一大步,但也是非常激动人心的。
  4. 地图服务的改进,Elastic 自己的 Tile 地图服务已经上线几个月了,我们现在能提供10个级别的缩放了,X-Pack 基础用户可以达到12个级别的缩放,并且我们正在尝试18个级别的缩放,并且从5.2开始,我们能让这些级别动态调整,不用发布新的 Kibana。
  5. 监控容器中的 Elasticsearch,现在我们可以监控容器里面的 Elasticsearch 实例的运行情况了,CPU 利用率、GC、堆栈使用情况等,如下图:
    monitoring-elasticsearch-in-containers.png

 
相关连接:
Download Kibana 5.2.0
Kibana 5.2.0 release notes
 
Beats 5.2.0 主要亮点:
  1. 新增 Beat:Heartbeat,一个新的正式的官方beat 成员,用于可用性监控,和所有的 beat 一样,轻量级,Heartbeat 可以用于很多场景,比如安全,你不希望暴露某个端口时,使用 Heartbeat,当你发现该端口对外开启了,就可以触发通知,或者服务/网站可用性检测,服务down 了可以及时感知,目前支持:ICMP、TCP 和 HTTP 类型的监控,目前 Heartbeat还处于 beat 阶段,暂不推荐用于生产环境。
  2. Metricbeat 可跟踪网络连接,从5.2开始,Metricbeat 导出了 linux 系统的应用程序的网络连接信息,每个进程打开的 tcp 套接字,本地及远程的 ip 都包含在内,基于它,你可以进行如下的图分析:
    connections-2.png
  3. 收集Prometheus导出的指标,从5.2开始,监控系统普罗米修斯的收集模块导出的数据可以提供给 Metricbeat 然后索引进 Elasticsearch。

 
相关连接:
Download Beats 5.2.0
Beats 5.2.0 release notes
 
Elastic Stack 下载链接:https://www.elastic.co/downloads
Bug 反馈:http://github.com/elastic

以后有版本的更新消息都会在这里发布一份中文版,欢迎大家关注。

x-pack java client 方式访问elasticsearch5.1.1集群出现异常

Elasticsearchkepmoving 回复了问题 • 2 人关注 • 1 个回复 • 6688 次浏览 • 2017-02-10 17:06 • 来自相关话题

es sort 默认值为-9223372036854776000,可以修改吗?

回复

Elasticsearchyuanzhiwei 发起了问题 • 1 人关注 • 0 个回复 • 6702 次浏览 • 2017-01-24 18:02 • 来自相关话题

docker安装elasticsearch 5.2.1 x-pack 无法登陆

Elasticsearchchenchongpan 回复了问题 • 4 人关注 • 2 个回复 • 8977 次浏览 • 2017-06-08 17:17 • 来自相关话题

pinyin分词 match和match_phrase的问题?

Elasticsearchmedcl 回复了问题 • 3 人关注 • 2 个回复 • 4090 次浏览 • 2017-01-24 21:08 • 来自相关话题

logstash-input-kafka后无数据

Logstashmess 回复了问题 • 3 人关注 • 2 个回复 • 3476 次浏览 • 2017-02-22 17:15 • 来自相关话题