Skip to content

Instantly share code, notes, and snippets.

@govindpatel
Last active April 18, 2017 08:11
Show Gist options
  • Save govindpatel/8b8a6cda7f62f874f181dee533c16777 to your computer and use it in GitHub Desktop.
Save govindpatel/8b8a6cda7f62f874f181dee533c16777 to your computer and use it in GitHub Desktop.
Document Structure and program for Luwak (Not working yet)
"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!"]
}
}
"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 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
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();
}
}
QueryMatched for user1 document : query4
QueryMatched for user1 document : query3
QueryMatched for user2 document : query4
QueryMatched for user2 document : query3
Done
// user name must match "Govind H Patel"
user.name:"Govind H Patel"
//not of (user.name is "Govind H Patel" AND user.info contains "code")
NOT (user.name: "Govind H Patel" AND user.info:code)
// user.upvoted.total >= 2
upvote:[2 TO *]
// user.info contains `Likes` prefix
user.info:Likes*
@romseygeek
Copy link

Hi, luwak doesn't use JSON for building InputDocuments, so you're just putting all of your data into the 'user' field there. You'll need to build it up field by field, something like:

InputDocument user2 = InputDocument.builder("user2")
    .addField("name", "Superman H Patel", new StandardAnalyzer())
    .addField("info", "Likes to fly", new StandardAnalyzer())
    ...
    .build()

@govindpatel
Copy link
Author

yes I did try field by field but that didn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment