String url = "http://localhost:8989/solr"; HttpSolrServer server = new HttpSolrServer(url); SolrInputDocume doc1 = new SolrInputDocument(); server.add(docs);
提示401错误,添加用户名和密码:
1 2 3 4 5
String url = "http://localhost:8989/solr"; HttpSolrServer server = new HttpSolrServer(url); HttpClientUtil.setBasicAuth((DefaultHttpClient) server.getHttpClient(), "index", "update"); SolrInputDocume doc1 = new SolrInputDocument(); server.add(docs);
提示 NonRepeatableRequestException, Cannot retry request with a non-repeatable request entity. 想跟踪过去,看看错误出自哪里,没办法调到源代码,于是尝试查询. 测试查询文档 将etc/webdefault.xml中对/update/的限制改成,/select/*,编写查询代码,
1 2 3 4 5
String url = "http://localhost:8989/solr"; HttpSolrServer server = new HttpSolrServer(url); SolrQuery query = new SolrQuery(); String q = "*:*"; query.setQuery(q);
提示401错误,添加用户名和密码:
1 2 3 4 5
String url = "http://localhost:8989/solr"; HttpSolrServer server = new HttpSolrServer(url); SolrQuery query = new SolrQuery(); String q = "*:*"; query.setQuery(q);
查询成功,
*问题解决 不明白原因,只是猜测post的信息不能反复使用,在setBasicAuth前面有一段说明, “Currently this is not preemtive authentication. So it is not currently possible to do a post request while using this setting.”,意思就是认证过程不是最先进行的,所以现在不能用于post,可是认证过程可以用于get,于是察看get的执行过程,发现它先执行一次,发现要认证,于是再执行一次,而第二次执行时会先执行认证过程. 对于post过程,如果 可以执行同样的过程,那就可以达到目的,关键问题是”Cannot retry request with a non-repeatable request entity”,于是查看solr-4470是如何实现的,看到HttpSolrServer里代码如下:
import java.io.File; import java.io.IOException; publicclassIndex{ publicstaticvoidmain(String[] args)throws IOException { //Codec codec = new SimpleTextCodec(); Codec codec = new HexinCodec(); //Codec codec = new Lucene46Codec(); String INDEX_DIR = "e:\\index"; Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_48); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48, analyzer); iwc.setCodec(codec); IndexWriter writer = null; iwc.setOpenMode(OpenMode.CREATE); iwc.setUseCompoundFile(false); try { writer = new IndexWriter(FSDirectory.open(new File(INDEX_DIR)), iwc); Document doc = new Document(); doc.add(new TextField("title", "who are you, you are a man", Field.Store.YES)); doc.add(new TextField("content", "A long way to go there. Please drive a car", Field.Store.NO)); writer.addDocument(doc); doc = new Document(); doc.add(new TextField("title", "are you sure", Field.Store.YES)); doc.add(new TextField("content", "He is a good man. He is a driver", Field.Store.NO)); writer.addDocument(doc); writer.commit(); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
String index = "e:\\index"; IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); String queryString = "driver"; Query query = new TermQuery(new Term("content", queryString)); System.out.println("Searching for: " + query.toString()); Date start = new Date(); TopDocs results = searcher.search(query, null, 100); Date end = new Date(); System.out.println("Time: "+(end.getTime()-start.getTime())+"ms"); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; System.out.println(numTotalHits + " total matching documents"); for (int i = 0; i < hits.length; i++) { String output = ""; Document doc = searcher.doc(hits[i].doc); output += "doc="+hits[i].doc+" score="+hits[i].score; String title = doc.get("title"); if (title != null) { output += " " + title; } System.out.println(output); } reader.close(); } }
在Eclipse中运行Index.java,此时会报错 A SPI class of type org.apache.lucene.codecs.Codec with name ‘Lucene46’ does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath.The current classpath supports the following names:[]
#$interface = "1.0" Sub main ' turn on synchronous mode so we don't miss any data crt.Screen.Synchronous = True crt.Screen.Send "ssh test@192.168.1.1" & VbCr ' Wait for a tring that looks like "password: " or "Password: " crt.Screen.WaitForString "assword:" ' Send your password followed by a carriage return crt.Screen.Send "test" & VbCr ' turn off synchronous mode to restore normal input processing crt.Screen.Synchronous = False EndSub
1.反向索引 2.检索词和布尔查询: 并查询: +new +house 或者 new AND house 或查询: new house 或者 new OR house 排除查询: new house –rental 或者 new house NOT rental 短语查询: “new home” OR “new house” 3 bedrooms” AND “walk in closet” AND “granite countertops” 分组查询: New AND (house OR (home NOT improvement NOT depot NOT grown)) (+(buying purchasing -renting) +(home house residence –(+property -bedroom)))
范围查询: yearsOld:[18 TO 21] 18 <= x <= 21 yearsOld:{18 TO 21} 18 < x < 21 yearsOld:[18 TO 21} 18 <= x < 21 created:[2012-02-01T00:00.0Z TO 2012-08-02T00:00.0Z]