Can someone help me add a new column in Google cloud Search Index using Java - google-app-engine

I tried adding a new column to an existing search index but it is throwing an error, the error as stated below:-
Field docname is present 0 times; expected 1
java.lang.IllegalArgumentException: Field docname is present 0 times; expected 1
I can see that the new column has been added to the search index but cannot retrieve the index.
By my observation i can see that the existing records in the index dont have the new column data and hence it is giving a this error, but the new records will be having this column values. Can anyone help me with this.

Upon having this problem myself today, I searched a bit in the documentation. It was a rather frustrating error as it didn't actually pin point where the problem was in my code.
It appears that when you use getOnlyField("something") on a Document (in this case on one of many returned from a search query), if that field does not actually exist yet in that specific document it throws the java.lang.IllegalArgumentException.
Because that can often be the case when you update an index with new columns, I'm using something like this to get around it:
public static Long getNumberField(ScoredDocument d, String name, Long defaultValue) {
try {
return d.getOnlyField(name).getNumber().longValue();
} catch (IllegalArgumentException e) {
return defaultValue;
}
}
Which is called in the search results code:
Long numberValue = SearchUtils.getNumberField(scoredDocument, "featuredOrder", -1L)
This allows me to catch that error and return a default value when it doesn't exist.
You can find the documentation here:
https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/search/Document.html#getOnlyField-java.lang.String-

Related

Getting null value when accessing fields from result in Searcher in Vespa

My Searcher is given below. When I hit search API then in my Searcher, I am getting null values when I tried to get fields of document. I am able to get id, relavance and source but not fields. Why is it so?Am I doing something wrong? Please help.
#Override
public Result search(Query query, Execution execution) {
// pass it down the chain to get a result
Result result = execution.search(query);
String title = result.hits().get(0).getField("title");
System.out.println(title);
// return the result up the chain
return result;
}
I am getting null value in title.
This is because results are initially surfaced without field data added (for performance).
Add
execution.fill(result);
before accessing fields (send the summary class to fill as second argument if you have multiple ones).
Use execution.fill(result) to fill the result before accessing fields. See also https://docs.vespa.ai/documentation/reference/inspecting-structured-data.html

java.lang.NullPointerException when using getCurrentRow in adf

I am developing a mater-detail page where I can insert a new row into a details table (AstAssetReturnsVO) and set its referential key to the ID of the master table (AstAssetsVO). The code is run as part of a method call activity right before I get to the JSFF call.
For some reason I get the NullPointerException when it gets to the tagged line below and I have no idea why getCurrentRow returns a null pointer. I checked the documentation here and it is supposed to return the current row of the iterator corresponding to the current row of the data control.
Please let me know what I am possibly doing wrong.
Best Regards,
public void assetReturnInitialization(){
getAstAssetReturnsVO().clearCache();
getAstAssetReturnsVO().executeEmptyRowSet();
Row row = getAstAssetReturnsVO().createRow();
row.setAttribute("Stat", 99);
row.setAttribute("AsrtDate", "1396/12/31");
getAstAssetsVO().executeQuery();
Row assetRow = getAstAssetsVO().getCurrentRow(); <<<<<<< ERROR OCCURS HERE
row.setAttribute("AsetId", assetRow.getAttribute("Id"));
getAstAssetsVO().getCurrentRow().setAttribute("Status", "Returned".toUpperCase());
getAstAssetReturnsVO().insertRow(row);
getAstAssetReturnsVO().setCurrentRow(row);
return ;
}
getAstAssetsVO().executeQuery();
Row assetRow = getAstAssetsVO().getCurrentRow(); <<<<<<< ERROR OCCURS HERE
This answer is specific to above two lines of code. I did not review your use case and rest of the code.
After vo.executeQuery() API, row iterator would point to one above the first row. So, you have two options. Either use vo.hasNext() to check if there is next row and loop through the records or if you just need first row, then use vo.first() to get it. Below is the sample code for later case.
getAstAssetsVO().executeQuery();
Row assetRow = getAstAssetsVO().first();
if (assetRow != null) {
//your logic
}

Sitecore search hits are null

I am running the below code
IQueryable<customSearchResultItem> query = context.GetQueryable<customSearchResultItem>().Where(CombinPredicates);
var hits = query.GetResults().Hits;
Sitecore.Diagnostics.Log.Info("search query hits:" + hits.Count(), context.Index);
foreach (var item in hits)
{
string builder = string.Empty;
try
{
string docitemid = item.Document.ItemId.ToString();
Sitecore.Diagnostics.Log.Info("document itemid:" + docitemid, item);
Item resultItem = item.Document.GetItem();
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Info("search error going through hits:" + ex.Message, ex);
builder += item.Document.ItemId.ToString() + "\n";
continue;
}
}
the above actually returns results in the "hits" variable. However, I was getting zero results rendered out to the front end of the site. the code above is my debugging code to see what is going on.
With the hits I get back, I just loop through, nothing more.
When performing the loop and writing to logs, the below 2 lines actually return as expected, a valid Sitecore itemID
string docitemid = item.Document.ItemId.ToString();
Sitecore.Diagnostics.Log.Info("document itemid:" + docitemid, item);
However, the following line
Item resultItem = item.Document.GetItem();
returns as NULL
why would this be?
to complicate further, this code actual works in a one environment but not another, hence the reason for the debugging.
Also, the Sitecore item is present it both master and web databases and in the SOLR index
I've been having a similar problem where I was searching in the Prod (web) environment items indexed via the Stage Sitecore instance. I believe the GetItem() method uses the database, item id, language and version of the search result item, which are parsed from the _uniqueid that looks something like this:
...
"_uniqueid":"sitecore://stage/{d31b5802-c123-4690-86ff-8566dc896814}?lang=en&ver=1&ndx=my_site_index"
...
So in my case it wasn't finding the Sitecore item, because it was looking in the stage database, where as my Sitecore.Context.Database was the web one.
I know this answer is way late :-), but I hope it helps someone else who happens to run into this issue.

Issues while Processing Large Strings in Grails Application

I am working on grails application. I am getting issues in processing string with length greater then 255 characters. When I try to update a record with string greater than 255 characters I get this exception:
nested exception is org.hibernate.exception.DataException: could not update:
Caused by: org.hibernate.exception.DataException: could not update: [com.ef.apps.mediasense.recordings.Calls#1]
Caused by: java.sql.DataTruncation: Data truncation
This is attribute in which I strore large string:
static mapping = {
tag column:'sessionTag'
}
And, This is the constraint I am applying on this:
static constraints = {
tag (nullable:true, maxSize:1000)
}
And, If I see the design of table, It looks like this for this attribute:
So, Everything seems fine then why It's not allowing me to store string having characters greater than 255.
I have tried this approach too, But no Luck:
static mapping = {
tag column:'sessionTag', type: 'text'
}
with constraint:
static constraints = {
tag (nullable:true, maxSize:1000)
}
And If I see the design, It looks like this:
But I am still getting the same issue. I just simply want to store string with greater than 255 characters in Grails. Guide me If I am doing anything wrong or I can achieve this by some other approach.
Thanks for your time and consideration :)

MVC 4 return entries from database excluding specific items

I've been changing this call around for some time and can not quite get it to work. I am trying to return some records from a database and exclude items based on their ID numbers.
This is a database of quotes and when someone hides a quote it stores the quoteID in a cookie (required) and when returning to the site, The cookie.value is read.
I need to filter these IDs out of the return query and display the results in a view using WebGrid. I have the cookie part working. I retrieve the values (could be multiple) and split this into a list of strings. I then run a foreach loop ans parse the string to an integer and then try to remove quotes that I have retrieved from the database. Here is the code as I am using it...
// Filter on Cookie Value using tokenizer
string value = Request.Cookies.Get("hideCookie").Value;
List<string> values = value.Split(' ').ToList();
var quotes2 = db.Quotes.Include(q => q.QName);
foreach (var i in values)
{
int idv = int.Parse(i);
quotes2 = from q in quotes2 where q.QuoteID != idv select q;
}
return View(quotes2.ToList());
This throws an error "A specified Include path is not valid. The EntityType 'Exercise4.Models.Quote' does not declare a navigation property with the name 'QName'." (QName being an entry in the database record).
If I remove the ToList() in the return View(), quotes2 gets passed to the View but throws this error on the webGrid "A specified Include path is not valid. The EntityType 'Exercise4.Models.Quote' does not declare a navigation property with the name 'QName'."
I'm sure there is a much better way of doing this. Can you please point me in the correct direction so I can stop banging my head against my desk...

Resources