RESTfull接口访问Elasticsearch

发布于:2024-04-04 ⋅ 阅读:(169) ⋅ 点赞:(0)

【数据库的健康值】
curl -X GET  "ip:9200/_cat/health"

【查看所有索引】
curl -X GET "ip:9200/_cat/indices?v"

【查看索引index_name】
curl -X GET "ip:9200/索引?pretty"


【创建索引/文档】
PUT "ip:9200/索引/文档id"
{请求体}

【更新文档】
POST /索引/类型/文档id/_update
{
    "doc": {
        "name": "赤羽信之戒",
        "age": 46
    }
}

【删除文档】
DELETE 索引/类型/文档Id

---------------------------------【查询文档】------------------------------
GET 索引/类型/文档id

GET 索引/类型/_search?q=name:smy

精确匹配
GET 索引/_search
{
  "query": {
    "match": {  
      "name": "任飘渺"
    }
  }
}

and 过滤

GET 索引/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "任飘渺"
          }
        },
        {
          "match": {
            "age": "40"
          }
        }
      ]
    }
  }
}

or 过滤

GET 索引/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "任飘渺"
          }
        },
        {
          "match": {
            "age": "40"
          }
        }
      ]
    }
  }
}

not 过滤

GET 索引/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "age": "40"
          }
        }
      ]
    }
  }
}

范围过滤
GET 索引/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "任飘渺"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gt": 20,
            "lt": 40
          }
        }
      }
    }
  }
}

精确查询  精确查询是通过term关键字来实现的,他的底层是通过直接查询倒排索引,效率更高!
关于分词的解释:
(1)term:直接查询精确的
(2)match:会使用分词器(会先分析文档,然后通过分析的文档进行查询!查询效率低)

说明:如果类型是text类型,那么是可以被分词器解析的;如果是keyword类型的,是不能被分词器解析,想使用match或者term来查询这个字段匹配的,只能查询出完全匹配的数据来,其他的数据差一个字符都不能被查询出来!

GET 索引/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "name": "任飘渺"
          }
        },
        {
          "term": {
            "age": "40"
          }
        }
      ]
    }
  }
}

过滤字段

GET 索引/_search
{
  "_source":["name","age"],
  "query": {
    "match": {
      "name": "任飘渺"
    }
  }
}

排序
GET 索引/_search
{
  "query": {
    "match": {
      "name": "任飘渺"
    }
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ],
  "from":0,
  "size":1
}