elasticsearch-映射

homeros    PHP    999+ 次    2017-12-12 17:39:47


映射(mapping),相当于关系型数据库中的表结构、字段类型。

  • 查看

    GET /mihuan_release/_mapping/product_info
    
  • string

    string类型域默认会被认为包含全文。就是说,它们的值在索引前,会通过一个分析器,针对于这个域的查询在搜索前也会经过一个分析器。

    string 域映射的两个最重要 属性是 index 和 analyzer 。

  • index

    analyzed:首先分析字符串,然后索引它。换句话说,以全文索引这个域。

    not_analyzed:索引这个域,所以它能够被搜索,但索引的是精确值。不会对它进行分析。

    no:不索引这个域。这个域不会被搜索到。

    {
        "tag": {
            "type":     "string",
            "index":    "not_analyzed"
        }
    }
    
  • 修改/增加/删除mapping

    1.可以增加一个字段域

    2.不能更改已存在的域,因为该域可能已经被索引,如果修改,可能会出错,不能被正常的索引。

  • 创建mapping
    PUT /gb
    {
    “mappings”: {
    “tweet” : {
    “properties” : {
    “tweet” : {
    “type” : “string”,
    “analyzer”: “pinyin”
    },
    “date” : {
    “type” : “date”
    },
    “name” : {
    “type” : “string”
    },
    “user_id” : {
    “type” : “long”
    }
    }
    }
    }
    }

  • 添加字段mapping
    PUT /gb/_mapping/tweet
    {
    “properties” : {
    “tag” : {
    “type” : “string”,
    “index”: “not_analyzed”
    }
    }
    }

路拉拉数据
PUT /wcloud
{
“mappings”: {
“1035346” : {
“properties” : {
“c” : {
“type” : “string”,
“index”: “not_analyzed”
},
“v” : {
“type” : “float”
},
“t” : {
“type” : “date”
},
“humid” : {
“type” : “float”
},
“gsm” : {
“type” : “integer”
},
“gps” : {
“type” : “integer”
},
“flag” : {
“type” : “string”,
“index”: “not_analyzed”
},
“mode” : {
“type” : “string”,
“index”: “not_analyzed”
},
“run” : {
“type” : “byte”,
“index”: “not_analyzed”
},
“stat” : {
“type” : “integer”,
“index”: “not_analyzed”
},
“location” : {
“type” : “geo_point”
},
“temper” : {
“type” : “float”
},
“angle” : {
“type” : “float”
}
}
}
}
}

c:基站,(gps、基站、wifi)
v:速度
a:null,加速度,暂时没有
t:数据收集时间戳
humid:湿度
temper:温度
gsm:基站信号强度
gps:gps信号强度
angle:角度
flag:是否是gps或者高德/GAODE/GPS
mode:型号(jiawei、ruixue)
run:运动或静止(0静止,1运动)
stat:静止计数(当前静止几次)
[la:,ln:]

新增数据

PUT /wcloud/1035346/1498579190
{
    "c":"3",
    "location":{"lat":"40.0764811","lon":"116.2356505"},
    "v":"0",
    "t":"1498579190",
    "humid":"52.555122",
    "temper":"31.704395",
    "gsm":99,
    "gps":33,
    "angle":0,
    "flag":"GAODE",
    "mode":"jiawei",
    "run":0,
    "stat":1
}

product_info settings拼音分析器

PUT mihuan_release
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "pinyin" : {
                    "tokenizer" : "my_pinyin"
                    }
            },
            "tokenizer" : {
                "my_pinyin" : {
                    "type" : "pinyin",
                    "keep_separate_first_letter" : true,
                    "keep_full_pinyin" : true,
                    "keep_original" : true,
                    "limit_first_letter_length" : 16,
                    "lowercase" : true,
                    "remove_duplicated_term" : true
                }
            }
        }
    }
}

product_info mapping

PUT mihuan_release/_mapping/product_info
{
            "properties" : {
                "id":{
                    "type":"long",
                    "index": "not_analyzed"
                },
                "createtime":{
                    "type":"date"
                },
                "updatetime":{
                    "type":"date"
                },
                "company_id":{
                    "type":"integer",
                    "index": "not_analyzed"
                },
                "product_code":{
                    "type":"string"
                },
                "goods_num":{
                     "type":"string"
                },
                "name":{
                    "type":"text",
                    "analyzer":"standard",
                    "fields":{
                        "pinyin":{
                            "type":"text",
                            "analyzer":"pinyin"
                        }
                    }
                },
                "price_tag":{
                    "type":"integer",
                    "index": "not_analyzed"
                },
                "unit":{
                    "type":"string",
                    "index": "not_analyzed"
                },
                "remarks":{
                    "type":"text",
                    "analyzer":"standard",
                    "fields":{
                        "pinyin":{
                            "type":"text",
                            "analyzer":"pinyin"
                        }
                    }
                },
                "is_delete":{
                    "type":"string",
                    "index": "not_analyzed"
                }
           }
        }

mapping里不能设置analyzer.

_settings_mapping放在一个put请求设置未能成功,分开即可。
通过fields来指定以不同的方式索引相同的字段,搜索时使用:remarks.pinyin:ldh

  • 测试拼音插件

  • 添加一条数据

    PUT /mihuan_release/product_info/1616176
    {
        "id":"1616176",
        "createtime":"2015-01-01T12:10:30Z",
        "updatetime":"2015-01-01T12:10:30Z",
        "company_id":"5053",
        "product_code":"22741fasdf224522",
        "goods_num":"dfadfadfwefeeee",
        "name":"自定义分析器",
        "price_tag":"001",
        "unit":"kg",
        "remarks":"使用单个值编辑填充查询字符串",
        "is_delete":0
    }
    

-测试成功

GET /mihuan_release/product_info/_search
{
   "query": {
       "match": {
          "remarks.pinyin": "shiyong"
       }
   }
}

测试拼音插件

分析器相关概念

  • 做全文搜索就需要对文档分析、建索引。从文档中提取词元(Token)的算法称为分词器(Tokenizer),在分词前预处理的算法称为字符过滤器(Character Filter),进一步处理词元的算法称为词元过滤器(Token Filter),最后得到词(Term)。这整个分析算法称为分析器(Analyzer)。

查询


上一篇: elasticsearch 安装

下一篇: es 拼音插件安装/php客户端调用es

最新评论

暂无评论

热门文章

最新评论

网站数据

网站文章数:481

今日UV/PV/IP:11/30/11

昨日UV/PV/IP:25/26 /25

TOP