全文检索
Posted menbe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了全文检索相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 盘古分词搜索
/// </summary>
/// <param name="keyWords">关键字</param>
[HttpGet]
public IEnumerable<Goods> Search(string keyWord)
{
IList<string> str = new List<string>();
if (keyWord != null)
{
if (Session["str"]!=null)
{
str = Session["str"] as List<string>;
str.Add(keyWord);
Session["str"] = str;
}
else
{
str.Add(keyWord);
Session["str"] = str; //保存文本框值
}
}
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(TestIndexPath), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
//用BooleanQuery把多个查询条件拼接起来成为一个大的查询条件
BooleanQuery query = new BooleanQuery();
BooleanClause clause = new BooleanClause(query, Occur.SHOULD);
clause.Occur = Occur.SHOULD;
//多条件查询
//搜索条件
PhraseQuery queryTitle = new PhraseQuery();
//把用户输入的“北京是首都”分词为“北京 是 首都”三个词,然后添加查询条件
foreach (string word in LuceneHealper.SplitWords(keyWord))
{
queryTitle.Add(new Term("G_Name", word));
}
query.Add(queryTitle, clause.Occur);//可以有
//多个查询条件的词之间的最大距离。在文章中相隔太远一般也就无意义
//搜索条件
PhraseQuery queryContent = new PhraseQuery();
//把用户输入的“北京是首都”分词为“北京 是 首都”三个词,然后添加查询条件
foreach (string word in LuceneHealper.SplitWords(keyWord))
{
queryContent.Add(new Term("G_Title", word));
}
query.Add(queryContent, clause.Occur);//可以有
//create 一个存储查询结果的容器
TopScoreDocCollector collector = TopScoreDocCollector.Create(100, true);
searcher.Search(query, null, collector);
ScoreDoc[] docs = collector.TopDocs().ScoreDocs; //得到所有查询结果中的文档
List<Goods> list = new List<Goods>();
foreach (ScoreDoc doc in docs)
{
int docID = doc.Doc; //得到查询结果文档的id(Lucene内部分配的id)
Document document = searcher.Doc(docID); //根据ID找到对应的Document
Goods story = new Goods();
story.GId = Convert.ToInt32(document.Get("GId"));
story.G_Name = document.Get("G_Name");
story.G_GoodsImg = document.Get("G_GoodsImg");
story.G_Title = document.Get("G_Title");
story.G_Price = decimal.Parse(document.Get("G_Price"));
list.Add(story);
}
return list.ToList();
}
/// <summary>
/// 创建索引
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CreateIndex()
{
//索引存放的物理路径
//this.CreateDirectory(); //给 indexPath 赋值
using (FSDirectory directory = FSDirectory.Open(new DirectoryInfo(TestIndexPath), new NativeFSLockFactory()))
{
bool isUpdate = IndexReader.IndexExists(directory); //判断索引库文件夹存在并且存在索引库特征文件
if (isUpdate)
{
//同时只能有一段代码对索引库进行写操作!当使用IndexWriter打开directory的时候会自动给索引库上锁。!!!
//如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁
if (IndexWriter.IsLocked(directory)) //如果索引库文件被锁定了 解锁
{
IndexWriter.Unlock(directory);
}
}
using (IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, IndexWriter.MaxFieldLength.UNLIMITED))
{
string str = Client.GetApi("get", "GetGoods");
List<Goods> list = JsonConvert.DeserializeObject<List<Goods>>(str);
foreach (Goods story in list)
{
writer.DeleteDocuments(new Term("GId", story.GId.ToString()));
Document document = new Document(); //一篇文章,一部小说
//要进行全文检索的字段要设置 Field.Index.ANALYZED !!!!!!!!!!!!!!!!!!!!!!!!!!
document.Add(new Field("GId", story.GId.ToString(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
document.Add(new Field("G_GoodsImg", story.G_GoodsImg, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("G_Remark", story.G_Remark, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("G_Name", story.G_Name, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("G_Price", story.G_Price.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES));
document.Add(new Field("G_Title", story.G_Title.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES));
document.Add(new Field("G_TypeId", story.G_TypeId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES));
document.Add(new Field("G_BrandId", story.G_BrandId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES));
document.Add(new Field("G_Area", story.G_Area.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES));
writer.AddDocument(document);
}
}
}
<div class="search-bar pr">
<a name="index_none_header_sysc" href="#"></a>
<form method="get" action="/UserLogin/Particulars">
<input id="keyWord" name="keyWord" type="text" placeholder="搜索" autocomplete="off" />
<input id="ai-topsearch" class="submit am-btn" value="搜索" index="1" type="submit" />
</form>
</div>
以上是关于全文检索的主要内容,如果未能解决你的问题,请参考以下文章