Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
added score on result set
Browse files Browse the repository at this point in the history
  • Loading branch information
maggiolo00 authored and maggiolo00 committed Jun 16, 2014
1 parent 1e88291 commit db0d5ef
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 16 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@
<artifactId>lucene-queryparser</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-misc</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-spatial</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.orientechnologies.common.concur.resource.OSharedResourceAdaptiveExternal;
import com.orientechnologies.lucene.manager.OLuceneIndexManagerAbstract;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.id.ORID;
Expand Down Expand Up @@ -154,6 +155,8 @@ public V get(Object key) {
return (V) lucene.get(key);
}



@Override
public void put(Object key, V value) {
lucene.put(key, value);
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/orientechnologies/lucene/OLuceneIndexType.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ public static Query createExactQuery(OIndexDefinition index, Object key) {
booleanQ.add(new TermQuery(new Term(OLuceneIndexManagerAbstract.KEY, key.toString())), BooleanClause.Occur.SHOULD);
}
query = booleanQ;
} else if (key instanceof OCompositeKey) {
BooleanQuery booleanQ = new BooleanQuery();
int i = 0;
OCompositeKey keys = (OCompositeKey) key;
for (String idx : index.getFields()) {
String val = (String) keys.getKeys().get(i);
booleanQ.add(new TermQuery(new Term(idx, val)), BooleanClause.Occur.MUST);
i++;

}
query = booleanQ;
}
return query;
}
Expand All @@ -96,8 +107,13 @@ public static Query createFullQuery(OIndexDefinition index, Object key, Analyzer
query = (String) key;
return getQueryParser(index, query, analyzer, version);
} else if (key instanceof OCompositeKey) {
query = ((OCompositeKey) key).getKeys().get(0).toString();
return getQueryParser(index, query, analyzer, version);
if (((OCompositeKey) key).getKeys().size() == 1) {
query = ((OCompositeKey) key).getKeys().get(0).toString();

return getQueryParser(index, query, analyzer, version);
} else {
return createExactQuery(index, key);
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* * Copyright 2014 Orient Technologies.
* *
* * Licensed 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.
*
*/

package com.orientechnologies.lucene.collections;

import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.index.OCompositeKey;
import com.orientechnologies.orient.core.index.OIndexFullText;

import java.util.List;

/**
* Created by enricorisa on 16/06/14.
*/
public class OFullTextCompositeKey extends OCompositeKey {
OCommandContext context;

public OFullTextCompositeKey(final List<?> keys) {
super(keys);
}

public OFullTextCompositeKey setContext(OCommandContext context) {
this.context = context;
return this;
}

public OCommandContext getContext() {
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.orientechnologies.common.listener.OProgressListener;
import com.orientechnologies.lucene.OLuceneIndex;
import com.orientechnologies.lucene.OLuceneIndexEngine;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ridbag.sbtree.OIndexRIDContainer;
Expand Down Expand Up @@ -80,8 +81,7 @@ public OIndexMultiValues put(Object key, OIdentifiable iSingleValue) {
}
}

@Override
public Set<OIdentifiable> get(Object key) {
public Set<OIdentifiable> get(Object key, OCommandContext context) {
checkForRebuild();

key = getCollatingValue(key);
Expand All @@ -101,6 +101,25 @@ public Set<OIdentifiable> get(Object key) {
}
}

@Override
public Set<OIdentifiable> get(Object key) {
checkForRebuild();

acquireSharedLock();
try {

final Set<OIdentifiable> values = indexEngine.get(key);

if (values == null)
return Collections.emptySet();

return values;

} finally {
releaseSharedLock();
}
}

@Override
public boolean remove(Object key, OIdentifiable value) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,37 @@
package com.orientechnologies.lucene.manager;

import java.io.IOException;
import java.util.*;

import com.orientechnologies.orient.core.index.*;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import com.orientechnologies.lucene.collections.OFullTextCompositeKey;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.command.traverse.OTraverse;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.codecs.TermStats;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.PriorityQueue;
import org.apache.lucene.util.Version;

import com.orientechnologies.lucene.OLuceneIndexType;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.index.OCompositeKey;
import com.orientechnologies.orient.core.index.OIndexCursor;
import com.orientechnologies.orient.core.index.OIndexException;
import com.orientechnologies.orient.core.index.OIndexKeyCursor;
import com.orientechnologies.orient.core.record.impl.ODocument;

public class OLuceneFullTextIndexManager extends OLuceneIndexManagerAbstract {
Expand Down Expand Up @@ -93,7 +104,12 @@ public Object get(Object key) {
Query q = null;
try {
q = OLuceneIndexType.createFullQuery(index, key, mgrWriter.getIndexWriter().getAnalyzer(), getVersion(metadata));
return getResults(q);

OCommandContext context = null;
if (key instanceof OFullTextCompositeKey) {
context = ((OFullTextCompositeKey) key).getContext();
}
return getResults(q, context);
} catch (ParseException e) {
throw new OIndexException("Error parsing lucene query ", e);
}
Expand Down Expand Up @@ -122,16 +138,22 @@ public void put(Object key, Object value) {
}
}

private Set<OIdentifiable> getResults(Query query) {
private Set<OIdentifiable> getResults(Query query, OCommandContext context) {
Set<OIdentifiable> results = new LinkedHashSet<>();
try {
IndexSearcher searcher = getSearcher();

Map<String, Float> scores = new HashMap<>();
TopDocs docs = searcher.search(query, Integer.MAX_VALUE);
ScoreDoc[] hits = docs.scoreDocs;
for (ScoreDoc score : hits) {
Document ret = searcher.doc(score.doc);
results.add(new ORecordId(ret.get(RID)));
String rId = ret.get(RID);
results.add(new ORecordId(rId));
scores.put(rId, score.score);
}
if (context != null) {
context.setVariable("$luceneScore", scores);
}
} catch (IOException e) {
throw new OIndexException("Error reading from Lucene index", e);
Expand Down Expand Up @@ -172,7 +194,12 @@ public OIndexCursor cursor(ValuesTransformer valuesTransformer) {

@Override
public OIndexKeyCursor keyCursor() {
return null;
return new OIndexKeyCursor() {
@Override
public Object next(int prefetchSize) {
return null;
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;

import com.orientechnologies.lucene.utils.OLuceneIndexUtils;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.index.OIndexException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.*;

import com.orientechnologies.lucene.shape.OShapeFactory;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.index.OIndexCursor;
import com.orientechnologies.orient.core.index.OIndexEngine;
import com.orientechnologies.orient.core.index.OIndexKeyCursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.orientechnologies.lucene.collections.OFullTextCompositeKey;
import com.orientechnologies.lucene.collections.OSpatialCompositeKey;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.ODatabaseComplex;
Expand All @@ -42,11 +44,11 @@ public OLuceneTextOperator() {
@Override
public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index, List<Object> keyParams, boolean ascSortOrder) {
OIndexCursor cursor;
Object indexResult = index.get(new OCompositeKey(keyParams));
Object indexResult = index.get(new OFullTextCompositeKey(keyParams).setContext(iContext));
if (indexResult == null || indexResult instanceof OIdentifiable)
cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, new OSpatialCompositeKey(keyParams));
cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, new OFullTextCompositeKey(keyParams));
else
cursor = new OIndexCursorCollectionValue(((Collection<OIdentifiable>) indexResult).iterator(), new OSpatialCompositeKey(
cursor = new OIndexCursorCollectionValue(((Collection<OIdentifiable>) indexResult).iterator(), new OFullTextCompositeKey(
keyParams));
return cursor;
}
Expand Down Expand Up @@ -81,6 +83,12 @@ public Collection<OIdentifiable> filterRecords(ODatabaseComplex<?> iRecord, List
@Override
public Object evaluateRecord(OIdentifiable iRecord, ODocument iCurrentResult, OSQLFilterCondition iCondition, Object iLeft,
Object iRight, OCommandContext iContext) {

Map<String, Float> scores = (Map<String, Float>) iContext.getVariable("$luceneScore");
if (scores != null) {
iContext.setVariable("$score", scores.get(iRecord.getIdentity().toString()));
}

return super.evaluateRecord(iRecord, iCurrentResult, iCondition, iLeft, iRight, iContext);
}
}

0 comments on commit db0d5ef

Please sign in to comment.