1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| private void indexe_Click(object sender, EventArgs e)
{
AddDocument(@"C:\Users\schaffora\Desktop\Test1.txt");
}
public void AddDocument(string path)
{
//Création d'un nouvel index.
IndexWriter writer = new IndexWriter(@"C:\Users\schaffora\Desktop\Test2\", new SimpleAnalyzer(), true);
//Création d'un nouveau document.
Document doc = new Document();
//Création de la variable qui va contenir le texte.
string rawText;
//Choix du type d'encodage
Encoding objEncoding = Encoding.Default;
// Lecture du fichier jusqu'à sa fin et récupération du texte dans rawText
using (StreamReader sr = new StreamReader(path, objEncoding))
{
rawText = sr.ReadToEnd();
}
doc.Add(Field.Text("text", rawText));
doc.Add(Field.UnStored("path", path));
//doc.Add(new Field("Text",rawText,true,true,true));
//doc.Add(new Field("path",path,true,true,true));
// ajout du document
writer.AddDocument(doc);
writer.Optimize();
writer.Close();
//Choix de l'index (index que l'on a créé juste en dessus)
IndexSearcher searcher = new IndexSearcher(@"C:\Users\schaffora\Desktop\Test2\");
Query query = new TermQuery(new Term("postBody", tbxMot.Text));
Hits hits = searcher.Search(query);
int J = 0;
for (int i = 0; i < hits.Length(); i++)
{
Document doc1 = hits.Doc(i);
J = i;
}
lblRep.Text = J.ToString();
} |