elasticSearch的属性的index doc

elasticSearch的属性的index doc,第1张

elasticSearch的属性的index doc

elasticSearch的属性的index doc_values h和nested嵌入式使用

需要ik中文检索:
"skuTitle": { 
    "type": "text", 
    "analyzer": "ik_smart"
}

使用ik_smart 分词类型,需要安装ik分词器插件哦

节省资源的设置:
"skuImg": { 
    "type": "keyword",
    "index": false, 
    "doc_values": false
}

index: 默认 true,如果为 false,表示该字段不会被索引,但是检索结果里面有,但字段本身不能 当做检索条件。

doc_values: 默认 true,设置为 false,表示不可以做排序、聚合以及脚本 *** 作,这样更节省磁盘空间。 还可以通过设定 doc_values 为 true,index 为 false 来让字段不能被搜索但可以用于排序、聚 合以及脚本 *** 作:

数组属性nested嵌入式设置
"attrs": 
{
    "type": "nested", 
    "properties": { 
        "attrId": { 
            "type": "long"
        },
        "attrName": { 
            "type": "keyword", 
            "index": false, 
            "doc_values": false
        },
        "attrValue": {
            "type": "keyword"
        }
    }
}

对于数组型属性不设置嵌入式结构,存储时会被扁平化处理:

想要保存两个人:
做一个索引,有个user数组属性,把它的存储人的名字
John Smith
Alice White

PUT my-index-000001/_doc/1
{
  "group" : "fans",
  "user" : [ 
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

默认会被扁平化存储:

扁平化存储后,它的文档将在内部转换为更像以下内容的文档:

存储对象的同属性被合并到了一起
{
  "group" :        "fans",
  "user.first" : [ "Alice", "john" ],
  "user.last" :  [ "smith", "white" ]
}

测试一下它的的特性:
我们找一个不存在的人:

我们找:Alice Smith
GET my-index-000001/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "user.first": "Alice" }},
        { "match": { "user.last":  "Smith" }}
      ]
    }
  }
}

我们却能找到结果:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.105360515,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.105360515,
        "_source" : {
          "group" : "fans",
          "user" : [
            {
              "first" : "John",
              "last" : "Smith"
            },
            {
              "first" : "Alice",
              "last" : "White"
            }
          ]
        }
      }
    ]
  }
}

查看他的mapping:

GET my-index-000001/_mapping
得到:
{
  "my-index-000001" : {
    "mappings" : {
      "properties" : {
        "group" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "user" : {
          "properties" : {
            "first" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "last" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }
      }
    }
  }
}

竟然有结果,不符合我们预期
接下来删除这个索引

DELETE my-index-000001

重新定义它,给它加上嵌入式的类型nested:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "user": {
        "type": "nested" 
      }
    }
  }
}

重新存储:

PUT my-index-000001/_doc/1
{
  "group" : "fans",
  "user" : [
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}

查找那个不存在的人:

GET my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "Smith" }} 
          ]
        }
      }
    }
  }
}
这货不该出现,就不出现了:
{
  "took" : 65,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5154199.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存