美文网首页
显示的定义一个Mapping

显示的定义一个Mapping

作者: 滴流乱转的小胖子 | 来源:发表于2020-07-07 07:03 被阅读0次

    一、如何显示的定义一个Mapping

    PUT movies
    {
      "mappings": {
        // define your mappings here  
      }
    }
    

    二、自定义Mapping的一些建议

    可以参考API手册,纯手写

    为了减少输入的工作量,减少出错率,可以依照以下步骤

    • 创建一个临时的index,写入一些样本数据
    • 通过访问Mapping API 获得该临时文件的动态Mapping定义
    • 修改后用,使用该配置创建你的索引
    • 删除临时索引

    三、控制当前字段是否被索引

    Index - 控制当前字段是否被索引,默认为true。如果设置成false,该字段不可被搜索


    image.png

    四、Index Options

    image.png

    五、null_value

    GET users/_search?q=mobile:NULL
    
    image.png

    六、copy_to 设置

    image.png
    _all字段是一个很少用到的字段,它连接所有字段的值构成一个用空格(space)分隔的大string,该string被analyzed和index,但是不被store。当你不知道不清楚document结构的时候,可以用_all。
    https://www.cnblogs.com/bonelee/p/6428441.html

    七、数组类型

    image.png
    #设置 index 为 false
    DELETE users
    PUT users
    {
        "mappings" : {
          "properties" : {
            "firstName" : {
              "type" : "text"
            },
            "lastName" : {
              "type" : "text"
            },
            "mobile" : {
              "type" : "text",
              "index": false
            }
          }
        }
    }
    
    PUT users/_doc/1
    {
      "firstName":"Ruan",
      "lastName": "Yiming",
      "mobile": "12345678"
    }
    
    POST /users/_search
    {
      "query": {
        "match": {
          "mobile":"12345678"
        }
      }
    }
    
    
    
    
    #设定Null_value
    
    DELETE users
    PUT users
    {
        "mappings" : {
          "properties" : {
            "firstName" : {
              "type" : "text"
            },
            "lastName" : {
              "type" : "text"
            },
            "mobile" : {
              "type" : "keyword",
              "null_value": "NULL"
            }
    
          }
        }
    }
    
    PUT users/_doc/1
    {
      "firstName":"Ruan",
      "lastName": "Yiming",
      "mobile": null
    }
    
    
    PUT users/_doc/2
    {
      "firstName":"Ruan2",
      "lastName": "Yiming2"
    
    }
    
    GET users/_search
    {
      "query": {
        "match": {
          "mobile":"NULL"
        }
      }
    
    }
    
    
    
    #设置 Copy to
    DELETE users
    PUT users
    {
      "mappings": {
        "properties": {
          "firstName":{
            "type": "text",
            "copy_to": "fullName"
          },
          "lastName":{
            "type": "text",
            "copy_to": "fullName"
          }
        }
      }
    }
    PUT users/_doc/1
    {
      "firstName":"Ruan",
      "lastName": "Yiming"
    }
    
    GET users/_search?q=fullName:(Ruan Yiming)
    
    POST users/_search
    {
      "query": {
        "match": {
           "fullName":{
            "query": "Ruan Yiming",
            "operator": "and"
          }
        }
      }
    }
    
    
    #数组类型
    PUT users/_doc/1
    {
      "name":"onebird",
      "interests":"reading"
    }
    
    PUT users/_doc/1
    {
      "name":"twobirds",
      "interests":["reading","music"]
    }
    
    POST users/_search
    {
      "query": {
            "match_all": {}
        }
    }
    
    GET users/_mapping
    

    补充阅读:
    Mapping Parameters https://www.elastic.co/guide/en/elasticsearch/reference/7.1/mapping-params.html

    相关文章

      网友评论

          本文标题:显示的定义一个Mapping

          本文链接:https://www.haomeiwen.com/subject/vxmoqktx.html