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

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"}}}

Related

Elastic search wildcard search space issue

Consider index field "ProductName" having the value "dove 3.75oz" and when user searches for "dove 3.75oz" text below bool query is working fine to retreive the document:
{"bool":{"must":[{"wildcard":{"ProductName":{"value":"dove"}}},{"wildcard":{"ProductName":{"value":"3.75oz"}}}]}}
If user searches for "dove 3.75 oz" (Space between "3.75" and "oz") the bool query is failing to retrieve the same document:
{"bool":{"must":[{"wildcard":{"ProductName":{"value":"dove"}}},{"wildcard":{"ProductName":{"value":"3.75 oz"}}}]}}
Question: How to design a query using a wildcard query that supports space or no spaces? Please share an example.
Text fields values are broken into tokens by default and then stored. So something like "hello man"" will be saved separately as hello and man because of the space between them. And that is exactly why this will not work with a wildcard query.
{"wildcard":{"ProductName":{"value":"3.75 oz"}}}
It only works for single tokens. For wildcard queries you can use a special field type called wildcard.
If you do not want to reindex your data, try phrase search like:
"match_phrase": {
"ProductName": {
"query": "3.75 oz"
}
}

APOC Elasticsearch query format

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/

The boolean fuzzy query in elasticsearch is not returning expected result

I am trying to build a fuzzy bool query on first and last names in elasticsearch 7.2.0. I have a document with "asim" and "banskota" as first and last name respectively. But when I query with "asi" or "asimmm" and the exact last name, elasticsearch returns no result. However, when queried with exact first name or "asimm", it returns me the intended result from the document.
I also wrote a "fuzzy" query instead of "match". I experimented with different fuzziness parameters, but the outcome is same. Both first name and last names are analyzed, and I queried the 'analyzer' API wrt how it analyze
'asim'. It is indexing the document with 'asim' as a single token with standard analyzer.
EDIT: It turns out that the fuzzy query works with 'Substitution' case, for example, it returns the result for 'asim' when queried with 'asmi' but not for deletion. It is surprising to me as the edit distance in the substitution is greater than in the deletion case. When the string length is greater, for instance with the last name 'Banskota', fuzzy matching works for 'deletion' case as well. What should I do to make the fuzzy search work in 'deletion' case with string length of 4 or 5?
fuzzy_body = {"size": 10,
"query":{
"bool":{
"must": [
{
"match":{"FIRST_NAME_N":{'query': 'asi',"fuzziness": "AUTO"}},
},
{
"fuzzy":{"LAST_NAME_N": "banskota"}
}
]
}
}
}
It turns out that if the name fields are indexed as keyword type, the query returns the expected results with "AUTO" fuzziness.

Elasticsearch - how to search exact string match with special charater (-) in the json document

i have been using ES 5.x version and this is my sample data set json.
{"id":"1"}
{.... "company" : "HCL-US",....}
{"id":"2"}
{.... "company" : "HCL",....}
{"id":"3"}
{.... "company" : "HCL-IND",....}
{"id":"4"}
{.... "company" : "HCL-AUS",....}
How can i search and get who is belonging to "HCL-US". i tried using this query "_search?q=company:"HCL-US"" , it is returning HCL * result. How can i match exact string with special string.
You can use Term Query that matches exact term. Assuming company is a text field, you will get a keyword version of the same , following query should do the needful
{
"query": {
"term": {
"company.keyword": {
"value": "HCL-US"
}
}
}
}
1/ You can specify a whitespace analyzer in the mapping for the field company. This analyzer will split the query only on whitespace while the standard will split on non-alphanumeric characters.
The standard analyzer is the one used when no analyzer is defined.
2/ Or your can query on company.keyword which is a field automatically created for text field since 5.X . This keyword is not analyzed and you can safely use a term query on it to do exact matching.

Lower case response from elastic search where as upper case is expected

I am trying to fetch data using elastic search with java using method
.addAggregation(terms(term))
The JSON response that I am expecting is
{
"key" : "TEST"
}
but I am getting the response as
{
"key" : "test"
}
which is in lower case, I want the response to be as it is stored. Please help here
The reason is that key is being analyzed e.g. lower-cased.
What you could do is have a "searchable" field that is being tokenized, but aggregate on a display field that preserves the casing. Or if you only want to aggregate (or if your search is case-sensitive and it seems for a field like key tokenization is also not desired) use the keyword type (https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html)

Resources