66_elasticSearch 基于nested object实现博客与评论嵌套关系

66_elasticSearch 基于nested object实现博客与评论嵌套关系

更多干货

  • 分布式实战(干货)
  • spring cloud 实战(干货)
  • mybatis 实战(干货)
  • spring boot 实战(干货)
  • React 入门实战(干货)
  • 构建中小型互联网企业架构(干货)
  • python 学习持续更新
  • ElasticSearch 笔记

概述

1、做一个实验,引出来为什么需要nested object

冗余数据方式的来建模,其实用的就是object类型,我们这里又要引入一种新的object类型,nested object类型

博客,评论,做的这种数据模型

PUT /website/blogs/6
{
  "title": "花无缺发表的一篇帖子",
  "content":  "我是花无缺,大家要不要考虑一下投资房产和买股票的事情啊。。。",
  "tags":  [ "投资", "理财" ],
  "comments": [ 
    {
      "name":    "小鱼儿",
      "comment": "什么股票啊?推荐一下呗",
      "age":     28,
      "stars":   4,
      "date":    "2016-09-01"
    },
    {
      "name":    "黄药师",
      "comment": "我喜欢投资房产,风,险大收益也大",
      "age":     31,
      "stars":   5,
      "date":    "2016-10-22"
    }
  ]
}

被年龄是28岁的黄药师评论过的博客,搜索

GET /website/blogs/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "comments.name": "黄药师" }},
        { "match": { "comments.age":  28      }} 
      ]
    }
  }
}
{
  "took": 102,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.8022683,
    "hits": [
      {
        "_index": "website",
        "_type": "blogs",
        "_id": "6",
        "_score": 1.8022683,
        "_source": {
          "title": "花无缺发表的一篇帖子",
          "content": "我是花无缺,大家要不要考虑一下投资房产和买股票的事情啊。。。",
          "tags": [
            "投资",
            "理财"
          ],
          "comments": [
            {
              "name": "小鱼儿",
              "comment": "什么股票啊?推荐一下呗",
              "age": 28,
              "stars": 4,
              "date": "2016-09-01"
            },
            {
              "name": "黄药师",
              "comment": "我喜欢投资房产,风,险大收益也大",
              "age": 31,
              "stars": 5,
              "date": "2016-10-22"
            }
          ]
        }
      }
    ]
  }
}

结果是。。。好像不太对啊???

object类型数据结构的底层存储。。。

{
  "title":            [ "花无缺", "发表", "一篇", "帖子" ],
  "content":             [ "我", "是", "花无缺", "大家", "要不要", "考虑", "一下", "投资", "房产", "买", "股票", "事情" ],
  "tags":             [ "投资", "理财" ],
  "comments.name":    [ "小鱼儿", "黄药师" ],
  "comments.comment": [ "什么", "股票", "推荐", "我", "喜欢", "投资", "房产", "风险", "收益", "大" ],
  "comments.age":     [ 28, 31 ],
  "comments.stars":   [ 4, 5 ],
  "comments.date":    [ 2016-09-01, 2016-10-22 ]
}

object类型底层数据结构,会将一个json数组中的数据,进行扁平化

所以,直接命中了这个document,name=黄药师,age=28,正好符合

2、引入nested object类型,来解决object类型底层数据结构导致的问题

修改mapping,将comments的类型从object设置为nested

PUT /website
{
  "mappings": {
    "blogs": {
      "properties": {
        "comments": {
          "type": "nested", 
          "properties": {
            "name":    { "type": "string"  },
            "comment": { "type": "string"  },
            "age":     { "type": "short"   },
            "stars":   { "type": "short"   },
            "date":    { "type": "date"    }
          }
        }
      }
    }
  }
}
{ 
  "comments.name":    [ "小鱼儿" ],
  "comments.comment": [ "什么", "股票", "推荐" ],
  "comments.age":     [ 28 ],
  "comments.stars":   [ 4 ],
  "comments.date":    [ 2014-09-01 ]
}
{ 
  "comments.name":    [ "黄药师" ],
  "comments.comment": [ "我", "喜欢", "投资", "房产", "风险", "收益", "大" ],
  "comments.age":     [ 31 ],
  "comments.stars":   [ 5 ],
  "comments.date":    [ 2014-10-22 ]
}
{ 
  "title":            [ "花无缺", "发表", "一篇", "帖子" ],
  "body":             [ "我", "是", "花无缺", "大家", "要不要", "考虑", "一下", "投资", "房产", "买", "股票", "事情" ],
  "tags":             [ "投资", "理财" ]
}

再次搜索,成功了。

GET /website/blogs/_search 
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "花无缺"
          }
        },
        {
          "nested": {
            "path": "comments",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "comments.name": "黄药师"
                    }
                  },
                  {
                    "match": {
                      "comments.age": 28
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

score_mode:max,min,avg,none,默认是avg

如果搜索命中了多个nested document,如何讲个多个nested document的分数合并为一个分数


相关文章

  • ElasticSearch 笔记

  • 1_ElasticSearch使用term filter来搜索数据

  • 2_ElasticSearch filter执行原理 bitset机制与caching机制

  • 3_ElasticSearch 基于bool组合多个filter条件来搜索数据

  • 4_ElasticSearch 使用terms搜索多个值

  • 5_ElasticSearch 基于range filter来进行范围过滤

  • 6_ElasticSearch 控制全文检索结果的精准度

  • 7_ElasticSearch term+bool实现的multiword搜索原理

  • 8_基于boost的搜索条件权重控制

  • 9_ElasticSearch 多shard场景下relevance score不准确

  • 10_ElasticSearch dis_max实现best fields策略进行多字段搜索

  • 11_ElasticSearch 基于tie_breaker参数优化dis_max搜索效果

  • 12_ElasticSearch multi_match语法实现dis_max+tie_breaker

  • 13_ElasticSearch multi_match+most fiels策略进行multi-field搜索

  • 14_ElasticSearch 使用most_fields策略进行cross-fields search

  • 15_ElasticSearch copy_to定制组合field进行cross-fields搜索

  • 16_ElasticSearch 使用原生cross-fiels 查询

  • 17_ElasticSearch phrase matching搜索

  • 18_ElasticSearch 基于slop参数实现近似匹配

  • 19_ElasticSearch 使用match和近似匹配实现召回率与精准度的平衡

  • 20_ElasticSearch rescoring机制优化近似匹配搜索的性能

  • 21_ElasticSearch 前缀搜索、通配符搜索、正则搜索

  • 22_ElasticSearch 搜索推荐match_phrase_prefix实现search-time

  • 23_ElsaticSearch 搜索推荐ngram分词机制实现index-time更多干货

  • 24_ElasticSearch TF&IDF算法以及向量空间模型

  • 25_ElasticSearch 揭秘lucene的相关度分数算法

  • 26_ElasticSearch 四种常见的相关度分数优化方法

  • 27_ElasticSearch用function_score自定义相关度分数算法

  • 28_ElasticSearch误拼写时的fuzzy模糊搜索技术

  • 29_ElasticSearchIK中文分词器的安装和使用

  • 30_ElasticSearch IK分词器配置文件 以及自定义词库

  • ElasticSearchIK中文分词器的安装和使用

  • 日志管理ELK


你可能感兴趣的:(【构建高可用架构】,【大数据】,【ElatisSearch】)