Hello solr

20180410191304255.png

Install

  1. install jdk1.8, source profile
  2. install solr7.5, tar -zxvf package
  3. try to start solr ./bin/solr start
    if using root user try ./bin/solr start -force

then ip + port can used to visit solr’s administration user interface.

Add core

in administration user interface create a core will warning create error.

copy official config file into new core, create core in interface again.

1
2
3
cd /usr/solr-7.5.0/server/solr
mkdir demo
cp -r configsets/_default/conf/ demo

or use command create:

1
./bin/solr create -c demo

Data import

db configuration

Create file data.xml in solr-7.5.0/server/solr/demo/conf
column in field represent filed in DB
name in field represent unique filed in solr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/demo"
user="demo"
password="demo"/>
<document>
<entity name="demo" query="select * from demo" >
<field column="Name" name="Name"/>
<field column="Age" name="Age"/>
<field column="Price" name="Price"/>
</entity>
</document>
</dataConfig>

modfiy solrconfig

modify solrconfig.xml add datasource option

1
2
3
4
5
<requestHandler name="/dataimport" class="solr.DataImportHandler">
<lst name="defaults">
<str name="config">data.xml</str>
</lst>
</requestHandler>

modify managed-schema

modify managed-schema add data field in searching

1
2
3
<field name="Name" type="string" indexed="true" stored="true"/>
<field name="Age" type="pint" indexed="false" stored="true"/>
<field name="Price" type="pdouble" indexed="false" stored="true"/>

add jar

1
2
3
4
cd /usr/solr-7.5.0
cp -r dist/solr-dataimporthandler-* server/solr-webapp/webapp/WEB-INF/lib/
cd server/solr-webapp/webapp/WEB-INF/lib/
curl -O http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar

restart solr ./bin/solr restart -force
in admin interface import data

chinese word segmentation

https://search.maven.org/search?q=g:com.github.magese

1
2
curl -O https://search.maven.org/remotecontent?filepath=com/github/magese/ik-analyzer/7.5.0/ik-analyzer-7.5.0.jar
cd /usr/solr-7.5.0/server/solr/demo/conf

modify managed-schema again

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
+++++++++++++
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>

<field name="Name" type="text_ik" indexed="true" stored="true" multiValued="true" />
+++++++++++++

-------------
<field name="Name" type="string" indexed="true" stored="true"/>
-------------

restart solr , then name field can be splited.

Delete data

delete in interface

In admin interface core’s document, use xml document type

1
2
3
<delete><query>id:1</query></delete>
<delete><query>*:*</query></delete>
<commit/>

delete in get

http://localhost:8080/solr/update/?stream.body=1&stream.contentType=text/xml;charset=utf-8&commit=true
http://localhost:8080/solr/update/?stream.body=Name:Turbo&stream.contentType=text/xml;charset=utf-8&commit=true

delete in post

1
2
curl  http://localhost:8080/update --data-binary  "<delete><query>title:abc</query></delete>"  -H 'Content- type :text/xml; charset=utf-8'
curl http://localhost:8080/update --data-binary "<commit/>" -H 'Content- type:text/xml; charset=utf-8'

Operation in java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class SolrUtils {

private String serverUrl = "http://localhost:8983/solr/articles";

public void Add(DemoDTO dto) throws IOException, SolrServerException {
HttpSolrClient client = new HttpSolrClient(serverUrl);
SolrInputDocument document = new SolrInputDocument();
client.addBean(dto);
client.commit();
}

public List<DemoDTO> search(String keywords, Integer page, Integer rows) throws SolrServerException, IOException {
HttpSolrClient client = new HttpSolrClient(serverUrl);
SolrQuery solrQuery = new SolrQuery();
// key words
solrQuery.set("q", "title:" + keywords);
// setting page start from 0, rows is page's size
solrQuery.setStart((Math.max(page, 1) - 1) * rows);
solrQuery.setRows(rows);

QueryResponse queryResponse = client.query(solrQuery);
SolrDocumentList results = queryResponse.getResults();
long numFound = results.getNumFound();

List<DemoDTO> dataDTOs = new ArrayList<DemoDTO>();
for (SolrDocument solrDocument : results) {
DemoDTO dto = new DemoDTO();
dto.setName(solrDocument.get("Name").toString());
dto.setPrice(Double.valueOf(solrDocument.get("Price").toString()));
dto.setAge(Integer.valueOf(solrDocument.get("Age").toString()));
dataDTOs.add(dto);
}
// List<DemoDTO> dataDTOs=queryResponse.getBeans(DemoDTO.class);
// System.out.println("sum:" + numFound);
return dataDTOs;
}

public void del() throws SolrServerException, IOException {
HttpSolrClient client = new HttpSolrClient(serverUrl);
List<String> names = new ArrayList<String>();
ids.add("david");
ids.add("pam");
ids.add("margot");
client.deleteById(ids);
client.commit();
}
}

https://blog.csdn.net/bljbljbljbljblj/article/details/83023125
https://blog.csdn.net/yuanlaijike/article/details/79886025
https://blog.csdn.net/m0_37595732/article/details/72830122
https://blog.csdn.net/long530439142/article/details/79353845
http://lucene.apache.org/solr/guide/7_5/query-screen.html
http://lucene.apache.org/solr/guide/7_5/the-standard-query-parser.html#the-standard-query-parser