一、简介
solr数据操作的途径有很多,这里介绍通过http请求方式进行数据操作,http请求方式在特定环境(如需快速删除数据)下拥有方便快捷的特点。这是以solr7.2.0为例进行介绍。
二、数据操作
数据操作对象主要有两类:
1.对core的操作(如查看、创建、卸载、字段的增删改);
2.对数据本身的操作(增删改查)。
这里以在shell中http请求进行介绍,详情请参考注释:
#!/bin/bash
host="192.168.0.107"
port=8983
core_name="stu"
core_instance_dir="/home/solr/server/solr/stu"
#查看所有core
curl -s -XGET "http://${host}:${port}/solr/admin/cores?action=STATUS"
#创建core,需要提前创建core的目录及控制文件(managed-schema、solrconfig.xml)
curl -s -XGET "http://${host}:${port}/solr/admin/cores?action=CREATE&name=${core_name}&instanceDir=${core_instance_dir}"
#重启solr,当修改了配置文件时
curl -s -XGET "http://${host}:${port}/solr/admin/cores?action=RELOAD&core=${core_name}"
#缷载core
curl -s -XGET "http://${host}:${port}/solr/admin/cores?action=UNLOAD&core=${core_name}"
#####################################
#core添加字段
curl -s -H 'content-type:application/json' -XPOST "http://${host}:${port}/solr/${core_name}/schema" -d '
{
"add-field":{
"name":"stu_id",
"type":"string",
"indexed":"true",
"required":"true",
"stored":"true"
},
"add-field":{
"name":"stu_name",
"type":"string",
"indexed":"true",
"required":"true",
"stored":"true"
}
}
'
#core删除字段,可与添加字段合并,只修改json即可
curl -s -H 'content-type:application/json' -XPOST "http://${host}:${port}/solr/${core_name}/schema" -d '
{
"delete-field":{
"name":"stu_name"
}
}
'
#core修改字段
curl -s -H 'content-type:application/json' -XPOST "http://${host}:${port}/solr/${core_name}/schema" -d '
{
"replace-field":{
"name":"stu_id",
"type":"string",
"indexed":"true",
"required":"true",
"stored":"true"
}
}
'
#查看core字段
curl -s -XGET "http://${host}:${port}/solr/${core_name}/schema"
curl -s -XGET "http://${host}:${port}/solr/${core_name}/schema/fields"
curl -s -XGET "http://${host}:${port}/solr/${core_name}/schema/fields/stu_id"
curl -s -XGET "http://${host}:${port}/solr/${core_name}/schema/fieldtypes"
curl -s -XGET "http://${host}:${port}/solr/${core_name}/schema/name"
curl -s -XGET "http://${host}:${port}/solr/${core_name}/schema/uniquekey"
###############################################
#查看指定core数据
curl -s -XGET "http://${host}:${port}/solr/${core_name}/select?q=*:*"
curl -s -XGET "http://${host}:${port}/solr/${core_name}/query?q=*:*"
#指定条件查询
curl -s -H 'content-type:application/json' -XGET "http://${host}:${port}/solr/${core_name}/query" -d '
{
"query":"stu_id:*",
"offset":0,
"limit":2
}
'
#查询所有
curl -s -H 'content-type:application/json' -XGET "http://${host}:${port}/solr/${core_name}/query" -d '
{
"query":"*:*"
}
'
#添加或更新单个文档
commit_within_milliseconds=100
curl -s -H 'content-type:application/json' -XPOST "http://${host}:${port}/solr/${core_name}/update/json/docs?commitWithin=${commit_within_milliseconds}" -d '
{
"id":"7",
"stu_id":"007",
"stu_name":"apple7"
}
'
#添加或更新多个方档
commit_within_milliseconds=100
curl -s -H 'content-type:application/json' -XPOST "http://${host}:${port}/solr/${core_name}/update?commitWithin=${commit_within_milliseconds}" -d '
[
{
"id":"5",
"stu_id":"005",
"stu_name":"apple5"
},
{
"id":"6",
"stu_id":"006",
"stu_name":"apple611"
}
]
'
#添加文档
commit_within_milliseconds=100
curl -s -H 'content-type:application/json' -XPOST "http://${host}:${port}/solr/${core_name}/update?commitWithin=${commit_within_milliseconds}" -d '
{
"add":{
"doc":{
"id":"8",
"stu_id":"008",
"stu_name":"apple8"
}
}
}
'
#指定条件删除文档
commit_within_milliseconds=100
curl -s -H 'content-type:application/json' -XPOST "http://${host}:${port}/solr/${core_name}/update?commitWithin=${commit_within_milliseconds}" -d '
{
"delete":{
"query":"stu_id:003"
}
}
'
#根据id删除文档
commit_within_milliseconds=100
curl -s -H 'content-type:application/json' -XPOST "http://${host}:${port}/solr/${core_name}/update?commitWithin=${commit_within_milliseconds}" -d '
{
"delete":{
"id":"2"
}
}
'
#不指定字段时,也是根据id删除文档
commit_within_milliseconds=100
curl -s -H 'content-type:application/json' -XPOST "http://${host}:${port}/solr/${core_name}/update?commitWithin=${commit_within_milliseconds}" -d '
{
"delete":["7", "8"]
}
'
#分面查询
curl -s -XGET "http://${host}:${port}/solr/${core_name}/query?q=stu_name:apple2&facet=on&facet.field=stu_id&facet.field=stu_name"
注意:
1、上面请求的solr版本为7.2.0,但在solr5.3等版本中,查询url路径中的”solr”是不需要的,
如请求url直接为:http://${host}:${port}/${core_name}/select?q=:
2、在数据更新或删除时,需要指定提交commitWithin=10(这里即为10毫秒内提交)