zombodb 与es 数据类型的映射处理
Postgres 类型 |
Elasticsearch JSON 映射定义 |
bytea |
{"type": "binary"} |
boolean |
{"type": "boolean"} |
smallint |
{"type": "short"} |
integer |
{"type": "integer"} |
bigint |
{"type": "long"} |
real |
{"type": "float"} |
double precision |
{"type": "double"} |
character varying |
{"type": "keyword","copy_to": "zdb_all","normalizer": "lowercase","ignore_above": 10922} |
text |
{"type": "text","analyzer": "zdb_standard","fielddata": true} |
time without time zone |
{"type": "date","format": "HH:mm:ss.SSSSSS","copy_to": "zdb_all"} |
time with time zone |
{"type": "date","format": "HH:mm:ss.SSSSSSZZ","copy_to": "zdb_all"} |
date |
{"type": "date","copy_to": "zdb_all"} |
timestamp without time zone |
{"type": "date","copy_to": "zdb_all"} |
timestamp with time zone |
{"type": "date","copy_to": "zdb_all"} |
json |
{"type": "nested","include_in_parent": true} |
jsonb |
{"type": "nested","include_in_parent": true} |
inet |
{"type": "ip","copy_to": "zdb_all"} |
zdb.fulltext |
{"type": "text","analyzer": "zdb_standard"} |
zdb.fulltext_with_shingles |
{"type": "text","analyzer": "fulltext_with_shingles","search_analyzer": "fulltext_with_shingles_search"} |
character varying (varchar) 没有被es 分词,整个值是被索引的,同时转换为小写
text 类型es 使用标准分析器,同时被转换为小写
json/jsonb 映射为es 的 nested对象
zombodb 自定义的领域类型
- zdb.fulltext 和text 类似,但可以为客户端应用程序提供额外的元数据信息,表明该列可能包含大量内容
- zdb.fulltext_with_shingles 与zdb.fulltext 类型 ,但是通过
2-gram single filter
支持高速的右截断通配符
特定语言类型
支持包好各类语言的支持
自定义分析器
zombodb 支持基于sql 扩展灵活的分析器、、过滤器
FUNCTION zdb.define_analyzer(name text,definition json)
- 使用说明
为了使用自定义分析器你同时必须自定义使用相同名称的pg domain
然后在表中作为列使用,同时
你也可以和自定义字段映射依赖使用通过zdb.define_field_mapping()
函数,注意修改任何分析器需要
重建索引
- token 过滤器
FUNCTION zdb.define_filter(name text,definition json)
FUNCTION zdb.define_char_filter(name text,definition json)
FUNCTION zdb.define_tokenizer(name text,definition json)
FUNCTION zdb.define_normalizer(name text,definition json)
FUNCTION zdb.define_type_mapping(type_name regtype,definition json)
特定字段的映射函数
除过domain type 映射pg type 到es 分析器,我们同时也可以定义表或者表中字段的映射,
注意修改字段的映射需要重建索引
FUNCTION zdb.define_field_mapping(table_name regclass,field_name text,definition json)
- 一个特殊映射函数
比较适合处理只在es 索引中存在的,比如映射到copy_to 属性
FUNCTION zdb.define_es_only_field(table_name regclass,definition json)
参考例子
- 代码
注意官方create index部分有点问题,我添加了url 参数,实际使用可以参考github 项目,有基于docker-compose 的代码
SELECT zdb.define_tokenizer(‘example_tokenizer‘,‘{
"type": "pattern","pattern": "_"
}‘);
SELECT zdb.define_analyzer(‘example‘,‘{
"tokenizer": "example_tokenizer"
}‘);
CREATE DOMAIN example AS text;
CREATE TABLE foo (
id serial8,some_field example
);
CREATE INDEX idxfoo ON foo USING zombodb ((foo.*))
WITH (url=‘http://elasticsearch:9200/‘);
INSERT INTO foo (some_field) VALUES (‘this_is_a_test‘);
SELECT * FROM foo WHERE foo ==> ‘some_field:this‘;
测试分析器
zombodb 内置了一些函数方便我们进行分析器的测试
FUNCTION zdb.analyze_with_field(
index regclass,field text,text text)
RETURNS TABLE (
type text,token text,"position" int,start_offset int,end_offset int)
SELECT * FROM zdb.analyze_with_field(‘idxproducts‘,‘keywords‘,‘this is a test‘);
type | token | position | start_offset | end_offset
------+----------------+----------+--------------+------------
word | this is a test | 0 | 0 | 14
(1 row)
es _all
字段说明
zombodb 禁止了es 的_all
字段使用了自己的字段命名 zdb_all
,默认所有非数字的类型字段都添加到了zdb_all
参考资料
https://github.com/zombodb/zombodb/blob/master/TYPE-MAPPING.md
https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html
https://www.cnblogs.com/rongfengliang/p/10638334.html