Skip to content

Instantly share code, notes, and snippets.

@chatman
Created January 21, 2015 17:28
Show Gist options
  • Save chatman/f54b434d2052bcc07b5c to your computer and use it in GitHub Desktop.
Save chatman/f54b434d2052bcc07b5c to your computer and use it in GitHub Desktop.
Question Answering Request Handler example for blob store ref guide
package org.apache.solr.core;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.handler.StandardRequestHandler;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
public class QuestionAnswerRequestHandler extends StandardRequestHandler {
List<QAPattern> patterns = new ArrayList<QuestionAnswerRequestHandler.QAPattern>();
public QuestionAnswerRequestHandler() {
// Date of birth patterns
patterns.add(new QAPattern("(when was )(.*)( born)", 2, "name_t:'ENTITY' AND type_s:person", "dob_dt"));
patterns.add(new QAPattern("(what is the birthdate of )(.*)", 2, "name_t:'ENTITY' AND type_s:person", "dob_dt"));
patterns.add(new QAPattern("(birthday of )(.*)", 2, "name_t:'ENTITY' AND type_s:person", "dob_dt"));
patterns.add(new QAPattern("(how old is )(.*)", 2, "name_t:'ENTITY' AND type_s:person", "dob_dt"));
patterns.add(new QAPattern("(what is the age of )(.*)", 2, "name_t:'ENTITY' AND type_s:person", "dob_dt"));
// Location/hometown patterns
patterns.add(new QAPattern("(where does )(.*)(live)", 2, "name_t:'ENTITY' AND type_s:person", "hometown_s"));
patterns.add(new QAPattern("(hometown of )(.*)", 2, "name_t:'ENTITY' AND type_s:person", "hometown_s"));
// Capital of a country
patterns.add(new QAPattern("(capital of )(.*)", 2, "name_t:'ENTITY' AND type_s:country", "capital_s"));
patterns.add(new QAPattern("(what is capital of )(.*)", 2, "name_t:'ENTITY' AND type_s:country", "capital_s"));
patterns.add(new QAPattern("(what is the capital of )(.*)", 2, "name_t:'ENTITY' AND type_s:country", "capital_s"));
}
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
SolrParams params = req.getOriginalParams();
ModifiableSolrParams mparams = new ModifiableSolrParams(params);
String question = params.get("q");
if(question==null)
return;
for(QAPattern pat: patterns) {
if(pat.doesMatch(question)) {
String q = pat.getQuery(question);
String fl = "id,name_t,"+pat.returnField;
mparams.set("q", q, "fl", fl);
req.forward("standard", mparams, rsp);
return;
}
}
// if nothing works, fall back to textual search
mparams.set("q", "description_t:'question'", "fl", "*");
req.forward("standard", mparams, rsp);
}
@Override
public String getDescription() {
return "Question answering query parser, accepts the question with 'q' parameter";
}
class QAPattern {
Pattern pattern;
int entityGroup;
String query;
String returnField;
public QAPattern(String regex, int entityGroup, String queryPattern, String returnField) {
this.entityGroup = entityGroup;
this.query = queryPattern;
this.returnField = returnField;
this.pattern = Pattern.compile(regex);
}
String getQuery(String input) {
String entity = extractEntity(input);
if(entity==null)
return null;
return query.replaceAll("ENTITY", entity);
}
String extractEntity(String input) {
Matcher mat = pattern.matcher(input);
if(mat.find())
return mat.group(entityGroup);
return null;
}
boolean doesMatch(String input) {
Matcher mat = pattern.matcher(input);
return mat.matches();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment