ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ElasticSearch学习九 文档查询

2022-06-25 19:31:09  阅读:157  来源: 互联网

标签:name 查询 person score 文档 address ElasticSearch type


九、文档的查询

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" : "北京市朝阳区"
        }
      }
    ]
  }
}
​
​

 

标签:name,查询,person,score,文档,address,ElasticSearch,type
来源: https://www.cnblogs.com/cplinux/p/16412253.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有