Adding normalization search.
This commit is contained in:
parent
33059b40fa
commit
ddc0d4e142
|
@ -56,40 +56,75 @@ class ResponseBool {
|
|||
|
||||
class SearchObject {
|
||||
private String title;
|
||||
private String titleNormalized;
|
||||
private String author;
|
||||
private String authorNormalized;
|
||||
private String content;
|
||||
private String contentNormalized;
|
||||
private String url;
|
||||
private String urlNormalized;
|
||||
private String urlImage;
|
||||
private String urlImageNormalized;
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public String getTitleNormalized() {
|
||||
return titleNormalized;
|
||||
}
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
public void setTitleNormalized(String titleNormalized) {
|
||||
this.titleNormalized = titleNormalized;
|
||||
}
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
public String getAuthorNormalized() {
|
||||
return authorNormalized;
|
||||
}
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
public void setAuthorNormalized(String authorNormalized) {
|
||||
this.authorNormalized = authorNormalized;
|
||||
}
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
public String getContentNormalized() {
|
||||
return contentNormalized;
|
||||
}
|
||||
public void setContentNormalized(String contentNormalized) {
|
||||
this.contentNormalized = contentNormalized;
|
||||
}
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
public String getUrlNormalized() {
|
||||
return urlNormalized;
|
||||
}
|
||||
public void setUrlNormalized(String urlNormalized) {
|
||||
this.urlNormalized = urlNormalized;
|
||||
}
|
||||
public String getUrlImage() {
|
||||
return urlImage;
|
||||
}
|
||||
public void setUrlImage(String urlImage) {
|
||||
this.urlImage = urlImage;
|
||||
}
|
||||
public String getUrlImageNormalized() {
|
||||
return urlImageNormalized;
|
||||
}
|
||||
public void setUrlImageNormalized(String urlImageNormalized) {
|
||||
this.urlImageNormalized = urlImageNormalized;
|
||||
}
|
||||
}
|
||||
|
||||
class ResponseSearch extends ResponseBool {
|
||||
|
@ -120,7 +155,7 @@ class LoadContentController {
|
|||
|
||||
@GetMapping(value="/search/{indexName}")
|
||||
public ResponseBool search(@PathVariable String indexName,
|
||||
@RequestParam(name = "q") String searchQuery) {
|
||||
@RequestParam(name = "q") String searchQuery) {
|
||||
if (indexName == null) {
|
||||
return returnFail("No queryString.");
|
||||
}
|
||||
|
@ -134,7 +169,7 @@ class LoadContentController {
|
|||
}
|
||||
try {
|
||||
Directory index = new NIOFSDirectory(pathIndex, NativeFSLockFactory.INSTANCE);
|
||||
Query q = new MultiFieldQueryParser(new String[]{"title", "content"}, analyzer).parse(searchQuery);
|
||||
Query q = new MultiFieldQueryParser(new String[] {"titleNormalized", "contentNormalized"}, analyzer).parse(searchQuery);
|
||||
IndexReader reader = DirectoryReader.open(index);
|
||||
IndexSearcher searcher = new IndexSearcher(reader);
|
||||
TopDocs docs = searcher.search(q, 10);
|
||||
|
@ -158,7 +193,7 @@ class LoadContentController {
|
|||
return returnFail("Unable to understand query.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static SearchObject readDocument(Document d) {
|
||||
SearchObject searchObject = new SearchObject();
|
||||
searchObject.setTitle(d.get("title"));
|
||||
|
@ -178,7 +213,7 @@ class LoadContentController {
|
|||
}
|
||||
return searchObject;
|
||||
}
|
||||
|
||||
|
||||
@PutMapping(value="/index/{indexName}")
|
||||
public ResponseBool create_index(@PathVariable String indexName, @RequestBody List<SearchObject> searchObjects) {
|
||||
try {
|
||||
|
@ -215,10 +250,12 @@ class LoadContentController {
|
|||
public static void addDoc(IndexWriter w, SearchObject searchObject) throws IOException {
|
||||
Document doc = new Document();
|
||||
doc.add(new TextField("title", searchObject.getTitle(), Field.Store.YES));
|
||||
doc.add(new TextField("titleNormalized", searchObject.getTitleNormalized(), Field.Store.YES));
|
||||
if (searchObject.getAuthor() != null) {
|
||||
doc.add(new TextField("author", searchObject.getAuthor(), Field.Store.YES));
|
||||
}
|
||||
doc.add(new TextField("content", searchObject.getContent(), Field.Store.YES));
|
||||
doc.add(new TextField("contentNormalized", searchObject.getContentNormalized(), Field.Store.YES));
|
||||
doc.add(new TextField("url", searchObject.getUrl(), Field.Store.YES));
|
||||
if (searchObject.getUrlImage() != null) {
|
||||
doc.add(new TextField("urlImage", searchObject.getUrlImage(), Field.Store.YES));
|
||||
|
|
Loading…
Reference in New Issue