mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
#19193: Upgrading to Lucene.NET 3.0.3
Work Item: 19193 --HG-- branch : 1.x
This commit is contained in:
Binary file not shown.
@@ -1,7 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using Lucene.Net.Documents;
|
using Lucene.Net.Documents;
|
||||||
using System.Globalization;
|
|
||||||
using Lucene.Net.Util;
|
|
||||||
using Orchard.Indexing;
|
using Orchard.Indexing;
|
||||||
|
|
||||||
namespace Lucene.Models {
|
namespace Lucene.Models {
|
||||||
@@ -20,26 +18,26 @@ namespace Lucene.Models {
|
|||||||
|
|
||||||
public int GetInt(string name) {
|
public int GetInt(string name) {
|
||||||
var field = _doc.GetField(name);
|
var field = _doc.GetField(name);
|
||||||
return field == null ? 0 : Int32.Parse(field.StringValue());
|
return field == null ? 0 : Int32.Parse(field.StringValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double GetDouble(string name) {
|
public double GetDouble(string name) {
|
||||||
var field = _doc.GetField(name);
|
var field = _doc.GetField(name);
|
||||||
return field == null ? 0 : double.Parse(field.StringValue());
|
return field == null ? 0 : double.Parse(field.StringValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool GetBoolean(string name) {
|
public bool GetBoolean(string name) {
|
||||||
return GetInt(name) > 0 ? true : false;
|
return GetInt(name) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetString(string name) {
|
public string GetString(string name) {
|
||||||
var field = _doc.GetField(name);
|
var field = _doc.GetField(name);
|
||||||
return field == null ? null : field.StringValue();
|
return field == null ? null : field.StringValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DateTime GetDateTime(string name) {
|
public DateTime GetDateTime(string name) {
|
||||||
var field = _doc.GetField(name);
|
var field = _doc.GetField(name);
|
||||||
return field == null ? DateTime.MinValue : DateTools.StringToDate(field.StringValue());
|
return field == null ? DateTime.MinValue : DateTools.StringToDate(field.StringValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -89,14 +89,9 @@ namespace Lucene.Services {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var reader = IndexReader.Open(GetDirectory(indexName), true);
|
using (var reader = IndexReader.Open(GetDirectory(indexName), true)) {
|
||||||
|
|
||||||
try {
|
|
||||||
return reader.NumDocs() == 0;
|
return reader.NumDocs() == 0;
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
reader.Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int NumDocs(string indexName) {
|
public int NumDocs(string indexName) {
|
||||||
@@ -104,21 +99,14 @@ namespace Lucene.Services {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var reader = IndexReader.Open(GetDirectory(indexName), true);
|
using (var reader = IndexReader.Open(GetDirectory(indexName), true)) {
|
||||||
|
|
||||||
try {
|
|
||||||
return reader.NumDocs();
|
return reader.NumDocs();
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
reader.Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateIndex(string indexName) {
|
public void CreateIndex(string indexName) {
|
||||||
var writer = new IndexWriter(GetDirectory(indexName), _analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
|
using (new IndexWriter(GetDirectory(indexName), _analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED)) {
|
||||||
writer.Close();
|
}
|
||||||
|
|
||||||
Logger.Information("Index [{0}] created", indexName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteIndex(string indexName) {
|
public void DeleteIndex(string indexName) {
|
||||||
@@ -135,6 +123,8 @@ namespace Lucene.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Store(string indexName, IEnumerable<LuceneDocumentIndex> indexDocuments) {
|
public void Store(string indexName, IEnumerable<LuceneDocumentIndex> indexDocuments) {
|
||||||
|
indexDocuments = indexDocuments.ToArray();
|
||||||
|
|
||||||
if (!indexDocuments.Any()) {
|
if (!indexDocuments.Any()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -142,26 +132,14 @@ namespace Lucene.Services {
|
|||||||
// Remove any previous document for these content items
|
// Remove any previous document for these content items
|
||||||
Delete(indexName, indexDocuments.Select(i => i.ContentItemId));
|
Delete(indexName, indexDocuments.Select(i => i.ContentItemId));
|
||||||
|
|
||||||
var writer = new IndexWriter(GetDirectory(indexName), _analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
|
using(var writer = new IndexWriter(GetDirectory(indexName), _analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED)) {
|
||||||
LuceneDocumentIndex current = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
foreach (var indexDocument in indexDocuments) {
|
foreach (var indexDocument in indexDocuments) {
|
||||||
current = indexDocument;
|
|
||||||
var doc = CreateDocument(indexDocument);
|
var doc = CreateDocument(indexDocument);
|
||||||
|
|
||||||
writer.AddDocument(doc);
|
writer.AddDocument(doc);
|
||||||
Logger.Debug("Document [{0}] indexed", indexDocument.ContentItemId);
|
Logger.Debug("Document [{0}] indexed", indexDocument.ContentItemId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
|
||||||
Logger.Error(ex, "An unexpected error occured while add the document [{0}] from the index [{1}].", current.ContentItemId, indexName);
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
writer.Optimize();
|
|
||||||
writer.Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Delete(string indexName, int documentId) {
|
public void Delete(string indexName, int documentId) {
|
||||||
@@ -169,18 +147,18 @@ namespace Lucene.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Delete(string indexName, IEnumerable<int> documentIds) {
|
public void Delete(string indexName, IEnumerable<int> documentIds) {
|
||||||
|
documentIds = documentIds.ToArray();
|
||||||
|
|
||||||
if (!documentIds.Any()) {
|
if (!documentIds.Any()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var writer = new IndexWriter(GetDirectory(indexName), _analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
|
using(var writer = new IndexWriter(GetDirectory(indexName), _analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED)) {
|
||||||
|
|
||||||
try {
|
|
||||||
var query = new BooleanQuery();
|
var query = new BooleanQuery();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
foreach (var id in documentIds) {
|
foreach (var id in documentIds) {
|
||||||
query.Add(new BooleanClause(new TermQuery(new Term("id", id.ToString())), BooleanClause.Occur.SHOULD));
|
query.Add(new BooleanClause(new TermQuery(new Term("id", id.ToString())), Occur.SHOULD));
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.DeleteDocuments(query);
|
writer.DeleteDocuments(query);
|
||||||
@@ -189,9 +167,6 @@ namespace Lucene.Services {
|
|||||||
Logger.Error(ex, "An unexpected error occured while removing the documents [{0}] from the index [{1}].", String.Join(", ", documentIds), indexName);
|
Logger.Error(ex, "An unexpected error occured while removing the documents [{0}] from the index [{1}].", String.Join(", ", documentIds), indexName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
writer.Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDocumentIndex New(int documentId) {
|
public IDocumentIndex New(int documentId) {
|
||||||
@@ -207,14 +182,9 @@ namespace Lucene.Services {
|
|||||||
return Enumerable.Empty<string>();
|
return Enumerable.Empty<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
var reader = IndexReader.Open(GetDirectory(indexName), true);
|
using(var reader = IndexReader.Open(GetDirectory(indexName), true)) {
|
||||||
|
|
||||||
try {
|
|
||||||
return reader.GetFieldNames(IndexReader.FieldOption.ALL).ToList();
|
return reader.GetFieldNames(IndexReader.FieldOption.ALL).ToList();
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
reader.Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -30,7 +29,7 @@ namespace Lucene.Services {
|
|||||||
private bool _asFilter;
|
private bool _asFilter;
|
||||||
|
|
||||||
// pending clause attributes
|
// pending clause attributes
|
||||||
private BooleanClause.Occur _occur;
|
private Occur _occur;
|
||||||
private bool _exactMatch;
|
private bool _exactMatch;
|
||||||
private float _boost;
|
private float _boost;
|
||||||
private Query _query;
|
private Query _query;
|
||||||
@@ -53,7 +52,7 @@ namespace Lucene.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ISearchBuilder Parse(string defaultField, string query, bool escape) {
|
public ISearchBuilder Parse(string defaultField, string query, bool escape) {
|
||||||
return Parse(new[] { defaultField }, query, escape);
|
return Parse(new[] {defaultField}, query, escape);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISearchBuilder Parse(string[] defaultFields, string query, bool escape) {
|
public ISearchBuilder Parse(string[] defaultFields, string query, bool escape) {
|
||||||
@@ -135,12 +134,12 @@ namespace Lucene.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ISearchBuilder Mandatory() {
|
public ISearchBuilder Mandatory() {
|
||||||
_occur = BooleanClause.Occur.MUST;
|
_occur = Occur.MUST;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISearchBuilder Forbidden() {
|
public ISearchBuilder Forbidden() {
|
||||||
_occur = BooleanClause.Occur.MUST_NOT;
|
_occur = Occur.MUST_NOT;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,7 +154,7 @@ namespace Lucene.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void InitPendingClause() {
|
private void InitPendingClause() {
|
||||||
_occur = BooleanClause.Occur.SHOULD;
|
_occur = Occur.SHOULD;
|
||||||
_exactMatch = false;
|
_exactMatch = false;
|
||||||
_query = null;
|
_query = null;
|
||||||
_boost = 0;
|
_boost = 0;
|
||||||
@@ -172,13 +171,13 @@ namespace Lucene.Services {
|
|||||||
// comparing floating-point numbers using an epsilon value
|
// comparing floating-point numbers using an epsilon value
|
||||||
const double epsilon = 0.001;
|
const double epsilon = 0.001;
|
||||||
if (Math.Abs(_boost - 0) > epsilon) {
|
if (Math.Abs(_boost - 0) > epsilon) {
|
||||||
_query.SetBoost(_boost);
|
_query.Boost = _boost;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_exactMatch) {
|
if (!_exactMatch) {
|
||||||
var termQuery = _query as TermQuery;
|
var termQuery = _query as TermQuery;
|
||||||
if (termQuery != null) {
|
if (termQuery != null) {
|
||||||
var term = termQuery.GetTerm();
|
var term = termQuery.Term;
|
||||||
// prefixed queries are case sensitive
|
// prefixed queries are case sensitive
|
||||||
_query = new PrefixQuery(term);
|
_query = new PrefixQuery(term);
|
||||||
}
|
}
|
||||||
@@ -259,14 +258,16 @@ namespace Lucene.Services {
|
|||||||
Query resultQuery = booleanQuery;
|
Query resultQuery = booleanQuery;
|
||||||
|
|
||||||
if (_clauses.Count == 0) {
|
if (_clauses.Count == 0) {
|
||||||
if (_filters.Count > 0) { // only filters applieds => transform to a boolean query
|
if (_filters.Count > 0) {
|
||||||
|
// only filters applieds => transform to a boolean query
|
||||||
foreach (var clause in _filters) {
|
foreach (var clause in _filters) {
|
||||||
booleanQuery.Add(clause);
|
booleanQuery.Add(clause);
|
||||||
}
|
}
|
||||||
|
|
||||||
resultQuery = booleanQuery;
|
resultQuery = booleanQuery;
|
||||||
}
|
}
|
||||||
else { // search all documents, without filter or clause
|
else {
|
||||||
|
// search all documents, without filter or clause
|
||||||
resultQuery = new MatchAllDocsQuery(null);
|
resultQuery = new MatchAllDocsQuery(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -302,11 +303,11 @@ namespace Lucene.Services {
|
|||||||
return Enumerable.Empty<ISearchHit>();
|
return Enumerable.Empty<ISearchHit>();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
using (searcher) {
|
||||||
var sort = String.IsNullOrEmpty(_sort)
|
var sort = String.IsNullOrEmpty(_sort)
|
||||||
? Sort.RELEVANCE
|
? Sort.RELEVANCE
|
||||||
: new Sort(new SortField(_sort, _comparer, _sortDescending));
|
: new Sort(new SortField(_sort, _comparer, _sortDescending));
|
||||||
var collector = TopFieldCollector.create(
|
var collector = TopFieldCollector.Create(
|
||||||
sort,
|
sort,
|
||||||
_count + _skip,
|
_count + _skip,
|
||||||
false,
|
false,
|
||||||
@@ -318,18 +319,14 @@ namespace Lucene.Services {
|
|||||||
searcher.Search(query, collector);
|
searcher.Search(query, collector);
|
||||||
|
|
||||||
var results = collector.TopDocs().ScoreDocs
|
var results = collector.TopDocs().ScoreDocs
|
||||||
.Skip(_skip)
|
.Skip(_skip)
|
||||||
.Select(scoreDoc => new LuceneSearchHit(searcher.Doc(scoreDoc.doc), scoreDoc.score))
|
.Select(scoreDoc => new LuceneSearchHit(searcher.Doc(scoreDoc.Doc), scoreDoc.Score))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
Logger.Debug("Search results: {0}", results.Count);
|
Logger.Debug("Search results: {0}", results.Count);
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
searcher.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Count() {
|
public int Count() {
|
||||||
@@ -345,16 +342,12 @@ namespace Lucene.Services {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
using (searcher) {
|
||||||
var hits = searcher.Search(query, Int16.MaxValue);
|
var hits = searcher.Search(query, Int16.MaxValue);
|
||||||
Logger.Information("Search results: {0}", hits.ScoreDocs.Length);
|
Logger.Information("Search results: {0}", hits.ScoreDocs.Length);
|
||||||
var length = hits.ScoreDocs.Length;
|
var length = hits.ScoreDocs.Length;
|
||||||
return Math.Min(length - _skip, _count);
|
return Math.Min(length - _skip, _count);
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
searcher.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISearchBits GetBits() {
|
public ISearchBits GetBits() {
|
||||||
@@ -370,30 +363,22 @@ namespace Lucene.Services {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
using (searcher) {
|
||||||
var filter = new QueryWrapperFilter(query);
|
var filter = new QueryWrapperFilter(query);
|
||||||
var bits = filter.GetDocIdSet(searcher.GetIndexReader());
|
var bits = filter.GetDocIdSet(searcher.IndexReader);
|
||||||
var disi = new OpenBitSetDISI(bits.Iterator(), searcher.MaxDoc());
|
var disi = new OpenBitSetDISI(bits.Iterator(), searcher.MaxDoc);
|
||||||
return new SearchBits(disi);
|
return new SearchBits(disi);
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
searcher.Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ISearchHit Get(int documentId) {
|
public ISearchHit Get(int documentId) {
|
||||||
var query = new TermQuery(new Term("id", documentId.ToString(CultureInfo.InvariantCulture)));
|
var query = new TermQuery(new Term("id", documentId.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
|
||||||
var searcher = new IndexSearcher(_directory, true);
|
using (var searcher = new IndexSearcher(_directory, true)) {
|
||||||
try {
|
|
||||||
var hits = searcher.Search(query, 1);
|
var hits = searcher.Search(query, 1);
|
||||||
Logger.Information("Search results: {0}", hits.ScoreDocs.Length);
|
Logger.Information("Search results: {0}", hits.ScoreDocs.Length);
|
||||||
return hits.ScoreDocs.Length > 0 ? new LuceneSearchHit(searcher.Doc(hits.ScoreDocs[0].doc), hits.ScoreDocs[0].score) : null;
|
return hits.ScoreDocs.Length > 0 ? new LuceneSearchHit(searcher.Doc(hits.ScoreDocs[0].Doc), hits.ScoreDocs[0].Score) : null;
|
||||||
}
|
|
||||||
finally {
|
|
||||||
searcher.Close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user