Syntax Error, cannot parse from lucene StandardQueryparser when using fq(filtered query) - apache

I am trying to pass a java String to Apache StandardQueryparser to get the Querynode.
Input - "fq=section:1"
All I need is section:1 in FILTER clause in QueryNode. This looks pretty straightforward but it throws
INVALID_SYNTAX_CANNOT_PARSE: Syntax Error, cannot parse fq=section:1:

Use a ConstantScoreQuery. It won't affect it score, and is the same was as the fq parameter is implemented in Solr:
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
// SolrRequestInfo reqInfo = SolrRequestInfo.getRequestInfo();
if (!(searcher instanceof SolrIndexSearcher)) {
// delete-by-query won't have SolrIndexSearcher
return new BoostQuery(new ConstantScoreQuery(q), 0).createWeight(searcher, scoreMode, 1f);
}
SolrIndexSearcher solrSearcher = (SolrIndexSearcher)searcher;
DocSet docs = solrSearcher.getDocSet(q);
// reqInfo.addCloseHook(docs); // needed for off-heap refcounting
return new BoostQuery(new SolrConstantScoreQuery(docs.getTopFilter()), 0).createWeight(searcher, scoreMode, 1f);
}

Related

is it possible to get the execute sql that contains parameter when debugging mybatis source

I am setting a breakpoint in mybatis source BaseExecutor's queryFromDatabase function in Intellij Idea, this code block look like this:
private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
List<E> list;
localCache.putObject(key, EXECUTION_PLACEHOLDER);
try {
list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
} finally {
localCache.removeObject(key);
}
localCache.putObject(key, list);
if (ms.getStatementType() == StatementType.CALLABLE) {
localOutputParameterCache.putObject(key, parameter);
}
return list;
}
but the boundSql content shows sql like this:
select * from article where channel_id in (?)
is it possible to get the execute sql in the trace? because the channel_id has more than 100 and the sql also contains other filter condition.

Can't get search for modification time to work

The example in package org.apache.lucene.demo works for text search.
But I can't get it to work using and displaying modification time.
It seems that the field modified is handled but no success using it.
Running SearchFiles prints hits for
Enter query:
+kompl*
but nothing here
+kompl* +modified:[0 TO 9999999999999]
Can someone provide an example for this?
I had the wrong assumption that file attributes are somehow implicitly available to me.
But ok, I had to do it by myself.
For indexing I added a simple integer
// provide stored date integer to query for [yyyymmdd]
Date dt = new Date(lastModified);
int myDays = (dt.getYear()+1900)*100*100 + (dt.getMonth()+1)*100 + dt.getDate();
doc.add(new IntPoint("moddate", myDays ));
doc.add(new StoredField("moddateVal", myDays ));
For searching I handle this field by an extended parser
public static class QueryParserModdate extends QueryParser {
public QueryParserModdate(String f, Analyzer a) {
super(f, a);
}
protected Query getRangeQuery(String field, String part1, String part2,
boolean startInclusive, boolean endInclusive)
throws ParseException {
if (field.equalsIgnoreCase("moddate")) {
int part1Int = Integer.MIN_VALUE;
int part2Int = Integer.MAX_VALUE;
try {
part1Int = Integer.parseInt(part1);
} catch (Exception e) {
...
Query query = IntPoint.newRangeQuery("moddate", part1Int,
part2Int);
return query;
}
return super.getRangeQuery(field, part1, part2, startInclusive,
endInclusive);
}
For sure not beautiful but working for me.

lucene 4.x user-defined Filter, the DocsEnum in getDocIdSet(....) returns null and fail to work

I make use of lucene 4.0 to build my search engine. I need to define a Filter when searching. The filter code like this will work fine:
public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs)
throws IOException {
String[] target_real_names = {"eMule"};
OpenBitSet obs = new OpenBitSet(context.reader().maxDoc());
for(String target_real_name : target_real_names){
TermQuery query=new TermQuery(new Term(Fields.PROJECT_REAL_NAME,target_real_name));
IndexSearcher indexSearcher=new IndexSearcher(context.reader());
TopDocs docs=indexSearcher.search(query,context.reader().maxDoc());
ScoreDoc[] scoreDocs=docs.scoreDocs;
if (scoreDocs.length==1) {
obs.set(scoreDocs[0].doc);
}
}
return obs;
}
but the code like this fail to work:
public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs)
throws IOException {
OpenBitSet obs = new OpenBitSet(context.reader().maxDoc());
String[] target_real_names = {"eMule"};
for(String target_real_name : target_real_names){
DocsEnum de = context.reader().termDocsEnum(new Term(Fields.PROJECT_REAL_NAME, target_real_name));
if(de.nextDoc()!= -1){
obs.set((long)de.docID());
}
}
return obs;
}
In this piece of code, the de will be null, I don't know why. Any one can help me?
Look at the javadoc of termDocsEnum() --> This will return null if either the field or term does not exist.
This means that it's totally normal that de is null when your term target_real_name does not exist.

How to get rid of StringTemplate warning "\n in string"

I'm using StringTemplate 4 to generate some Java source files.
The templates are really simple, e.g.:
QueryHandler(method) ::="
public class Obtenir<method.name>Handler extends QueryHandler\<List\<<method.name>Db>> implements IQueryHandler\<List\<<method.name>>>{
private IQuery\<List\<<method.name>Db>> query;
private <method.name>Converter converter;
#Inject
public Obtenir<method.name>Handler(IQuery\<List\<<method.name>Db>> query, <method.name>Converter converter, IStatisticsCollecter theStatsCollecter){
super(theStatsCollecter);
if(query == null){
throw new IllegalArgumentException(\"The query argument cannot be null\");
}
if(converter == null){
throw new IllegalArgumentException(\"Illegal argument for converter(null)\");
}
this.query = query;
this.converter = converter;
}
public List\<<method.name>> handle(Object... params) throws JdbcException {
final String method = \"obtenir<method.name>\";
DaoQueryStatusCallable status = new DaoQueryStatusCallable();
List\<<method.name>Db> result = invoke(query, status, method);
return converter.convert(result);
}
}
"
The code is even simpler:
STGroup group = new STGroupFile("src/main/resources/QueryHandler.stg");
ST wsTemplate = group.getInstanceOf("QueryHandler");
wsTemplate.add("method", m);
System.out.println(wsTemplate.render());
The template lines are separated by Unix EOLs (\n).
When I execute the code, StringTemplate is emitting a warning "QueryHandler.stg 1:25: \n in string".
The result is correct, but I'd still like to get rid of this message.
Anybody ever had this problem and knows how to solve it?
t() ::= "..." is meant only for single lines. Please use
t() ::= <<
...
>>
to get multi-line templates.
Ter

How to handle null pointer exceptions in elasticsearch

I'm using elasticsearch and i was trying to handle the case when the database is empty
#SuppressWarnings("unchecked")
public <M extends Model> SearchResults<M> findPage(int page, String search, String searchFields, String orderBy, String order, String where) {
BoolQueryBuilder qb = buildQueryBuilder(search, searchFields, where);
Query<M> query = (Query<M>) ElasticSearch.query(qb, entityClass);
// FIXME Currently we ignore the orderBy and order fields
query.from((page - 1) * getPageSize()).size(getPageSize());
query.hydrate(true);
return query.fetch();
}
the error at return query.fetch();
i'm trying to implement a try and catch statement but it's not working, any one can help with this please?