淘先锋技术网

首页 1 2 3 4 5 6 7

join

DELETE gp-join-001
PUT gp-join-001/
{
  "mappings": {
    "properties": {
      "parentName":{
        "type": "text"
      },
      "join_name":{
        "type": "join",
        "relations":{
          "father":"son"
        }
      }
      
    }
  }
}

GET gp-join-001/_search
GET gp-join-001

PUT gp-join-001/_doc/1?routing=1
{
  "parentName":"gp",
  "join_name":{
    "name":"father"
  }
}

PUT gp-join-001/_doc/2?routing=1
{
  "parentName":"gp-es",
  "join_name":{
    "name":"son",
    "parent":"1"
  }
}


GET gp-join-001/_search
{
  "query": {
    "parent_id":{
      "type":"son",
      "id":1
    }
  }
}

nested 嵌套

  • 解决键值对查询误差
  • 通过内置查询关联合并为一条数据
DELETE gp-nested-001
PUT gp-nested_001
{
  "mappings": {
    "properties": {
      "companyNane":{
        "type": "text"
      },
      "area":{
        "properties": {
          "pro":{
            "type":"keyword"
          },
          "city":{
            "type":"keyword"
          }
        }
      }
    }
  }
}
GET gp-nested_001

POST gp-nested_001/_doc
{
  "companyName":"gp-nested3",
  "area":[
    {
      "pro":"HN3",
      "city":"CS3"
    },
    {
      "pro":"HB",
      "city":"SJZ"
    },
    {
      "pro":"HN",
      "city":"ZZ"
    },
    {
      "pro":"NM",
      "city":"HHHT"
    }
    ]
  
}

GET gp-nested_001/_search

  • 条件搜索 must 相当于 and 整个数组满足查询条件即可
GET gp-nested_001/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "area.pro": {
              "value": "HN2"
            }
          }
        },
        {
          "term": {
            "area.city": {
              "value": "ZZ"
            }
          }
        }
      ]
    }
  }
}

nested

DELETE gp-nested-002
PUT gp-nested_002
{
  "mappings": {
    "properties": {
      "companyNane":{
        "type": "text"
      },
      "area":{
        "type": "nested", 
        "properties": {
          "pro":{
            "type":"keyword"
          },
          "city":{
            "type":"keyword"
          }
        }
      }
    }
  }
}

GET gp-nested_002
GET gp-nested_002/_search


POST gp-nested_002/_doc
{
  "companyName":"gp-nested",
  "area":[
    {
      "pro":"HN",
      "city":"CS"
    },
    {
      "pro":"HB",
      "city":"SJZ"
    },
    {
      "pro":"HN",
      "city":"ZZ"
    },
    {
      "pro":"NM",
      "city":"HHHT"
    }
    ]
  
}
  • 条件搜索 必须严格一致 字段嵌套限制默认50 对象嵌套限制默认1w
  • 更新不友好 慎重使用
GET gp-nested_002/_search
{
  "query": {
    "nested": {
      "path": "area",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "area.pro": {
                  "value": "HN2"
                }
              }
            },
            {
              "term": {
                "area.city": {
                  "value": "CS2"
                }
              }
            }
          ]
        }
      }
    }
  }
}

辅助字段类型应用

别名alias 只可查询 不可操作数据

DELETE gp_alias_name_001
PUT gp_alias_name_001
{
  "mappings": {
    "properties": {
      "company1":{
        "type": "text"
      },
      "companyName":{
        "type": "alias",
        "path":"company1"
      }
    }
  }
  
}

怎么更新数据

POST gp_alias_name_001/_doc
{
“company1”:“gp-es222”

}

GET gp_alias_name_001/_search

单值多字段 multi-fileds

DELETE gp_multi_fileds_001

POST gp_multi_fileds_001/_doc
{
  "company1":"gp-es222"
  
}

GET gp_multi_fileds_001
GET gp_multi_fileds_001/_search

copy_to 复制 合并


DELETE gp_copy_to_001
PUT gp_copy_to_001
{
  "mappings": {
    "properties": {
      "company1":{
        "type": "keyword",
        "copy_to": "allFileds"
      },
      "company2":{
        "type": "keyword",
        "copy_to": "allFileds"
      },
      "allFileds":{
        "type": "text"
      }
    }
  }
}

POST gp_copy_to_001/_doc
{
  "company1":"333",
  "company2":"222"
}

GET gp_copy_to_001/_search

GET gp_copy_to_001/_search
{
  "query": {
    "match": {
      "allFileds": "111"
    }
  }
}

数据字段属性

  • index 字段是否被搜索 默认true 不需要检索的字段设置成false 提升性能
DELETE gp-index-001

PUT gp-index-001
{
  
  "mappings": {
    "properties": {
      "indexName":{
        "type": "keyword",
        "index": false
      }
    }
  }
}

POST gp-index-001/_doc
{
  "indexName":"cc"
}

GET gp-index-001
GET gp-index-001/_search
{
  "query": {
    "term": {
      "indexName": {
        "value": "cc"
      }
    }
  }
}

store 是否存储

  • es默认原始数据存储在_source里,其实也可以在Lucene里存储一份,通过此关键字控制,不宜过多 默认false
  • 应用场景: 原始数据_source被禁用,也需要修改原始数据
DELETE gp-store-001

PUT gp-store-001
{
  
  "mappings": {
    "_source": {
      "enabled": false
    }, 
    "properties": {
      "storeName":{
        "type": "keyword",
        "store": true
      }
    }
  }
}

POST gp-store-001/_doc
{
  "storeName":"cc"
}

GET gp-store-001
GET gp-store-001/_search
GET gp-store-001/_search
{
  "query": {
    "term": {
      "storeName": {
        "value": "cc"
      }
    }
  }
}

GET gp-store-001/_search
{
  "stored_fields": ["storeName"]
}

enabled 是否启用 ??? 注意是 索引 还是字段

DELETE gp-enabled-001

PUT gp-enabled-001
{
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "enabledName": {
        "type": "object",
        "enabled": "true"
      }
    }
  }
}

POST gp-enabled-001/_doc
{
  "enabledName": {
    "pro": "HB",
    "city": "CZ",
    "count": "HJ"
  }
}

GET gp-enabled-001
GET gp-enabled-001/_search
GET gp-enabled-001/_search
{
  "query": {
    "term": {
      "enabledName.city": {
        "value": "CZ"
      }
    }
  }
}

doc_value 能否聚合排序

  • doc_value 列式数据存储(聚合能力好) es 默认存储两份数据,一份原始数据,一份列式数据 默认true 但是text类型默认不打开 避免分词的字段做排序

  • 应用领域 基于该字段做聚合分析 基于该字段做排序


DELETE gp_doc_value_001

PUT gp_doc_value_001
{
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "docValueName": {
        "type": "keyword",
        "doc_values": true
      },"docValueAge": {
        "type": "keyword",
        "doc_values": false
      }
    }
  }
}

POST gp_doc_value_001/_doc
{
  "docValueName": "gp21",
  "docValueAge":"333"
  
}

GET gp_doc_value_001
GET gp_doc_value_001/_search
GET gp_doc_value_001/_search
{
  "sort": [
    {
      "docValueName": {
        "order": "desc"
      }
    }
  ]
}

null_value 空值设置

  • 填充值为null时,填充默认值 怪怪的
DELETE gp_null_value_001

PUT gp_null_value_001
{
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "docValueName": {
        "type": "keyword",
        "doc_values": true,
        "null_value": "default"
      },"docValueAge": {
        "type": "keyword",
        "doc_values": true
      }
    }
  }
}

POST gp_null_value_001/_doc
{
  "docValueName": "gp21",
  "docValueAge":"111"
  
}
POST gp_null_value_001/_doc
{
  "docValueName":null,
  "docValueAge":"222"
  
}
POST gp_null_value_001/_doc
{
  "docValueAge":"333"
  
}

GET gp_null_value_001
GET gp_null_value_001/_search
GET gp_null_value_001/_search
{
  "sort": [
    {
      "docValueName": {
        "order": "desc"
      }
    }
  ]
}

properties 子对象控制

  • 如果enabledName中有子对象但是属性不确定 可以使用object
PUT gp-enabled-001
{
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "enabledName": {
        "type": "object",
        "enabled": "true"
      }
    }
  }
}

如果enabledName中有子对象如果属性确定 需要使用properties对应

PUT gp-enabled-001
{
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "enabledName": {
        "properties": {
          "firstName":{
            "type":"keyword"
          },
          "lastName":{
            "type":"keyword"
          }
        }
      }
    }
  }
}

dynamic 子对象动态扩展

  • 是否容许对象下面的属性自由扩展

ignore_above

  • keyword 类型小 字符过长 检索意义不大 索引会被禁用 数据不可被检索 超过设定长度 索引被禁用 默认长度255 最大65535

DELETE gp_ignore_above_001

PUT gp_ignore_above_001
{
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "docValueName": {
        "type": "keyword",
        "doc_values": true,
        "ignore_above": 12
      },"docValueAge": {
        "type": "keyword",
        "doc_values": true
      }
    }
  }
}

POST gp_ignore_above_001/_doc
{
  "docValueName": "gp211233",
  "docValueAge":"333"
  
}

GET gp_ignore_above_001
GET gp_ignore_above_001/_search
GET gp_ignore_above_001/_search
{
  "sort": [
    {
      "docValueName": {
        "order": "desc"
      }
    }
  ]
}

fields 多字段

coerce 数值类型强制约束

  • 默认 false integer 只可存储integer数据

  • 数据类数据并非一直符合标准,有的会带一些格式之类的


DELETE gp_coerce_001

PUT gp_coerce_001
{
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "docValueName": {
        "type": "keyword",
        "doc_values": true,
        "ignore_above": 12
      },"docValueAge": {
        "type": "keyword",
        "doc_values": true
      },"income":{
        "type": "integer",
        "coerce": false
      }
    }
  }
}

POST gp_coerce_001/_doc
{
  "docValueName": "gp211233",
  "docValueAge":"333",
  "income":100
  
}

POST gp_coerce_001/_doc
{
  "docValueName": "gp211233",
  "docValueAge":"333",
  "income":100.12456
  
}

GET gp_coerce_001
GET gp_coerce_001/_search
GET gp_coerce_001/_search
{
  "sort": [
    {
      "docValueName": {
        "order": "desc"
      }
    }
  ]
}

format

  • 设置成支持两种

DELETE gp_format_001

PUT gp_format_001
{
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
     "createDate":{
        "type": "date", 
        "format": "epoch_millis||basic_date_time"
      }
    }
  }
}

POST gp_format_001/_doc
{
  "createDate": "04/09/2020"
  
}
GET gp_format_001
GET gp_format_001/_search

高级数据字段

analyzer 分词器 指定分词器

similarity 分词关联算法 BM25 TF/IDF Bool

boots 加权 分词加权

norms 分词规范化

normalizer 规整器 keyword 类型 统一大小写处理

fielddata 字段数据

DELETE gp_fielddata_002
PUT gp_fielddata_002
{
  "mappings": {
    "properties": {
      "companyNane":{
        "type": "text"
      },
      "area":{
        "type": "nested", 
        "properties": {
          "pro":{
            "type":"text",
            "fielddata":true
          },
          "city":{
            "type":"keyword"
          }
        }
      }
    }
  }
}

POST gp_fielddata_002/_doc
{
  "companyNane":"gp",
  "area":{
    "pro":"HB",
    "city":"HJ"
  }
}

POST gp_fielddata_002/_doc
{
  "companyNane":"gp1",
  "area":{
    "pro":"HB1",
    "city":"HJ1"
  }
}
POST gp_fielddata_002/_doc
{
  "companyNane":"gp2",
  "area":{
    "pro":"HB2",
    "city":"HJ2"
  }
}


GET gp_fielddata_002 
GET gp_fielddata_002/_search
{
  "sort": [
    {
      "area.pro": {
        "order": "desc"
      }
    }
  ]
}

全局序号

eager_global_ordinals 类似自增id 方便 统计 排序

  • 索引字段与属性都属于静态设置,若后期变更历史数据需要重建索引才能生效