使用es搜索数据

es提供了DSL(domain specific Language 领域专定语言),其实就是一个json体,用http请求的方式向es中获取数据。

DSL分为2类

  1. leaf query clause:可以理解为简单的查询语句,这种类型的语句比较多,像match,term,range等,表示一些特定功能的查询语句
  2. compound query clause:组合查询语句,这类由多个leaf query clause 或者 compound query clause 语句组成。例如bool,dis_max等等。
    es本身提供很多搜索特性如,过滤,排序,分页,折叠(collapse),滚动查询(scroll query)等等。

1.boolean query

boolean查询,可以由must,should,filter,must_not等字句组成,它们都可以定义多个子句。
must和should中的定义的语句,参与相关性的计算(_score分数),filter和must_not子句在filter阶段,不参与相关性的计算。
must:表示must定义的字句中的条件必须匹配,
should:期望匹配should字句中定义的条件,可以设置should中最少需要匹配的子句minimum_should_match ,在must语句不存在的情况下,如果定义了should语句同时must和filter都不存在的,那么默认至少要匹配一个should子句
must_not:返回的结果不能匹配该语句,和filter语句一样,不会参与相关性的计算。
我们以官方提供的一个索引数据文件举例:
有一个银行员工的信息的索引结构,定义如下:

{
   
  "bank" : {
   
    "mappings" : {
   
      "properties" : {
   
        "account_number" : {
   
          "type" : "long"
        },
        "address" : {
   
          "type" : "text",
          "fields" : {
   
            "keyword" : {
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "age" : {
   
          "type" : "long"
        },
        "balance" : {
   
          "type" : "long"
        },
        "city" : {
   
          "type" : "text",
          "fields" : {
   
            "keyword" : {
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "email" : {
   
          "type" : "text",
          "fields" : {
   
            "keyword" : {
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "employer" : {
   
          "type" : "text",
          "fields" : {
   
            "keyword" : {
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "firstname" : {
   
          "type" : "text",
          "fields" : {
   
            "keyword" : {
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "gender" : {
   
          "type" : "text",
          "fields" : {
   
            "keyword" : {
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "lastname" : {
   
          "type" : "text",
          "fields" : {
   
            "keyword" : {
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "state" : {
   
          "type" : "text",
          "fields" : {
   
            "keyword" : {
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "user_id" : {
   
          "type" : "alias",
          "path" : "account_number"
        }
      }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用ElasticSearch实现搜索,需要以下步骤: 1. 安装ElasticSearch并启动它 2. 创建一个索引,定义需要搜索的字段和数据类型 3. 将数据添加到索引中 4. 使用查询DSL(Domain Specific Language)构建查询请求 5. 发送查询请求到ElasticSearch,获取搜索结果 下面是一个简单的例子: 1. 安装ElasticSearch并启动它 2. 创建一个索引,定义需要搜索的字段和数据类型 ``` PUT /my_index { "mappings": { "properties": { "title": { "type": "text" }, "content": { "type": "text" }, "publish_date": { "type": "date" } } } } ``` 3. 将数据添加到索引中 ``` POST /my_index/_doc/1 { "title": "ElasticSearch Tutorial", "content": "This is a tutorial on how to use ElasticSearch for searching data", "publish_date": "2021-01-01" } ``` 4. 使用查询DSL构建查询请求 ``` GET /my_index/_search { "query": { "match": { "content": "ElasticSearch" } } } ``` 5. 发送查询请求到ElasticSearch,获取搜索结果 ``` { "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 0.2876821, "hits": [ { "_index": "my_index", "_type": "_doc", "_id": "1", "_score": 0.2876821, "_source": { "title": "ElasticSearch Tutorial", "content": "This is a tutorial on how to use ElasticSearch for searching data", "publish_date": "2021-01-01" } } ] } } ``` 这个例子展示了如何使用ElasticSearch进行文本搜索。当然,ElasticSearch还支持各种各样的查询类型,比如范围查询、布尔查询、聚合查询等。具体的查询DSL语法可以参考ElasticSearch官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值