Elasticsearch(十一)结构化搜索

 

批量造点数据

创建索引

PUT /my_store
{
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 1
  }
}

命令行形式:

curl -H "Content-Type: application/json" -XPUT 'http://10.24.54.241:9200/my_store' -d '{"settings" : {"number_of_shards" : 2,"number_of_replicas" : 1}}'

创建成功,如下图:

批量插入数据:

Kibana上运行的命令:

POST /my_store/products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8" }

命令行窗口运行的命令:

curl -i -H "Content-Type: application/json" -XPOST 'http://10.24.54.241:9200/my_store/products/_bulk?pretty' --data-binary @bulk.json

DevTools

查询数据

命令如下:

GET /my_store/products/_search?pretty
curl -XGET 'http://10.24.54.241:9200/my_store/products/_search?pretty'

结果:

{
  "took" : 67,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_store",
        "_type" : "products",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "price" : 30,
          "productID" : "JODL-X-1937-#pV7"
        }
      },
      {
        "_index" : "my_store",
        "_type" : "products",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "price" : 10,
          "productID" : "XHDK-A-1293-#fJ3"
        }
      },
      {
        "_index" : "my_store",
        "_type" : "products",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "price" : 20,
          "productID" : "KDKE-B-9947-#kL5"
        }
      },
      {
        "_index" : "my_store",
        "_type" : "products",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "price" : 30,
          "productID" : "QQPX-R-3956-#aD8"
        }
      }
    ]
  }
}

 

1、查找准确值

对于准确值,你需要使用过滤器。过滤器的重要性在于它们非常的快。

命令:

GET /my_store/products/_search
{
    "query" : {
        "bool" : {
            "filter" : {
                "term" : { 
                    "price" : 20
                }
            }
        }
    }
}

curl命令行:

curl -H "Content-Type: application/json" -XPOST 'http://10.24.54.241:9200/my_store/products/_search?pretty' -d '
{
    "query" : {
        "bool" : {
            "filter" : {
                "term" : { 
                    "price" : 20
                }
            }
        }
    }
}'

结果:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my_store",
        "_type" : "products",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "price" : 20,
          "productID" : "KDKE-B-9947-#kL5"
        }
      }
    ]
  }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值