Solr添加文档到索引

2019-03-27 01:08|来源: 网路

solr添加文档非常方便,不用像Lucene那样一个一个添加Field,省去了很多的麻烦下面看操作

方法一:

 1     private static String URI = "http://localhost:8080/solr/";
 2 
 3     private CommonsHttpSolrServer httpSolrServer = null;
 4 
 5     @Before
 6     public void init() {
 7         try {
 8             httpSolrServer = new CommonsHttpSolrServer(URI);
 9         } catch (MalformedURLException e) {
10             e.printStackTrace();
11         }
12     }
13 
14     @Test
15     public void test1() {
16         try {
17             SolrInputDocument document = new SolrInputDocument();
18             document.addField("id", "1");
19             document.addField("news_title", "这是我的第一个solr程序");
20             document.addField("news_content", "希望能够运行起来");
21             httpSolrServer.add(document);
22             httpSolrServer.commit();
23         } catch (SolrServerException e) {
24             e.printStackTrace();
25         } catch (IOException e) {
26             e.printStackTrace();
27         }
28     }

Note:id为唯一,不可重复,如果重复,solr会自动将索引中id相同的元素更新为现在的属性
域的名称可以在schema.xml的Field中设置,默认的有很多Field,我们也可以使用默认的

   <field name="news_title" type="textComplex" indexed="true" stored="true" />
   <field name="news_content" type="textComplex" indexed="true" sotred="true" />

 

方法2:直接添加对象

2.1 定义对象

 1 package com.solr.entity;
 2 
 3 import org.apache.solr.client.solrj.beans.Field;
 4 
 5 public class News {
 6     private String id;
 7     private String title;
 8     private String content;
 9 
10     public News(){}
11     
12     public News(String id, String title, String content) {
13         this.id = id;
14         this.title = title;
15         this.content = content;
16     }
17 
18     public String getId() {
19         return id;
20     }
21 
22     @Field
23     public void setId(String id) {
24         this.id = id;
25     }
26 
27     public String getTitle() {
28         return title;
29     }
30 
31     @Field("news_title")
32     public void setTitle(String title) {
33         this.title = title;
34     }
35 
36     public String getContent() {
37         return content;
38     }
39 
40     @Field("news_content")
41     public void setContent(String content) {
42         this.content = content;
43     }
44 
45 }

2.2 添加对象到索引

 1     @Test
 2     public void test2() {
 3         try {
 4             List<News> list = new ArrayList<News>();
 5             News news1 = new News("2", "title2", "content2");
 6             list.add(news1);
 7             News news2 = new News("3", "title3", "content3");
 8             list.add(news2);
 9 
10             httpSolrServer.addBeans(list);
11             httpSolrServer.commit();
12         } catch (SolrServerException e) {
13             e.printStackTrace();
14         } catch (IOException e) {
15             e.printStackTrace();
16         }
17     }

Note:如果对象的某个域里面的属性为数组,我们需要在schema.xml的Field中设置 multiValued="true"


转自:http://www.cnblogs.com/Laupaul/archive/2012/04/23/2467169

相关问答

更多
  • @Alec您的理解是正确的。 你无法取回原始文件。 因此,您可以选择单独存储原始文档,在主数据存储中生成唯一ID,并将该唯一ID链接到文档的SOLR导出,以便链接回搜索结果。 事实上,SOLR是为了提高搜索速度而设计的,并不像RDBMS那样具有事务友好性。 所以在我的项目中,我使用这种策略来维护备用数据存储区作为所有应用程序数据(不仅仅是文档)的权威来源。 为了给出一些文档处理的内部结构,我建议你看看Solr Wiki上的例子https://wiki.apache.org/solr/ExtractingRe ...
  • 您正在看到此行为,因为您要做的第一件事就是清除索引。 solr.Delete(SolrQuery.All) 这将从索引中删除所有文档。 因此,一旦重建索引开始,索引将为空。 现在,在后续代码中,您将批量添加回索引。 但是,在发出提交之前,查询索引的用户将看不到您添加到索引的任何新文档。 由于您正在批量添加文档和发布提交,因此解释了在重建时文档数量增加的原因以及为什么所有文档都不可见。 在最后一次提交发布之前,索引中的计数和总文档数不会是7500。 可能有几个选项可以帮助您缓解这种情况。 使用commitW ...
  • Solr是否支持部分文档索引更新? 不。 查看常见问题解答 。 有什么可以做的吗? 是的,IIRC在JIRA项目中存在一个问题。 查一查,询问缺少什么,为实施它做出贡献。 Does Solr support partial document index updates? Nope. Check out the FAQ. Is there anything that can be done? Yes, IIRC there was an issue in the project JIRA about it. ...
  • 你描述的大部分索引部分还挺好的,至少在高层次上。 原因是,为什么你要恢复所有文档 - 这是因为你的字段是存储在你的Solr模式中的字段(至少默认为true) 这意味着,除了word1 - > doc1,doc3 word2 - > doc2,doc3等的发布列表外 Solr / Lucene还存储该字段的原始内容,因此它将能够将其返回给您。 你可以通过在你的模式中stored=false来明确地关闭它,或者通过在fl部分中过滤出来,并且只需要fl=id (或类似的东西) 如果您只想返回文档的一部分,搜索到的 ...
  • 尝试Solr Schema API : curl http://localhost:8983/solr/yourcollection/schema -X POST -H 'Content-type:application/json' --data-binary '{ "add-field" : { "name":"myfield", "type":"string", "stored":true } }' 只需使用reindex操作即可为AFAIK填写现有文档的此字段 ...
  • 你在找这样的东西吗? public void insertOneDoc() throws SolrServerException, IOException { HttpSolrServer solr = new HttpSolrServer(solrIndexPath); SolrInputDocument doc = new SolrInputDocument(); doc.addField("fieldName", "fieldValue"); solr.add(doc); solr ...
  • 如果要对delta查询使用时间字段(例如“lastModified”),请确保在创建新行时也设置该字段。 例如,如果您的表有4列id,name,updatedOn,addedOn,并且您在data-config.xml文件中使用updatedOn来标识已更改的行,那么请确保您的updatedOn对于新行不为null。 If you are using a temporal field (such as 'lastModified') for the delta query, make sure you al ...