ElasticsearchIllegalArgumentException[suggester [completion] requires context to be setup] - elasticsearch

I am using Elastic Search to implement an autosuggest field for an index called people:
The Mapping is as below for the field person_name_suggest -
person_name_suggest: {
type: "completion",
analyzer: "simple",
payloads: true,
preserve_separators: true,
preserve_position_increments: true,
max_input_length: 50,
context: {
office_scope: {
type: "category",
path: "office_scope",
default: [
"0"
]
}
}
},
The request that I need to Elastic Search is as follows:
{
"suggest":{
"suggestions":{
"text":"M","
completion":{
"field":"person_name_suggest",
"context":890,
"size":10
}
}
}
}
I get the following error -
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[OsbgjmewT569a-7ZoNCMtg][people_2016_10_29][0]: SearchParseException[[people_2016_10_29][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"suggest":{"suggestions":{"text":"M","completion":{"field":"person_name_suggest","context":890,"size":10}}}}]]]; nested: ElasticsearchIllegalArgumentException[suggester [completion] requires context to be setup]; }
From what I can see, I have the completion suggester set up right.
Could someone point me in the right direction?
The version of ElasticSearch being used is 1.6

There is a syntax error in query. You have to specify the name of context
Try this
{
"suggest":{
"suggestions":{
"text":"M","
completion":{
"field":"person_name_suggest",
"context":{"office_scope":890},
"size":10
}
}
}
}

Related

Elasticsearch match with filter

I need a query that makes partial match on a string and filter outside documents that have a specific value for a field.
I tried this payload for es:
payload = {
search_request: {
_source: [ 'name', 'source','pg_id' ],
query: {
match: { name: query_string }
bool: {
must_not: {
term: { "source.source": source_value }
}
}
},
size: 100
},
query_hint: query,
algorithm: algorithm,
field_mapping: { title: ["_source.name", "_source.source"]}
}
But ES trows this error:
{
:error=> {
:root_cause=> [
{
:type=>"parse_exception",
:reason=>
"failed to parse search source. expected field name but got [
START_OBJECT
] "}],
:type=>" search_phase_execution_exception",
:reason=>"all shards failed",
:phase=>"query",
:grouped=>true,
:failed_shards=> [
{
:shard=>0,
:index=>"articles",
:node=>"3BUP3eN_TB2-zExigd_k2g",
:reason=> {
:type=>"parse_exception",
:reason=>
"failed to parse search source. expected field name but got [
START_OBJECT
] "
}
}
]
},
:status=>400
}
I am using Elasticsearch 2.4
First of all your json format is not valid. Check for a commas and quotes.
Also if you need just to filtrate documents - filters are much faster than queries. Check documentation

elasticsearch get statistics on analyzed field

i am trying to get statistics on analyzed string field.
i am trying to get AVG length of string field (in this example its title, and title sometimes can be empty/none).
tried:
GET book/_search
{
"facets" : {
"stat1" : {
"statistical" : {
"script" : "_source.title?.length()"
}
}
}
}
and i get an error:
Query Failed [Failed to execute main query]]; nested: NullPointerException; }]",
"status": 500
}
how can i accomplish that?
Any reason why you are using facets and not aggregations? Unless you use an Elasticsearch version that only supports facets, I recommend switching to aggregations. Facets are deprecated in 1.x and removed completely in 2.x.
And an aggregation like this one should work just fine:
GET /book/_search
{
"aggs": {
"stat1": {
"stats": {
"script": "_source.title?.length() ?: 0"
}
}
}
}

PercolateException[failed to percolate]; nested - Percolate API on nested document

I am using elasticsearch 1.1.
Normally in this version, percolator on nested documents should work.
Although, i am trying to do this but I get the following error:
failures: [
{
index: test
shard: 4
reason: BroadcastShardOperationFailedException[[test][4] ]; nested: PercolateException[failed to percolate]; nested: ElasticsearchIllegalArgumentException[Nothing to percolate];
}
]
I have the following percolator (sorry elasticsearch head removed me all the quotes):
{
_index: test
_type: .percolator
_id: 27
_version: 1
_score: 1
_source: {
query: {
filtered: {
query: {
match_all: { }
}
filter: {
nested: {
filter: {
term: {
city: london
}
}
path: location
}
}
}
}
}
}
And while trying to percolate this document I am getting the error:
{
...
"location": {
"date": "2014-05-05T15:07:58",
"namedplaces": {
"city": "london"
}
}
}
Any idea why it doesn't work ?
EDIT :
In elasticsearch log I got more precision about the error:
[2014-05-06 13:33:48,972][DEBUG][action.percolate ] [Tomazooma] [test][2], node[H42BBxajRs2w2NmllMnp7g], [P], s[STARTED]: Failed to execute [org.elasticsearch.action.percolate.PercolateReque
st#7399452e]
org.elasticsearch.percolator.PercolateException: failed to percolate
at org.elasticsearch.action.percolate.TransportPercolateAction.shardOperation(TransportPercolateAction.java:198)
at org.elasticsearch.action.percolate.TransportPercolateAction.shardOperation(TransportPercolateAction.java:55)
at org.elasticsearch.action.support.broadcast.TransportBroadcastOperationAction$AsyncBroadcastAction$2.run(TransportBroadcastOperationAction.java:226)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.elasticsearch.ElasticsearchIllegalArgumentException: Nothing to percolate
at org.elasticsearch.percolator.PercolatorService.percolate(PercolatorService.java:187)
at org.elasticsearch.action.percolate.TransportPercolateAction.shardOperation(TransportPercolateAction.java:194)
... 5 more
The documentation of ES is not really clear about it. But when you look at this page, you will see that when you are percolating you need to surround your indexed document by doc{}. It is indeed compulsory otherwise the exception that you've got will appears:
Try to do so on :
{
"doc":{
...
"location": {
"date": "2014-05-05T15:07:58",
"namedplaces": {
"city": "london"
}
}
}
}
I hope it will help ;-)
Another reason for the Nothing to percolate exception is not setting the Content-Length HTTP header.
Because the GET request has a body, it should also have a 'Content-Length' HTTP header, but not all APIs will set this for you; as I found out the hard way!

Get child where parent id using java api

I have "organizations" index structure :
{
"organization" : {
"properties" : {
"id" : { "type" : "long" },
"name" : { "type" : "string" }
}
}
}
and another index "campaigns" :
{
"campaign" : {
"properties" : {
"id" : { "type" : "long" },
"name" : { "type" : "string" }
},
"_parent" : {
"type" : "organization"
}
}
}
when i search in campaigns that has specific organization id :
QueryBuilder qb = QueryBuilders.boolQuery()
.must(QueryBuilders.hasParentQuery(SearchEntityEnum.ORGANIZATION.getType(), QueryBuilders.queryString("*" + keyword.replaceAll(" ", "* *") + "*").field(SearchEntityFieldNames.CommonFieldNames.NAME)
.analyzeWildcard(true)));
SearchResponse response = client.prepareSearch(SearchEntityEnum.CAMPAIGN.getIndex())
.setTypes(SearchEntityEnum.CAMPAIGN.getType())
.setSearchType(SearchType.QUERY_AND_FETCH)
.setQuery(qb)
.execute().actionGet();
This error occur :
org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase [query_fetch], all shards failed; shardFailures {[-RCGH_JbTmeTGDSvWp9_wA][campaigns][2]: SearchParseException[[campaigns][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }{[-RCGH_JbTmeTGDSvWp9_wA][campaigns][1]: SearchParseException[[campaigns][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }{[-RCGH_JbTmeTGDSvWp9_wA][campaigns][4]: SearchParseException[[campaigns][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }{[-RCGH_JbTmeTGDSvWp9_wA][campaigns][0]: SearchParseException[[campaigns][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }{[-RCGH_JbTmeTGDSvWp9_wA][campaigns][3]: SearchParseException[[campaigns][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }
I had the same problem yesterday. I found out that the parent and child types both need to be in the same index in order for the parent to be visible to the child.
In this example, organization and campaign are different types in different indexes. You will need to create one index with both of these types defined within it. Everything else I believe is good here so let me know if you have any further issues.
I believe (might be wrong) that you should define campaigns as a type within organizations index. also you should set routing to true to make this works.

ElasticSearch sort exception - might be NEST related

We recently upgraded from ES 0.23 to 0.90 and now my queries, eg:
{
"sort": {
"companyname.sort": {
"order": "asc"
}
}
}
cause an exception:
{
error: SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[Y8U7j2f-QvS75-6SfxlGuA][boss][0]:
QueryPhaseExecutionException[[boss][0]:
query[ConstantScore(cache(_type:SalesCompany))],from[0],size[10],sort[<custom:"companyname.sort":
org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource#292f8639>]:
Query Failed [Failed to execute main query]]; nested: NullPointerException; }{[Y8U7j2f-QvS75-6SfxlGuA][boss][1]: QueryPhaseExecutionException[[boss][1]:
query[ConstantScore(cache(_type:SalesCompany))],from[0],size[10],sort[<custom:"companyname.sort":
org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource#292f8639>]: Query Failed [Failed to execute main query]]; nested: NullPointerException; }]
status: 500
}
If I run the same query on our staging environment with an index that was created with 0.23 - but has ES 0.90 installed - it works fine. The mapping is:
companyname: {
type: multi_field
fields: {
companyname: {
type: string
store: true
analyzer: simple
}
sort: {
type: string
index: not_analyzed
store: true
omit_norms: true
index_options: docs
analyzer: simple
include_in_all: false
}
}
}
I'm using the latest version of NEST from a .Net C# app to create the index and query it.
I havent yet tried to recreate the index on the staging environment as I don't want to break that until I know how to fix it :)
Anyone got any ideas?
Thanks
I have recently switched to ES 0.90.7 and latest Nest. All is now well in the world.

Resources