Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SENSEI-302 Modify request API to allow clients to specify which stored f... #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ public String sendBQLRaw(String bql) {
}

public Map<Long, JSONObject> sendGetRequest(long... uids) throws IOException, JSONException {
return sendGetRequest(true, true, uids);
}

public Map<Long, JSONObject> sendGetRequest(boolean includeValue, boolean includeFields,
long... uids) throws IOException, JSONException {
Map<Long, JSONObject> ret = new LinkedHashMap<Long, JSONObject>(uids.length);
String response = sendPostRaw(getStoreGetUrl(), new JSONArray(uids).toString());
String response = sendPostRaw(getStoreGetUrl(includeValue, includeFields),
new JSONArray(uids).toString());
if (response == null || response.length() == 0) {
return ret;
}
Expand All @@ -95,8 +101,25 @@ public String getSearchUrl() {
}

public String getStoreGetUrl() {
if (url != null) return url + "/get";
return "http://" + host + ":" + port + "/sensei/get";
return getStoreGetUrl(false, false);
}

public String getStoreGetUrl(boolean includeValue, boolean includeFields) {
String baseUrl;
if (url != null) {
baseUrl = url + "/get";
} else {
baseUrl = "http://" + host + ":" + port + "/sensei/get";
}
if (includeValue && includeFields) {
return baseUrl + "?value=true&fields=true";
} else if(includeValue) {
return baseUrl + "?value=true";
} else if (includeFields) {
return baseUrl + "?fields=true";
} else {
return baseUrl;
}
}

private JSONObject jsonResponse(String output) throws JSONException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ public class SenseiClientRequest {
private final List<Object> sort = new ArrayList<Object>();
private final Map<String, Facet> facets = new HashMap<String, Facet>();
/**
* Flag indicating whether stored fields are to be fetched
* Flag indicating whether all stored fields are to be fetched
*/
private boolean fetchAllStoredFields;
/**
* Flag indicating whether the stored value is to be fetched
*/
private boolean fetchStoredValue;
/**
* Stored fields to fetch
*/
private boolean fetchStored;
private List<String> fieldsToFetch;
private final List<String> termVectors = new ArrayList<String>();
/**
Expand Down Expand Up @@ -95,8 +102,13 @@ public Builder paging(int size, int offset) {
return this;
}

public Builder fetchStored(boolean fetchStored) {
request.fetchStored = fetchStored;
public Builder fetchAllStoredFields(boolean fetchAllStoredFields) {
request.fetchAllStoredFields = fetchAllStoredFields;
return this;
}

public Builder fetchStoredValue(boolean fetchStoredValue) {
request.fetchStoredValue = fetchStoredValue;
return this;
}

Expand Down Expand Up @@ -244,8 +256,12 @@ public Map<String, Facet> getFacets() {
return facets;
}

public boolean isFetchStored() {
return fetchStored;
public boolean isFetchAllStoredFields() {
return fetchAllStoredFields;
}

public boolean isFetchStoredValue() {
return fetchStoredValue;
}

public List<String> getTermVectors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void main(String[] args) throws Exception {
Operator.and))
.addSelection(
Selection.terms("color", Arrays.asList("red"), new ArrayList<String>(), Operator.or))
.paging(10, 0).fetchStored(true).addSort(Sort.desc("price")).build();
.paging(10, 0).fetchAllStoredFields(true).addSort(Sort.desc("price")).build();
JSONObject serialized = (JSONObject) JsonSerializer.serialize(senseiRequest);
System.out.println(serialized.toString(2));
SenseiResult senseiResult = new SenseiServiceProxy("localhost", 8080)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void main(String[] args) throws Exception {
SenseiClientRequest senseiRequest = SenseiClientRequest
.builder()
.paging(10, 0)
.fetchStored(true)
.fetchAllStoredFields(true)
.addSelection(
Selection.terms("color", Arrays.asList("red", "blue"), Collections.<String> emptyList(),
Operator.or)).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void clear() {
_req = new SenseiRequest();
_req.setOffset(0);
_req.setCount(5);
_req.setFetchStoredFields(true);
_req.setFetchAllStoredFields(true);
}

public void clearSelections() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.lucene.document.DocumentStoredFieldVisitor;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.SortField;
Expand Down Expand Up @@ -72,22 +73,31 @@ SenseiHit getSenseiHit(SenseiRequest req) {
}
ZoieSegmentReader<?> zoieSegmentReader = (ZoieSegmentReader<?>) innerReader;
SenseiHit hit = new SenseiHit();
if (req.isFetchStoredFields()) {

if (req.isFetchAllStoredFields()) {
try {
hit.setStoredFields(reader.document(doc));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
} else if (req.getStoredFieldsToFetch() != null && !req.getStoredFieldsToFetch().isEmpty()) {
try {
DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(req.getStoredFieldsToFetch());
reader.document(doc, visitor);
hit.setStoredFields(visitor.getDocument());
} catch(Exception e) {
logger.error(e.getMessage(),e);
}
}

if (req.isFetchStoredFields()) {
if (req.isFetchStoredValue()) {
try {
BytesRef bytesRef = zoieSegmentReader.getStoredValue(doc);
if (bytesRef != null) {
hit.setStoredValue(bytesRef.bytes);
}
} catch (Exception e) {
logger.error("Exception in getSenseiHit", e);
logger.error("Exception fetching stored value in getSenseiHit", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.senseidb.search.node;

import java.net.InetSocketAddress;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -52,6 +55,19 @@ public static void recoverSrcData(SenseiResult res, SenseiHit[] hits, boolean is
byte[] dataBytes = hit.getStoredValue();
if (dataBytes == null || dataBytes.length == 0) {
dataBytes = hit.getFieldBinaryValue(AbstractZoieIndexable.DOCUMENT_STORE_FIELD);
if (dataBytes != null) {
List<SerializableField> fields = new ArrayList<SerializableField>(hit.getStoredFields());
ListIterator<SerializableField> lIter = fields.listIterator();
while (lIter.hasNext()) {
SerializableField field = lIter.next();
if (AbstractZoieIndexable.DOCUMENT_STORE_FIELD.equals(field.name())) {
lIter.remove();
}
}
hit.setStoredFields(fields);
}
} else {
hit.setStoredValue(null);
}

if (dataBytes != null && dataBytes.length > 0) {
Expand Down Expand Up @@ -83,9 +99,11 @@ public static void recoverSrcData(SenseiResult res, SenseiHit[] hits, boolean is
public SenseiResult mergeResults(SenseiRequest request, List<SenseiResult> resultList) {
SenseiResult res = ResultMerger.merge(request, resultList, false);

if (request.isFetchStoredFields()) {
boolean fetchStoredFields = request.isFetchAllStoredFields() ||
request.getStoredFieldsToFetch() != null && !request.getStoredFieldsToFetch().isEmpty();
if (fetchStoredFields || request.isFetchStoredValue()) {
long start = System.currentTimeMillis();
recoverSrcData(res, res.getSenseiHits(), request.isFetchStoredFields());
recoverSrcData(res, res.getSenseiHits(), fetchStoredFields);
res.setTime(res.getTime() + (System.currentTimeMillis() - start));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public class SenseiRequest implements AbstractSenseiRequest, Cloneable {
private int _count;
private int _origOffset;
private int _origCount;
private boolean _fetchStoredFields;
private boolean _origFetchStoredFields;
private boolean _fetchAllStoredFields;
private boolean _origFetchAllStoredFields;
private boolean _fetchStoredValue;
private Map<String, FacetHandlerInitializerParam> _facetInitParamMap;
private Set<Integer> _partitions;
private boolean _showExplanation;
Expand All @@ -63,7 +64,8 @@ public SenseiRequest() {
_selections = new HashMap<String, BrowseSelection>();
_sortSpecs = new ArrayList<SerializableSortField>();
_facetSpecMap = new HashMap<String, FacetSpec>();
_fetchStoredFields = false;
_fetchAllStoredFields = false;
_fetchStoredValue = false;
_partitions = null;
_showExplanation = false;
_routeParam = null;
Expand Down Expand Up @@ -204,7 +206,7 @@ public Map<String, FacetSpec> getFacetSpecs() {
public void saveState() {
_origOffset = _offset;
_origCount = _count;
_origFetchStoredFields = _fetchStoredFields;
_origFetchAllStoredFields = _fetchAllStoredFields;
if (_origFacetSpecMaxCounts == null && _facetSpecMap != null) {
_origFacetSpecMaxCounts = new HashMap<String, Integer>();
for (Map.Entry<String, FacetSpec> entry : _facetSpecMap.entrySet()) {
Expand All @@ -219,7 +221,7 @@ public void saveState() {
public void restoreState() {
_offset = _origOffset;
_count = _origCount;
_fetchStoredFields = _origFetchStoredFields;
_fetchAllStoredFields = _origFetchAllStoredFields;
if (_facetSpecMap != null) {
for (Map.Entry<String, FacetSpec> entry : _facetSpecMap.entrySet()) {
FacetSpec spec = entry.getValue();
Expand Down Expand Up @@ -252,12 +254,20 @@ public void clearSort() {
_sortSpecs.clear();
}

public boolean isFetchStoredFields() {
return _fetchStoredFields;
public boolean isFetchAllStoredFields() {
return _fetchAllStoredFields;
}

public void setFetchStoredFields(boolean fetchStoredFields) {
_fetchStoredFields = fetchStoredFields;
public void setFetchAllStoredFields(boolean fetchAllStoredFields) {
_fetchAllStoredFields = fetchAllStoredFields;
}

public boolean isFetchStoredValue(){
return _fetchStoredValue;
}

public void setFetchStoredValue(boolean fetchStoredValue) {
_fetchStoredValue = fetchStoredValue;
}

/**
Expand Down Expand Up @@ -467,7 +477,9 @@ public String toString() {
if (_routeParam != null) buf.append("route param: ").append(_routeParam).append('\n');
if (_groupBy != null) buf.append("group by: ").append(_groupBy).append('\n');
buf.append("max per group: ").append(_maxPerGroup).append('\n');
buf.append("fetch stored fields: ").append(_fetchStoredFields).append('\n');
buf.append("fetch all stored fields: ").append(_fetchAllStoredFields).append('\n');
buf.append("stored fields to fetch: ").append(_storedFieldsToFetch).append('\n');
buf.append("fetch stored value: ").append(_fetchStoredValue).append('\n');
return buf.toString();
}

Expand All @@ -492,7 +504,8 @@ public SenseiRequest clone() {
clone.setQuery(this.getQuery());
clone.setOffset(this.getOffset());
clone.setCount(this.getCount());
clone.setFetchStoredFields(this.isFetchStoredFields());
clone.setFetchAllStoredFields(this.isFetchAllStoredFields());
clone.setFetchStoredValue(this.isFetchStoredValue());
clone.setFacetHandlerInitParamMap(this.getFacetHandlerInitParamMap());
clone.setPartitions(this.getPartitions());
clone.setShowExplanation(this.isShowExplanation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,18 @@ private void handleStoreGetRequest(HttpServletRequest req, HttpServletResponse r
if (jsonString != null) ids = new FastJSONArray(jsonString);
}

boolean includeFields = req.getParameter("fields") == null ||
Boolean.TRUE.toString().equalsIgnoreCase(req.getParameter("fields"));
boolean includeValue = req.getParameter("value") == null ||
Boolean.TRUE.toString().equalsIgnoreCase(req.getParameter("value"));

query = "get=" + String.valueOf(ids);

String[] vals = RequestConverter2.getStrings(ids);
if (vals != null && vals.length != 0) {
senseiReq = new SenseiRequest();
senseiReq.setFetchStoredFields(true);
senseiReq.setFetchAllStoredFields(includeFields);
senseiReq.setFetchStoredValue(includeValue);
senseiReq.setCount(vals.length);
BrowseSelection sel = new BrowseSelection(SenseiFacetHandlerBuilder.UID_FACET_NAME);
sel.setValues(vals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_FACET_ORDER;
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_FACET_ORDER_HITS;
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_FACET_ORDER_VAL;
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_FETCH_STORED;
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_FETCH_ALL_STORED_FIELDS;
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_FETCH_STORED_VALUE;
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_FETCH_TERMVECTOR;
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_FIELDS_TO_FETCH;
import static com.senseidb.servlet.SenseiSearchServletParams.PARAM_GROUP_BY;
Expand Down Expand Up @@ -322,8 +323,10 @@ public static JSONArray buildJSONHits(SenseiRequest req, SenseiHit[] hits) throw

// get fetchStored even if request does not have it because it could be set at the
// federated broker level
if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_SRC_DATA)
|| req.isFetchStoredFields() || hit.getSrcData() != null) {
if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_SRC_DATA) ||
req.isFetchAllStoredFields() ||
(req.getStoredFieldsToFetch() != null && !req.getStoredFieldsToFetch().isEmpty()) || hit.getSrcData() != null)
{
hitObj.put(PARAM_RESULT_HIT_SRC_DATA, hit.getSrcData());
}
if (fieldMap != null) {
Expand Down Expand Up @@ -518,7 +521,8 @@ public static void convertScalarParams(SenseiRequest senseiReq, DataConfiguratio
senseiReq.setOffset(params.getInt(PARAM_OFFSET, 0));
senseiReq.setCount(params.getInt(PARAM_COUNT, 10));
senseiReq.setShowExplanation(params.getBoolean(PARAM_SHOW_EXPLAIN, false));
senseiReq.setFetchStoredFields(params.getBoolean(PARAM_FETCH_STORED, false));
senseiReq.setFetchAllStoredFields(params.getBoolean(PARAM_FETCH_ALL_STORED_FIELDS, false));
senseiReq.setFetchStoredValue(params.getBoolean(PARAM_FETCH_STORED_VALUE, false));

String[] fetchTVs = params.getStringArray(PARAM_FETCH_TERMVECTOR);
if (fetchTVs != null && fetchTVs.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public interface SenseiSearchServletParams {
public static final String PARAM_SORT_SCORE_REVERSE = "relrev";
public static final String PARAM_SORT_DOC = "doc";
public static final String PARAM_SORT_DOC_REVERSE = "docrev";
public static final String PARAM_FETCH_STORED = "fetchstored";
public static final String PARAM_FETCH_ALL_STORED_FIELDS = "fetchallstoredfields";
public static final String PARAM_FETCH_STORED_VALUE = "fetchstoredvalue";
public static final String PARAM_FIELDS_TO_FETCH = "fieldstofetch";
public static final String PARAM_FETCH_TERMVECTOR = "fetchtermvector";
public static final String PARAM_SHOW_EXPLAIN = "showexplain";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private SenseiResult browse(SenseiRequest senseiRequest, MultiBoboBrowser browse
}
// SortCollector collector =
// browser.getSortCollector(req.getSort(),req.getQuery(), offset, count,
// req.isFetchStoredFields(),false);
// req.isFetchAllStoredFields(),false);

// Map<String, FacetAccessible> facetCollectors = new HashMap<String,
// FacetAccessible>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,10 @@ public static void convertPartitionParams(List<NameValuePair> qparams, Set<Integ
}

public static void convertScalarParams(List<NameValuePair> qparams, SenseiRequest req) {
qparams.add(new BasicNameValuePair(SenseiSearchServletParams.PARAM_FETCH_STORED, Boolean
.toString(req.isFetchStoredFields())));
qparams.add(new BasicNameValuePair(SenseiSearchServletParams.PARAM_FETCH_ALL_STORED_FIELDS, Boolean
.toString(req.isFetchAllStoredFields())));
qparams.add(new BasicNameValuePair(SenseiSearchServletParams.PARAM_FETCH_STORED_VALUE, Boolean
.toString(req.isFetchStoredValue())));
qparams.add(new BasicNameValuePair(SenseiSearchServletParams.PARAM_SHOW_EXPLAIN, Boolean
.toString(req.isShowExplanation())));
qparams.add(new BasicNameValuePair(SenseiSearchServletParams.PARAM_OFFSET, Integer.toString(req
Expand Down
Loading