关于文档的基本操作(重点)

63 阅读2分钟

基本操作

简单的搜索

GET /test3/_doc/1

GET /test3/_doc/_search?q=name:小叶曲

GET /test3/_doc/_search?q=name:小叶曲11

简答的条件查询,可以根据默认的映射规则,产生基本的查询!

复杂操作搜索

select ( 排序,分页,高亮,模糊查询,精准查询!) 

查询匹配

GET /test3/_doc/_search
{
  "query": {
    "match":{
      "name": "44"
    }
  }
}

输出结果,不想要那么多

GET /test3/_doc/_search
{
  "query": {
    "match":{
      "name": "小叶曲11"
    }
  },
  "_source": ["name","birthday"] 
}

排序

desc: 倒序        asc:正序

GET /test3/_doc/_search
{
  "query": {
    "match":{
      "name": "小叶曲"
    }
  },
  "_source": ["name","age"] , 
  "sort": [
    {
      "age":{
        "order":"desc" 
      }
    }
    ]
    
}

分页查询

GET /test3/_doc/_search
{
  "query": {
    "match":{
      "name": "小叶曲"
    }
  },
  "_source": ["name","birthday"] , 
  "sort": [
    {
      "age":{
        "order":"asc" 
      }
    }
    ],
    "from": 0,
    "size": 2
}

数据下标还是从0开始的~

布尔值查询

must (and) ,所有的条件都要符合 where age = 3 and name = 小叶曲

GET /test3/_doc/_search
{
  "query": {
    "bool":{
      "must":[
        {
          "match":{
          "name": "小叶曲"
          }
        },
        {
          "match":{
          "age": "3"
          }
        }
        ]
    }
  }
}

should(or) ,所有的条件都要符合 where age = 3 or name = 小叶曲

GET /test3/_doc/_search
{
  "query": {
    "bool":{
      "should":[
        {
          "match":{
          "name": "11"
          }
        },
        {
          "match":{
          "age": "3"
          }
        }
        ]
    }
  }
}

must_not (not)

GET /test3/_doc/_search
{
  "query": {
    "bool":{
      "must_not":[
        {
          "match":{
          "name": "11"
          }
        },
        {
          "match":{
          "age": "3"
          }
        }
        ]
    }
  }
}

过滤器 filter

  • gt 大于
  • gte 大于等于
  • lt 小于
  • lte 小于等于!
GET /test3/_doc/_search
{
  "query": {
    "bool":{
      "must":[
        {
          "match":{
          "name": "小叶曲"
          }
        }
      ],
      "filter": {
          "range": {
            "age": {
              "gte": 1,
              "lte": 3
            }
          }
        }
    }
  }
}

匹配多个条件!

GET /test3/_doc/_search
{
  "query": {
    "match":{
      "tags": "靓 技术"
    }
  }
}

精确查询

term 查询是直接通过倒排索引指定的词条进程精确查找的!

关于分词:

  • term ,直接查询精确的
  • match,会使用分词器解析!(先分析文档,然后在通过分析的文档进行查询!)

两个类型 text keyword

GET _analyze
{
  "analyzer": "keyword",
  "text": "张三说我喜欢你"
}

GET _analyze
{
  "analyzer": "standard",
  "text": "张三说我喜欢你"
}

创建  testdb 并指定字段的类型:

PUT /testdb
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "desc": {
        "type": "keyword"
      }
    }
  }
}

GET /testdb/_doc/_search
{
  "query": {
    "term":{
      "desc": "法外狂徒 desc"
    }
  }
}

多个值匹配精确查询

GET /testdb/_doc/_search
{
  "query": {
    "bool":{
      "should":[
        {
          "term":{
          "t1": "44"
          }
        },
        {
          "term":{
          "t1": "55"
          }
        }
        ]
    }
  }
}

高亮查询

GET /test3/_doc/_search
{
  "query": {
    "match":{
      "name": "小叶曲11"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

GET /test3/_doc/_search
{
  "query": {
    "match":{
      "name": "小叶曲11"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color:red'>",
    "post_tags": "</p>",
    "fields": {
      "name": {}
    }
  }
}