APOC Elasticsearch query format - elasticsearch

I am using Neo4j 4.0.3, Elasticsearch 6.8.3 and APOC.
I want to run an Elasticsearch aggregation query through Neo4J using the APOC Elasticsearch procedure.
The query is defined in the request body and works on Elasticsearch but I'm not sure how to construct the query parameter through the procedure. In Elasticsearch I can pass the request body as a source parameter (with a source content type of JSON).
To demonstrate, I've tried to get a simple query working.
This works in Elasticsearch:
GET http://localhost:9200/trends/trend/_search?source={"query": {"match" : {"type" : "ENTITY"}}}&source_content_type=application/json
but when I try this through the ES procedure:
CALL apoc.es.query('elasticsearch:9200', 'trends', 'trend', 'source={"query":{"match":{"type":"ENTITY"}}}&source_content_type=application/json', null)
Neo4J gives the following error:
Failed to invoke procedure `apoc.es.query`: Caused by: java.net.URISyntaxException: Illegal character in query at index 54: http://elasticsearch:9200/trends/trend/_search?source={"query":{"match":{"type":%22ENTITY%22%7D%7D%7D````
What do I need to do to pass the query correctly?

Got it to work, I overlooked the payload parameter. So the APOC call was this:
CALL apoc.es.query('elasticsearch:9200', 'trends', 'trend', null, '{"query":{"match":{"type":"ENTITY"}}}')
Alternatively I could have passed the query as a Map
CALL apoc.es.query('elasticsearch:9200', 'trends', 'trend', apoc.map.fromPairs([
["source_content_type", "application/json"],
["source", '{"query":{"match":{"type":"ENTITY"}}}']
]), null)

If you want to use a scroll ID and also a payload to specify the query, something like this works:
CALL apoc.es.query('localhost','indexName','doc','scroll=1m','{
"query":{
"query_string":{
"query": "name:somebody AND date:[now-30d TO now]"
}
},
"size":"1000"
}') yield value
with value._scroll_id as scrollId, value.hits.hits as hits
// Do something with hits
UNWIND hits as hit
return hit
This format makes editing the query easier: I've found inconsistent results and URL escaping rules when putting the query in a string in the fourth parameter position.
See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html and https://neo4j.com/docs/labs/apoc/4.0/database-integration/elasticsearch/

Related

Is it possible to query by field data type in Elasticsearch?

I am needing to do a query in Elasticsearch by field data type. I have not been successful in creating that query. I want to be able to {1) specify the type I want to search for in the query, i.e. all fields of {"type"="boolean"}, and also, (2) get the field and see what the type is for that field.
Reason is to check that the field is designated correctly. Let's say I inserted the following data into this index and fields and I now want to see what the data types of those fields are programmatically. How would I query that?
POST /index_name1/_doc/
{
"field1":"hello_field_2",
"field2":"123456.54321",
"field3.field4": false,
"field3.field5.field10":"POINT(-117.918976 33.812511)",
"field3.field5.field8": "field_of_dragons",
"field3.field5.field9": "2022-05-26T07:47:26.133275Z"
}
I have tried:
GET /index_name1/_search
{
"query":{
"wildcard":{
"field3.field4":{ "type":"*"}
}
}
}
That gives [wildcard] query does not support [type].
I've tried many other queries and searched the documentation and threads, but can't find anything that will do this. It has got to be possible, right?

Elastic GET by ID query on rollover alias fails with ""Alias [...] has more than one indices associated with it..."

Our new rollover indices just rolled over. Now this query...
GET http://my.elastic/system-logs/_doc/7e8017d8-0cb8-4b9e-b021-b2a4b4ac71c7
...fails with this:
"Alias [system-logs] has more than one indices associated with it [[system-logs-000002, system-logs-000001]], can't execute a single index op"
But doing the same thing with _search works fine:
GET http://my.elastic/system-logs/_search/
{
"query": {
"bool": {
"must": [{"term": {"_id": "a1906f52-3957-4f4b-9b40-531422e3a04e"}}]
}
}
}
The exception comes from this code, which looks like there is an allowAliasesToMultipleIndices setting for this, but I haven't been able to find a place to set it.
We're on Elastic 6.8.
In the first http request, you are just trying to find the doc with particular id on an index which in turn is an alias of more than one index.
That's the problem.
Reason:
_doc is a mapping type in elastic search. It is used to segregate documents in the same index. So it cannot check across the indices. It is deprecated. Refer, this also
And you need to use GET request with the permitted queries[like your second example] (term, terms, match, query_string, simple_query_string). Refer

Elastic search error - [prefix] query does not support [prefix]

When I post the following query, I got an error response
{"error":{"root_cause":[{"type":"parsing_exception","reason":"[prefix] query does not support [prefix]","line":1,"col":119}],"type":"parsing_exception","reason":"[prefix] query does not support [prefix]","line":1,"col":119},"status":400}
Here's my POST query:
{"from":0,"size":10,"sort":{"_score":"desc"},"query":{"bool":{"must":{"bool":{"should":[[{"prefix":{"title":{"prefix":"of","boost":"1.0"}}}],{"multi_match":{"query":"of","fields":["title^1.0"]}},{"query_string":{"query":"(\"of\")","fields":["title^1.0"]}}]}}}}}
How can I make this query work for my autocomplete search?
The prefix command accepts "value" not "prefix". Also the "boost" value is numeric type, so it shouldn't be enclosed in quotes.
So it should be:
{"prefix":{"title":{"value":"of","boost":1.0}}}
Instead of:
{"prefix":{"title":{"prefix":"of","boost":"1.0"}}}

How to make query for elasticsearch in spring boot like we do in sense plugin

this is query which i have done in sense
GET /_search?q=2016
it searches in whole db and get results for all entries which has "2016" in any field.
Giving q as query paramater run as query string query. So you have to use java api for Query String. You can use :
SearchResponse response = client.prepareSearch("index_name")
.setTypes("type1Name")
.setQuery(QueryBuilders.queryString("2016"))
.execute()
.actionGet();

Elastic search : Quesry not executing

Here is mapping of my elastic search
{"MYAPP":{"mappings":{
"XX":{
"_ttl":{"enabled":true},
"properties":{"propX":{"type":"integer"}}
},
"YY":{
"_ttl":{"enabled":true},
"properties":{"propY":{"type":"integer"}}
},
}
}
}
I want execute query like
propX:XYZ AND propY:ABC
The problem is if i do this
propX:XYZ AND propY:ABC
It return nothing, but this
propX:XYZ
return result. I think the problem is propX and propY are two different region thats why using both returns nothing.
Here is my JAVA code:
SearchResponse response = client.prepareSearch("MYAPP")
.setQuery(QueryBuilders.queryString("propX:XYZ AND propY:ABC")).execute()
.actionGet();
According to your mapping, the propX and propY are located in different mapping types.
Assume you follow the index mapping. Each mapping type has it own document, in other word, all document inside type XX only have the propX field. And all document inside type YY only have the propY field.
So there is no document, that have the two fields in the same time and this is why you cannot find any document.

Resources