Nutch1.2外掛程式實現or查詢

來源:互聯網
上載者:User

Nutch的搜尋前台的預設搜尋方式是and,也就是所有關鍵詞都必須出現。現在想實現or查詢發現nutch本身並不支援,在去官方論壇搜尋無果,改原始碼無果的情況下想到用外掛程式實現or查詢,於是參照query-base外掛程式改寫了個query-or外掛程式,源碼如下:

public class OrQueryFilter implements QueryFilter {<br />private Configuration conf;<br />float myBoost = 0f;<br />private String[] FIELDS = { "url", "anchor", "content", "title", "host" };<br />private static final int URL_BOOST = 0;<br />private static final int ANCHOR_BOOST = 1;<br />private static final int CONTENT_BOOST = 2;<br />private static final int TITLE_BOOST = 3;<br />private static final int HOST_BOOST = 4;</p><p>private static int SLOP = Integer.MAX_VALUE;</p><p>private float PHRASE_BOOST;</p><p>private float[] FIELD_BOOSTS = new float[5];</p><p>/**<br /> * Set the boost factor for url matches, relative to content and anchor<br /> * matches<br /> */<br />public void setUrlBoost(float boost) {<br />FIELD_BOOSTS[URL_BOOST] = boost;<br />}</p><p>/**<br /> * Set the boost factor for title/anchor matches, relative to url and<br /> * content matches.<br /> */<br />public void setAnchorBoost(float boost) {<br />FIELD_BOOSTS[ANCHOR_BOOST] = boost;<br />}</p><p>/**<br /> * Set the boost factor for sloppy phrase matches relative to unordered term<br /> * matches.<br /> */<br />public void setPhraseBoost(float boost) {<br />PHRASE_BOOST = boost;<br />}</p><p>public void setConf(Configuration conf) {<br />this.conf = conf;<br />}</p><p>public Configuration getConf() {<br />return this.conf;<br />}</p><p>@Override<br />public BooleanQuery filter(Query input, BooleanQuery output)<br />throws QueryException {</p><p>for (Clause c : input.getClauses()) {</p><p>if (!c.getField().equals("or"))<br />continue;</p><p>String value = c.getTerm().toString();<br />BooleanQuery bq = new BooleanQuery();</p><p>for (int f = 0; f < FIELDS.length; f++) {<br />Clause o = c;<br />if (o.isPhrase()) {<br />String[] opt = new CommonGrams(getConf())<br />.optimizePhrase(o.getPhrase(), FIELDS[f]);<br />if (opt.length == 1) {<br />o = new Clause(new Term(opt[0]), o.isRequired(),<br />o.isProhibited(), getConf());<br />} else {<br />o = new Clause(new Phrase(opt), o.isRequired(),<br />o.isProhibited(), getConf());<br />}<br />}<br />bq.add(o.isPhrase() ? exactPhrase(o.getPhrase(), FIELDS[f],<br />FIELD_BOOSTS[f]) : termQuery(FIELDS[f],<br />o.getTerm(), FIELD_BOOSTS[f]),<br />BooleanClause.Occur.SHOULD);</p><p>}<br />bq.setBoost(myBoost);<br />output.add(bq, BooleanClause.Occur.SHOULD);<br />}</p><p>return output;<br />}</p><p>private org.apache.lucene.search.Query exactPhrase(Phrase nutchPhrase,<br />String field, float boost) {<br />Term[] terms = nutchPhrase.getTerms();<br />PhraseQuery exactPhrase = new PhraseQuery();<br />for (int i = 0; i < terms.length; i++) {<br />exactPhrase.add(luceneTerm(field, terms[i]));<br />}<br />exactPhrase.setBoost(boost);<br />return exactPhrase;<br />}</p><p>private org.apache.lucene.search.Query termQuery(String field, Term term,<br />float boost) {<br />TermQuery result = new TermQuery(luceneTerm(field, term));<br />result.setBoost(boost);<br />return result;<br />}</p><p>/** Utility to construct a Lucene Term given a Nutch query term and field. */<br />private static org.apache.lucene.index.Term luceneTerm(String field,<br />Term term) {<br />return new org.apache.lucene.index.Term(field, term.toString());<br />}<br />}<br />

使用方式:查詢是輸入 or:關鍵詞1or:關鍵詞2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.