Couchbase - Elasticsearch search issue - elasticsearch

I've followed the Couchbase - ElasticSearch tutorial integration and I'm testing it with the beer-sample bucket.
I have an issue.
I can do a query like:
{
"query": {
"match": {
"doc.name": "IPA"
}
}
}
but if I search like that:
{
"query": {
"filtered": {
"query": {
"match_all": { }
},
"filter": {
"term": { "doc.name": "IPA" }
}
}
}
}
I don't obtain any result.
With other string field I don't have problems, for example, the "type" : "beer"
{
"query": {
"match": {
"doc.type": "beer"
}
}
}
{
"query": {
"filtered": {
"query": {
"match_all": { }
},
"filter": {
"term": { "doc.name": "beer" }
}
}
}
}
I don't know why.
Thanks in advance

It is because of your analyzer. For strings, the default analyzer lowercases the imput. So, IPA is indexed as ipa.
A term filter does not analyze your imput, and thus, you search for IPA and in your index, you have ipa --> IPA != ipa , and thus, the document do not match.
The match query, on the other end, analyzes your input using the analyzer that was set for the field, thus, your input is lowercased and you search for ipa.
I hope it makes sense.

Related

Elasticsearch filter two fields with full text match

{
"query": {
"bool": {
"must" : [
{ "match": { "metadata.cloudAccountId": "462854006774" } },
{ "match": { "metadata.customerId": "3d472521-2a36-49d7-9080-5af57bf1af14" } },
]
}
}
}
Here i wants to filter by two fields. And it is full text match.
cloudAccountId working fine but, for customerId when i a changing anything from last part still it is
working. How can i do must match so it will give result if the string is exact for both.
If you want exact match use Term Query.
{
"query": {
"bool": {
"filter": [
{
"term": {
"metadata.cloudAccountId": "462854006774"
}
},
{
"term": {
"metadata.customerId": "3d472521-2a36-49d7-9080-5af57bf1af14"
}
}
]
}
}
}

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

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

Elasticsearch conditional query for nested array

Using the following document, I'm trying to perform an Elasticsearch keyword query, conditionally excluding field data from the scope of the search. Is this possible?
{
"Name":"doc1",
"UserData":[
{
"EnteredBy":"Eric",
"Description":"Desc entered by Eric, abc"
},
{
"EnteredBy":"Alex",
"Description":"Desc entered by Alex, def"
}
]
}
The Elasticsearch query I need will allow me to search across the whole document, except it should exclude from the search UserData items where EnteredBy does not match the specified user.
The following queries would return results:
User:Eric doc1
User:Eric abc
User:Alex doc1
User:Fred doc1
The following queries would not return results:
User:Eric def
User:Fred def
Everything I've tried thus far, ends up filtering content based on the presence of UserData nodes which apply to the specified user. I can't think of a way to specify that a field should be searched, only if the EnteredBy field matches.
I could restructure the document, if that would solve the problem.
Edit 1
The index..
PUT index1
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 0
},
"mappings": {
"properties" : {
"UserData" : {
"type":"nested"
},
"Name": {
"type":"text"
}
}
}
}
Edit 2
The query below is providing the results that I need, except for the child entity, I have to search in a specific field. If I change the second condition of the nested search into a query_string search, then it no longer uses the EnteredBy condition.
GET index1/_search
{
"query": {
"bool": {
"should": [
{
"nested":
{
"path": "UserData",
"query": {
"bool": {
"must": [{
"match": {
"UserData.EnteredBy": "Eric"
}},
{
"match": {
"UserData.Description": "def"
}
}]
}
}
}
},
{
"query_string":
{
"query": "doc1x"
}
}
]
}
}
}
This query appears to be working. I think I answered my own question.
GET index1/_search
{
"query": {
"bool": {
"should": [
{
"nested":
{
"path": "UserData",
"query": {
"bool": {
"must": [{
"match": {
"UserData.EnteredBy": "Eric"
}},
{
"query_string": {
"query": "def"
}
}]
}
}
}
},
{
"query_string":
{
"query": "doc1"
}
}
]
}
}
}

Elastic search wildcard query working by text query not

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..

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.

Resources