Sitecore 6.6 and Lucene upgrade issue - lucene

We recently upgraded to Sitecore 6.6 and are running into issues with the search and crawl functionality from Lucene since 6.6 uses a newer version and has some of the methods/functions have been made obsolete.
The code below used to work fine with the previous version of Lucene.NET 2.3 but isn't working in 2.9. Can you tell us what we are doing wrong and help us rectify this piece of code? The error we get while compiling is
`Lucene.Net.Search.IndexSearcher` does not contain a definition for 'Search'
and no extension method 'Search' accepting a first argument of type
`Lucene.Net.Search.IndexSearcher` could be found (are you missing a using
directive or an assembly reference?)
This error happens on this line - Sitecore.Search.SearchHits hits = new SearchHits(context.Searcher.Search(query,sort));. I'm guessing that it will be a simple fix but I'm unsure of how to go about fixing it.
private static SearchResultCollection GetSearchResults(Query query, Sort sort, int startingIndex, int getCount, out int totalHits)
{
SearchResultCollection retVal = new SearchResultCollection();
Sitecore.Search.Index searchIndex = Sitecore.Search.SearchManager.GetIndex("content");
using (Sitecore.Search.IndexSearchContext context = searchIndex.CreateSearchContext())
{
Sitecore.Search.SearchHits hits = new SearchHits(context.Searcher.Search(query,sort));
totalHits = hits.Length;
//since index is zero based... adjust the numbers
startingIndex = (startingIndex - 1) * getCount;
getCount = (getCount > totalHits || totalHits < startingIndex + getCount)
? hits.Length - startingIndex : getCount;
retVal = hits.FetchResults(startingIndex, getCount);
}
return retVal;
}
Thanks

Sitecore 6.6 uses Lucene 2.9. The code below is your code updated to support the newer version of Lucene. There are 2 major changes:
Search method is executed with 2 additional parameters (Filter which is set to null and maxDocs which is set to int.MaxValue).
SearchHits constructor takes IndexReader instance as the second parameter.
Code below should work exactly as you expect.
using (Sitecore.Search.IndexSearchContext context = searchIndex.CreateSearchContext())
{
TopFieldDocs docs = context.Searcher.Search(query, null, int.MaxValue, sort);
Sitecore.Search.SearchHits hits = new SearchHits(docs, context.Searcher.GetIndexReader());
totalHits = hits.Length;
startingIndex = (startingIndex - 1) * getCount;
getCount = (getCount > totalHits || totalHits < startingIndex + getCount) ? hits.Length - startingIndex : getCount;
retVal = hits.FetchResults(startingIndex, getCount);
}

Not terribly familiar with Sitecore, but Searcher.search(Query, Sort) was deprecated in Lucene 2.9, and looks like wasn't present at all in Lucene.Net. Instead, call Searcher.search(Query, Filter, int, Sort). The second argument (Filter) can be null, and the third (int) indicates the number of documents to return from the search.

Related

Java executed statement not returning string data in resultset

I have a simple SQL code that returns one record but when I execute it from Java, it does not return the string portions of the record, only numerical. The fields are VARCHAR2 but do not get extracted into my resultset. Following is the code. The database connectivity portion has been edited out for posting in the forum but it does connect. I have also attached the output. Any guidance would be appreciated as my searches on the web have returned empty. -Greg
package testsql;
import java.sql.*;
public class TestSQL {
String SQLtracknbr;
int SQLtracklength;
int numberOfColumns;
String coltypename;
int coldispsize;
String SQLschemaname;
public static void main(String[] args)
throws ClassNotFoundException, SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
String url = "jdbc:oracle:thin:#oracam.corp.mot.com:1522:oracam";
String SQLcode = "select DISTINCT tracking_number from sfc_unit_process_track where tracking_number = 'CAH15F6WW9'";
System.out.println(SQLcode);
Connection conn =
DriverManager.getConnection(url,"report","report");
conn.setAutoCommit(false);
try (Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery(SQLcode)) {
ResultSetMetaData rsmd = rset.getMetaData();
while (rset.next()) {
int numberOfColumns = rsmd.getColumnCount();
boolean b = rsmd.isSearchable(1);
String coltypename = rsmd.getColumnTypeName(1);
int coldispsize = rsmd.getColumnDisplaySize(1);
String SQLschemaname = rsmd.getSchemaName(1);
String SQLtracknbr = rset.getString(1);
int SQLtracklength = SQLtracknbr.length();
if (SQLtracknbr == null)
System.out.println("NULL**********************.");
else
System.out.println("NOT NULL.");
System.out.println("numberOfColumns = " + numberOfColumns);
System.out.println("column type = " + coltypename);
System.out.println("column display size = " + coldispsize);
System.out.println("tracking_number = " + SQLtracknbr);
System.out.println("track number length = " + SQLtracklength);
System.out.println("schema name = " + SQLschemaname);
}
}
System.out.println ("*******End of code*******");
}
}
The result of what is executed in Java is below:
run:
select DISTINCT tracking_number from sfc_unit_process_track where tracking_number = 'CAH15F6WW9'
NOT NULL.
numberOfColumns = 1
column type = VARCHAR2
column display size = 30
tracking_number =
track number length = 0
schema name =
*******End of code*******
BUILD SUCCESSFUL (total time: 0 seconds)
This seems to be caused by an incompatibility between the driver you're using, ojdbc7.jar, and the version of the database you're connecting to, 9i.
According to the JDBC FAQ section "What are the various supported Oracle database version vs JDBC compliant versions vs JDK version supported?", the JDK 7/8 driver ojdbc7.jar that you're using is only support for Oracle 12c.
Oracle generally only support client/server versions two release apart (see My Oracle Support note 207303.1), and the Oracle 12c and 9i client and server have never been supported either way around. JDBC is slightly different of course, but it may be related, as drivers are installed with the Oracle software.
You will have to upgrade your database to a supported version, or - perhaps more practically in the short term - use an earlier driver. The Wayback Machine snapshot of the JDBC FAQ from 2013 says the 11.2.0 JDBC drivers - which includes ojdbc6.jar and ojcbd5.jar - can talk to RDBMS 9.2.0. So either of those ought to work...

SearchManager.GetIndex is not working - using lucene search

I am applying search functionality in my system using Sitecore 7.0 (actually I am converting my code from Sitecore 6.5 to sitecore 7.0). When I try to get index using Sitecore.Search.SearchManager.GetIndex method, I find configuration property with null value.
My sample code in 6.5 is as below
webDb = Sitecore.Context.Database;
Sitecore.Data.Indexing.Index indx = webDb.Indexes["system"]; //Getting warning - deprecated method
Item bucketItem = Sitecore.Context.Item;
IndexSearcher indexSearcher = indx.GetSearcher(webDb);
topDocs = GetContent(keyWords, webDb, indexSearcher, year, regionName);
if (topDocs != null)
{
int totalMatchItemCount = topDocs.TotalHits;
if (totalMatchItemCount > 0)
{
returnValues = new Item[totalMatchItemCount];
int i = 0;
foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs)
{
Document doc = indexSearcher.Doc(scoreDoc.Doc);
Item item = Index.GetItem(doc, webDb);//Getting warning - deprecated method
returnValues[i++] = item;
}
}
}
Its works fine but gives error of deprecated method on below lines,
Sitecore.Data.Indexing.Index indx = webDb.Indexes["system"];
and
Item item = Index.GetItem(doc, webDb);
My converted code of Sitecore 7.0 is as below,
var children = new List<Item>();
Sitecore.Search.Index searchIndx = Sitecore.Search.SearchManager.GetIndex("system");//Shows SearchManager._Configuration with NULL value, hence all methods and property getting with exception.
using (var searchContext = searchIndx.CreateSearchContext())
{
var ftQuery = new Sitecore.Search.FullTextQuery(keyWords);
var hits = searchContext.Search(ftQuery);
var results = hits.FetchResults(0, hits.Length);
foreach (Sitecore.Search.SearchResult result in results)
{
//My stuff
}
}
When I am trying to fetch value using Sitecore 7.0, get the below exception
Could not create instance of type: Lucene.Net.Analysis.Standard.StandardAnalyzer. No matching constructor was found.
Thanks.
I have a feeling this is because you're still using the pre-Sitecore 7 Sitecore.Search API rather than the newer Sitecore.ContentSearch.
Try this for some further help: Searching with the new Sitecore 7 API
Use below code to get index in Sitecore 7.
// Index
public static string IndexName
{
get
{
return (Sitecore.Context.Database.Name.ToLower()) == "master" ? "sitecore_master_index" : "sitecore_web_index";
}
}
public static ISearchIndex _index;
public static ISearchIndex Index
{
get
{
if (_index == null) { _index = ContentSearchManager.GetIndex(IndexName); }
return _index;
}
}
Could not create instance of type:
Lucene.Net.Analysis.Standard.StandardAnalyzer. No matching constructor
was found.
I've seen this error occur when there are DLLs from multiple Sitecore versions in your /bin folder, so I'd agree with #TwentyGotoTen: it sounds like you either missed out a step in the upgrade process, or you need to check your deploy process to make sure your solution isn't referencing older versions of Sitecore assemblies.

Lucene - Iterating through TermsEnum for docfreq

I am trying to get the doc frequency for each term in term enum. But I getting everytime only a "1" for the document frequency for all terms. Any hint, what the problem could be? This is my code:
Terms terms = reader.getTermVector(docId, field);
TermsEnum termsEnum = null;
termsEnum = terms.iterator(termsEnum);
BytesRef termText = null;
while((termsEnum.next()) != null){
int docNumbersWithTerm = termsEnum.docfreq();
System.out.println(docNumbersWithTerm);
}
The Terms instance from IndexReader.getTermVector acts as if you have a single-document index, comprised entirely of the document specified. Since there is only one document to consider in this context, you should always get docfreq() = 1. You could generate the docfreq from the full index using the IndexReader.docFreq method:
int docNumbersWithTerm = reader.docFreq(new Term(termsEnum.term(), field));
System.out.println(docNumbersWithTerm);

Find list of terms indexed by Lucene

Is it possible to extract the list of all the terms in a Lucene index as a list of strings? I couldn't find that functionality in the doc. Thanks!
In Lucene 4 (and 5):
Terms terms = SlowCompositeReaderWrapper.wrap(directoryReader).terms("field");
Edit:
This seems to be the 'correct' way now (Lucene 6 and up):
LuceneDictionary ld = new LuceneDictionary( indexReader, "field" );
BytesRefIterator iterator = ld.getWordsIterator();
BytesRef byteRef = null;
while ( ( byteRef = iterator.next() ) != null )
{
String term = byteRef.utf8ToString();
}
Lucene 3:
C#: C# Lucene get all the index
Java:
IndexReader indexReader = IndexReader.open(path);
TermEnum termEnum = indexReader.terms();
while (termEnum.next()) {
Term term = termEnum.term();
System.out.println(term.text());
}
termEnum.close();
indexReader.close();
Java (all terms for a specific field): How can I get the list of unique terms from a specific field in Lucene?
Python: Finding a single fields terms with Lucene (PyLucene)

NHibernate.Search - SQL Server 2005 - hitting max parameter limit 2100 !

I am using NHibernate.Search libraries in my project for free text search. Recently when I started getting more than 2100 results, I started getting max parameter length error from SQL Server.
Does NHibernate.Search take care of such situation ? Any workaround anyone ?
You could modify NHibernate.Search code to take care of this, or, use custom paging, IE get number of hits for your search, then page nhibernate search results accordingly.
public IList<TEntity> Search<TEntity>(Query query, bool? active, string orderBy)
{
var search = NHibernate.Search.Search.CreateFullTextSession(this.session);
var total = search.CreateFullTextQuery(query, typeof(TEntity)).ResultSize;
var first = 0;
var l = new List<TEntity>();
while (total > 0)
{
l.AddRange(search.CreateFullTextQuery(query, typeof(TEntity))
.SetFirstResult(first)
.SetMaxResults(1000)
.List<TEntity>());
first += 1000;
total -= 1000;
}
return l;
}
See : IFullTextQuery - exception if there are too may objects