ElasticSearch学习九 文档查询

九、文档的查询

Elasticsearch提供了基于JSON的DSL(Domain Specific Language)来定义查询。常见的查询类型包括。

  • 查询所有:查询出所有数据,一般测试用。例如:match_all

  • 全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。例如:

    match_query

    multi_match_query

  • 精确查询:根据精确词条值查找数据,一般是查找keyword、数值、日期、boolean等类型字段。例如:

    ids

    range

    term

  • 地理(geo)查询:根据经纬度查询。例如:

    geo_distance

    geo_bounding_box

  • 复合(compound)查询:复合查询可以将上述各种查询条件组合起来,合并查询条件。例如:

    bool

    function_score

9.1、term查询

Elasticsearch默认采用的是standard,这个分词器是一个字一个词,例如我们在person这个索引里面有以下几条数据

{
  "took" : 49,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 0.105360515,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.105360515,
        "_source" : {
          "name" : "张三",
          "age" : 30,
          "address" : "杭州市滨江区"
        }
      },
      {
        "_index" : "person",
        "_type" : "_doc",
        "_id" : "e0DxjYABtRuVRBuRfJYL",
        "_score" : 0.105360515,
        "_source" : {
          "name" : "李四",
          "age" : 30,
          "address" : "杭州市滨江区"
        }
      },
      {
        "_index" : "person",
        "_type" : "_doc",
        "_id" : "fEDxjYABtRuVRBuRg5aL",
        "_score" : 0.105360515,
        "_source" : {
          "name" : "李四",
          "age" : 30,
          "address" : "杭州市滨江区"
        }
      },
      {
        "_index" : "person",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.105360515,
        "_source" : {
          "name" : "李四",
          "age" : 30,
          "address" : "杭州市滨江区"
        }
      }
    ]
  }
}

 

采用term进行查询

GET person/_search
{
  "query": {
    "term": {
      "address": {
        "value": "杭州"
      }
    }
  }
}

 

返回的结果

{
  "took" : 47,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

 

任何词条都没有查询到。

 

9.2、创建指定分词器的索引

创建索引

put http://192.168.2.128:9200/person//入参
{
    "mappings":{
        "properties":{
            "name":{
                "type":"keyword"
            },
            "address":{
                "type":"text",
                "analyzer":"ik_max_word"
            }
        }
    }
}//结果
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "person"
}

 

查询索引

查询指定分词器的索引

get http://192.168.2.128:9200/person// 运行结果
{
    "person": {
        "aliases": {},
        "mappings": {
            "properties": {
                "address": {
                    "type": "text",
                    "analyzer": "ik_max_word" //address字段使用ik分词器进行分词
                },
                "name": {
                    "type": "keyword"
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1651665740384",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "nYelz6sgQMWpcNEt-hrGUA",
                "version": {
                    "created": "7040299"
                },
                "provided_name": "person"
            }
        }
    }
}

 

添加文档

POST http://192.168.2.128:9200/person/_doc/1
{
 "name" : "李四",
  "address" : "杭州市滨江区"
}
​
​
POST http://192.168.2.128:9200/person/_doc/2
{
 "name" : "李四",
  "address" : "北京市朝阳区"
}

 

分词查询

GET http://192.168.2.128:9200/person/_search//入参
{
  "query": {
    "term": {
      "address": {
        "value": "北京"
      }
    }
  }
}//结果
{
  "took" : 758,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.6931472,
        "_source" : {
          "name" : "李四",
          "address" : "北京市朝阳区"
        }
      }
    ]
  }
}

 

9.3、match查询

match会对查询条件进行分词,然后匹配的结果的并集返回。

GET person/_search
{
  "query": {
    "match": {
      "address": "北京杭州"
    }
  }
}//查询结果
{
  "took" : 736,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6931472,
        "_source" : {
          "name" : "李四",
          "address" : "杭州市滨江区"
        }
      },
      {
        "_index" : "person",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.6931472,
        "_source" : {
          "name" : "李四",
          "address" : "北京市朝阳区"
        }
      }
    ]
  }
}
​
​

 

posted @   阿瞒123  阅读(99)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
历史上的今天:
2016-06-25 构造函数constructor 与析构函数destructor(三)
2016-06-25 构造函数constructor 与析构函数destructor(一)
2016-06-25 构造函数constructor 与析构函数destructor(二)
点击右上角即可分享
微信分享提示

目录导航