Solr 7.7.2多表導入

單個question表導入時,表中id字段會指定成field id
data-config.xml:

<dataConfig>
  <dataSource type="JdbcDataSource"
	      driver="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost/wenda"
              user="root"
              password="pwd"/>
  <document>
    <entity name="question" query="select id,title,content from question">
       <field column="content" name="question_content"/>
       <field column="title" name="question_title"/>
    </entity>
  </document>
</dataConfig>

managed-schema:

    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
    <!-- docValues are enabled by default for long type so we don't need to index the version field  -->
    <field name="_version_" type="plong" indexed="false" stored="false"/>
    <field name="_root_" type="string" indexed="true" stored="false" docValues="false" />
    <field name="_text_" type="text_ik" indexed="true" stored="true" multiValued="true"/>
    
    <!--對標題和內容建索引,中文分詞,indexed是否建索引,stored索引數據是否保存,multiValued是否有重複值-->
    <field name="question_title" type="text_ik" indexed="true" stored="true" multiValued="true"/>
    <field name="question_content" type="text_ik" indexed="true" stored="true" multiValued="true"/>

response的highlighting的key即爲問題id,搜索結果裏需要有原id以查詢數據庫中完整記錄,可以直接用highlighting部分的key

我們還需完成評論搜索功能,這涉及到多表id重複問題,爲了保留原id,question id字段和comment id 字段都需要另外指定field,作爲uniquekey的id爲uuid格式
這裏只要定義qid和cid field,不需要對id做其他配置

managed-schema:

    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
    <!-- docValues are enabled by default for long type so we don't need to index the version field  -->
    <field name="_version_" type="plong" indexed="false" stored="false"/>
    <field name="_root_" type="string" indexed="true" stored="false" docValues="false" />
    <field name="_text_" type="text_ik" indexed="true" stored="true" multiValued="true"/>
    
    <!--對標題和內容建索引,中文分詞,indexed是否建索引,stored索引數據是否保存,multiValued是否有重複值-->
    <field name="question_title" type="text_ik" indexed="true" stored="true" multiValued="true"/>
    <field name="question_content" type="text_ik" indexed="true" stored="true" multiValued="true"/>
    <field name="qid" type="string" indexed="false"/>
    <!--評論-->
    <field name="comment_content" type="text_ik" indexed="true" stored="true" multiValued="true"/>
    <field name="cid" type="string" indexed="false"/>
    
    ...
    
    <uniqueKey>id</uniqueKey>

data-config.xml:

<dataConfig>
  <dataSource type="JdbcDataSource"
	      driver="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost/wenda"
              user="root"
              password="pwd"/>
  <document>
    <entity name="question" query="select id,title,content from question">
       <field column="content" name="question_content"/>
       <field column="title" name="question_title"/>
	   <field column="id" name="qid"/>
    </entity>
	<entity name="comment" query="select id,content from comment">
       <field column="content" name="comment_content"/>
	   <field column="id" name="cid"/>
    </entity>
  </document>
</dataConfig>

這樣查詢的時候會有qid和cid的鍵值對
在這裏插入圖片描述
此時highlighting部分的id不爲問題或評論id在這裏插入圖片描述
需要通過SolrDocumentList取得response部分裏的qid和cid,通過document.get(“id”)找到對應高亮字段

    //搜索問題和評論
    public ViewObject search(String keyword, int offset, int count,
                                           String hlPre, String hlPos) throws Exception{
        List<Question> questionList = new ArrayList<>();
        List<Comment> commentList = new ArrayList<>();

        SolrQuery query = new SolrQuery(keyword);
        query.setRows(count);
        query.setStart(offset);
        query.setHighlight(true);
        query.setHighlightSimplePre(hlPre);
        query.setHighlightSimplePost(hlPos);
        query.set("hl.fl", QUESTION_TITLE_FIELD+", "+QUESTION_CONTENT_FIELD+", "+COMMENT_CONTENT_FIELD);
        QueryResponse response = client.query(query);
        
        SolrDocumentList solrDocumentList = response.getResults();
        for (int i = 0; i < solrDocumentList.size(); i++) {
            SolrDocument document = solrDocumentList.get(i);
            Map<String, List<String>> entry = response.getHighlighting().get(document.get("id"));
            //區分問題和評論
            if (document.containsKey(COMMENT_CONTENT_FIELD)) {  //評論
                Comment c = new Comment();
                //評論id
                if (document.containsKey("cid")) {
                    c.setId(Integer.parseInt(document.get("cid").toString()));
                } else {
                    c.setId(Integer.parseInt(document.get("id").toString()));
                }
                if (entry.containsKey(COMMENT_CONTENT_FIELD)) {
                    if (entry.get(COMMENT_CONTENT_FIELD).size() > 0) {
                        c.setContent(entry.get(COMMENT_CONTENT_FIELD).get(0));
                    }
                }
                commentList.add(c);
            } else {  //問題
                Question q = new Question();
                //有些標題包含關鍵字,有些內容包含,需要進行區分
                //問題id
                if (document.containsKey("qid")) {
                    q.setId(Integer.parseInt(document.get("qid").toString()));
                } else {
                    q.setId(Integer.parseInt(document.get("id").toString()));
                }
                if (entry.containsKey(QUESTION_TITLE_FIELD)) {
                    if (entry.get(QUESTION_CONTENT_FIELD).size() > 0) {
                        q.setTitle(entry.get(QUESTION_CONTENT_FIELD).get(0));
                    }
                }
                if (entry.containsKey(QUESTION_CONTENT_FIELD)) {
                    if (entry.get(QUESTION_CONTENT_FIELD).size() > 0) {
                        q.setContent(entry.get(QUESTION_CONTENT_FIELD).get(0));
                    }
                }
                questionList.add(q);
            }
        }

        ViewObject vo = new ViewObject();
        vo.set("question", questionList);
        vo.set("comment", commentList);
        return vo;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章