Elastic search wildcard query working by text query not - elasticsearch

I have a query where using a wildcard on the filter returns results but using the actual text in the results to do a query returns 0 hits.
This works
{
"query": {
"filtered": {
"query": { "match_all": { } },
"filter": {
"nested": {
"path": "houses",
"query": {
"filtered": {
"query": { "match_all": { } },
"filter": {
"wildcard": {
"houses.name": "????"
}
}
}
}
}
}
}
}
}
If it returns a house named "Eve gardens", this returns nothing.
{
"query": {
"filtered": {
"query": { "match_all": { } },
"filter": {
"nested": {
"path": "houses",
"query": {
"filtered": {
"query": { "match_all": { } },
"filter": {
"wildcard": {
"houses.name": "Eve gardens"
}
}
}
}
}
}
}
}
}
Please help!

See this:
As per your query houses.name seems to be an analyzed string which means Eve gardens would be tokenized and stored as eve , gardens not as Eve gardens as a whole. As per documentation
The wildcard query operates on terms. If you use them to query an analyzed field, they will examine each term in the field, not the field as a whole.
So if you do
"wildcard": {"houses.name": "Eve gardens"}
this will not work.
Try
"wildcard": {"houses.name": "eve*"}
It should fetch you the results. Hope this helps..

Related

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

Multiple values in nested elastic search 2 query

I have a nested object named 'bundles', that usually contains more than one object. Using this query I can succesfully query on the id of an object in bundles, but I fail to write a query that can query on multiple id's. Suggestions?
{
"query": {
"nested": {
"path": "bundles",
"query": {
"bool": {
"must": [
{
"match": {
"bundles.id": 43273
}
}
]
}
},
"inner_hits": {}
}
}
}
Perhaps you want "should" instead of "must" in the boolean filter. For example:
{
"query": {
"nested": {
"path": "bundles",
"query": {
"bool": {
"should": [
{
"match": {
"bundles.id": 43273
},
{
"match": {
"bundles.id": 433373
}
}
]
}
}
}
}
}
You could also use terms query if the field can be matched exactly. For example:
{
"query": {
"nested": {
"path": "bundles",
"query": {
"bool": {
"must": [
{
"terms": {
"bundles.id": [1140000000, 114]
}
}
]
}
}
}
}
}'

How to implement the following condition in elasticsearch query?

I have an index with some documents having a field named "access_type" . It can have 2 values, either "faculty" or "students".
For the documents with "faculty" as the value for "access_type", there will be another field called "faculties" which is a list of faculty name.
So an example document would look like below:
{
"access_type": "faculty",
"faculties": [
"facultyId1",
"facultyId2",
"facultyId3"
]
}
Now if we have two inputs say one is for the access_type and another is for the faculties.
If I get the following input "faculty" and "facultyId4" . First I need to filter out all the documents matching the access type "faculty" and then in the resulting results the "facuultyId4" should search against the field "faculties". Since the "facultyId4" is not in the above document,it should not be considered a hit.
How can I implement this as an elasticsearch query?
POST http://your.elastic.host:9200/index/type/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"term": {
"access_type": "faculty"
}
},
{
"term": {
"faculties": "facultyId4"
}
}
]
}
}
}
}
}
Hope this will for work.
GET index/type/_search
{
"query": {
"filtered": {
"filter": {
"and": {
"filters": [
{
"query": {
"match": {
"access_type": "faculty"
}
}
},
{
"query": {
"match": {
"faculties": "facultyId4"
}
}
}
]
}
}
}
}
}

How can I add another condition to filter on in this elasticsearch query?

I have an elasticsearch query that filters on business_process field. I would like to add another match clause so both fields have the specified data and I would like to add highlight on business_process field. How would I do that in elastic? Thank you in advance.
{
"query": {
"filtered": {
"query": {
"match": {
"business_process": "loading"
}
},
"filter": {
"missing": {
"field": "client_cru_all"
}
}
}
}
}
The ES documentation is your friend. The bool query is probably what you are looking for. Something like this should do what you want.
POST /test_index/_search
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"match": {
"business_process": "loading"
}
},
{
"match": {
"another_field": "some text"
}
}
]
}
},
"filter": {
"missing": {
"field": "client_cru_all"
}
}
}
}
}
As far as highlighting goes, I'd start here. If you can't get it to work, post what you tried in your question.

Filter a wildcard search in Elastic Search

I'm trying to add a complex filter on a wildcard query with elastic search. The filter seems to be working, however, the results don't talk into account the wildcard. Is this possible or is there an alternative to the wildcard filter? Query is as follows:
{
"query": {
"filtered": {
"query": {
"wildcard": {
"name": "*frog*"
},
"filter": {
"bool": {
"must": {
"term": {
"is_animal": false
}
}
},
"or": [
{
"terms": {
"reptiles.codes": [
27
]
}
},
{
"nested": {
"path": "owners",
"query": {
"bool": {
"should": {
"term": {
"pets": "cat"
}
}
}
}
}
},
{
"nested": {
"path": "locations",
"query": {
"bool": {
"should": {
"term": {
"home": true
}
}
}
}
}
}
]
}
}
}
}
}
Alternatively, can I add the wildcard as a filter inside my "bool": { "must": .... }} ?
You should definitely use ngram token filter in your analyzer instead of running a wildcard which is really slow, especially if it starts with a star which is the case here.
That being said, I don't understand why the wildcard part is not applied here. Any chance you could reproduce your case with a full example? May be you have specific analyzer?

Resources