前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ElasticSearch Server 扩展的弹性搜索解决方案

ElasticSearch Server 扩展的弹性搜索解决方案

作者头像
凹谷
发布2020-04-11 20:46:17
1.5K0
发布2020-04-11 20:46:17
举报

不要失去信心,只要坚持不懈,就终会有成果的。

ElasticSearch REST API 操作:

GET 获取所请求的对象状态

POST改变当前对象状态

PUT 创建对象

DELETE 销毁对象

概念:

索引(相当数据库,包含行(代表文档)和列(字段)的表)、副本(用于控制查询性能及数据故障)、分片(每个分片就是一个Lucene索引)

通用属性:

index_name:该属性存储在索引中字段的名称,不指定默认为字段定义的对象名称

index:该属性取值为analyzed或no,字符串也可以设置成not_analyzed,设置analyzed该字段被索引,可以被搜索,设置no该字段不能被搜索,字符串设置为not_analyzed该字段可以被搜索但是不分析,只能原样搜索。

store:取值为no或yes,标注该字段是否存储原始值,即使没有指定原始值也可以通过_source返回

boost:默认1,表示该字段在文档中的重要性,分数越高越重要

null_value:表示该字段在文档中不存在时应写入何值

include_in_all:该属性指定某字段是否包含到_all字段中

字段类型:字符型、数值型、布尔型、二进制型、multi_field类型

分词器:内置分词器(eg:standard、simple、keyword等)、自定义分词器

模板:可应用到所有索引,存放到config/templates/目录下,模板之间可覆盖

路由:routing参数(put数据时指定)、_routing字段(定义字段指定,相比routing参数要慢一些)

别名:可以为一个索引或多个索引定义另一个名字,也支持过滤别名(支持filter指定数据用于别名)

过滤器支持缓存 _cache参数

搜索数据:简单查询、复合查询、排序、支持调用脚本config/scripts目录下

支持数据类型:对象、数组、高亮、处理文件、自动补全、地址位置

简单查询:

#通过URI请求查询

GET /library/book/_search?q=title:crime&pretty=true

#term 查询 term查询不被解析,只能精确查询,可以指定多个索引、多个类型

代码语言:javascript
复制
GET /library/book/_search?pretty=ture ' -d '
{
  "query": {
    "term": {
      "title": {
        "value": "crime"
      }
    }
  }
}

#分页和结果规模

代码语言:javascript
复制
GET /library/_search?pretty=true '-d'
{
"from": 0,
"size": 3,
"query": {
  "term": {
    "title": {
      "value": "the"
    }
  }
}
}

#返回版本号

代码语言:javascript
复制
GET /library/_search?pretty=true '-d'
{
  "version": true,
  "query": {
    "term": {
      "title": {
        "value": "crime"
      }
    }
  }
}

#限制结果分数,添加min_score设置最小分数,高于0.60的文档

代码语言:javascript
复制
GET /library/_search?pretty=true '-d'
{
 "min_score":0.60,
 "query": {
   "term": {
     "title": {
       "value": "crime"
     }
   }
 }
}

#指定搜索位置

代码语言:javascript
复制
GET /library/_search?preference=_local
{
  "query": {
    "term": {
      "title": {
        "value": "crime"
      }
    }
  }
}

#term查询 不被解析,匹配精确的词项,指定boost属性查询权重10倍

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "term": {
      "title": {
        "value": "crime",
        "boost": 10
      }
    }
  }
}

#terms查询,minimum属性设置为1,至少匹配一个词项,为2需同时包含2个词项

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "terms": {
      "tags": [
        "novel",
        "book"
      ]
    }
  }
}

#match查询,提取查询参数中给定的值,分析这些值,匹配包含crime and 或punishment的所有文档

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "match": {
      "title": "Crime and punishment"
    }
  }
}

#布尔match查询

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "match": {
      "title":{
        "query": "crime and punishment"
        , "operator": "and"
      }
    }
  }
}

#match_phrase 查询,与布尔区别是构造一个短语查询,slop查询短语中间隔几个未知单词算匹配成功

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "crime and punishment",
        "slop": 12
      }
    }
  }
}

#match_phrase_prefix

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "match_phrase_prefix": {
      "title": {
        "query": "crime and punishment",
        "slop": 1,
        "max_expansions": 10
      }
    }
  }
}

#multi_macth查询,查询作用到多个字段上

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "multi_match": {
      "query": "crime and punishment",
      "fields": ["title","otitle"]
    }
  }
}

#query_string查询,支持lucene所有查询语法

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "title:crime^10 +title:punishment -otitle:cat"
    }
  }
}
代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "query_string": {
      "fields": ["title","otitle"],
      "query": "crime punishment",
      "use_dis_max": true
    }
  }
}

#ids 查询

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "ids": {
      "values": ["10","11","12"]
    }
  }
}

#prefix 查询,类似于term查询,类似与多term查询

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "prefix": {
      "title": {
        "value": "cri"
      }
    }
  }
}

#fuzz 查询基于模糊串,计算给定词项与文档的编辑距离来得到结果,该类查询对CPU资源消耗是昂贵的,对模糊匹配场景很实用

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "fuzzy": {
      "title": "cirme"
    }
  }
}

#match_all 查询匹配索引中所有文档的简单查询

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "match_all": {}
  }
}

#wildcard查询允许使用*和?通配符

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "wildcard": {
      "title": {
        "value": "cr*e"
      }
    }
  }
}

#range 查询

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "range": {
      "year": {
        "gte": 1700,
        "lte": 1900
      }
    }
  }
}

##########过滤器(不影响打分)#####

#range过滤器

代码语言:javascript
复制
GET library/_search?pretty
{
  "post_filter": {
    "range": {
      "year": {
        "gte": 1930,
        "lte": 1990
      }
    }
  }
}

#exists过滤器 有指定字段的文档

代码语言:javascript
复制
GET library/_search?pretty
{
  "post_filter": {
    "exists": {
      "field": "year"
    }
  }
}

#script过滤器(error)

代码语言:javascript
复制
GET library/_search?pretty
{
  "post_filter": {
    "script": {
      "script":"now - doc['year'].value >100"
      , "params": {
        "now":2019
      }
    }
  }
}

#limit过滤器,限制每个分片返回文档数目(error)

代码语言:javascript
复制
GET library/_search?pretty
{
  "post_filter": {
    "limit": {
      "value": 1
    }
  }
}

#ids过滤器 得到标识符为2个文档

代码语言:javascript
复制
GET library/_search?pretty
{
  "post_filter": {
    "ids": {
      "type": ["book"],
      "values": [
        "2"
      ]
    }
  }
}

#过滤器组合 bool、and、or和not过滤器(error,重点在搞一下)

代码语言:javascript
复制
GET library/_search?pretty
{
  "post_filter": {
    "not": {
      "and":[{
        "term":{
          "title":"crime"
        }
      },
      {
        "or":[
          {
            "range":{
              "year": {
                 "gte": 1930,
                 "lte": 1990
                }
            }
          },{
            "term":{
              "available":true
            }
          }
          ]
      }]
    }
  }
}

############组合查询############

#bool查询

#should 可以匹配也可以不匹配

#must 必须在返回的文档上匹配上

#must_not 不能在返回的文档上匹配上

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "title": {
            "value": "crime"
          }
        }}
      ],
      "should": [
        {
          "range": {
            "year": {
              "gte": 1900,
              "lte": 2000
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "otitle": {
              "value": "nothing"
            }
          }
        }
      ]
    }
  }
}

#constant_score 查询,该查询用于封装另一个查询(过滤器),返回的每个文档都得到一个恒定值

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "constant_score": {
        "query": {
          "term": {
            "title": {
              "value": "crime"
            }
          }
        },
      "boost": 1.2
    }
  }
}

#indices查询,支持在多个索引中查询

##########数据排序##########

#默认排序

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "terms": {
      "title": [
        "crime",
        "front",
        "punishment"
      ]
    }
  }
  , "sort": [
    {
      "_score": {
        "order": "asc"
      }
    }
  ]
}

#error

代码语言:javascript
复制
POST library/book/ '-d'
{
  "title":{
    "type":"multi_field",
    "fields":{
      "title":{"type":"text"},
      "sort":{"type":"text","index":"not_analyzed"}
    }
  }
}

#高亮,默认<em></em>

代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "term": {
      "title": {
        "value": "crime"
      }
    }
  }
  , "highlight": {
    "pre_tags": ["<b>"],
    "post_tags": ["</b>"],
    "fields": {
      "title": {}
    }
  }
}
代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "term": {
      "title": {
        "value": "crime"
      }
    }
  }
  , "highlight": {
    "fields": {
      "title": {"pre_tags": ["<b>"],"post_tags": ["</b>"]}
    }
  }
}
代码语言:javascript
复制
GET library/_search?pretty
{
  "query": {
    "constant_score": {
      "filter": {
        "query_string": {
          "query": "available:false author:heller"
        }
      },
      "boost": 5
    }
  }
}
代码语言:javascript
复制
GET /library/_search
{
  "query": {
    "match_all": {}
  }
}

#添加属性字段

代码语言:javascript
复制
PUT /library/book/_mapping
{
  "properties": {
    "name":{"type": "text","store": true,"index": true}
  }
}

#创建嵌套文档

代码语言:javascript
复制
PUT /library/book/_mapping
{
  "properties": {
    "variation":{
      "type": "nested",
      "properties": {
        "size":{"type": "text","store": true,"index": true},
        "color":{"type": "text","store": true,"index": true}
      }
    }
  }
}
代码语言:javascript
复制
PUT /library/book/6
{
  "title":"The Book",
  "author":"Test author",
  "name":" the Sorec ",
  "variation":[
    {"size":"xxl","color":"red"},
    {"size":"xxxl","color":"green"}
    ]
}
代码语言:javascript
复制
GET /library/book/_search/
{
  "query": {
    "nested": {
      "path": "variation",
      "query": {
        "bool": {
          "must": [
            {"term": {"variation.size": "xxl"}},
            {"term": {"variation.color": "red"}}
          ]
        }
      }
    }
  }
}

有问题欢迎留言讨论。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-07-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大数据与微服务架构 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档