Nested Term ElasticSearch 7 problem / Elastica Don't work - BEGINNER - elasticsearch

I'm trying to get exact search with slug in nested element in ElasticSearch but it seems like that it doesn't work.
So when i'm trying a simple nested match with "my-slug" i get result with "my" and "slug", Normal...
GET my-index/_search
{
"query": {
"nested": {
"path": "productTranslation",
"query": {
"bool": {
"must": [
{
"match": {
"productTranslation.slug": "my-slug"
}
}
]
}
}
}
}
}
But i have no result when i'm trying with term or filter search.
GET my-index/_search
{
"query": {
"nested": {
"path": "productTranslation",
"query": {
"bool": {
"filter": [
{
"term": {
"productTranslation.slug": "my-slug"
}
}
]
}
}
}
}
}
Any idea where the error lies??
Thank's for help.

Term query doesn't perform any analysis on the term. So, in term query you need to have an exact match.
If you have not explicitly defined any mapping then you need to add .keyword to the productTranslation.slug field. This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after productTranslation.slug field).
{
"query": {
"nested": {
"path": "productTranslation",
"query": {
"bool": {
"filter": [
{
"term": {
"productTranslation.slug.keyword": "my-slug" // note this
}
}
]
}
}
}
}
}
OR you can change the data type of the productTranslation.slug field to keyword type
{
"mappings": {
"properties": {
"productTranslation": {
"properties": {
"slug": {
"type": "keyword"
}
}
}
}
}
}

Related

ElasticSearch adjacent words for nested queries

I'm using ES 7.14/Kibana 7.10, I have to search for adjacent words (any order), hence I'm using this query:
{
"query":{
"bool":{
"must":[
{
"query_string":{
"query":"*antonio* *banderas*",
"fields":[
"text"
],
"default_operator":"and",
}
}]
}
}
}
This works ok for a text plain field. Now, I have a nested field metadata, let's say the mapping is
{
"mappings:": {
"properties": {
"text": {
"type": "text"
},
"metadata": {
"type": "nested",
"properties": {
"text": {
"type": "text"
}
}
}
}
}
}
and I would like to search that nested field in the same way (adjacent words search), so assumed that it's is possibile to write a nested query for query_string in this way
{
"query": {
"query_string": {
"query": "metadata.text:*antonio* *banderas*"
}
}
}
How to adapt this approach to the previous one with default_operator=and etc.? If I do
{
"query": {
"query_string": {
"query": "metadata.text:*antonio* *banderas*",
"default_operator": "and"
}
}
}
I don't get any result (but any error too).
A similar question, but related to matching adjacent words for multiple nested fields is here.
Adjacent word with any order should not be search with query_string but wildcard or match or term or span_term
There is also a mapping type wildcard optimised for this usage, depends on what type of queries you will need.
So for you first example :
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"text": "*antonio*"
}
},
{
"wildcard": {
"text": "*banderas*"
}
}
]
}
}
}
OR
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"text": "*antonio*banderas*"
}
}
]
}
}
}
and for nested queries :
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "metadata",
"query": {
"bool": {
"must": [
{
"wildcard": {
"metadata.text": "*antonio*"
}
},
{
"wildcard": {
"metadata.text": "*banderas*"
}
}
]
}
}
}
}
]
}
}
}

Multi-Nested Elastic Query

I am trying to return all documents that match a specific keyword property in a collection.
The image below represents the document hierarchy.
The AggregateRoot has a collection of GUID Id's in the property DomainElements.
I am trying write a query that returns all documents where the Id matches a specific Guid value.
the following image shows the nested path in the index schema;
When I attempt this query I do not return any results...
{
"query": {
"nested": {
"path": "boundedContexts",
"query": {
"nested": {
"path": "boundedContexts.aggregateRoots",
"query": {
"nested": {
"path": "boundedContexts.aggregateRoots.domainElements",
"query": {
"bool": {
"must": [
{ "match": { "domainelements.id": "48be3bb4-838d-44a5-85e3-c5ea4fa8ee36" } }
]
}
}
}
}
}
}
}
}
}
Here is the index documents which shows the data I am trying to query does exist ...
What am I missing?
Is there a more simplified way to query these results?
You almost got it -- just make sure the complete nested path is included in the field name you're targeting:
{
"query": {
"nested": {
"path": "boundedContexts",
"query": {
"nested": {
"path": "boundedContexts.aggregateRoots",
"query": {
"nested": {
"path": "boundedContexts.aggregateRoots.domainElements",
"query": {
"bool": {
"must": [
{
"match": {
"boundedContexts.aggregateRoots.domainElements.id": "48be3bb4-838d-44a5-85e3-c5ea4fa8ee36"
}
}
]
}
}
}
}
}
}
}
}
}
BTW there's no need for so many (sub)nested queries. The inner-most nested path is enough:
{
"query": {
"nested": {
"path": "boundedContexts.aggregateRoots.domainElements",
"query": {
"bool": {
"must": [
{
"match": {
"boundedContexts.aggregateRoots.domainElements.id": "48be3bb4-838d-44a5-85e3-c5ea4fa8ee36"
}
}
]
}
}
}
}
}

Elastic Search - Query with dynamic object and wildcard

I have data in the following format:
{ "_id":1,
"s_id":121211,
"data_detail":{
"name":"John",
"phone_number":08089320xxx,
"city":"ABC"
}
}
I need to search data through elastic search which will query where s_id=? and any text which is available in data_detail object. Example s_id=121211 AND ABC. I need wildcard on data_detail object.
Keys for the data_detail object is not fixed.
Thanks in advance.
I would consider using a bool query with multi_match and term query like this. I haven't tested this, but something on these lines should work I guess.
GET test_index/_search
{
"query": {
"nested": {
"path": "data_detail",
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "ABC",
"fields": [
"data_detail.*"
]
}
},
{
"term": {
"s_id": {
"value": "121211"
}
}
}
]
}
}
}
}
}
Solved this by using the following query:
{
"query": {
"bool": {
"must": [
{
"query_string":{
"fields":["data_detail.*"],
"query": "*str*",
"analyze_wildcard":true
}
},
{
"term": {
"s_id": {
"value": "121211"
}
}
}
]
}
}
}

ElasticSearch multiple string to search with wildcard query

I'm trying to have multiple wildcard query match in my elasticsearch query in Kibana. I can't quite figure it out.
Basically I want any document with an attribute type="erreur"
and I want to exclude all documents that match the strings "An established*" or "java.lang.*" on the field descr_courte
{
"query": {
"bool": {
"must": {
"term": {
"type": "erreur"
}
},
"must_not": {
"wildcard": {
"descr_courte": ["An established*", "java.lang.*"]
}
}
}
}
}
if I put a single wildcard query it works fine
{
"query": {
"bool": {
"must": {
"term": {
"type": "erreur"
}
},
"must_not": {
"wildcard": {
"descr_courte":
"An established*"
}
}
}
}
}
the error I get:
Error: Request to Elasticsearch failed: {"error":{"root_cause":[{"type":"illegal_state_exception","reason":"Can't get text on a START_ARRAY at 1:454"}],"type":"search_phase_execution_exception","reason":"all shards
Any idea?
Try putting them is separate clauses.
{
"query": {
"bool": {
"must": {
"term": {
"type": "erreur"
},
"must_not": [
{
"wildcard": {
"descr_courte": "An established*"
}
},
{
"wildcard": {
"descr_courte": "java.lang.*"
}
}
]
}
}
}
}
My guess is that you can't make an array for wildcard query like ["An established*", "java.lang.*"], so you need to:
{
"query": {
"{
"must": {
"term": {
"type": "erreur"
}
},
"must_not": {
"regexp": {
"descr_courte": "(An established|java\.lang\.).*"
}
}
}
}
}
More info about regexp query in https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-regexp-query.html
Another option is to combine your query terms with the logical operators NOT, AND and OR in the query string
{
"query": {
"query_string" : {
"query" : "type:erreur AND NOT(descr_courte:An established* OR descr_courte:java.lang.*)"
}
}
}
See more info at https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_wildcards

NOT condition in elasticsearch

I am trying to implement NOT condition in elasticsearch query.
Can I Implement filter inside bool or I need to write separate
filter as below. Any optimum solution is there?
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "fashion"
}
},
{
"term": {
"post_status": "publish"
}
}
]
}
},
"filter": {
"not": {
"filter": {
"term": {
"post_type": "page"
}
}
}
}
}
You can use a must_not clause:
{
"query": {
"bool": {
"must": [
{
"match": {
"_all": "fashion"
}
},
{
"term": {
"post_status": "publish"
}
}
],
"must_not": {
"term": {
"post_type": "page"
}
}
}
}
}
Also, I'd recommend using a match filter instead of query_string, as query_string requires the much more strict Lucene syntax (and is therefor more error prone), whereas match works more like a search box: it will automatically transform a human readable query to a Lucene query.

Resources