Jon
Jon

Reputation: 13

Can someone help me add a new column in Google cloud Search Index using Java

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.

Upvotes: 0

Views: 385

Answers (1)

lostpebble
lostpebble

Reputation: 338

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-

Upvotes: 0

Related Questions