ElasticSearch - 踩坑笔记

89 阅读1分钟

索引内数据行超过1万会报错?

    [root@localhost ~] curl -XPUT [ES服务器地址IP]:[ES运行端口]/[索引名称]/_settings -H 'Content-Type: application/json' -d '{ "index.max_result_window" :"[数据长度]"}' -u elastic:[ES密码]

elasticsearch数据修改就不见了?

    先说重点,数据修改后找不到不是丢了,因为ES的更新操作默认不会实时更新(当然如果有需要你可以在更新操作时追加参数refresh=true),或者在配置文件中添加参数refresh_interval=刷新时间[单位/秒]

IK分词器如何使用?

    # 如已安装了IK分词器可跳过安装步骤
    root@localhost ~] wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.11.1/elasticsearch-analysis-ik-7.11.1.zip
    [root@localhost ~] mkdir /usr/share/elasticsearch/plugins/analysis-ik
    # 将压缩包解压至上述目录,并重启ES
    
    # 示例代码
    $this->elasticsearch->indices()->create([
        'index' => '[索引名称]',
        'include_type_name' =>  true,
        'body'  => [
            'mappings' => [
                'magic' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties'    =>  [
                        '[字段名称]'      => [
                            'type'      => 'text',
                            'analyzer'  =>  'ik_max_word', // 此项非必须,如果需要使用分词需要声明
                        ],
                    ]
                ]
            ]
        ]
    ]);
    
    $this->elasticsearch->search([
            'index'             =>  'ik_name_test',
            'from'              =>  0,  // 从哪一条开始查
            'size'              =>  100,  // 一次查询多少条
            'track_total_hits'  =>  true,   // 返回准确数据行数
            'body'              =>  [
                'query'     =>  [
                    'bool'  =>   [
                        'should'  =>  [
                            ['match' => ['[索引字段]' => '[筛选条件]']]
                        ]
                    ]
                ],
                'highlight' =>  [   // 高亮显示
                    'fields'    =>  [
                        '[索引字段]'  =>  [
                            'pre_tags'  =>  '<b style="color:red">',
                            'post_tags' =>  '</b>'
                        ]
                    ]
                ],
            ]
        ]);
    
    

获取数据时使用字段排序会报错?

    # 重新创建索引
    $this->elasticsearch->indices()->create([
            'index' => '[索引名称]',
            'include_type_name' =>  true,
            'body'  => [
                'mappings' => [
                    'magic' => [
                        '_source' => [
                            'enabled' => true
                        ],
                        'properties'    =>  [
                            '字段'      => [
                                'type'      => '[字段类型]',
                                'format'    => 'yyyy-MM-dd HH:mm:ss', // 此项非必须,如果是日期需要声明格式
                                'analyzer'  =>  'ik_max_word', // 此项非必须,如果需要使用分词需要声明
                                'index'     =>  true,   // 此项非必须,默认为true,是否需要被索引
                            ],
                            // 其他字段
                        ]
                    ]
                ]
            ]
    ]);            
    # 常用字段类型
    (1):字符串
        text,keyword
    (2):数字
        long, integer, short, byte, double, float, half_float, scaled_float
    (3):日期
        date,date_nanos(纳秒)
    (4):布尔
        boolean
    (5):Binary
        binary
    (6):Range
        integer_range, float_range, long_range, double_range, date_range
    (7):地理数据
        geo_point (纬度/经度积分),geo_shape (用于多边形等复杂形状)