moteurrecherche.Controleur.java Source code

Java tutorial

Introduction

Here is the source code for moteurrecherche.Controleur.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package moteurrecherche;

import Recherche.CustomizedScoreQuery;
import Recherche.Indexer;
import Recherche.Resultat;
import Recherche.SearchFiles;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.stage.FileChooser;
import com.jfoenix.controls.JFXTextField;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 *
 * @author JUGURTHA
 */
public class Controleur implements Initializable {

    @FXML
    private JFXTextField motCle;
    @FXML
    private HBox Conteneur;
    @FXML
    private TableView table;

    private int entier;

    public void Indexation(ActionEvent event) {
        Indexer In = new Indexer();
        FileChooser fileChooser = new FileChooser();
        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JSON Files", "*.JSON", "*.json"));
        fileChooser.setTitle("Choisir un fichier Json  Indexer");
        File selectedFile = fileChooser.showOpenDialog(null);
        if (selectedFile != null) {
            In.Indexation(selectedFile);
        }
        entier = 1;

    }

    public void Search(ActionEvent event) {

        if (entier == 1) {

            if (motCle.getText().length() != 0) {
                Conteneur.getChildren().clear();
                SearchFiles sr = new SearchFiles();
                System.out.println(motCle.getText().length());

                try {
                    Path path = Paths.get("index");
                    Directory dir = FSDirectory.open(path);
                    IndexReader ireader = DirectoryReader.open(dir);
                    IndexSearcher s = new IndexSearcher(ireader);
                    StandardAnalyzer analyzer = new StandardAnalyzer();

                    QueryParser parser = new QueryParser("text", analyzer);
                    Query wrappedQuery = new CustomizedScoreQuery(parser.parse(motCle.getText()), ireader);
                    TopDocs topdocs = s.search(wrappedQuery, 20);
                    ArrayList<Resultat> Res = new ArrayList<>();
                    for (ScoreDoc sd : topdocs.scoreDocs) {
                        Res.add(new Resultat(s.doc(sd.doc).get("text"), s.doc(sd.doc).get("date"),
                                s.doc(sd.doc).get("identifiant"), s.doc(sd.doc).get("auteur"), sd.score,
                                s.doc(sd.doc).get("Social")));

                    }

                    ObservableList<Resultat> obs = FXCollections.observableArrayList(Res);

                    TableColumn<Resultat, String> Texte = new TableColumn<>("Texte");
                    Texte.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getText()));
                    TableColumn<Resultat, String> auteur = new TableColumn<>("Auteur");
                    auteur.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getAuteur()));
                    TableColumn<Resultat, String> Date = new TableColumn<>("Date");
                    Date.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getDate()));
                    TableColumn<Resultat, String> Identifiant = new TableColumn<>("Identifiant");
                    Identifiant.setCellValueFactory(
                            data -> new SimpleStringProperty(data.getValue().getIdentifiant()));
                    TableColumn<Resultat, String> Social = new TableColumn<>("Score social");
                    Social.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getScoreSocial()));
                    TableColumn<Resultat, String> Score = new TableColumn<>("Score Final");
                    Score.setCellValueFactory(
                            data -> new SimpleFloatProperty(data.getValue().getScoreFinal()).asString());
                    table.getColumns().addAll(auteur, Date, Social, Score, Texte);
                    table.setItems(obs);
                    table.autosize();
                    ireader.close();
                    Conteneur.getChildren().add(table);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {

                System.out.println("J adore les chatons");
            }
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

}