Skip to content

Commit

Permalink
advanced search, search relevant improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogefest committed Oct 23, 2021
1 parent ecef69e commit 21fda01
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 18 deletions.
91 changes: 82 additions & 9 deletions src/main/java/com/ogefest/filehunter/IndexRead.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.LongRange;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexNotFoundException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
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.queryparser.xml.builders.BooleanQueryBuilder;
import org.apache.lucene.queryparser.xml.builders.RangeQueryBuilder;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.QueryBuilder;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class IndexRead {

Expand Down Expand Up @@ -96,7 +102,12 @@ public SearchResult getByUuid(String uuid) {
return searchResults.get(0);
}


public ArrayList<SearchResult> query(String q) {
return query(q, new HashMap<>());
}

public ArrayList<SearchResult> query(String q, HashMap<String, String> filters) {

ArrayList<SearchResult> result = new ArrayList<>();
if (searcher == null) {
Expand All @@ -107,10 +118,30 @@ public ArrayList<SearchResult> query(String q) {

QueryParser parser = new QueryParser("path", analyzer);
parser.setDefaultOperator(QueryParser.Operator.AND);
Query pathQueryTmp = parser.parse(q);
BoostQuery pathQuery = new BoostQuery(pathQueryTmp, 1.2f);

QueryParser parserContent = new QueryParser("content", analyzer);
parserContent.setDefaultOperator(QueryParser.Operator.AND);
Query contentQuery = parserContent.parse(q);


// path^1.2 OR content
BooleanQuery.Builder mainQuery = new BooleanQuery.Builder();
mainQuery.add(pathQuery, BooleanClause.Occur.SHOULD);
mainQuery.add(contentQuery, BooleanClause.Occur.SHOULD);


Query query = parser.parse(q);
// BoostQuery bq = new BoostQuery(query);
TopDocs hits = searcher.search(query, 100);
// final + filters
BooleanQuery.Builder finalQuery = new BooleanQuery.Builder();
finalQuery.add(mainQuery.build(), BooleanClause.Occur.MUST);
addSingleFilters(finalQuery, filters);
addRangeFilters(finalQuery, filters);


Query searchQuery = finalQuery.build();

TopDocs hits = searcher.search(searchQuery, 100);
for(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);

Expand All @@ -127,9 +158,51 @@ public ArrayList<SearchResult> query(String q) {
return result;
}

// public ArrayList<FileInfo> getFilesToUpdateMetadata() {
private void addSingleFilters(BooleanQuery.Builder query, HashMap<String, String> queryFilters) {

String supportedFields[] = { "index", "ext", "name", "content", "path" };
List<String> supportedFilters = Arrays.asList(supportedFields);

for (String k : supportedFilters) {
if (queryFilters.containsKey(k)) {
query.add(new TermQuery(new Term(k, queryFilters.get(k))), BooleanClause.Occur.MUST);
}
}
}

private void addRangeFilters(BooleanQuery.Builder query, HashMap<String, String> queryFilters) {
HashMap<String, List<String>> supported = new HashMap<>();

String sizeKeys[] = {"minsize", "maxsize"};
supported.put("size", Arrays.asList(sizeKeys));

String modifiedKeys[] = {"modified_after", "modified_before"};
supported.put("last_modified", Arrays.asList(modifiedKeys));

String createdKeys[] = {"created_after", "created_before"};
supported.put("created", Arrays.asList(createdKeys));

for (String k : supported.keySet()) {

String fieldMin = supported.get(k).get(0);
String fieldMax = supported.get(k).get(1);

// }
if (!queryFilters.containsKey(fieldMin) && !queryFilters.containsKey(fieldMax)) {
continue;
}
long fieldMinLong = 0;
if (queryFilters.containsKey(fieldMin)) {
fieldMinLong = Long.parseLong(queryFilters.get(fieldMin));
}

long fieldMaxLong = Long.MAX_VALUE;
if (queryFilters.containsKey(fieldMax) && !queryFilters.get(fieldMax).equals("0")) {
fieldMaxLong = Long.parseLong(queryFilters.get(fieldMax));
}

query.add(LongPoint.newRangeQuery(k, fieldMinLong, fieldMaxLong), BooleanClause.Occur.MUST);
}
}

public int getNumDocs() {
if (reader == null) {
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/ogefest/filehunter/api/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import com.ogefest.filehunter.SearchResult;

import javax.inject.Inject;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Path("/search")
public class SearchController {
Expand All @@ -17,10 +23,20 @@ public class SearchController {
App app;

@GET
public ArrayList<SearchResult> search(@QueryParam("q") String query) {
public ArrayList<SearchResult> search(@QueryParam("q") String query, @Context UriInfo uinfo) {

MultivaluedMap<String, String> abc = uinfo.getQueryParameters();
HashMap<String, String> filters = new HashMap<>();
String kkey = "";
for (String k : abc.keySet()) {
if (k.indexOf("filter[") == 0) {
kkey = k.substring(7, k.length()-1);
filters.put(kkey, abc.getFirst(k));
}
}

IndexRead ir = new IndexRead(app.getConfiguration());
ArrayList<SearchResult> result = ir.query(query);
ArrayList<SearchResult> result = ir.query(query, filters);

return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/META-INF/resources/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"files": {
"main.css": "/static/css/main.ee01ecee.chunk.css",
"main.js": "/static/js/main.6c323a16.chunk.js",
"main.js.map": "/static/js/main.6c323a16.chunk.js.map",
"main.js": "/static/js/main.61eb00ba.chunk.js",
"main.js.map": "/static/js/main.61eb00ba.chunk.js.map",
"runtime-main.js": "/static/js/runtime-main.80fb9e64.js",
"runtime-main.js.map": "/static/js/runtime-main.80fb9e64.js.map",
"static/css/2.ee8d6a27.chunk.css": "/static/css/2.ee8d6a27.chunk.css",
Expand All @@ -21,6 +21,6 @@
"static/css/2.ee8d6a27.chunk.css",
"static/js/2.6733b7d9.chunk.js",
"static/css/main.ee01ecee.chunk.css",
"static/js/main.6c323a16.chunk.js"
"static/js/main.61eb00ba.chunk.js"
]
}
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/resources/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en" class="h-100"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="manifest" href="/manifest.json"/><title>Filehunter</title><link href="/static/css/2.ee8d6a27.chunk.css" rel="stylesheet"><link href="/static/css/main.ee01ecee.chunk.css" rel="stylesheet"></head><body class="d-flex flex-column h-100"><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function r(r){for(var n,i,a=r[0],c=r[1],l=r[2],s=0,p=[];s<a.length;s++)i=a[s],Object.prototype.hasOwnProperty.call(o,i)&&o[i]&&p.push(o[i][0]),o[i]=0;for(n in c)Object.prototype.hasOwnProperty.call(c,n)&&(e[n]=c[n]);for(f&&f(r);p.length;)p.shift()();return u.push.apply(u,l||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,a=1;a<t.length;a++){var c=t[a];0!==o[c]&&(n=!1)}n&&(u.splice(r--,1),e=i(i.s=t[0]))}return e}var n={},o={1:0},u=[];function i(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,i),t.l=!0,t.exports}i.e=function(e){var r=[],t=o[e];if(0!==t)if(t)r.push(t[2]);else{var n=new Promise((function(r,n){t=o[e]=[r,n]}));r.push(t[2]=n);var u,a=document.createElement("script");a.charset="utf-8",a.timeout=120,i.nc&&a.setAttribute("nonce",i.nc),a.src=function(e){return i.p+"static/js/"+({}[e]||e)+"."+{3:"64a585ac"}[e]+".chunk.js"}(e);var c=new Error;u=function(r){a.onerror=a.onload=null,clearTimeout(l);var t=o[e];if(0!==t){if(t){var n=r&&("load"===r.type?"missing":r.type),u=r&&r.target&&r.target.src;c.message="Loading chunk "+e+" failed.\n("+n+": "+u+")",c.name="ChunkLoadError",c.type=n,c.request=u,t[1](c)}o[e]=void 0}};var l=setTimeout((function(){u({type:"timeout",target:a})}),12e4);a.onerror=a.onload=u,document.head.appendChild(a)}return Promise.all(r)},i.m=e,i.c=n,i.d=function(e,r,t){i.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,r){if(1&r&&(e=i(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(i.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)i.d(t,n,function(r){return e[r]}.bind(null,n));return t},i.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(r,"a",r),r},i.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},i.p="/",i.oe=function(e){throw console.error(e),e};var a=this["webpackJsonpfilehunter-front"]=this["webpackJsonpfilehunter-front"]||[],c=a.push.bind(a);a.push=r,a=a.slice();for(var l=0;l<a.length;l++)r(a[l]);var f=c;t()}([])</script><script src="/static/js/2.6733b7d9.chunk.js"></script><script src="/static/js/main.6c323a16.chunk.js"></script></body></html>
<!doctype html><html lang="en" class="h-100"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="manifest" href="/manifest.json"/><title>Filehunter</title><link href="/static/css/2.ee8d6a27.chunk.css" rel="stylesheet"><link href="/static/css/main.ee01ecee.chunk.css" rel="stylesheet"></head><body class="d-flex flex-column h-100"><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function r(r){for(var n,i,a=r[0],c=r[1],l=r[2],s=0,p=[];s<a.length;s++)i=a[s],Object.prototype.hasOwnProperty.call(o,i)&&o[i]&&p.push(o[i][0]),o[i]=0;for(n in c)Object.prototype.hasOwnProperty.call(c,n)&&(e[n]=c[n]);for(f&&f(r);p.length;)p.shift()();return u.push.apply(u,l||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,a=1;a<t.length;a++){var c=t[a];0!==o[c]&&(n=!1)}n&&(u.splice(r--,1),e=i(i.s=t[0]))}return e}var n={},o={1:0},u=[];function i(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,i),t.l=!0,t.exports}i.e=function(e){var r=[],t=o[e];if(0!==t)if(t)r.push(t[2]);else{var n=new Promise((function(r,n){t=o[e]=[r,n]}));r.push(t[2]=n);var u,a=document.createElement("script");a.charset="utf-8",a.timeout=120,i.nc&&a.setAttribute("nonce",i.nc),a.src=function(e){return i.p+"static/js/"+({}[e]||e)+"."+{3:"64a585ac"}[e]+".chunk.js"}(e);var c=new Error;u=function(r){a.onerror=a.onload=null,clearTimeout(l);var t=o[e];if(0!==t){if(t){var n=r&&("load"===r.type?"missing":r.type),u=r&&r.target&&r.target.src;c.message="Loading chunk "+e+" failed.\n("+n+": "+u+")",c.name="ChunkLoadError",c.type=n,c.request=u,t[1](c)}o[e]=void 0}};var l=setTimeout((function(){u({type:"timeout",target:a})}),12e4);a.onerror=a.onload=u,document.head.appendChild(a)}return Promise.all(r)},i.m=e,i.c=n,i.d=function(e,r,t){i.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,r){if(1&r&&(e=i(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(i.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)i.d(t,n,function(r){return e[r]}.bind(null,n));return t},i.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(r,"a",r),r},i.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},i.p="/",i.oe=function(e){throw console.error(e),e};var a=this["webpackJsonpfilehunter-front"]=this["webpackJsonpfilehunter-front"]||[],c=a.push.bind(a);a.push=r,a=a.slice();for(var l=0;l<a.length;l++)r(a[l]);var f=c;t()}([])</script><script src="/static/js/2.6733b7d9.chunk.js"></script><script src="/static/js/main.61eb00ba.chunk.js"></script></body></html>

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

0 comments on commit 21fda01

Please sign in to comment.