001    package railo.runtime.search.lucene2;
002    
003    import java.io.IOException;
004    
005    import org.apache.lucene.analysis.Analyzer;
006    import org.apache.lucene.document.Document;
007    import org.apache.lucene.search.Hits;
008    
009    import railo.runtime.search.SearchResultItemPro;
010    import railo.runtime.search.lucene2.highlight.Highlight;
011    
012    public class SearchResulItemHits implements SearchResultItemPro {
013    
014            
015            
016            private Hits hits;
017            private int index;
018            private Object highlighter;
019            private Analyzer analyzer;
020            private String id;
021            private String categoryTree;
022            private String category;
023            private int maxNumFragments;
024            private int maxLength;
025            private Document doc;
026    
027            public SearchResulItemHits(Hits hits, int index, Object highlighter,Analyzer analyzer,
028                            String id, String categoryTree, String category,int maxNumFragments, int maxLength) {
029                    this.hits=hits;
030                    this.index=index;
031                    this.highlighter=highlighter;
032                    this.analyzer=analyzer;
033                    this.id=id;
034                    this.categoryTree=categoryTree;
035                    this.category=category;
036                    this.maxNumFragments=maxNumFragments;
037                    this.maxLength=maxLength;
038            }
039    
040            /**
041             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getAuthor()
042             */
043            public String getAuthor() {
044                    return doc("author");
045            }
046    
047            /**
048             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getCategory()
049             */
050            public String getCategory() {
051                    return category;
052            }
053    
054            /**
055             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getCategoryTree()
056             */
057            public String getCategoryTree() {
058                    return categoryTree;
059            }
060    
061            /**
062             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getCustom1()
063             */
064            public String getCustom1() {
065                    return doc("custom1");
066            }
067    
068            /**
069             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getCustom2()
070             */
071            public String getCustom2() {
072                    return doc("custom2");
073            }
074    
075            /**
076             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getCustom3()
077             */
078            public String getCustom3() {
079                    return doc("custom3");
080            }
081    
082            /**
083             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getCustom4()
084             */
085            public String getCustom4() {
086                    return doc("custom4");
087            }
088    
089            /**
090             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getId()
091             */
092            public String getId() {
093                    return id;
094            }
095    
096            /**
097             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getKey()
098             */
099            public String getKey() {
100                    return doc("key");
101            }
102    
103            /**
104             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getMimeType()
105             */
106            public String getMimeType() {
107                    return doc("mime-type");
108            }
109    
110            public int getRecordsSearched() {
111                    // TODO Auto-generated method stub
112                    return 0;
113            }
114        
115    
116            /**
117             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getScore()
118             */
119            public float getScore() {
120                    try {
121                            return hits.score(index);
122                    } catch (IOException e) {
123                            return 0;
124                    }
125            }
126    
127            /**
128             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getSize()
129             */
130            public String getSize() {
131                    return doc("size");
132            }
133    
134            /**
135             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getSummary()
136             */
137            public String getSummary() {
138                    return doc("summary");
139            }
140    
141            /**
142             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getTitle()
143             */
144            public String getTitle() {
145                    return doc("title");
146            }
147    
148            /**
149             * @see railo.runtime.search.coreDuplicate.SearchResulItem#getUrl()
150             */
151            public String getUrl() {
152                    return doc("url");
153            }
154            
155            /** FUTURE add to interface
156             * @return the contextSummary
157             */
158            public String getContextSummary() {
159                    String contextSummary="";
160                    if(maxNumFragments>0){
161                            contextSummary=Highlight.createContextSummary(highlighter,analyzer,doc("contents"),maxNumFragments,maxLength,"");
162                    }
163                    return contextSummary;
164            }
165    
166            private String doc(String field) {
167                    if(doc==null){
168                            try {
169                                    doc=hits.doc(index);
170                            } catch (IOException e) {
171                                    e.printStackTrace();
172                            }
173                    }
174                    return doc.get(field);
175            }
176    }