Elasticsearch 搜索 API

Elasticsearch 搜索 API

搜索多个索引

# 未指定文档时,返回的是所有索引的文档
# 默认返回 10 个文档
GET /_search
# 返回 20 个文档
GET /_search?size=20
# 对多个索引进行搜索
GET /twitter,test,catalog/_search
# 搜索以 index 开头的索引并且排除 index3
GET /index*,-index3/_search

搜索单个索引

# 搜索单个文档
# 默认返回 10 个文档
GET twitter/_search
# 分页查询
# from 从 0 开始
GET twitter/_search?size=2&from=2
# DFS 查询(Elasticsearch 的领域特定语言),分页查询
GET twitter/_search
{
  "size": 2,
  "from": 2,
  "query": {
    "match_all": {}
  }
}

只返回特定字段

# 只显示特定字段
# 只显示总数
GET twitter/_search?filter_path=hits.total
# 只返回评分和 city 字段
GET twitter/_search?filter_path=hits.hits._score,hits.hits._source.city
# 只返回 user 和 city
GET twitter/_search
{
  "_source": ["user", "city"],
  "query": {
    "match_all": {}
  }
}
# 只返回 user 和 city
# includes 和 excludes 参数支持通配符
GET twitter/_search
{
  "_source": {
    "includes": ["user", "city"]
  },
  "query": {
    "match_all": {}
  }
}
# 使用 fields 指定返回字段,不返回 _source
GET twitter/_search
{
  "_source": false,
  "fields": ["user", "city"],
  "query": {
    "match_all": {}
  }
}

统计文档数量

# 统计文档总数
GET twitter/_count
# 根据条件统计文档总数
GET twitter/_count
{
  "query": {
    "match": {
      "city": "北京"
    }
  }
}

查询索引配置

# 查询索引配置
GET twitter/_settings
# 配置索引,如果索引已经存在,需要删除后重新再索引
PUT twitter1
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

修改索引 Mapping

Elasticsearch 是 schemaless 的(无须预先创建索引和 Mapping 即可插入文档)

自动识别的字段类型可能不准确,如果知道要插入文档的字段类型,可以预先创建文档

# 查询索引 Mapping
GET twitter/_mapping

修正字段类型需要删除索引后在重新创建索引

# localtion 被自动识别成 text 类型,将其修正成 geo_point 类型
DELETE twitter

PUT twitter
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

PUT twitter/_mapping
{
  "properties": {
    "address": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "age": {
      "type": "long"
    },
    "city": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "country": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "location": {
      "type": "geo_point"
    },
    "message": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "province": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "uid": {
      "type": "long"
    },
    "user": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}
GET twitter/_mapping
# 手动创建索引后,批量插入文档
POST _bulk
{"index":{"_index":"twitter","_id":1}}
{"user":"双榆树-张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}}
{"index":{"_index":"twitter","_id":2}}
{"user":"东城区-老刘","message":"出发,下一站云南!","uid":3,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}}
{"index":{"_index":"twitter","_id":3}}
{"user":"东城区-李四","message":"happy birthday!","uid":4,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}}
{"index":{"_index":"twitter","_id":4}}
{"user":"朝阳区-老贾","message":"123,gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}}
{"index":{"_index":"twitter","_id":5}}
{"user":"朝阳区-老王","message":"Happy BirthDay My Friend!","uid":6,"age":50,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}}
{"index":{"_index":"twitter","_id":6}}
{"user":"虹桥-老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}}

DSL 查询

DSL(领域特定语言)即 Elastchsearch 定义的查询语言

Match Query

# 查询城市是北京的文档
GET twitter/_search
{
  "query": {
    "match": {
      "city": "北京"
    }
  }
}
# 查询消息包含 “出” 的wed
# 默认按 _score 从大到小排序,即按相关性排序
GET twitter/_search
{
  "fields": ["message"],
  "_source": false, 
  "query": {
    "match": {
      "message": "出"
    }
  }
}
# 指定最小分值
GET twitter/_search
{
  "min_score": 1.0,
  "fields": ["message"],
  "_source": false, 
  "query": {
    "match": {
      "message": "出"
    }
  }
}
# 不计算相关性
# 只进行是否判断
GET twitter/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "city.keyword": "北京"
        }
      }
    }
  }
}
# Match Query 默认进行 OR 操作
# 即分词是用 OR 连接的,只有 user 中包含任意一个中文关键字即可
GET twitter/_search
{
  "query": {
    "match": {
      "user": "朝阳区-老贾"
    }
  }
}
# 进行 OR 操作
GET twitter/_search
{
  "query": {
    "match": {
      "user": {
        "query": "朝阳区-老贾",
        "operator": "or"
      }
    }
  }
}
# 至少要匹配 3 个分词
GET twitter/_search
{
  "query": {
    "match": {
      "user": {
        "query": "朝阳区-老贾",
        "operator": "or",
        "minimum_should_match": 3
      }
    }
  }
}
# 进行 and 操作
# 即需要匹配整个关键字
GET twitter/_search
{
  "query": {
    "match": {
      "user": {
        "query": "朝阳区-老贾",
        "operator": "and"
      }
    }
  }
}

Highlighting

# 默认使用 <em> 标签突出匹配的分词
# 默认的 Standard 分词器会将单个汉字作为一个分词
GET twitter/_search
{
  "query": {
    "match": {
      "address": "北京"
    }
  },
  "highlight": {
    "fields": {
      "address": {}
    }
  }
}
# 指定突出的 HTML 标签
GET twitter/_search
{
  "query": {
    "match": {
      "address": "北京"
    }
  },
  "highlight": {
    "pre_tags": "<b>",
    "post_tags": "</b>", 
    "fields": {
      "address": {}
    }
  }
}

ids Query

# 批量 id 查询
GET twitter/_search
{
  "query": {
    "ids": {
      "values": [1, 2]
    }
  }
}

参阅

posted @ 2022-10-17 23:03  廖子博  阅读(35)  评论(0编辑  收藏  举报