Skip to main content
 首页 » 编程设计

Elasticsearch应用必备知识点

2022年07月19日159落叶无声

Elasticsearch应用必备知识点

本文介绍Elasticsearch实际应用中常用的一些知识内容,包括数据类型介绍、动态模板等。

1. 数据类型

elasticsearch支持多种数据类型,常见核心类型包括:
string:text和keyword
Numberic:scaled_float(需要指定scaling_factor为100,2.34,存储为234)
Date:date
Boolean:boolean

数值类型示例:

PUT my_index 
{ 
  "mappings": { 
    "properties": { 
      "number_of_bytes": { 
        "type": "integer" 
      }, 
      "time_in_seconds": { 
        "type": "float" 
      }, 
      "price": { 
        "type": "scaled_float", 
        "scaling_factor": 100 
      } 
    } 
  } 
} 

上面定义了my_index中三个属性,分别为整形、浮点型和scaling_factor类型。

下面看日期类型示例:

PUT my_index 
{ 
  "mappings": { 
    "properties": { 
      "date": { 
        "type":   "date", 
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd" 
      } 
    } 
  } 
} 

因为日期类型会自动匹配,这里明确了日期格式。

文本类型可以索引或不索引:

PUT my_index 
{ 
  "mappings": { 
    "properties": { 
      "full_name": { 
        "type":  "text" 
      } 
    } 
  } 
} 

text对文件进行分词,如邮件内容,keyword不分词,如邮件地址。也可以两者同时使用示例:

PUT my_index 
{ 
  "mappings": { 
    "properties": { 
      "city": { 
        "type": "text", 
        "fields": { 
          "raw": {  
            "type":  "keyword" 
          } 
        } 
      } 
    } 
  } 
} 
 
PUT my_index/_doc/1 
{ 
  "city": "New York" 
} 
 
PUT my_index/_doc/2 
{ 
  "city": "York" 
} 
 
GET my_index/_search 
{ 
  "query": { 
    "match": { 
      "city": "york"  
    } 
  }, 
  "sort": { 
    "city.raw": "asc"  
  }, 
  "aggs": { 
    "Cities": { 
      "terms": { 
        "field": "city.raw"  
      } 
    } 
  } 
} 

因为city字段有索引,同时也保留city.raw为关键字,因此可以对该字段同时进行索引和排序。

2. 中文分词插件

elasticsearch默认中文分词是对每个字进行分词,一般需要使用第三方中文分词插件。这里使用hanlp创建,其默认没有启用过滤停顿词。
我们在创建索引时需在setting中设置启动停顿词:

“analysis”: {
“analyzer” : {
“hanlp_analyzer” : {
“tokenizer” : “nc_hanlp”
}
},
“tokenizer” : {
“nc_hanlp” : {
“type” : “hanlp”,
“enable_stop_dictionary” : true
}
}
}

3.索引

3.1. 获取所有索引

获取所有索引信息,后面结果返回精简版本。

GET /_cat/indices?v 
GET /_cat/aliases?v 

3.2. 查询索引映射

查看具体某个索引的详细内容:

GET /your_index_name/_mapping 
 

3.3. 动态模板

与solar不同,elasticsearch默认无需指定mapping,其根据第一个文档自动匹配相应类型,动态增加新的字段。但没有mapping会影响性能,而且会造成搜索不准确。
其针对字符串类型默认会索引,但对一些如身份证字段不需要索引。但我们可以配置动态模板,明确给符合条件的未知字段设定相应属性。

下面示例定义动态模板,已知字段明确设定类型,未知字段通过动态模板进行指定:

PUT customer_info_index 
{ 
    "settings": {  
            "number_of_shards": 3,    # 分片数量 
            "number_of_replicas": 1,  # 副本数量 
            "analysis": {             # 分析器 
                "analyzer" : { 
                    "hanlp_analyzer" : { 
                        "tokenizer" : "nc_hanlp" 
                    } 
                }, 
                "tokenizer" : { 
                    "nc_hanlp" : { 
                        "type" : "hanlp", 
                        "enable_stop_dictionary" : true 
                    } 
                } 
            } 
    }, 
    "mappings": { 
        "properties": { 
        "comp_name": {"type":  "text"},    # 公司名称    
        "id_type": {"type":  "keyword"},   # 证件类型 
        "id_code": {"type":  "keyword"},   # 证件号码 
        "create_dept": {"type":  "keyword"},             
        "create_date": {"type":  "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"},      
        "flag": {"type":  "keyword"}                          
        }, 
        "dynamic_date_formats": ["yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"], 
        "dynamic_templates": [ 
            { 
                "strings_as_keywords": { 
                    "match_mapping_type": "string", 
                    "mapping": { 
                        "type": "keyword", 
                        "doc_values": false 
                    } 
                } 
            }, 
            { 
                "unindexed_longs": { 
                    "match_mapping_type": "long", 
                    "mapping": { 
                        "type": "long", 
                        "index": false 
                    } 
                } 
            }, 
            { 
                "unindexed_doubles": { 
                    "match_mapping_type": "double", 
                    "mapping": { 
                        "type": "float",  
                        "index": false 
                    } 
                } 
            } 
        ] 
    } 
} 
 

4. 分页

检索结果应该是分页返回,可以通过分页参数from和size进行指定,也可以在查询体内设定:

GET /_search 
{ 
    "from" : 0, "size" : 10, 
    "query" : { 
        "term" : { "user" : "kimchy" } 
    } 
} 

但from+size不能超过index.max_result_window设定值,默认为10000.实际应用中可以让用户仅选择前20页。

5. 总结

简单汇总elasticsearch数据类型、动态模板等几个知识点,并通过示例进行说明,希望对你有点帮助。


本文参考链接:https://blog.csdn.net/neweastsun/article/details/102534121
阅读延展