【ES】指定Mapping创建索引、插入数据并查询

【创建数据资产索引】

curl -H "Content-Type: application/json" -XPUT 'localhost:9200/asset' -d' {
   "mappings" : {
      "properties" : {
        "sn" : {
          "type" : "integer"
        },
        "name":{
            "type":"keyword"            
        },
    "url":{
            "type":"text"            
        }
      }
    }
}'

 

【以指定ID的方式创建首个数据】

curl -H "Content-Type: application/json" -XPUT 'localhost:9200/asset/_doc/1' -d' {
   "sn":"1",
   "name":"computert440p",
   "url":"www.myasset.com/t440p"
}'

 

【让系统分配ID的方式创建三个数据】

curl -H "Content-Type: application/json" -XPOST 'localhost:9200/asset/_doc' -d' {
   "sn":"2",
   "name":"computert14",
   "url":"www.myasset.com/t14"
}'

curl -H "Content-Type: application/json" -XPOST 'localhost:9200/asset/_doc' -d' {
   "sn":"3",
   "name":"washingMachineHaier",
   "url":"www.myasset.com/washingMachineHaier"
}'

curl -H "Content-Type: application/json" -XPOST 'localhost:9200/asset/_doc' -d' {
   "sn":"4",
   "name":"televisionShape",
   "url":"www.myasset.com/televisionShape"
}'

 

【全体资产查询】

命令:

curl -H "Content-Type: application/json" -XGET 'localhost:9200/asset/_doc/_search?pretty'

反馈(因控制台挤占只获取了部分内容):

......    
"max_score" : 1.0, "hits" : [ { "_index" : "asset", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "sn" : "1", "name" : "computert440p", "url" : "www.myasset.com/t440p" } }, { "_index" : "asset", "_type" : "_doc", "_id" : "UOJQIYEBi-L_XvTimYPH", "_score" : 1.0, "_source" : { "sn" : "2", "name" : "computert14", "url" : "www.myasset.com/t14" } }, { "_index" : "asset", "_type" : "_doc", "_id" : "UeJRIYEBi-L_XvTiwIMq", "_score" : 1.0, "_source" : { "sn" : "3", "name" : "washingMachineHaier", "url" : "www.myasset.com/washingMachineHaier" } }, { "_index" : "asset", "_type" : "_doc", "_id" : "UuJRIYEBi-L_XvTi4YOM", "_score" : 1.0, "_source" : { "sn" : "4", "name" : "televisionShape", "url" : "www.myasset.com/televisionShape" } } ] } }

 

【按字段匹配(name内容等于television)方式查询】

命令:

curl -H "Content-Type: application/json" -XGET 'localhost:9200/asset/_search?pretty' -d' {
   "query":{
      "match":{
         "name":{
         "query":"televisionShape"
     }
      }
   }
}'

反馈:

[hy@localhost ~]$ curl -H "Content-Type: application/json" -XGET 'localhost:9200/asset/_search?pretty' -d' {
>    "query":{
>       "match":{
>          "name":{
>      "query":"televisionShape"
>  }
>       }
>    }
> }'
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "asset",
        "_type" : "_doc",
        "_id" : "UuJRIYEBi-L_XvTi4YOM",
        "_score" : 1.2039728,
        "_source" : {
          "sn" : "4",
          "name" : "televisionShape",
          "url" : "www.myasset.com/televisionShape"
        }
      }
    ]
  }
}

 

【按前缀模糊匹配方式查询(相当于 like ‘prefix%’)】

命令:

curl -H "Content-Type: application/json" -XGET 'localhost:9200/asset/_search?pretty' -d' {
   "query":{
      "prefix":{
         "name":{
         "value":"computer"
     }
      }
   }
}'

反馈:

[hy@localhost ~]$ curl -H "Content-Type: application/json" -XGET 'localhost:9200/asset/_search?pretty' -d' {
>    "query":{
>       "prefix":{
>          "name":{
>      "value":"computer"
>  }
>       }
>    }
> }'
{
  "took" : 130,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "asset",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "sn" : "1",
          "name" : "computert440p",
          "url" : "www.myasset.com/t440p"
        }
      },
      {
        "_index" : "asset",
        "_type" : "_doc",
        "_id" : "UOJQIYEBi-L_XvTimYPH",
        "_score" : 1.0,
        "_source" : {
          "sn" : "2",
          "name" : "computert14",
          "url" : "www.myasset.com/t14"
        }
      }
    ]
  }
}

 

【通配符查询(wildcard方式,*匹配多个字符,?匹配一个字符)】

命令:

curl -H "Content-Type: application/json" -XGET 'localhost:9200/asset/_search?pretty' -d' {
   "query":{
      "wildcard":{
         "name":{
         "value":"*puter*"
     }
      }
   }
}'

反馈:

[hy@localhost ~]$ curl -H "Content-Type: application/json" -XGET 'localhost:9200/asset/_search?pretty' -d' {
>    "query":{
>       "wildcard":{
>          "name":{
>      "value":"*puter*"
>  }
>       }
>    }
> }'
{
  "took" : 22,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "asset",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "sn" : "1",
          "name" : "computert440p",
          "url" : "www.myasset.com/t440p"
        }
      },
      {
        "_index" : "asset",
        "_type" : "_doc",
        "_id" : "UOJQIYEBi-L_XvTimYPH",
        "_score" : 1.0,
        "_source" : {
          "sn" : "2",
          "name" : "computert14",
          "url" : "www.myasset.com/t14"
        }
      }
    ]
  }
}

 

【正则查询regexp】

命令:

curl -H "Content-Type: application/json" -XGET 'localhost:9200/asset/_search?pretty' -d' {
   "query":{
      "regexp":{
         "name":{
         "value":"([a-zA-Z]+)(440)([a-zA-Z]+)",
             "flags":"ALL"
     }
      }
   }
}'

输出:

[hy@localhost ~]$ curl -H "Content-Type: application/json" -XGET 'localhost:9200/asset/_search?pretty' -d' {
>    "query":{
>       "regexp":{
>          "name":{
>      "value":"([a-zA-Z]+)(440)([a-zA-Z]+)",
>              "flags":"ALL"
>  }
>       }
>    }
> }'
{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "asset",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "sn" : "1",
          "name" : "computert440p",
          "url" : "www.myasset.com/t440p"
        }
      }
    ]
  }
}

 

参考资料:

1. https://blog.csdn.net/qq_38146392/article/details/121399656

2. https://wenku.baidu.com/view/20dfbc2cb868a98271fe910ef12d2af90242a81b?aggId=75398777ae02de80d4d8d15abe23482fb4da02cb

3. https://www.modb.pro/db/114068

 

END

posted @ 2022-06-02 06:32  逆火狂飙  阅读(1989)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东