Last active
April 18, 2017 08:11
-
-
Save govindpatel/8b8a6cda7f62f874f181dee533c16777 to your computer and use it in GitHub Desktop.
Document Structure and program for Luwak (Not working yet)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"user": { | |
"name":"Govind H Patel", | |
"info": "Likes to code. Hard to find.", | |
"summer": { | |
"activity": "Likes to dive in river", | |
"time":60 | |
}, | |
"weekend": { | |
"play": "nothing", | |
"queries": 4 | |
}, | |
"upvoted": { | |
"total": 2, | |
"document": ["this is first document", "Hello Again!"] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"user": { | |
"name":"Superman H Patel", | |
"info": "Likes to fly.", | |
"summer": { | |
"activity": "Likes to workout.", | |
"time": 98 | |
}, | |
"weekend": { | |
"play": "Go out with Lois Lane", | |
"queries": 5 | |
}, | |
"upvoted": { | |
"total": 0 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this should be the output for the program | |
QueryMatched for user1 document : query1 | |
QueryMatched for user1 document : query3 | |
QueryMatched for user1 document : query4 | |
QueryMatched for user2 document : query2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
import org.apache.lucene.search.*; | |
import uk.co.flax.luwak.*; | |
import uk.co.flax.luwak.matchers.SimpleMatcher; | |
import uk.co.flax.luwak.presearcher.TermFilteredPresearcher; | |
import uk.co.flax.luwak.queryparsers.LuceneQueryParser; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by govindpatel. | |
*/ | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
Monitor monitor = new Monitor(new LuceneQueryParser(null), new TermFilteredPresearcher()); | |
// user 1 json document | |
String user1Document = "{" + | |
" \"name\":\"Govind H Patel\"," + | |
" \"info\": \"Likes to code. Hard to find.\"," + | |
" \"summer\": {" + | |
" \"activity\": \"Likes to dive in river\"," + | |
" \"time\":60" + | |
" }," + | |
" \"weekend\": {" + | |
" \"play\": \"nothing\"," + | |
" \"queries\": 4" + | |
" }," + | |
" \"upvoted\": {" + | |
" \"total\": 2," + | |
" \"document\": [\"this is first document\", \"Hello Again!\"]" + | |
" }" + | |
"}"; | |
// user 2 json document | |
String user2Document = "{" + | |
" \"name\":\"Superman H Patel\"," + | |
" \"info\": \"Likes to fly.\"," + | |
" \"summer\": {" + | |
" \"activity\": \"Likes to workout.\"," + | |
" \"time\": 98" + | |
" }," + | |
" \"weekend\": {" + | |
" \"play\": \"Go out with Lois Lane\"," + | |
" \"queries\": 5" + | |
" }," + | |
" \"upvoted\": {" + | |
" \"total\": 0" + | |
" }" + | |
"}"; | |
// document for user 1 | |
InputDocument documentForUser1 = InputDocument.builder("user1") | |
.addField("user", user1Document, new StandardAnalyzer()) | |
.build(); | |
// document for user 2 | |
InputDocument documentForUser2 = InputDocument.builder("user2") | |
.addField("user", user2Document, new StandardAnalyzer()) | |
.build(); | |
// queries | |
// user name must match "Govind H Patel" | |
MonitorQuery monitorQuery1 = new MonitorQuery("query1", "user.name:\"Govind H Patel\""); | |
//not of (user.name is "Govind H Patel" AND user.info contains "code") | |
MonitorQuery monitorQuery2 = new MonitorQuery("query2", "NOT (user.name:Govind H Patel AND user.info:code)"); | |
// user.upvoted.total >= 2 | |
MonitorQuery monitorQuery3 = new MonitorQuery("query3", NumericRangeQuery.newLongRange("upvote", 2L, Long.MAX_VALUE, true, true).toString()); | |
// user.info contains `Likes` prefix | |
MonitorQuery monitorQuery4 = new MonitorQuery("query4", "user.info:Likes*"); | |
ArrayList<MonitorQuery> monitorQuery = new ArrayList<MonitorQuery>(); | |
monitorQuery.add(monitorQuery1); | |
monitorQuery.add(monitorQuery2); | |
monitorQuery.add(monitorQuery3); | |
monitorQuery.add(monitorQuery4); | |
// update the monitor | |
List<QueryError> errors = monitor.update(monitorQuery); | |
Matches<QueryMatch> matchesForUser1 = monitor.match(documentForUser1, SimpleMatcher.FACTORY); | |
Matches<QueryMatch> matchesForUser2 = monitor.match(documentForUser2, SimpleMatcher.FACTORY); | |
for (String s : matchesForUser1.getPresearcherHits()) { | |
System.out.println("QueryMatched for user1 document : " + s); | |
} | |
for (String s : matchesForUser2.getPresearcherHits()) { | |
System.out.println("QueryMatched for user2 document : " + s); | |
} | |
System.out.println("Done"); | |
monitor.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QueryMatched for user1 document : query4 | |
QueryMatched for user1 document : query3 | |
QueryMatched for user2 document : query4 | |
QueryMatched for user2 document : query3 | |
Done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// user name must match "Govind H Patel" | |
user.name:"Govind H Patel" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//not of (user.name is "Govind H Patel" AND user.info contains "code") | |
NOT (user.name: "Govind H Patel" AND user.info:code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// user.upvoted.total >= 2 | |
upvote:[2 TO *] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// user.info contains `Likes` prefix | |
user.info:Likes* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes I did try field by field but that didn't work.