@Test public void test_twoTerm() throws SyntaxError { QParser parser = new SimilarityParser("first second",new ModifiableSolrParams(),mockCorrectParams,req("first second")); Query luceneQuery = parser.parse(); Assert.assertEquals(BooleanQuery.class, luceneQuery.getClass()); BooleanQuery realQuery = (BooleanQuery)luceneQuery; Assert.assertEquals(2,realQuery.clauses().size()); AugmentedTermQuery expected1 = new AugmentedTermQuery( AugmentedTermQuery.ModelMethod.Generalized, new Term("text", "first"), new TermWeightTuple[]{SimilarityApiMock.similarTerm("text")} ); Assert.assertEquals(expected1,realQuery.clauses().get(0).getQuery()); AugmentedTermQuery expected2 = new AugmentedTermQuery( AugmentedTermQuery.ModelMethod.Generalized, new Term("text", "second"), new TermWeightTuple[]{SimilarityApiMock.similarTerm("text")} ); Assert.assertEquals(expected2,realQuery.clauses().get(1).getQuery()); }
public Query parse() throws SyntaxError { final IndexSchema schema = req.getSchema(); String defaultField = QueryParsing.getDefaultField(schema, getParam(CommonParams.DF)); if(defaultField == null){ defaultField = "*"; } final Analyzer analyzer = schema.getQueryAnalyzer(); TokenStream tokenStream = analyzer.tokenStream(defaultField, qstr); String[] queryTerms = null; try { queryTerms = termsFromTokenStream(tokenStream); } catch (IOException e) { throw new SyntaxError(e); } SimilarTermModel[] similarTerms = null; try{ similarTerms = similarityApi.GetSimilarTerms(defaultField, queryTerms); } catch (IOException e){ if(failOnConnectionError){ throw new RuntimeException(e); } logger.error("Exception while talking to the api @ " + params.get(config_url),e); similarTerms = new SimilarTermModel[queryTerms.length]; for (int i = 0; i < queryTerms.length; i++) { similarTerms[i] = new SimilarTermModel(new Term(defaultField,queryTerms[i]), new TermWeightTuple[0]); } } Query query; if(similarTerms.length == 1) { query = new AugmentedTermQuery(modelMethod, similarTerms[0].queryTerm, similarTerms[0].similarTerms); }else{ BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (SimilarTermModel model : similarTerms) { builder.add(new AugmentedTermQuery(modelMethod, model.queryTerm, model.similarTerms), BooleanClause.Occur.SHOULD); } query = builder.build(); } if(logger.isInfoEnabled()) { logger.info("Created query: " + query.toString()); } return query; }
SimilarityParser extends QParser { public Query parse() throws SyntaxError { final IndexSchema schema = req.getSchema(); String defaultField = QueryParsing.getDefaultField(schema, getParam(CommonParams.DF)); if(defaultField == null){ defaultField = "*"; } final Analyzer analyzer = schema.getQueryAnalyzer(); TokenStream tokenStream = analyzer.tokenStream(defaultField, qstr); String[] queryTerms = null; try { queryTerms = termsFromTokenStream(tokenStream); } catch (IOException e) { throw new SyntaxError(e); } SimilarTermModel[] similarTerms = null; try{ similarTerms = similarityApi.GetSimilarTerms(defaultField, queryTerms); } catch (IOException e){ if(failOnConnectionError){ throw new RuntimeException(e); } logger.error("Exception while talking to the api @ " + params.get(config_url),e); similarTerms = new SimilarTermModel[queryTerms.length]; for (int i = 0; i < queryTerms.length; i++) { similarTerms[i] = new SimilarTermModel(new Term(defaultField,queryTerms[i]), new TermWeightTuple[0]); } } Query query; if(similarTerms.length == 1) { query = new AugmentedTermQuery(modelMethod, similarTerms[0].queryTerm, similarTerms[0].similarTerms); }else{ BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (SimilarTermModel model : similarTerms) { builder.add(new AugmentedTermQuery(modelMethod, model.queryTerm, model.similarTerms), BooleanClause.Occur.SHOULD); } query = builder.build(); } if(logger.isInfoEnabled()) { logger.info("Created query: " + query.toString()); } return query; } }
SimilarityParser extends QParser { public Query parse() throws SyntaxError { final IndexSchema schema = req.getSchema(); String defaultField = QueryParsing.getDefaultField(schema, getParam(CommonParams.DF)); if(defaultField == null){ defaultField = "*"; } final Analyzer analyzer = schema.getQueryAnalyzer(); TokenStream tokenStream = analyzer.tokenStream(defaultField, qstr); String[] queryTerms = null; try { queryTerms = termsFromTokenStream(tokenStream); } catch (IOException e) { throw new SyntaxError(e); } SimilarTermModel[] similarTerms = null; try{ similarTerms = similarityApi.GetSimilarTerms(defaultField, queryTerms); } catch (IOException e){ if(failOnConnectionError){ throw new RuntimeException(e); } logger.error("Exception while talking to the api @ " + params.get(config_url),e); similarTerms = new SimilarTermModel[queryTerms.length]; for (int i = 0; i < queryTerms.length; i++) { similarTerms[i] = new SimilarTermModel(new Term(defaultField,queryTerms[i]), new TermWeightTuple[0]); } } Query query; if(similarTerms.length == 1) { query = new AugmentedTermQuery(modelMethod, similarTerms[0].queryTerm, similarTerms[0].similarTerms); }else{ BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (SimilarTermModel model : similarTerms) { builder.add(new AugmentedTermQuery(modelMethod, model.queryTerm, model.similarTerms), BooleanClause.Occur.SHOULD); } query = builder.build(); } if(logger.isInfoEnabled()) { logger.info("Created query: " + query.toString()); } return query; } SimilarityParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req); }
SimilarityParser extends QParser { public Query parse() throws SyntaxError { final IndexSchema schema = req.getSchema(); String defaultField = QueryParsing.getDefaultField(schema, getParam(CommonParams.DF)); if(defaultField == null){ defaultField = "*"; } final Analyzer analyzer = schema.getQueryAnalyzer(); TokenStream tokenStream = analyzer.tokenStream(defaultField, qstr); String[] queryTerms = null; try { queryTerms = termsFromTokenStream(tokenStream); } catch (IOException e) { throw new SyntaxError(e); } SimilarTermModel[] similarTerms = null; try{ similarTerms = similarityApi.GetSimilarTerms(defaultField, queryTerms); } catch (IOException e){ if(failOnConnectionError){ throw new RuntimeException(e); } logger.error("Exception while talking to the api @ " + params.get(config_url),e); similarTerms = new SimilarTermModel[queryTerms.length]; for (int i = 0; i < queryTerms.length; i++) { similarTerms[i] = new SimilarTermModel(new Term(defaultField,queryTerms[i]), new TermWeightTuple[0]); } } Query query; if(similarTerms.length == 1) { query = new AugmentedTermQuery(modelMethod, similarTerms[0].queryTerm, similarTerms[0].similarTerms); }else{ BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (SimilarTermModel model : similarTerms) { builder.add(new AugmentedTermQuery(modelMethod, model.queryTerm, model.similarTerms), BooleanClause.Occur.SHOULD); } query = builder.build(); } if(logger.isInfoEnabled()) { logger.info("Created query: " + query.toString()); } return query; } SimilarityParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req); Query parse(); }
SimilarityParser extends QParser { public Query parse() throws SyntaxError { final IndexSchema schema = req.getSchema(); String defaultField = QueryParsing.getDefaultField(schema, getParam(CommonParams.DF)); if(defaultField == null){ defaultField = "*"; } final Analyzer analyzer = schema.getQueryAnalyzer(); TokenStream tokenStream = analyzer.tokenStream(defaultField, qstr); String[] queryTerms = null; try { queryTerms = termsFromTokenStream(tokenStream); } catch (IOException e) { throw new SyntaxError(e); } SimilarTermModel[] similarTerms = null; try{ similarTerms = similarityApi.GetSimilarTerms(defaultField, queryTerms); } catch (IOException e){ if(failOnConnectionError){ throw new RuntimeException(e); } logger.error("Exception while talking to the api @ " + params.get(config_url),e); similarTerms = new SimilarTermModel[queryTerms.length]; for (int i = 0; i < queryTerms.length; i++) { similarTerms[i] = new SimilarTermModel(new Term(defaultField,queryTerms[i]), new TermWeightTuple[0]); } } Query query; if(similarTerms.length == 1) { query = new AugmentedTermQuery(modelMethod, similarTerms[0].queryTerm, similarTerms[0].similarTerms); }else{ BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (SimilarTermModel model : similarTerms) { builder.add(new AugmentedTermQuery(modelMethod, model.queryTerm, model.similarTerms), BooleanClause.Occur.SHOULD); } query = builder.build(); } if(logger.isInfoEnabled()) { logger.info("Created query: " + query.toString()); } return query; } SimilarityParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req); Query parse();  }