ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[elasticsearch笔记] Joining Queries - nested

2019-07-25 16:37:50  阅读:326  来源: 互联网

标签:count name parent nested elasticsearch Queries overflow type stack


note

  • 干货 | Elasticsearch Nested类型深入详解
  • elasticsearch(lucene)使用的库没有内部对象的概念,因此内部对象被扁平化为一个简单的字段名称和值列表。
  • 为了解决这个问题,需要用到 nested,nested对象会被作为隐式对象处理。其内容不会被扁平化处理。
  • 关联 doc 必须保存于同一个分片,所以在构建 index的时候,需要添加 routing=parent_id 参数

demo

nested

DELETE /netest_index
PUT /netest_index
{
  "mappings": {
    "properties": {
      "title":{
        "type":"text"
      },
      "comments": {
        "type": "nested",
        "properties": {
          "name":{
            "type":"text"
          },
          "comment":{
            "type":"text"
          }
        }
      }
    }
  }
}

POST /netest_index/_doc/1
{
  "title":"es",
  "comments":[ 
    {
      "name": "zhengcj",
      "count": 6
    },
    {
      "name":"dongyc",
      "count":1
    }
  ]
}

POST /netest_index/_doc/2
{
  "title":"java",
  "comments": [
    {
      "name": "zhengcj",
      "count": 6
    },
    {
      "name":"ljr",
      "count":2
    }
  ]
}

GET /netest_index/_search
{
    "query": {
        "nested" : {
            "path" : "comments",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"comments.name" : "zhengcj"} },
                    { "range" : {"comments.count" : {"gte" : 6}} }
                    ]
                }
            },
            "score_mode" : "avg"
        }
    }
}

POST netest_index/_update/1
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.comments.removeIf(it->it.name=='dongyc')"
  }
}

POST /netest_index/_update/1
{
  "script":{
    "source": "for(e in ctx._source.comments){if(e.name=='zhengcj'){e.count+=1;}}"
  }
}

GET /netest_index/_search
{
  "size": 0,
  "aggs":{
    "nested_aggs":{
      "nested":{
        "path":"comments"
      },
      "aggs":{
        "min_count":{
          "min":{
            "field":"comments.count"
          }
        }
      }
    }
  }
}

join/has_parent/has_child

init data

DELETE stack_overflow
PUT stack_overflow
{
  "mappings": {
    "properties": {
      "join": {
        "type": "join",
        "relations": {
          "question": "answer"
        }
      },
      "key":{
        "type": "keyword"
      },
      "view_count":{
        "type":"integer"
      }
    }
  }
}

PUT stack_overflow/_doc/1
{
  "join": {
    "name": "question"
  },
  "body": "<p>I have Windows 2003 server and i bought a new Windows 2008 server...",
  "title": "Whats the best way to file transfer my site from server to a newer one?",
  "tags": [
    "windows-server-2003",
    "windows-server-2008",
    "file-transfer"
  ],
  "key":"2003-2008",
  "view_count":10
}

PUT stack_overflow/_doc/2
{
  "join": {
    "name": "question"
  },
  "body": "ELK body",
  "title": "ELK",
  "tags": [
    "es",
    "kibana",
    "logstash"
  ],
  "key":"ELK",
  "view_count":2
}

# 必须包含 routing=n,关联 doc 必须保存于同一个分片
PUT stack_overflow/_doc/11?routing=1
{
  "join": {
    "name": "answer",
    "parent": "1"
  },
  "owner": {
    "location": "Norfolk, United Kingdom",
    "display_name": "Sam",
    "id": 48
  },
  "body": "<p>Unfortunately you're pretty much limited to FTP...",
  "creation_date": "2009-05-04T13:45:37.030",
  "key":"FTP",
  "view_count":3
}

PUT stack_overflow/_doc/12?routing=1&refresh
{
  "join": {
    "name": "answer",
    "parent": "1"
  },
  "owner": {
    "location": "Norfolk, United Kingdom",
    "display_name": "Troll",
    "id": 49
  },
  "body": "<p>Use Linux...",
  "creation_date": "2009-05-05T13:45:37.030",
  "key":"Linux",
  "view_count":4
}

PUT stack_overflow/_doc/21?routing=2
{
  "join": {
    "name": "answer",
    "parent": "2"
  },
  "owner": {
    "location": "Norfolk, United Kingdom",
    "display_name": "Sam",
    "id": 48
  },
  "body": "this is answer to es in body",
  "creation_date": "2009-05-04T13:45:37.030",
  "key":"es",
  "view_count":5
}

join

查询一个人回答过问题的标签,即这个人关注的问题

POST stack_overflow/_search?size=0
{
  "aggs": {
    "top-names": {
      "terms": {
        "field": "owner.display_name.keyword",
        "size": 10
      },
      "aggs": {
        "to-questions": {
          "parent": {
            "type": "answer"
          },
          "aggs": {
            "top-tags": {
              "terms": {
                "field": "tags.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

has_parent

根据 parent是否满足条件,返回child

GET /stack_overflow/_search
{
  "query": {
    "has_parent": {
      "parent_type": "question",
      "query": {
        "term": {
          "key": {
            "value": "ELK"
          }
        }
      }
    }
  }
}

GET /stack_overflow/_search
{
  "query": {
    "has_parent": {
      "parent_type": "question",
      "score":true,
      "query": {
        "function_score": {
          "script_score": {
            "script": "_score * doc['view_count'].value+1"
          }
        }
      }
    }
  }
}

has_child

根据 child是否满足条件,返回parent

GET /stack_overflow/_search
{
  "query": {
    "has_child": {
      "type": "answer",
      "query": {
        "match_all": {}
      },
      "max_children": 5,
      "min_children": 2,
      "score_mode": "min"
    }
  }
}

标签:count,name,parent,nested,elasticsearch,Queries,overflow,type,stack
来源: https://blog.csdn.net/weixin_43834662/article/details/97266631

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有