如果我們有一個基於JavaScript的應用需要使用solr,此時再使用xml的格式可能就是一個很好的選擇,因為它太大而且還需要轉換。因此可以采用json,進行相應的處理。
下面就是具體的實現方式:
schema.xml的部分配置如下:
<field name="id" type="string" stored="true" indexed="true"/> <field name="name" type="string" stored="true" indexed="true" omitNorms="false"/> <field name="author" type="string" stored="true" indexed="true" multiValued="true"/>
下面是需要准備進行索引的json數據格式,並且需要對某些書籍的書名標識一下它們的重要程度高於其它書籍:books.json
{ "add": { "overwrite": true, "doc": { "id": 1, "name": "Some book", "author": ["John", "Marry"] } } , "add": { "overwrite": true, "boost": 2.5, "doc": { "id": 2, "name": "Important Book", "author": ["Harry", "Jane"] } } , "add": { "overwrite": true, "doc": { "id": 3, "name": "Some other book", "author": "Marry" } } }
為了能夠將上面的json文檔提交給solr,還需要在solrconfig.xml文件中添加以下配置,為json格式的文檔處理添加一個請求處理方式:
<requestHandler name="/update/json" class="solr.JsonUpdateRequestHandler" />
以上的配置完畢之后,我們可以采用以下的方式開始向solr提交我們剛才的json文檔了
curl http://localhost:8983/solr/update/json --data-binary @books.json -H 'Content-type:text/json; charset=utf-8'