Elasticserach安装

下载并启动

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-darwin-x86_64.tar.gz

tar -xzv elasticsearch-7.0.0-darwin-x86_64.tar.gz

cd elasticsearch-7.0.0/bin

./elasticsearch

验证启动成功

1、通过jps,可以看到Elasticserach进程id

2、Elasticserach默认的web端口是9200,通过访问url地址,可以看到返回Elasticserach的集群、版本等信息。说明已经启动成功。

curl -get http://localhost:9200

{
  "name" : "xx-Pro.local",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "dItzL8pMQZSuJD4Z05GIQA",
  "version" : {
    "number" : "7.0.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "b7e28a7",
    "build_date" : "2019-04-05T22:55:32.697037Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.7.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

 

安装中文分词

./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.0.0/elasticsearch-analysis-ik-7.0.0.zip

安装完成后,重启Elasticsearch,就会加载插件。

 

Example

  1. create index
    curl -XPUT http://localhost:9200/index
    
  2. create mapping

    curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d'
    {
            "properties": {
                "content": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_smart"
                }
            }

    }'

  3. index some doc
    curl -XPOST http://localhost:9200/index/_create/1 -H 'Content-Type:application/json' -d'
    {"content":"美国留给伊拉克的是个烂摊子吗"}
    '
    curl -XPOST http://localhost:9200/index/_create/2 -H 'Content-Type:application/json' -d'
    {"content":"公安部:各地校车将享最高路权"}
    '

   4.query

-- 根据单个条件查询,匹配“校车”

curl -XPOST http://localhost:9200/index/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "校车" }}
}
'

返回
{"took":93,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.6931472,"hits":[{"_index":"index","_type":"_doc","_id":"2","_score":0.6931472,"_source":
{"content":"公安部:各地校车将享最高路权"}

 -- 根据多个条件查询 ,必需同时匹配content和title的值

curl -XPOST http://localhost:9200/index/_search  -H 'Content-Type:application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "1111"
          }
        },
        {
          "match": {
            "title": "yuefeng"
          }
        }
      ]
    }
  }
}'

 

-- 根据多个值查询,必需匹配content,可选匹配title

curl -XPOST http://localhost:9200/index/_search  -H 'Content-Type:application/json' -d'
{
  "query": {
    "bool": {
      "must": 
        {
          "match": {
            "content": "1111"
          }
        },
        "must_not":
        {
          "match": {
            "title": "yuef11eng"
          }
        }
      
    }
  }
}'

 

 

参考 : https://github.com/medcl/elasticsearch-analysis-ik

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值