Elasticsearch filter two fields with full text match - elasticsearch

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

Related

How to filter result set of elasticsearch from another bool condition

I have to fetch data from API which use ElasticSearch.
The conditions of data fetching are firstname should start with given string and company status should be active,
so I have used the below query
"span_first": {
"match": {
"span_term": {
"employee.firstname": "tas"
}
},
"end": 1
}
to match firstname and now i need to filter the data from companyStatus,
"bool": {
"must": [
{
"match": {
"employee.companyStatus": "Active"
}
}
]
}
I'm trying to plug the above bool query into the span_first query
but I have no idea how to do it,
Can someone help me to create the query, sorry if this is a dumb question,
I'm totally new to Elasticsearch.
You can try to use Term Query for filter status and Match Query for search terms.
GET edx_test/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"employee.companyStatus": "Active"
}
}
],
"must": [
{
"match": {
"employee.firstname": "tas"
}
}
]
}
}
}
Read more:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
If both the span_first and match query must be true then you can have both the queries in a must clause like below:
GET test_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"employee.companyStatus": "Active"
}
},
{
"span_first": {
"match": {
"span_term": {
"employee.firstname": "tas"
}
},
"end": 1
}
}
]
}
}
}

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

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

elasticsearch query filter "or" with wildcards

I try to use elasticsearch to make a query with wildcard (the use of *) for an autocomplete function.
It was working good with bool query like this (where value can contain *) :
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": value
}
}
]
}
}
}
But now I want to search only an input value in some fields ( T_FAMILY or T_GENUS or T_SCIENTIFICNAME) and use wildcards $ and *:
{
"fields": [
"T_FAMILY",
"T_GENUS",
"T_SCIENTIFICNAME"
],
"query": {
"filtered": {
"filter": {
"or": [
{
"term": {
"T_FAMILY": value
}
},
{
"term": {
"T_GENUS": value
}
},
{
"term": {
"T_SCIENTIFICNAME": value
}
}
]
}
}
}
}
the query work but the wildcards don't.
I can't find why.
The 3 fields are analyzed in the mapping.

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