es-mapping-parameters-5

21.null_value

null_value参数默认值为null,可以指定值来替换显式的空值,以便对其进行索引和搜索。

DELETE my-index-000001
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "status_code": {
        "type":       "keyword",
        "null_value": "NULL" 
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "status_code": null
}

PUT my-index-000001/_doc/2
{
  "status_code": [] 
}

PUT my-index-000001/_doc/3
{
  "status_code": [
    null
  ]
}

PUT my-index-000001/_doc/4
{
  "status_code": "NULL"
}

#结果:三条数据,
#其中,空数组不包含显式的 null,因此不会用 null _ 值替换。所以这条数据没有查询出来
GET my-index-000001/_search
{
  "query": {
    "term": {
      "status_code": "NULL" 
    }
  }
}

22.position_increment_gap

文本字段在分析时会考虑词条的位置,以便能够支持邻近或短语查询。当索引具有多个值的文本字段时,在值之间添加一个“假”间隙,以防止大多数短语查询在值之间匹配。这个间隙的大小使用position_increment_gap配置,默认值为100。

DELETE my-index-000001
PUT my-index-000001/_doc/1
{
  "names": [ "John Abraham", "Lincoln Smith"]
}

#短语查询Abraham Lincoln
#结果:为空
#预期是有一条,为什么没有数据呢?因为AbrahamLincoln 位于不同的字符串
GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": {
        "query": "Abraham Lincoln" 
      }
    }
  }
}
##短语查询Abraham Lincoln 
##结果:一条
##AbrahamLincoln 位于不同的字符串中,因为 slop > position _ increment _ gap。
## slop参数告诉match_phrase查询词条能够相隔多少空隙时仍然将文档视为匹配。
GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": {
        "query": "Abraham Lincoln",
        "slop": 101 
      }
    }
  }
}

#为什么不生效
DELETE my-index-000001
##设置position_increment_gap=0,
#下一个数组元素中的第一个项与前一个数组元素中的最后一个项相差0个项。
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "names": {
        "type": "text",
        "position_increment_gap": 0 
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "names": [ "John Abraham", "Lincoln Smith"]
}

##短语查询 Abraham Lincoln
## 之前不生效的结果,生效了
GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": "Abraham Lincoln" 
    }
  }
}

23.properties

类型映射、object字段和nested字段包含的子字段称为属性(properties)。这些属性可以是任何数据类型,包括object和nested。属性可以在通过下列方式添加:
(1)通过在创建索引时显式地定义它们。
(2)通过在使用PUT Mapping API添加或更新映射类型时显式地定义它们。
(3)动态地索引包含新字段的文档。

#创建索引时显式地定义,属性添加到映射类型、对象字段和嵌套字段
#顶级映射定义中的属性。
#manager对象字段下的属性。
#employees嵌套字段下的属性。
DELETE my-index-000001
PUT my-index-000001
{
  "mappings": {
    "properties": { 
      "manager": {
        "properties": { 
          "age":  { "type": "integer" },
          "name": { "type": "text"  }
        }
      },
      "employees": {
        "type": "nested",
        "properties": { 
          "age":  { "type": "integer" },
          "name": { "type": "text"  }
        }
      }
    }
  }
}

PUT my-index-000001/_doc/1 
{
  "region": "US",
  "manager": {
    "name": "Alice White",
    "age": 30
  },
  "employees": [
    {
      "name": "John Smith",
      "age": 34
    },
    {
      "name": "Peter Brown",
      "age": 26
    }
  ]
}

PUT my-index-000001/_doc/2
{
  "region": "US",
  "manager": {
    "name": "Alice White",
    "age": "30"
  },
  "employees": [
    {
      "name": "John Smith",
      "age": "34"
    },
    {
      "name": "Peter Brown",
      "age": "26"
    }
  ]
}

GET my-index-000001/_search

GET my-index-000001/_search
{
  "query" : {
    "range" : {
      "employees.age": { 
        "gte" : "26"
      }
    }
  }
}

## 内部字段可以在查询、聚合等中引用,使用点符号:
GET my-index-000001/_search
{
  "query": {
    "match": {
      "manager.name": "Alice White"
    }
  },
  "aggs": {
    "Employees": {
      "nested": {
        "path": "employees"
      },
      "aggs": {
        "Employee Ages": {
          "histogram": {
            "field": "employees.age",
            "interval": 5
          }
        }
      }
    }
  }
}

24.search_analyzer 搜索的时候使用的分析

  • 分析器主要有两种情况会被使用:
    第一种是插入文档时,将text类型的字段做分词然后插入倒排索引,
    第二种就是在查询时,先对要查询的text类型的输入做分词,再去倒排索引搜索

  • 使用search_analyzer
    如果想要让 索引 和 查询 时使用不同的分词器,ElasticSearch也是能支持的,只需要在字段上加上search_analyzer参数
    (1)在索引时,只会去看字段有没有定义analyzer,有定义的话就用定义的,没定义就用ES预设的
    (2)在查询时,会先去看字段有没有定义search_analyzer,如果没有定义,就去看有没有analyzer,再没有定义,才会去使用ES预设的

DELETE my-index-000001
#设置search_analyzer为standard,analyzer=autocomplete
PUT my-index-000001
{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "autocomplete": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "standard"
      }
    }
  }
}

##使用standard去解析的话, terms: [ q, qu, qui, quic, quick, b, br, bro, brow, brown, f, fo, fox ]
PUT my-index-000001/_doc/1
{
  "text": "Quick Brown Fox" 
}

PUT my-index-000001/_doc/2
{
  "text": "Quick Fox" 
}
## 搜索 terms: [ quick, br ]
##结果:查询记录为一条,符合预期。
GET my-index-000001/_search
{
  "query": {
    "match": {
      "text": {
        "query": "Quick Br", 
        "operator": "and"
      }
    }
  }
}

25.similarity

similarity相似度用于配置每个字段的评分算法或相似度算法。

  • 指定评分的相似度算法
    BM25-默认的评分算法
    TF/IDF-以前默认的算法
    bollean-只匹配,不排序

  • similarity参数主要用于文本字段,但也可以应用于其他字段类型。

## default_field 字段使用 BM25 匹配算法
## classic_field 字段使用 classic匹配算法(即 TF / IDF)
## boolean_sim_field字段使用 boolean similarity.

DELETE my-index-000001
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "default_field": { 
        "type": "text"
      },
      "boolean_sim_field": {
        "type": "text",
        "similarity": "boolean" 
      }
    }
  }
}

## 测试boolean_sim_field字段
# 索引
POST /my-index-000001/_doc/1
{
  "boolean_sim_field" : "hello good"
}
POST /my-index-000001/_doc/2
{
  "boolean_sim_field" : "hello me hello"
}
POST /my-index-000001/_doc/3
{
  "default_field" : "hello good"
}
POST /my-index-000001/_doc/4
{
  "default_field" : "hello me hello"
}


# 搜索,有评分排行 default_field
##结果:两条记录,有_score字段,按_scorede大小排序
GET /my-index-000001/_search
{
  "query" : {
    "match" : {
      "default_field" : "hello"
    }
  }
}

# 搜索,没有评分排行 boolean_sim_field
##结果:两条记录,_score都是1.0
GET /my-index-000001/_search
{
  "query" : {
    "match" : {
      "boolean_sim_field": "hello"
    }
  }
}


# similarity
PUT /similarity_test
{
  "mappings" : {
    "properties" : {
      "name1" : {
        "type"   : "text",
        "fields" : {
          "boolean" : {
            "type"       : "text",
            "similarity" : "boolean"
          }
        }
      }
    }
  }
}

# 索引
POST /similarity_test/_doc/1
{
  "name1" : "hello good"
}

# 索引
POST /similarity_test/_doc/2
{
  "name1" : "hello me hello"
}

# 搜索,有评分排行
GET /similarity_test/_search
{
  "query" : {
    "match" : {
      "name1" : "hello"
    }
  }
}

# 搜索,没有评分排行
GET /similarity_test/_search
{
  "query" : {
    "match" : {
      "name1.boolean" : "hello"
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值