Developer forum

Forum » Development » Query content index DW9 using API

Query content index DW9 using API

Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Im trying to use the API to fetch data from the index by looking at this code example.
http://developer.dynamicweb-cms.com/documentation/for-developers/ecommerce/indexed-search/search-query-syntax.aspx

Unfortunatly it does not look like this example is valid for DW9? At least I cant find any reference to Dynamicweb.Searching.
Dont know if its because of DW9 or if im missing some DLL reference.

Also the code example is for querying products index, im trying to query content index. Does anyone have a code example for that?


Replies

 
Kevin Steffer
Kevin Steffer
Reply

I've found the Searching namespace inside the Dynamicweb.Indexing.dll

 
Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Strange, I cant find it myself.

https://www.screencast.com/t/KkEV7dD2lknw
https://www.screencast.com/t/QHfh28TIU​

Does anyone have any code examples?

 
Kevin Steffer
Kevin Steffer
Reply
This post has been marked as an answer

Something like this?

var queryService = Dynamicweb.Extensibility.ServiceLocator.Current.GetInstance<Dynamicweb.Indexing.Querying.IQueryService>();
Dynamicweb.Indexing.Querying.QuerySettings querySettings = new Dynamicweb.Indexing.Querying.QuerySettings();

System.Collections.Generic.Dictionary<string, object> parameters = new System.Collections.Generic.Dictionary<string, object>();
parameters.Add("<PARAM 1>", param1Value);

querySettings.Parameters = parameters;
Dynamicweb.Indexing.Querying.IQueryResult result = queryService.Query(queryService.LoadQuery("<YOUR REPOSITORY>", "<YOUR QUERY>.query"), querySettings);
Votes for this answer: 1
 
Nicolai Pedersen
Reply

Hi Kevin

Yes, that is how you use the 'new' index - and the way you should go @Martin.

Your original post refers to the old index which no longer exists (in DW9).

BR Nicolai

 
Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Perfect! I'll give it a try, thanks :)

 
Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Regarding the query result. I get the result in "plain text" if I do it like this.

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var json = serializer.Serialize(result);

But what datatype is the result? How can I iterate throughthe result in a strongly typed way?

 
Nicolai Pedersen
Reply

Hi Martin

The result has a QueryResult property - that is what you should look at.

So something like this:

foreach (object resultObject in result.QueryResult) {
        IDictionary dictionary = resultObject as IDictionary;
        if (dictionary != null) {
            foreach (object key in dictionary.Keys) {
                if ((key != null)) {
                    // something                
                }
            }
        }
    }
 
Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Nice, thanks! Just had to do one slight adjustment to make it work.

IDictionary<string, object> dictionary = resultObject as IDictionary<string, object>;

 

You must be logged in to post in the forum