创建一个雇员目录。该目录应当能培养雇员认同感及支持实时、高效、动态协作,因此有一些业务需求:
支持包含多值标签、数值、以及全文本的数据
检索任一雇员的完整信息
允许结构化搜索,比如查询 30 岁以上的员工
允许简单的全文搜索以及较复杂的短语搜索
支持在匹配文档内容中高亮显示搜索片段
支持基于数据创建和管理分析仪表盘
列出可用的索引
curl ‘localhost:9200/_cat/indices?v’
创建雇员目录
雇员目录,我们将做如下操作:
每个雇员索引一个文档,包含该雇员的所有信息。
每个文档都将是 employee 类型 。
该类型位于 索引 megacorp 内。
该索引保存在我们的 Elasticsearch 集群中
查询字符串参数 (例如 ?pretty 将格式化地输出 JSON 返回值,使其更容易阅读)
挖掘雇员中最受欢迎的兴趣
curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
'
注意fielddata 默认为不在全文检索内
官方说明
text field(interests) 已经存在
给已经存在的type employee 添加映射属性
curl -XPUT 'localhost:9200/megacorp/_mapping/employee?pretty' -H 'Content-Type: application/json' -d'
{
"properties": {
"interests": {
"type": "text",
"fielddata": true
}
}
}
'
text field不存在
Before you enable fielddata, consider why you are using a text field for aggregations, sorting, or in a script. It usually doesn’t make sense to do so.
A text field is analyzed before indexing so that a value like New York can be found by searching for new or for york. A terms aggregation on this field will return a new bucket and a york bucket, when you probably want a single bucket called New York.
Instead, you should have a text field for full text searches, and an unanalyzed keyword field with doc_values enabled for aggregations, as follows:
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"my_type": {
"properties": {
"my_field": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
'
text field存在
You can enable fielddata on an existing text field using the PUT mapping API as follows:
curl -XPUT 'localhost:9200/my_index/_mapping/my_type?pretty' -H 'Content-Type: application/json' -d'
{
"properties": {
"my_field": {
"type": "text",
"fielddata": true
}
}
}
'
被多次加载
Fielddata filtering can be used to reduce the number of terms loaded into memory, and thus reduce memory usage. Terms can be filtered by frequency:
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"my_type": {
"properties": {
"tag": {
"type": "text",
"fielddata": true,
"fielddata_frequency_filter": {
"min": 0.001,
"max": 0.1,
"min_segment_size": 500
}
}
}
}
}
}
'