上一节我们将数据同步到ES中,接下来ES会通过内置分词器进行分词,当搜索某一个商品,ES就会通过分词展示出与关键字相关的商品。但是我们数据库中基本都是中文,而LogStash中默认是不包含中文分词器的,那么就需要我们手动配置。

通过Logstash模板配置

1.获取Logstash模板

GET    http://10.0.0.219:9200/_template/logstash



2.在sync目录下创建logstash-ik.json,将logstash模板修改后粘贴其中。
修改前:

{
    "logstash": {
        "order": 0,
        "version": 60001,
        "index_patterns": [
            "logstash-*"
        ],
        "settings": {
            "index": {
                "refresh_interval": "5s"
            }
        },
        "mappings": {
            "_default_": {
                "dynamic_templates": [
                    {
                        "message_field": {
                            "path_match": "message",
                            "match_mapping_type": "string",
                            "mapping": {
                                "type": "text",
                                "norms": false
                            }
                        }
                    },
                    {
                        "string_fields": {
                            "match": "*",
                            "match_mapping_type": "string",
                            "mapping": {
                                "type": "text",
                                "norms": false,
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            }
                        }
                    }
                ],
                "properties": {
                    "@timestamp": {
                        "type": "date"
                    },
                    "@version": {
                        "type": "keyword"
                    },
                    "geoip": {
                        "dynamic": true,
                        "properties": {
                            "ip": {
                                "type": "ip"
                            },
                            "location": {
                                "type": "geo_point"
                            },
                            "latitude": {
                                "type": "half_float"
                            },
                            "longitude": {
                                "type": "half_float"
                            }
                        }
                    }
                }
            }
        },
        "aliases": {}
    }
}


修改后:

{
    "order": 0,                         ###修改处
    "version": 1,                       ###修改处
    "index_patterns": ["*"],
    "settings": {
        "index": {
            "refresh_interval": "5s"
        }
    },
    "mappings": {
        "_default_": {
            "dynamic_templates": [
                {
                    "message_field": {
                        "path_match": "message",
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "text",
                            "norms": false
                        }
                    }
                },
                {
                    "string_fields": {
                        "match": "*",
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "text",
                            "norms": false,
                            "analyzer": "ik_max_word",            ###修改处
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                }
            ],
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "@version": {
                    "type": "keyword"
                },
                "geoip": {
                    "dynamic": true,
                    "properties": {
                        "ip": {
                            "type": "ip"
                        },
                        "location": {
                            "type": "geo_point"
                        },
                        "latitude": {
                            "type": "half_float"
                        },
                        "longitude": {
                            "type": "half_float"
                        }
                    }
                }
            }
        }
    },
    "aliases": {}
}

 

3.修改配置文件logstash-db-sync.conf 

   .
   .
   .
output {
    elasticsearch {
        # es地址
        hosts => ["localhost:9200"]
        # 同步的索引名,提前创建索引
        index => "foodie-items-ik"                               ####修改处
        # 设置_docID和数据相同,这里需要与sql脚本中的id字段一致
        document_id => "%{itemId}"
        
        # 定义模板名称
        template_name => "myik"                               ####新增
        # 模板所在位置
        template => "/application/logstash-6.4.3/sync/logstash-ik.json"                ####新增
        # 重写模板
        template_overwrite => true                    ####新增
        # 默认为true,false关闭logstash自动管理模板功能,如果自定义模板,则设置为false
        manage_template => false                      ####新增
    }
   .
   .
   .
}

 

4.创建索引foodie-items-ik

 

5.启动Logstash

bin/logstash -f /application/logstash-6.4.3/sync/logstash-db-sync-ik.conf


6.浏览数据

 

最后修改于 2020-04-15 11:09:16
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇