Elasticsearch入门

中文文档

1、新增

curl -H "Content-Type: application/json" -XPUT 'localhost:9200/index_test/test_type/1?pretty' -d ' # 这里的pretty参数的作用是使得返回的json显示地更加好看。1是文档的id值(唯一键)。
{
    "name": "zhangsan",
    "age" : "12"
}'

2、查询

1. 按ID查询单个文档
curl -u root:pwd http://127.0.0.1:9200
curl -XGET 'localhost:9200/index_test/test_type/1?pretty'
2. 按条件查询
curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:kimchy&pretty=true'

twitter是索引的名称
tweet是类型的名称
pretty是将返回的信息以可读的JSON形式返回

{
"took": 9,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 36,
"max_score": 7.3576975,
"hits": [
{
"_index": "index_t",
"_type": "t",
"_id": "cJVupGkBzU6g49Db24A7",
"_score": 7.3576975,
"_source": {
"datetime": "2019年03月19日20时-03月20日02时",
"city": "天津市区",
"time": "2019031917",
"desc": "多云,降水概率20%,南风3-4级"
}
},....

took:是查询花费的时间,毫秒单位
time_out:标识查询是否超时
_shards:描述了查询分片的信息,查询了多少个分片、成功的分片数量、失败的分片数量等
hits:搜索的结果,total是全部的满足的文档数目,hits是返回的实际数目(默认是10)
_score是文档的分数信息,与排名相关度有关。

#查询多个匹配条件,size不指定,那么默认返回10条
http://localhost:9200/index_t/t/_search?q=time:2019031917 AND city=tianjin&size=100
curl -XPOST 'http://localhost:9200/index_t/_search?pretty=true' -d '
{
  "query": {
    "bool": {
      "must":[
{"match":{"time":"2019031917"}},
{"match":{"city":"天津市区"}}
  ]
    }
  }
}'
#from从0开始,请求了第10-30的文档
http://localhost:9200/index_t/_search?q=time:2019031917 AND city=tianjin&from=10&size=20
curl -XPOST 'localhost:9200/index_t/_search?pretty' -d '
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 20
}'
#match_all搜索全部的文档
#指定文档返回的排序方式
curl -XPOST 'localhost:9200/index_t/_search?pretty' -d '
{
  "query": { "match_all": {} },
  "sort": { "balance": { "order": "desc" } }
}'

查询数值范围1536854400000<=time<=1536940800000

{
  "query": {
    "range": {
      "time": {
        "gte": 1536854400000,
        "lte": 1536940800000

      }
    }
  }
}

查询1536854400000<=time<=1536940800000 and Station_Id_C=‘54464’

{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "Station_Id_C": "54464"
        }},
		{
    "range": {
      "time": {
        "gte": 1536854400000,
        "lte": 1536940800000

      }
    }
  }
     ]
    }
  }
}
3.模糊查询
{
    "query": {
       "wildcard" : { "disasterName2.keyword" : "*雷电*" }
    }
}


注意:Fuzzy Query将在6.0中正式移除,使用匹配查询替代模糊查询。

{
    "query": {
        "fuzzy" : {
            "disasterName2" : {
                "value" :         "雷电",
                 "fuzziness" :    1
            }
        }
    }
}

3、查看所有索引

curl -XGET 'http://localhost:9200/_cat/indices?v'

4、查看某个索引

curl -XGET 'http://localhost:9200/index'

5、删除索引

curl -XDELETE 'http://localhost:9200/index?pretty'
删除id的索引
curl -XDELETE 'http://localhost:9200/index/type/{id}'

6、删除部分记录,不删除索引

curl -X POST "localhost:9200/index/type/_delete_by_query" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {
    }
  }
}
{
  "query": {
    "range": {
      "time": {
        "gte": 1586398563000
      }
    }
  }
}

7、time倒序排列

{
  "query": {
    "match_all": {
    }
  },
  "sort": [
    {
      "time": {
        "order": "desc"
      }
    }
  ]
}

8、查询location字段不存在的记录,或存在(must)

{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "location"
                }
            }
        }
    }
}

type!=‘hrrr’

{
    "query": {
        "bool": {
            "must_not": {
                "match": {
                    "type": "hrrr"
                }
            }
        }
    }
}

9、更新所有

PUT /index/type/id
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

10、更新filename,修改为2019071007-yqgb.docx

POST /index/type/id/_update
{
  "doc": {
    "filename": "2019071007-yqgb.docx"
  }
}

11、更新后,删除interests字段

POST /index/type/id/_update
{
  "script":    "ctx._source.remove(\"interests\")"
}

12、更新后,增加tel字段

POST /index/type/id/_update
{
  "script":    "ctx._source.tel=\"158111\""
}

13、创建索引

PUT /test
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "properties" : {
            "field1" : { "type" : "text" }
        }
    }
}

14、自动创建索引

PUT _cluster/settings
{
    "persistent": {
        "action.auto_create_index": "true" 
    }
}

15、查询时间字段


{
  "query": {
    "range": {
      "date": {
        "gte": "2019-01-01",
        "lte": "2019-01-31"
      }
    }
  }
}

16、增加索引字段

PUT indexName/_mapping/indexType
{
  "properties": {
       "I": {
           "type":     "float"
       }
   }
}

17、in查询

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "range": {
                                    "time": {
                                        "from": 1637107200000,
                                        "to": 1637107200000
                                    }
                                }
                            },
                            {
                                "terms": {
                                    "Station_Id_C": [
                                        "58360",
                                        "54301"
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
String[] stas = staIds.split(",");
shouldQuery.must(QueryBuilders.termsQuery("Station_Id_C",stas));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ElasticSearch是一个开源的分布式搜索引擎,基于Lucene库。它可以快速地存储、搜索和分析大量的数据。ElasticSearch被广泛用于日志分析、全文搜索、安全分析和商业智能等领域。 以下是ElasticSearch入门指南: 1. 安装ElasticSearch:你可以从ElasticSearch官网下载并安装ElasticSearch。根据你的操作系统选择相应的版本。 2. 启动ElasticSearch:启动ElasticSearch非常简单。只需在终端中运行elasticsearch命令即可。 3. 探索ElasticSearch:通过在浏览器中输入http://localhost:9200/,可以访问ElasticSearch的REST API,并能看到ElasticSearch的基本信息。 4. 创建索引:在ElasticSearch中,数据被存储在索引中。你可以通过发送PUT请求来创建一个新的索引。例如,你可以使用以下命令来创建一个名为“my_index”的新索引: ``` PUT /my_index { "settings": { "number_of_shards": 1, "number_of_replicas": 0 } } ``` 5. 添加文档:在ElasticSearch中,文档是指一个JSON对象。你可以使用以下命令将文档添加到“my_index”索引中: ``` PUT /my_index/_doc/1 { "title": "Elasticsearch入门", "author": "John", "content": "Elasticsearch是一个开源的分布式搜索引擎" } ``` 6. 搜索文档:你可以使用以下命令来搜索“my_index”索引中的所有文档: ``` GET /my_index/_search ``` 7. 进行查询:你可以使用查询语句来搜索“my_index”索引。例如,你可以使用以下命令来搜索标题包含“Elasticsearch”的所有文档: ``` GET /my_index/_search { "query": { "match": { "title": "Elasticsearch" } } } ``` 这就是ElasticSearch入门指南。对于更深入的学习,你可以查看ElasticSearch官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值