Home Solr5.x &&SolrCloud
Post
Cancel

Solr5.x &&SolrCloud

Solr5 相比Solr4或更低版本,感觉最大的Future就是不依赖于其他Serlet容器了。不再需要部署war到Jetty或者Tomcat了。酸爽!

单独的服务进程。算是开包装即用,离ES又近了一步!

通过zkcli.sh更新配置

Lucene5.x-ReleaseNote

Solr5.0-ReleaseNote

Collections API

删除一个collection,collection名字+action=DELETE

1
http://host:port/solr/admin/collections?action=DELETE&name=game

reload

1
http://localhost:8983/solr/admin/collections?action=RELOAD&name=game

CLUSTERSTATUS

1
http://172.16.82.188:8983/solr/admin/collections?action=CLUSTERSTATUS

用collection用zkCli.sh比较方便,其中/data/solr/game 就是conf文件路径,包括solr.xml,solrconfig.xml,schema.xml等

1
./bin/solr create_collection -c game  -d /data/solr/game -shards 2 -replicationFactor 1

启动solrcloud,只需每台执行

1
./bin/solr start -c -m 2g -z zkhost:zkport

还有个问题是如果连接的zk串使用chroot,需要bootstrap

1
2
./server/scripts/cloud-scripts/zkcli.sh -zkhost 127.0.0.1:2181 \
   -cmd bootstrap -solrhome /server/solr

创建core

1
./bin/solr create_collection -c appcloud  -d /data/solr/theme-cloud   -shards 3  -replicationFactor 2

创建到指定节点

1
http://host1:port/solr/admin/collections?action=CREATE&name=music&collection.configName=music&numShards=1&replicationFactor=2&maxShardsPerNode=1&createNodeSet=host2:8080_solr,host3:8080_solr

CLUSTERSTATUS 请求URL

1
http://host:port/solr/admin/collections?action=CLUSTERSTATUS

返回值,这里还是看appcloud的

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<response>
    <lst name="responseHeader">
        <int name="status">0</int>
        <int name="QTime">45</int>
    </lst>
    <lst name="cluster">
        <lst name="collections">
            <lst name="app_package">...</lst>
            <lst name="appcloud">
                <lst name="shards">
                    <lst name="shard1">
                        <str name="range">80000000-d554ffff</str>
                        <str name="state">active</str>
                        <lst name="replicas">
                            <lst name="core_node3">
                                <str name="state">active</str>
                                <str name="core">appcloud_shard1_replica2</str>
                                <str name="node_name">172.16.82.186:8983_solr</str>
                                <str name="base_url">http://172.16.82.186:8983/solr</str>
                            </lst>
                            <lst name="core_node6">
                                <str name="state">active</str>
                                <str name="core">appcloud_shard1_replica1</str>
                                <str name="node_name">172.16.82.186:9000_solr</str>
                                <str name="base_url">http://172.16.82.186:9000/solr</str>
                                <str name="leader">true</str>
                            </lst>
                        </lst>
                    </lst>
                    <lst name="shard2">
                        <str name="range">d5550000-2aa9ffff</str>
                        <str name="state">active</str>
                        <lst name="replicas">
                            <lst name="core_node4">
                                <str name="state">active</str>
                                <str name="core">appcloud_shard2_replica2</str>
                                <str name="node_name">172.16.82.186:9000_solr</str>
                                <str name="base_url">http://172.16.82.186:9000/solr</str>
                            </lst>
                            <lst name="core_node5">
                                <str name="state">active</str>
                                <str name="core">appcloud_shard2_replica1</str>
                                <str name="node_name">172.16.82.188:8983_solr</str>
                                <str name="base_url">http://172.16.82.188:8983/solr</str>
                                <str name="leader">true</str>
                            </lst>
                        </lst>
                    </lst>
                    <lst name="shard3">
                        <str name="range">2aaa0000-7fffffff</str>
                        <str name="state">active</str>
                        <lst name="replicas">
                            <lst name="core_node1">
                                <str name="state">active</str>
                                <str name="core">appcloud_shard3_replica1</str>
                                <str name="node_name">172.16.82.186:8983_solr</str>
                                <str name="base_url">http://172.16.82.186:8983/solr</str>
                            </lst>
                            <lst name="core_node2">
                                <str name="state">active</str>
                                <str name="core">appcloud_shard3_replica2</str>
                                <str name="node_name">172.16.82.188:8983_solr</str>
                                <str name="base_url">http://172.16.82.188:8983/solr</str>
                                <str name="leader">true</str>
                            </lst>
                        </lst>
                    </lst>
                </lst>
                <str name="maxShardsPerNode">2</str>
                <lst name="router">
                    <str name="name">compositeId</str>
                </lst>
                <str name="replicationFactor">2</str>
                <str name="autoAddReplicas">false</str>
                <str name="configName">appcloud</str>
            </lst>
            <lst name="game1">...</lst>
            <lst name="theme101">...</lst>
            <lst name="theme">...</lst>
        </lst>
        <arr name="live_nodes">
            <str>172.16.82.188:8983_solr</str>
            <str>172.16.82.186:9000_solr</str>
            <str>172.16.82.186:8983_solr</str>
        </arr>
    </lst>
</response>

boost

How_can_I_boost_the_score_of_newer_documents how-to-boost-fields-in-solr

1

FunctionQuery

product 乘法

ClientUtils.escapeQueryChars与QueryParser.escape()的异同

很明显,QueryParser.escape()不对空格做处理,可以将空格转义成其他符号如”\“,如可以将”Better Than Revenge”转换成”Better\Than\ Revenge”

ClientUtils.escapeQueryChars

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static String escapeQueryChars(String s) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      // These characters are part of the query syntax and must be escaped
      if (c == '\\' || c == '+' || c == '-' || c == '!'  || c == '(' || c == ')' || c == ':'
        || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
        || c == '*' || c == '?' || c == '|' || c == '&'  || c == ';' || c == '/'
        || Character.isWhitespace(c)) {
        sb.append('\\');
      }
      sb.append(c);
    }
    return sb.toString();
  }

QueryParser.escape()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 public static String escape(String s) {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < s.length(); i++) {
     char c = s.charAt(i);
     // These characters are part of the query syntax and must be escaped
     if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'
       || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
       || c == '*' || c == '?' || c == '|' || c == '&' || c == '/') {
       sb.append('\\');
     }
     sb.append(c);
   }
   return sb.toString();
 }

扩展词库更新

1
2
$ ls solr-5.2.1/server/solr-webapp/webapp/WEB-INF/classes/
ext.dic  IKAnalyzer.cfg.xml  stopword.dic

前缀搜索中的坑,形如songname:(Better Than Reveng*),歌名为Better Than Revenge,搜索词中间包含空格(对query进行trim()比不可少)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<field name="suggest_field" type="suggest_type" indexed="true" stored="false"/>
<copyField source="name" dest="suggest_field"/>
<fieldType name="suggest_type" class="solr.TextField"
	positionIncrementGap="100">
	<analyzer type="index">
		<charFilter class="solr.PatternReplaceCharFilterFactory"
			pattern="\s*" replacement="" />
		<filter class="solr.LowerCaseFilterFactory" />
		<filter class="solr.TrimFilterFactory" />
		<tokenizer class="solr.KeywordTokenizerFactory" />
	</analyzer>
	<analyzer type="query">
		<charFilter class="solr.PatternReplaceCharFilterFactory"
			pattern="\s*" replacement="" />
		<filter class="solr.LowerCaseFilterFactory" />
		<filter class="solr.TrimFilterFactory" />
		<tokenizer class="solr.KeywordTokenizerFactory" />
	</analyzer>
</fieldType>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<fieldType name="suggest_string" class="solr.TextField"
	positionIncrementGap="100">
	<analyzer type="index">
		<tokenizer class="solr.NGramTokenizerFactory" minGramSize="2"
			maxGramSize="250" />
		<filter class="solr.LowerCaseFilterFactory" />
		<filter class="solr.TrimFilterFactory" />
		<filter class="solr.PatternReplaceFilterFactory" pattern="([/\s+])"
			replacement="-" replace="all" />
	</analyzer>
	<analyzer type="query">
		<tokenizer class="solr.EdgeNGramTokenizerFactory"
			minGramSize="2" maxGramSize="250" />
		<filter class="solr.LowerCaseFilterFactory" />
		<filter class="solr.TrimFilterFactory" />
		<filter class="solr.PatternReplaceFilterFactory" pattern="([/\s+])"
			replacement="-" replace="all" />
	</analyzer>
</fieldType>

参考链接Solr wildcard query with whitespace

This post is licensed under CC BY 4.0 by the author.