How to get only one field instead of list in Elasticsearch - elasticsearch

So, there I have request that return one or two fields depends on their existence in document. But I need to receive only one field (no matter which one). How can I do it?
{
"_source" : ["yearOfBirth", "fullBirthDate"],
"size": 1000,
"query": {
"bool": {
"must": [],
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"range": {
"yearOfBirth": {
"gte": "1900",
"lte": "2020"
}
}
},
{
"range": {
"fullBirthDate": {
"gte": "1900",
"lte": "2020"
}
}
}
]
}
}
]
}
}
}
}
}

Related

need something like coalesce in elasticsearch

My current elasticsearch query is-
{
"must": [
{
"range": {
"firstClosedAt": {
"gte": 1667948400000,
"lte": 1668034800000
}
}
},
{
"term": {
"status": "CLOSED"
}
}
I want to modify it such that if "firstClosedAt" is null or not present then look for "closedAt".
Just like we have coalesce("firstClosedAt","closedAt") in sql
Help would be appreciated
There's no coalesce equivalent in ES, but you can do the query like below, which can read like: "either use firstClosedAt OR use closedAt if firstClosedAt does not exist":
{
"query": {
"bool": {
"filter": [
{
"term": {
"status": "CLOSED"
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"range": {
"firstClosedAt": {
"gte": 1667948400000,
"lte": 1668034800000
}
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "firstClosedAt"
}
},
"filter": {
"range": {
"closedAt": {
"gte": 1667948400000,
"lte": 1668034800000
}
}
}
}
}
]
}
}
]
}
}
}
You could, however, create a much simpler query if you create another date field at indexing time which would either take the value of firstClosedAt or closedAt if firstClosedAt does not exist

With Elasticsearch, how to use an OR instead of AND within filter->terms query?

I have this following query with elastic:
{
"query": {
"bool": {
"filter": [{
"terms": {
"participants.group": ["group1","group2"]
}
}, {
"range": {
"recordDate": {
"gte": "2020-05-14 00:00:00.000",
"lte": "2020-07-22 20:30:56.566"
}
}
}]
}
}
}
Currently, this finds records with participants with group "group1" and "group2".
How to change the query so it finds records with participants from "group1" or "group2?
Is it possible to do it without changing the structure of the query?
I'm assuming that the field participants.group is of keyword type and not text type.
Assuming that, the query you have roughly translates to (group1) or (group2) or (group1 and group2).
All you need to do is modify the query as below and add a must_not clause like below:
POST my_filter_index/_search
{
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"range": {
"recordDate": {
"gte": "2020-05-14 00:00:00.000",
"lte": "2020-07-22 20:30:56.566"
}
}
}
],
"should": [
{
"terms": {
"participants.group": ["group1", "group2"]
}
}
]
}
}
],
"must_not": [
{
"bool": {
"must": [
{
"term": {
"participants.group": "group1"
}
},
{
"term": {
"participants.group": "group2"
}
}
]
}
}
]
}
}
}
Let me know if that works!

ElasticSearch should/must clause not working as expected

Below is my elastic query
GET _search
{
"query": {
"bool": {
"must": {
"match": {
"marriages.marriage_year": "1630"
}
},
"should": {
"match": {
"first_name": {
"query": "mary",
"fuzziness": "2"
}
}
},
"must": {
"range": {
"marriages.marriage_year": {
"gt": "1620",
"lte": "1740"
}
}
}
}
}
}
It is returning data with marriages.marriage_year= "1630" with Mary as first_name as highest score.I also want to include marriages.marriage_year between 1620 - 1740 which are not shown in the results. It is showing data only for marriage_year 1630
That's because you have two bool/must clauses and the second one gets eliminated when the JSON query is parsed. Rewrite it like this instead and it will work:
{
"query": {
"bool": {
"must": [
{
"match": {
"marriages.marriage_year": "1630"
}
},
{
"range": {
"marriages.marriage_year": {
"gt": "1620",
"lte": "1740"
}
}
}
],
"should": {
"match": {
"first_name": {
"query": "mary",
"fuzziness": "2"
}
}
}
}
}
}
UPDATE
Then you need to do it differently and in the bool/must you need to have only the range query and move the match inside the bool/should section:
{
"query": {
"bool": {
"must": [
{
"range": {
"marriages.marriage_year": {
"gt": "1620",
"lte": "1740"
}
}
}
],
"should": [
{
"match": {
"first_name": {
"query": "mary",
"fuzziness": "2"
}
}
},
{
"match": {
"marriages.marriage_year": "1630"
}
}
]
}
}
}

Use of range in Elasticsearch query

Below is the elastic search query. I need to use both the range and missing in a query.How can I change the below query
{
"query": {
"bool": {
"must": [
{
"constant_score": {
"filter": {
"missing": {
"field": "url"
}
}
}
}
],
"should": []
}
},
"_source": [
"id",
"com_name",
"website",
"_foundation._rating"
]
}
I need to add range to the above query. Kindly help me add the below section to the above query
"range": {
"_foundation._rating": {
"gte": 1,
"lte": 4
}
I suspect that the query you need is the following, i.e. the url field must be missing and the _foundation._rating field must be between and 1 and 4 (inclusive):
{
"query": {
"bool": {
"must": [
{
"missing": {
"field": "url"
}
},
{
"range": {
"_foundation._rating": {
"gte": 1,
"lte": 4
}
}
}
]
}
},
"_source": [
"id",
"com_name",
"url",
"_foundation._rating"
]
}
Based on the version of your elastic search, if you are using 5.x, you must use exists inside a must_not clause.
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html
Try the below query:
{
"query": {
"range": {
"bool": {
"must": [
{
"term": {
"_foundation._rating": {
"gte": 1,
"lte": 4
}
}
}
],
"must_not": {
"exists": {
"field": "url"
}
}
}
}
}
}

elasticsearch boolean search syntax

Can anybody please explain why this elasticsearch syntax is incorrect. I'm struggling to get my head around basic syntaxing
This works
"query": {
"filtered": {
"filter": {
"bool" : {
"should": {
"terms": {
"headline":["aut"]
}
},
"must": {
"range": {
"date_at" : {
"gt": "1900-01-01 00:00:00",
"lt": "1980-01-01 00:00:00"
}
}
}
}
}
}
}
However, this query doesn't work
"query": {
"filtered": {
"filter": {
"bool" : {
"should": {
"terms": {
"headline":["aut"]
}
},
"must": {
"range": {
"date_at" : {
"gt": "1900-01-01 00:00:00",
"lt": "1980-01-01 00:00:00"
}
},
"term": {
"headline": "et"
}
}
}
}
}
}
The addition of the "term" clause inside the boolean "must" is causing a syntax error, all shards broken etc... The issue appears to be I want to use the same index twice inside two different bools specifically
headline MUST contain "foo"
headline SHOULD contain "bar"
Is it possible?
Have you tried this?
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"terms": {
"headline": [
"aut"
]
}
}
],
"must": [
{
"range": {
"date_at": {
"gt": "1900-01-01 00:00:00",
"lt": "1980-01-01 00:00:00"
}
}
},
{
"term": {
"headline": "et"
}
}
]
}
}
}
}
}
That would be the direct interpretation of what you're trying to do, but this is probably what you actually need:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"range": {
"date_at": {
"gt": "1900-01-01 00:00:00",
"lt": "1980-01-01 00:00:00"
}
}
},
{
"term": {
"headline": "et"
}
}
]
}
}
}
}
}
Here is some code I used to play around with it:
http://sense.qbox.io/gist/ea16ff321397c2187ef503541019d52c564b7460

Resources