java 如何从solr中选择使用java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27012774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:04:03 来源:igfitidea点击:
How to select from solr use java?
提问by somebody
I save data in solr:
我在 solr 中保存数据:
String solrUrl = "http://localhost:8984/solr";
SolrServer solrServer = new HttpSolrServer( solrUrl );
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "1");
doc.addField("first_name", "Ann");
doc.addField("last_name", "Smit");
doc.addField("email", "[email protected]");
try {
solrServer.add(doc);
solrServer.commit();
} catch (SolrServerException e) {/* */}
And select data from solr:
并从 solr 中选择数据:
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.addFilterQuery("first_name:Ann*");
query.addFilterQuery("last_name:Ann*");
query.setFields("id","first_name","last_name","email");
QueryResponse response = null;
try {
response = solrServer.query(query);
} catch (SolrServerException e) {/* */ }
SolrDocumentList list = response.getResults();
I have search criteria: first name orlast name should contain value Ann
. How to set FilterQuery
for select saved data?
我有搜索条件:名字或姓氏应包含 value Ann
。如何设置FilterQuery
选择保存的数据?
回答by somebody
Should use:
应该使用:
SolrQuery parameters = new SolrQuery();
query.setQuery("first_name:qwe2 OR last_name:qwe2");
query.setFields("id","first_name","last_name","email");
QueryResponse response = null;
try {
response = solrServer.query(parameters);
} catch (SolrServerException e) {/* */ }
SolrDocumentList list = response.getResults();
回答by Amit Jain
Search using SolrJ
使用 SolrJ 搜索
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import java.net.MalformedURLException;
public class SolrJSearcher {
public static void main(String[] args) throws MalformedURLException, SolrServerException {
HttpSolrServer solr = new HttpSolrServer("url");
SolrQuery query = new SolrQuery();
query.setQuery("sony digital camera");
query.addFilterQuery("cat:electronics","store:amazon.com");
query.setFields("id","price","merchant","cat","store");
query.setStart(0);
query.set("defType", "edismax");
QueryResponse response = solr.query(query);
SolrDocumentList results = response.getResults();
for (int i = 0; i < results.size(); ++i) {
System.out.println(results.get(i));
}
}
}