如何开始在Java中使用Apache solr并运行简单的查询而不会遇到错误?(How to start using Apache solr with Java and run simple quer

编程入门 行业动态 更新时间:2024-10-28 15:33:00
如何开始在Java中使用Apache solr并运行简单的查询而不会遇到错误?(How to start using Apache solr with Java and run simple queries without running into errors?)

我一直在尝试在Windows中运行并尝试Apache Solr(连接和运行Java查询)两天,并且花了大约15个不同的计算器搜索和5个长教程来克服我面临的所有错误和问题。 这个问题的目的是为那些遇到同样问题的人提供自我问答。 我使用的是:Windows 7,Apache solr 5.5.0,Maven和Eclipse。 注意:我知道我没有完全覆盖所有内容,但我希望我能帮助人们克服一些问题!

将会(希望)涵盖的问题包括:

我在哪里下载solr? (SO独立) 我如何从Windows控制台启动solr? 我如何创建一个集合(从Windows控制台)? 我如何从Java创建连接? (SO独立) 如何将文档添加到我的收藏中? (SO独立) 如何从我的收藏中删除文档? (SO独立) 我如何查询我的文档? (SO独立) 我可以从浏览器访问我的收藏集吗?

I have been trying to run and try Apache solr (connecting and running Java queries) in Windows for two days, and it took me about 15 different stackoverflow searches and 5 long tutorials to overcome all the errors and questions that I was facing. This question is intended as a self-Q&A for those experiencing the same problems. I'm using: Windows 7, Apache solr 5.5.0, Maven and Eclipse. Note: I know I'm not covering absolutely everything, but I hope I can help people overcome some of their problems!

Questions that will be (hopefully) covered:

Where do I download solr? (SO independent) How do I start solr (from the windows console)? How do I create a collection (from the windows console)? How do I create a connection from Java? (SO independent) How do I add a document to my collection? (SO independent) How do I delete a document from my collection? (SO independent) How do I query my documents? (SO independent) Can I access my collection from the browser and how?

最满意答案

我在哪里下载solr? (SO独立)

你可以从这里得到solr 5.5.0。

我如何从Windows控制台启动solr?

$ solr-5.5.0/bin/solr.cmd start

我如何创建一个集合(从Windows控制台)?

$ solr-5.5.0/bin/solr create -c gettingstarted

“起步”是你收藏的名字。

我如何从Java创建连接? (SO独立)

这部分有点棘手,因为它已经改变了不同的solr版本。 现在,你可以这样做:

String urlString = "http://localhost:8983/solr/gettingstarted"; SolrClient solr = new HttpSolrClient(urlString);

注意这个url不包含“#/〜cores”。 它会给你连接错误,如果你像这样复制完整的网址。

如何将文档添加到我的收藏中? (SO独立)

// Adds a document containing two fields: id and first_name, with // values "123" and "randombee" respectively SolrInputDocument document = new SolrInputDocument(); document.addField("id", "123"); document.addField("first_name", "randombee"); solr.add(document); solr.commit(); // you MUST do this to commit the changes to your collection

如何从我的收藏中删除文档? (SO独立)

有两种不同的方法可以做到这一点:按ID删除文档(如果您已经知道该字段)或通过查询删除文档。

// Deletes document with id="mymockid" solr.deleteById("mymockid"); solr.commit(); // Deletes all documents with first_name:randombee solr.deleteByQuery("first_name:randombee"); solr.commit();

我如何查询我的文档? (SO独立)

关于如何做到这一点的两个例子。 如果要查询集合中的所有文档,可以在查询中使用*:*来实现它:

// Queries all documents (*:*) and shows only the id SolrQuery query = new SolrQuery(); query.setQuery("*:*"); // actual query QueryResponse response = null; try { response = solr.query(query); } catch (SolrServerException e) {/* */ } SolrDocumentList list = response.getResults(); System.out.println(list.toString()); // print the results of the query

查询特定数据:

// Queries documents with first_name=randombee or id=SP2514N // and shows fields id and name query = new SolrQuery(); query.setQuery("first_name:randombee OR id:SP2514N"); query.setFields("id", "name"); // set fields you want to show response = null; try { response = solr.query(query); } catch (SolrServerException e) {/* */ } list = response.getResults(); System.out.println(list.toString());

这给了我这个结果:

{numFound = 1,start = 0,docs = [SolrDocument {id = 123,first_name = randombee}]}

我可以从浏览器访问我的收藏吗?

假设solr正在本地主机和端口8983中运行,访问http:// localhost:8983 / solr / mycollection / browse将显示集合“mycollection”中的所有文档。 请注意,该路径不是 http:// localhost:8983 / solr /#/〜cores / gettingstarted ,即使这是访问所有内核时显示的路径。 您必须删除#/〜核心部分并添加/浏览以查看您的集合。

Where do I download solr? (SO independent)

You can get solr 5.5.0 from here.

How do I start solr (from the windows console)?

$ solr-5.5.0/bin/solr.cmd start

How do I create a collection (from the windows console)?

$ solr-5.5.0/bin/solr create -c gettingstarted

where "gettingstarted" is the name of your collection.

How do I create a connection from Java? (SO independent)

This part is a bit tricky, since it has changed over different solr releases. Right now, you can do it like this:

String urlString = "http://localhost:8983/solr/gettingstarted"; SolrClient solr = new HttpSolrClient(urlString);

Notice that the url doesn't contain "#/~cores". It will give you connection errors if you copy the full url like that.

How do I add a document to my collection? (SO independent)

// Adds a document containing two fields: id and first_name, with // values "123" and "randombee" respectively SolrInputDocument document = new SolrInputDocument(); document.addField("id", "123"); document.addField("first_name", "randombee"); solr.add(document); solr.commit(); // you MUST do this to commit the changes to your collection

How do I delete a document from my collection? (SO independent)

There are two different ways you can do this: deleting a document by id (if you already know that field) or deleting a document by query.

// Deletes document with id="mymockid" solr.deleteById("mymockid"); solr.commit(); // Deletes all documents with first_name:randombee solr.deleteByQuery("first_name:randombee"); solr.commit();

How do I query my documents? (SO independent)

Two examples on how to do it. If you want to query all the documents in the collection, you can use *:* in your query to achieve it:

// Queries all documents (*:*) and shows only the id SolrQuery query = new SolrQuery(); query.setQuery("*:*"); // actual query QueryResponse response = null; try { response = solr.query(query); } catch (SolrServerException e) {/* */ } SolrDocumentList list = response.getResults(); System.out.println(list.toString()); // print the results of the query

Query for specific data:

// Queries documents with first_name=randombee or id=SP2514N // and shows fields id and name query = new SolrQuery(); query.setQuery("first_name:randombee OR id:SP2514N"); query.setFields("id", "name"); // set fields you want to show response = null; try { response = solr.query(query); } catch (SolrServerException e) {/* */ } list = response.getResults(); System.out.println(list.toString());

which gives me this result:

{numFound=1,start=0,docs=[SolrDocument{id=123, first_name=randombee}]}

Can I access my collection from the browser?

Assuming solr is running in localhost and port 8983, accessing http://localhost:8983/solr/mycollection/browse will show you all the documents in the collection "mycollection". Note that the path is not http://localhost:8983/solr/#/~cores/gettingstarted, even if that is the path showing when you access all the cores. You have to remove the #/~cores part and add the /browse to see your collection.

更多推荐

本文发布于:2023-07-22 19:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   简单   solr   Apache   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!