Solr Example | |
大纲 | 笔记Notes |
|
正文内容 前提要求,需要安装java-runtime, 也可以直接安装jdk Debian10 : sudo apt install default-jdk 或者 指定版本:sudo apt install openjdk-11-jdk 直接使用的话,下载linux的bin版 直接tar zxvf solr-8.3.0.tgz , cd solr-8.3.0 后续所有的命令可以在当前目录下, 执行 bin/solr xxx 即可 帮助文档 gxl@deb10:~/solr/solr-8.3.0$ bin/solr -h Usage: solr COMMAND OPTIONS where COMMAND is one of: start, stop, restart, status, healthcheck, create, create_core, create_collection, delete, version, zk, auth, assert, config, autoscaling, export Standalone server example (start Solr running in the background on port 8984): ./solr start -p 8984 SolrCloud example (start Solr running in SolrCloud mode using localhost:2181 to connect to Zookeeper, with 1g max heap size and remote Java debug options enabled): ./solr start -c -m 1g -z localhost:2181 -a "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044" Omit '-z localhost:2181' from the above command if you have defined ZK_HOST in solr.in.sh. Pass -help after any COMMAND to see command-specific usage information, such as: ./solr start -help or ./solr stop -help Demo使用 练习一:集群启动、collection创建、删除和常用搜索命令 ./bin/solr start -e cloud (cloud模式启动,会启动多进程,通过zk管理集群) 如果需要单机模式 bin/solr start 即可 在提示 collection name的时候,需要把gettingstarted 替换为 techproducts, 不然后面无法导入数据 在提示 配置文件的时候,需要用 sample_techproducts_configs 替换 _default 导入测试数据: bin/post -c techproducts example/exampledocs/* 也可以通过java命令,导入: java -jar -Dc=techproducts -Dauto example\exampledocs\post.jar example\exampledocs\* 数据导入成功,就可以查询了,可以通过网页端,或者使用curl工具 或者 查询单一条件: 限定字段查询: 按类别查询: 短语查询: "CAS latency" 组合查询,使用+ (%2B)表示and,- 表示except 查询同时包含 electronics 和 music的产品 查询包含 electronics,但是不包含music的产品 空间搜索: collection的新建、删除: bin/solr delete -c techproducts bin/solr create -c -s 2 -rf 2 #新建两个分区、两个备份的collection solr集群的停止: bin/solr stop -all 练习二:schema的建立、搜索 bin/solr create -c films -s 2 -rf 2 其中属性文件默认使用 _default curl -X POST -H 'Content-type:application/json' --data-binary '{"add-field": {"name":"name", "type":"text_general", "multiValued":false, "stored":true}}' http://localhost:8983/solr/films/schema 创建catchall copyfield,慎用,生产环境可能会创建两重索引,比较耗时 curl -X POST -H 'Content-type:application/json' --data-binary '{"add-copy-field" : {"source":"*","dest":"_text_"}}' http://localhost:8983/solr/films/schema 导入文件,任意执行一种文件即可: bin/post -c films example/films/films.json bin/post -c films example/films/films.xml bin/post -c films example/films/films.csv -params "f.genre.split=true&f.directed_by.split=true&f.genre.separator=|&f.directed_by.separator=|” 使用java原始命令: java -jar -Dc=films -Dauto example\exampledocs\post.jar example\films\*.json java -jar -Dc=films -Dauto example\exampledocs\post.jar example\films\*.xml java -jar -Dc=films -Dparams=f.genre.split=true&f.directed_by.split=true&f.genre.separator=|&f.directed_by.separator=| -Dauto example\exampledocs\post.jar example\films\*.csv 查询: Enter "comedy" in the q box and hit Execute Query again. You should see get 417 results 特征(facts) 查询: To see facet counts from all documents (q=*:*): turn on faceting (facet=true), and specify the field to facet on via the facet.field parameter. If you only want facets, and no document contents, specify rows=0. The curl command below will return facet counts for the genre_str field: If you wanted to control the number of items in a bucket, you could do something like this: 按区间特征查询:20年前 到 今年区间,间隔1年 多轴特征查询:genre_str 和 directed_by_str 两个维度 curl "http://localhost:8983/solr/films/select?q=*:*&rows=0&facet=on&facet.pivot=genre_str,directed_by_str" 测试collection的删除: bin/solr delete -c films 练习三:索引自己的数据 建立本地文档的collection ./bin/solr create -c localDocs -s 2 -rf 2 导入本地文档中的文件 ./bin/post -c localDocs ~/Documents 如果文档更新,通过 bin/post 更新导入 Execute the following command to delete a specific document: bin/post -c localDocs -d "" To delete all documents, you can use "delete-by-query" command like: bin/post -c localDocs -d "” 删除所有example数据 bin/solr stop -all ; rm -Rf example/cloud/ |
总结Summary | |
启动、创建collection、导入数据、停止、删除 创建schema |