Guest User

Untitled

a guest
Oct 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. package org.healthonnet.solr;
  2.  
  3. import java.io.IOException;
  4. import java.io.StringReader;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7.  
  8. import org.apache.lucene.analysis.Analyzer;
  9. import org.apache.lucene.analysis.Token;
  10. import org.apache.lucene.analysis.TokenStream;
  11. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
  12. import org.apache.lucene.analysis.tokenattributes.FlagsAttribute;
  13. import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
  14. import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
  15. import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
  16. import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
  17. import org.apache.lucene.index.IndexReader;
  18. import org.apache.solr.common.SolrException;
  19. import org.apache.solr.common.params.CommonParams;
  20. import org.apache.solr.common.params.ShardParams;
  21. import org.apache.solr.common.params.SolrParams;
  22. import org.apache.solr.common.util.NamedList;
  23. import org.apache.solr.common.util.SimpleOrderedMap;
  24. import org.apache.solr.handler.component.ResponseBuilder;
  25. import org.apache.solr.handler.component.SpellCheckComponent;
  26. import org.apache.solr.spelling.SolrSpellChecker;
  27. import org.apache.solr.spelling.SpellingOptions;
  28. import org.apache.solr.spelling.SpellingResult;
  29.  
  30. /**
  31. * Wrapper around the SpellCheckComponent that gives a more quiet output. It
  32. * only gives collations; does not give any suggestions. This is useful when you
  33. * set spellcheck.count to a large number, but you really only want to check the
  34. * collations. It avoids writing a ton of output and therefore cuts down on I/O.
  35. *
  36. * @author nolan
  37. *
  38. */
  39. public class QuietSpellCheckComponent extends SpellCheckComponent {
  40.  
  41. @Override
  42. @SuppressWarnings("unchecked")
  43. public void process(ResponseBuilder rb) throws IOException {
  44. SolrParams params = rb.req.getParams();
  45. if (!params.getBool(COMPONENT_NAME, false) || spellCheckers.isEmpty()) {
  46. return;
  47. }
  48. boolean shardRequest = "true".equals(params.get(ShardParams.IS_SHARD));
  49. String q = params.get(SPELLCHECK_Q);
  50. SolrSpellChecker spellChecker = getSpellChecker(params);
  51. Collection<Token> tokens = null;
  52. if (q != null) {
  53. // we have a spell check param, tokenize it with the query analyzer
  54. // applicable for this spellchecker
  55. tokens = getTokens(q, spellChecker.getQueryAnalyzer());
  56. } else {
  57. q = rb.getQueryString();
  58. if (q == null) {
  59. q = params.get(CommonParams.Q);
  60. }
  61. tokens = queryConverter.convert(q);
  62. }
  63. if (tokens != null && tokens.isEmpty() == false) {
  64. if (spellChecker != null) {
  65. int count = params.getInt(SPELLCHECK_COUNT, 1);
  66. boolean onlyMorePopular = params.getBool(SPELLCHECK_ONLY_MORE_POPULAR, DEFAULT_ONLY_MORE_POPULAR);
  67. boolean extendedResults = params.getBool(SPELLCHECK_EXTENDED_RESULTS, false);
  68. NamedList response = new SimpleOrderedMap();
  69. IndexReader reader = rb.req.getSearcher().getReader();
  70. boolean collate = params.getBool(SPELLCHECK_COLLATE, false);
  71. float accuracy = params.getFloat(SPELLCHECK_ACCURACY, Float.MIN_VALUE);
  72. SolrParams customParams = getCustomParams(getDictionaryName(params), params, shardRequest);
  73. SpellingOptions options = new SpellingOptions(tokens, reader,
  74. count, onlyMorePopular, extendedResults, accuracy,
  75. customParams);
  76. SpellingResult spellingResult = spellChecker.getSuggestions(options);
  77. if (spellingResult != null) {
  78. NamedList suggestions = toNamedList(shardRequest,
  79. spellingResult, q, extendedResults, collate);
  80. if (collate) {
  81. addCollationsToResponse(params, spellingResult, rb, q, suggestions);
  82. }
  83. // clear the spellingResult after making collations from it.
  84. // This is the only change I'm making compared to
  85. // SpellCheckComponent!
  86. for (int i = suggestions.size() - 1; i >= 0; i--) {
  87. if ("collation".equals(suggestions.getName(i))) {
  88. Object val = suggestions.getVal(i);
  89. if (val instanceof NamedList
  90. && ((NamedList) val).size() > 0
  91. && "collationQuery".equals(((NamedList) val).getName(0))) {
  92. continue;
  93. }
  94. }
  95. // otherwise it's a non-collation; delete it
  96. suggestions.remove(i);
  97. }
  98. response.add("suggestions", suggestions);
  99. rb.rsp.add("spellcheck", response);
  100. }
  101.  
  102. } else {
  103. throw new SolrException(SolrException.ErrorCode.NOT_FOUND,
  104. "Specified dictionary does not exist.");
  105. }
  106. }
  107. }
  108.  
  109. private Collection<Token> getTokens(String q, Analyzer analyzer)
  110. throws IOException {
  111. Collection<Token> result = new ArrayList<Token>();
  112. TokenStream ts = analyzer.reusableTokenStream("", new StringReader(q));
  113. ts.reset();
  114. // TODO: support custom attributes
  115. CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
  116. OffsetAttribute offsetAtt = ts.addAttribute(OffsetAttribute.class);
  117. TypeAttribute typeAtt = ts.addAttribute(TypeAttribute.class);
  118. FlagsAttribute flagsAtt = ts.addAttribute(FlagsAttribute.class);
  119. PayloadAttribute payloadAtt = ts.addAttribute(PayloadAttribute.class);
  120. PositionIncrementAttribute posIncAtt = ts
  121. .addAttribute(PositionIncrementAttribute.class);
  122.  
  123. while (ts.incrementToken()) {
  124. Token token = new Token();
  125. token.copyBuffer(termAtt.buffer(), 0, termAtt.length());
  126. token.setOffset(offsetAtt.startOffset(), offsetAtt.endOffset());
  127. token.setType(typeAtt.type());
  128. token.setFlags(flagsAtt.getFlags());
  129. token.setPayload(payloadAtt.getPayload());
  130. token.setPositionIncrement(posIncAtt.getPositionIncrement());
  131. result.add(token);
  132. }
  133. ts.end();
  134. ts.close();
  135. return result;
  136. }
  137.  
  138. private String getDictionaryName(SolrParams params) {
  139. String dictName = params.get(SPELLCHECK_DICT);
  140. if (dictName == null) {
  141. dictName = SolrSpellChecker.DEFAULT_DICTIONARY_NAME;
  142. }
  143. return dictName;
  144. }
  145.  
  146. }
Add Comment
Please, Sign In to add comment