001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.search.lucene2;
020
021import java.io.IOException;
022
023import lucee.runtime.search.SearchException;
024import lucee.runtime.search.SearchResulItem;
025import lucee.runtime.search.lucene2.highlight.Highlight;
026
027import org.apache.lucene.analysis.Analyzer;
028import org.apache.lucene.document.Document;
029import org.apache.lucene.search.Hits;
030
031public class SearchResulItemHits implements SearchResulItem {
032
033        
034        
035        private Hits hits;
036        private int index;
037        private Object highlighter;
038        private Analyzer analyzer;
039        private String id;
040        private String categoryTree;
041        private String category;
042        private int maxNumFragments;
043        private int maxLength;
044        private Document doc;
045
046        public SearchResulItemHits(Hits hits, int index, Object highlighter,Analyzer analyzer,
047                        String id, String categoryTree, String category,int maxNumFragments, int maxLength) {
048                this.hits=hits;
049                this.index=index;
050                this.highlighter=highlighter;
051                this.analyzer=analyzer;
052                this.id=id;
053                this.categoryTree=categoryTree;
054                this.category=category;
055                this.maxNumFragments=maxNumFragments;
056                this.maxLength=maxLength;
057        }
058
059        @Override
060        public String getAuthor() {
061                return doc("author");
062        }
063
064        @Override
065        public String getCategory() {
066                return category;
067        }
068
069        @Override
070        public String getCategoryTree() {
071                return categoryTree;
072        }
073
074        @Override
075        public String getCustom1() {
076                return doc("custom1");
077        }
078
079        @Override
080        public String getCustom2() {
081                return doc("custom2");
082        }
083
084        @Override
085        public String getCustom3() {
086                return doc("custom3");
087        }
088
089        @Override
090        public String getCustom4() {
091                return doc("custom4");
092        }
093    
094    public String getCustom(int index) throws SearchException {
095        if(index==1) return doc("custom1");
096        if(index==2) return doc("custom2");
097        if(index==3) return doc("custom3");
098        if(index==4) return doc("custom4");
099        
100        throw new SearchException("invalid index ["+index+"], valid index is [1,2,3,4]");
101    }
102
103        @Override
104        public String getId() {
105                return id;
106        }
107
108        @Override
109        public String getKey() {
110                return doc("key");
111        }
112
113        @Override
114        public String getMimeType() {
115                return doc("mime-type");
116        }
117
118        public int getRecordsSearched() {
119                // TODO Auto-generated method stub
120                return 0;
121        }
122    
123
124        @Override
125        public float getScore() {
126                try {
127                        return hits.score(index);
128                } catch (IOException e) {
129                        return 0;
130                }
131        }
132
133        @Override
134        public String getSize() {
135                return doc("size");
136        }
137
138        @Override
139        public String getSummary() {
140                return doc("summary");
141        }
142
143        @Override
144        public String getTitle() {
145                return doc("title");
146        }
147
148        @Override
149        public String getUrl() {
150                return doc("url");
151        }
152        
153        /** FUTURE add to interface
154         * @return the contextSummary
155         */
156        public String getContextSummary() {
157                String contextSummary="";
158                if(maxNumFragments>0){
159                        contextSummary=Highlight.createContextSummary(highlighter,analyzer,doc("contents"),maxNumFragments,maxLength,"");
160                }
161                return contextSummary;
162        }
163
164        private String doc(String field) {
165                if(doc==null){
166                        try {
167                                doc=hits.doc(index);
168                        } catch (IOException e) {
169                                e.printStackTrace();
170                        }
171                }
172                return doc.get(field);
173        }
174}