Developer forum

Forum » Development » Reading from an index?

Reading from an index?

Kasper Laursen
Reply

This seems like a very trivial question, but I have been unable to figure out how to perform even the simplest of queries against an index using code.

The simplest of pointers or any link to documentation on this would be much appreciated.

 

Best Regards,

Kasper


Replies

 
Nicolai Pedersen
Reply

Hi Kasper

Maybe you can ellaborate on what you need to do - then we might be able to send you in the right direction.

The index in Dynamicweb is just a Lucene index - so you can access that directly and do whatever you want using the Lucene API. That will give you full control. It is Lucene 3.03: https://lucenenet.apache.org/docs/3.0.3/Index.html

Something like this:

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true

    QueryParser parser = new QueryParser("fieldname", analyzer);
    Query query = parser.parse("text");
    ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;

    for (int i = 0; i < hits.length; i++) {
      Document hitDoc = isearcher.doc(hits[i].doc);
      var fieldValue = hitDoc.get("fieldname");
    }
    isearcher.close();
    directory.close();

But I am not sure that is what you want to do.

Dynamicweb has an abstraction on top of the index that handles a lot of the infrastructure, and you can utilize that using the query publisher: https://doc.dynamicweb.com/documentation-9/indexing/apps/query-publisher

Our implementation gives you access to a number of things - examples can be found here: https://doc.dynamicweb.com/training/t3-platform-developer/t3-platform-developer/3-5-extending-indexing

BR Nicolai

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi Kasper,

If you want a more Dynamicweb centric solution, here's an example of how to query an index using Dynamicweb APIs.

public void DoSearch(string queryPath)
{
	var queryService = ServiceLocator.Current.GetInstance<IQueryService>();
	var query = queryService.LoadQuery(queryPath);
	var settings = new QuerySettings
	{
		Skip = 30,
		Take = 30,
		Parameters = new Dictionary<string, object>
		{
			{ "ProductID", new[] { "PROD10", "PROD11", "PROD12", "PROD13", "PROD14" } },
			{ "LanguageID", "LANG2" }
		}
	};
	var result = queryService.Query(query, settings);

	var autoIds = new List<long>();
	foreach (Dictionary<string, object> document in result.QueryResult)
	{
		var productAutoId = (long)document["AutoID"];
		autoIds.Add(productAutoId);
	}
	var products = Services.Products.GetByAutoIDs(autoIds);
}

This example reads from a given index using a query, the queryPath must be the full path to a Dynamicweb query.

It is possible to create a query using code, but it's more involved, so I've omitted an example. If this is what you're looking for, please let me know.

- Jeppe

Votes for this answer: 1
 
Kasper Laursen
Reply

Thanks for the replies.

I just found this thread dealing with the same: http://doc.dynamicweb.com/forum/development/query-content-index-dw9-using-api?PID=1605

I would have expected to find something like this somewhere in the documentation, but wasn't able to.

 

In any case. The Query Publisher is certainly useful, but not what I need right now. It is also well documented! :)

It's also very good to know that I can actually access the Lucene index directly if I wanted to, but since I am working inside Dynamicweb I'd like to stay with the libraries you have provided.

 

Best regards,

Kasper

 

You must be logged in to post in the forum