Elasticsearch: 权威指南 学习索引别名和结构化查询练习

索引别名和零停机

创建一个索引

PUT my_index_v1

查询索引信息

GET my_index_v1

给my_index_v1索引指定一个索引别名my_index_v2

PUT my_index_v1/_alias/my_index_v2

查看my_index_v1的详情信息

GET my_index_v1

查看别名my_index_v2 指向了哪一个索引

GET /*/_alias/my_index_v2

查看my_index_v1有哪些别名

GET /my_index_v1/_alias

PUT my_index

####一个别名可以指向多个索引,所以我们在添加别名到新索引的同时必须从旧的索引中删除它。这个操作需要原子化,这意味着我们需要使用 _aliases

POST /_aliases
{
    "actions": [
        { "remove": { "index": "my_index_v1", "alias": "my_index_v2" }},
        { "add":    { "index": "my_index", "alias": "my_index_v2" }}
    ]
}

GET my_index
GET my_index_v1

POST /my_store/_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" }

结构化查询 price = 20的数据并且不计算得分

GET my_store/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "price": "20"
        }
      },
      "boost": 1.2
    }
  }
}
GET my_store

###结构化查询 productID = QQPX-R-3956-#aD8

直接通过term查询 productID 是查询不到数据的,因为他对我们的数据进行了分词。所以可以通过productID.keyword进行查询,或者重建索引,指定改字段不分词

GET my_store/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "productID.keyword": "QQPX-R-3956-#aD8"
        }
      },
      "boost": 1.2
    }
  }
}

DELETE my_store
PUT my_store
{
  "mappings": {
    "properties": {
      "productID": {
        "type": "keyword"
      }
    }
  }
}

现在直接进行查询就可以查询到相应的结果了

GET my_store/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "productID": "QQPX-R-3956-#aD8"
        }
      },
      "boost": 1.2
    }
  }
}

GET my_store

bool查询price=20 或id=XHDK-A-1293-#fJ3 并且价格不等于30的数据列表

GET my_store/_search
{
  "explain": true, 
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "price": {
              "value": "30"
            }
          }
        }
      ],
      "should": [
        {
          "term": {
            "price": {
              "value": "20"
            }
          }
        },
        {
          "term": {
            "productID": {
              "value": "XHDK-A-1293-#fJ3"
            }
          }
        }
      ]
    }
  }
}

查询id=XHDK-A-1293-#fJ3 或者 id=JODL-X-1937-#pV7 并且price = 30的数据链表

GET my_store/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "productID": {
              "value": "XHDK-A-1293-#fJ3"
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "productID": {
                    "value": "JODL-X-1937-#pV7"
                  }
                }
              },
              {
                "term": {
                  "price": {
                    "value": "30"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

数字范围查询

GET my_store/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "price": [
            "20",
            "30"
          ]
        }
      },
      "boost": 1.2
    }
  }
}

字符串范围查询

GET my_store/_search
{
  "query": {
    "range": {
      "productID": {
        "gte": "A",
        "lte": "Q"
      }
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值