elasticsearch的简单使用介绍以及基本的操作

elasticsearch

1.number_of_shards:分片数量,类似于数据库里面分库分表,一经定义不可更改。主要响应写操作
2.number_of_replicas:副本数,用于备份分片的,和分片里面的数据保持一致,主要响应读操作,副本越多读取就越快。
3.分布式索引一定要注意分片数量不能更改,所以在创建的时候一定要预先估算好数据大小,一般在8CPU16G的机器上一个分片不要超过300g。索引会根据分片的配置来均匀的响应用户请求
4.如果调整了分片数那就要重建索引。

创建一个索引
PUT /test
{
   "settings" : {
      "number_of_shards" : 1, //分片
      "number_of_replicas" : 1  //分片副本
 }
更新其replicas状态,但是不能更新shards状态
PUT /test/_settings
{
      "number_of_replicas" : 0
}
创建索引,指定id建立索引
PUT /test/_doc/1
{
    "name": "今生", 
    "age": 30
}
指定id全量修改索引
PUT /test/_doc/1
{
    "name": "今生2"
}
指定id部分字段修改
POST /test/_doc/1/_update
{
  "doc":{
    "name":"今生2"
  }
}
指定_create防止重复创建,如果已经存在则失败,以下语句执行第二次会报错
POST /test/_doc/2/_create
{
  "name":"今生",
  "age":1
}

使用搜索全部
GET /test/_search
获取指定id
GET /test/_doc/1
不指定id建立索引,es会默认给你生成一个
POST /test/_doc/
{
    "name": "今生two", 
    "age": 30
}
删除文档:指定id
delete /test/_doc/1
删除索引
DELETE /test
结构化创建索引
PUT /test
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
  "_doc":{
         "properties": {
           "name":{"type": "text","analyzer":"ik_max_word","search_analyzer": "ik_smart"},
           "sname":{"type": "text","analyzer":"ik_smart"},
           "enname":{"type":"text","analyzer":"english"},
           "age":{"type": "integer"}
         }
    }
  }
}

Es基础类型:

Text:字符串类型,可以被分析;
Keyword:不能被分析,只可以精确匹配的字符串类型
Date:日期类型,通常配合format使用 比如{“type”:”date”,”format”:”yyyy-MM-dd”}
Long,integer,short…
Boolean
Array:数组类型
Object:一般是json
Ip:ip地址
geo_point:地理位置 {
“lat”:
“lon”:
}

Es查询:

(1)主键查询:

GET /test/_doc/1

(2)查询all

GET /test/_search
{
  "query":{
    "match_all": {}
  }
}

(3)分页查询:

GET /test/_search
{
  "query":{
    "match_all": {}
  },
  "from":0,
  "size":1
}

(4)带条件查询:

GET /test/_search
{
  "query":{
    "match": {"name":"今生"}
  }
}

(5)带排序:

GET /test/_search
{
  "query":{
    "match": {"name":"今生"}
  },
  "sort":[
  	{"age":{"order":"desc"}}
  ]
}

(6)聚合查询:

GET /test/_search
{
  "query":{
    "match": {"name":"今生"}
  },
  "sort":[
  	{"age":{"order":"desc"}}
  ],
  "aggs":{
  	"group_by_age":{
  		"terms":{
  			"field":"age"
  		}
  	}
  }
}

分析器

一般常用的有三种;默认的stander,English,IK
Ik分词在建立的时候要注意:建索引采用ik_max_word 检索采用ik_smart

GET /test/_analyze
{
  "field": "name",
  "text": "my name is jinsheng and i like eating apples and running"
}
GET /test/_analyze
{
  "field": "sname",
  "text": "深圳市软件园一期"
}

es 进阶查询

Match查询

Match时最为简单的一种查询了,它会完全按照我们定义的字段analyzer进行索引内查询。
比如Ik分词的 在查询的时候也会进行分词。里面还可以指定我们想要的查询方式 比如 or 或者 and

GET /book/_search
{
  "query": {
    "match": {
      "bookName": "童话故事大全"
    }
  }
}
对分词进行or 或者 and查询
GET /book/_search
{
  "query": {
    "match": {
      "bookName": {
        "query": "大自然的声音",
        "operator": "and"
      }
    }
  }
}

Term查询:

对查询的字段不进行分词的精准查询,除非满足关键词或者倒排索引的分词,否则查询不到

GET /book/_search
{
  "query": {
    "term": {
      "bookName": {
        "value": "童话故事大全"
      }
    }
  }
}

匹配度查询:

在检索的时候有一个最小匹配查询

GET /book/_search
{
  "query": {
    "match": {
      "bookName": {
        "query": "安徒生的大自然童话故事",
        "operator": "or",
        "minimum_should_match": 2
      }
    }
  }
}

多字段查询

GET /book/_search
{
  "query": {
    "multi_match": {
      "query": "大自然的旅行故事",
      "fields": ["bookName","discription"]
    }
  }
}
多字段添加权重
GET /book/_search
{
  "explain": true, 
  "query": {
    "multi_match": {
      "query": "大自然的旅行故事",
      "fields": ["bookName^10","discription"]
    }
  }
}
平滑一下 更加突出权重:
GET /book/_search
{
  "explain": true, 
  "query": {
    "multi_match": {
      "query": "大自然的旅行故事",
      "fields": ["bookName^10","discription"],
      "tie_breaker": 0.3 // max plus 0.3 times others of,最大值加上其他值的0.3倍
    }
  }
}
取最好的字段:
GET /book/_search
{
  "explain": true, 
  "query": {
    "multi_match": {
      "query": "大自然的旅行故事",
      "fields": ["bookName","discription"],
      "type": "best_fields"
    }
  }
}
多字段分值相加
GET /book/_search
{
  "explain": true, 
  "query": {
    "multi_match": {
      "query": "大自然的旅行故事",
      "fields": ["bookName","discription"],
      "type": "most_fields"
    }
  }
}
以分词为单位分别在每个字段里面得分取最大的相加,非常适用于以词作为权重的系统
GET /book/_search
{
  "explain": true, 
  "query": {
    "multi_match": {
      "query": "大自然的旅行故事",
      "fields": ["bookName","discription"],
      "type": "cross_fields"
    }
  }
}
queryString:
GET /book/_search
{
  "query": {
    "query_string": {
      "fields": ["bookName"],
      "query": "大自然 AND 旅行"
    }
  }
}

queryString:
简单查询中经常使用,可以配合 AND OR NOT 使用

Bool查询

Must:所有的条件都是ture
Must not:所有的条件都是false
Should:在其条件中只要有一个为ture即可,但是true越多的排在越前面

GET /book/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "bookName": "安徒生"
          }
        },
        {
          "match": {
            "discription": "丑小鸭"
          }
        }
      ]
    }
  }
}

9.过滤查询
filter:
filter如何进行排序?
(1)自行使用sort字段
(2)联合其他的查询一起使用

GET /book/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "commentNum": {
              "lte": 2000,
              "gte": 1
            }
          }
        },
        {
          "term": {
            "author":"朱奎"
          }
        }
      ]
    }
  } ,
  "sort": [
    {
      "commentNum": {
        "order": "desc"
      }
    }
  ]
}

Function score.

Score字段:

带打分的match
GET /book/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "bookName": "先生"
          }
        }
      ], 
      "filter": [
        {
          "range": {
            "commentNum": {
              "lte": 2000,
              "gte": 1
            }
          }
        },
        {
          "term": {
            "author":"王德启,王晶/著"
          }
        }
      ]
    }
  }
}

GET /book/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query": "大自然的旅行故事",
          "fields": ["bookName","discription"],
          "operator": "or",
          "type": "most_fields"
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "commentNum",
            "modifier": "log2p",
            "factor": 8
          }
        }
      ],
      
      "score_mode": "sum",
      "boost_mode": "sum"
    }
  }
}

同义词

在我们的搜索引擎中往往会出现以下这样的情况 比如:我搜苹果
这时候是不是既要出苹果也要出Iphone呢?
是的,那么这种情况怎么处理呢?同义词就派上用场啦。
(1)在Es服务器上新加同义词文件
synonyms.txt

苹果,iphone,apple
美丽,漂亮,气质好

(2)在构建索引的时候设置同义词type
(3)指定mapping的分析器为同义词

PUT /test11
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 1,
    "analysis": {
      "filter": {
        "my_synonym_filter": {
          "type": "synonym",
          "synonyms_path": "analysis-ik/synonyms.txt"
        }
      },
      "analyzer": {
        "ik_syno": {
          "type": "custom",
          "tokenizer": "ik_smart",
          "filter": [
            "my_synonym_filter"
          ]
        },
        "ik_syno_max": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "filter": [
            "my_synonym_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "ik_syno_max",
          "search_analyzer": "ik_syno"
        }
      }
    }
  }
}

PUT /test11/_doc/1
{
  "name":"苹果"
}

GET /test11/_analyze
{
  "field": "name",
  "text": "气质好"
}

GET /test11/_search
{
  "query": {
    "match": {
      "name": "apple"
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值