首页 > 其他分享 > 在elasticsearch中对象object嵌套的使用

在elasticsearch中对象object嵌套的使用

时间:2022-08-30 16:45:33浏览次数:75  
标签:info name object 嵌套 elasticsearch test deviceNo desc

1.添加映射

PUT /object-test/
{
    "mappings":{
        "properties":{
            "deviceNo":{
                "type": "keyword"
            },
            "info":{
                "type":"object",
                "properties":{
                  "name":{
                    "type": "keyword"
                  },
                  "age":{
                    "type":"integer"
                  },
                  "desc":{
                    "type": "text"
                  }
                }
            }
        }
    }
}

 

2.添加数据

POST /object-test/_doc/1
{
  "deviceNo":"ME0001",
  "info":{
    "name":"1号工具",
    "age": 20,
    "desc": "很好用的"
  }
}


POST /object-test/_doc/2
{
  "deviceNo":"ME0002",
  "info":{
    "name":"2号工具",
    "age": 21,
    "desc": "一般化"
  }
}

POST /object-test/_doc/3
{
  "deviceNo":"ME0003",
  "info":{
    "name":"3号工具",
    "age": 21,
    "desc": "不好用"
  }
}


POST /object-test/_doc/4
{
  "deviceNo":"ME0004",
  "info":{
    "name":"4号工具",
    "age": 22,
    "desc": "还行啊"
  }
}

 

3.测试

// 单独查询
GET /object-test/_search
{
  "query": {
    "term": {
      "info.name": {
        "value": "4号工具"
      }
    }
  }
}


// 组合查询
GET /object-test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "deviceNo": {
              "value": "ME0004"
            }
          }
        },
        {
          "term": {
            "info.name": {
              "value": "4号工具"
            }
          }
        }
      ]
    }
  }
}

 

  后一个效果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.4079456,
    "hits" : [
      {
        "_index" : "object-test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 2.4079456,
        "_source" : {
          "deviceNo" : "ME0004",
          "info" : {
            "name" : "4号工具",
            "age" : 22,
            "desc" : "还行啊"
          }
        }
      }
    ]
  }
}

 

标签:info,name,object,嵌套,elasticsearch,test,deviceNo,desc
From: https://www.cnblogs.com/juncaoit/p/16639926.html

相关文章