Elastic Search Query For Making Range For 2 Mappings - elasticsearch

Hi i want make query that x greater than min_rent and x least than max_rent.
Here the mappings.
'mappings' => [
'project_listing_v1' => [
'properties' => [
'location' => [
'type' => 'geo_point'
],
'min_rent'=>[
'type'=>'short',
],
'max_rent'=>[
'type'=>'short',
]
]

You can use a bool query with a must clause as such:
GET project_listing_v1/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"min_rent": {
"gt": x
}
}
},
{
"range": {
"max_rent": {
"lt": x
}
}
}
]
}
}
}
Note: You have to replace 'x' in the query with the actual value.
More on bool queries: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

Related

how to get count of not-null value based on specific field in Elasticsearch

I have elastic search index and I need total count of records that one of fields ("actual_start") is not-null how can I do this?
I have wrote this query and I want to append count of not-null actual start value to the result of my query:
$params = [
'index' => "appointment_request",
'body' => [
'from' => $request['from'],
'size' => $request['size'],
"query" => [
"bool"=>[
"must" => [
[
"term" => [
"doctor_id" => [
"value" => $request['doctor_id']
]
]
],
[
"match" => [
"book_date" => $request['book_date']
]
],
]
],
]
]
];
Take a look at Exists Query
Try this:
GET <your-index>/_count
{
"query": {
"exists": {
"field": "actual_start"
}
}
}
Then you can read the count value which will give you the total count of records that actual_start is not-null.
You can also replace _count with _search and total value under hits will give you a total count (in case you also want the hits) .
If you want to do the opposite (all records which actual_start is null):
GET <your-index>/_count
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "actual_start"
}
}
]
}
}
}
UPDATE
If I understand you correctly you want to append your current query with the exists query.
Example:
GET <your-index>/_search
{
"query": {
"bool": {
"must": [
{
<put-your-query-here>
},
{
"exists": {
"field": "actual_start"
}
}
]
}
}
}

Converting JSON to Elastic NEST query doesn't work as intended

I'm trying to convert the following JSON to NEST, but it's not working as intended. It does match the field with the website, but it doesn't match the range, so I get some very old results.
When using Kibana to search, I send this request:
"query": {
"bool": {
"must": [],
"filter": [
{
"bool": {
"should": [
{
"match": {
"domain": "website.com"
}
}
],
"minimum_should_match": 1
}
},
{
"range": {
"#timestamp": {
"gte": "2020-08-03T12:37:07.821Z",
"lte": "2020-08-18T12:37:07.821Z",
"format": "strict_date_optional_time"
}
}
}
],
"should": [],
"must_not": []
}
},
And converted to NEST:
SearchDescriptor<ApacheRequest> Query(SearchDescriptor<ApacheRequest> qc)
{
var query = qc.Query(q =>
q.Bool(b =>
b.Filter(f =>
f.Bool(fb =>
fb.Should(sh =>
sh.Match(ma => ma
.Field(x => x.Domain)
.Query("website.com")
)
)
),
f => f.Range(r => r.GreaterThanOrEquals(timestamp))
)
)
);
return query;
}
As I said, it matches the domain, but not the range. I get results a month back, even though I've tested that my timestamp is correct.
What am I doing wrong?
Ah, I found the issue.. I'm not supposed to use .Range() but rather .DateRange(). Now my query looks like this:
SearchDescriptor<ApacheRequest> Query(SearchDescriptor<ApacheRequest> qc)
{
var query = qc.Query(q =>
q.Bool(b =>
b.Filter(f =>
f.Bool(fb =>
fb.Must(sh =>
sh.Match(ma => ma
.Field(x => x.Domain)
.Query("website.com")
)
)
),
f => f.DateRange(r =>
r.Field(fi => fi.Timestamp).GreaterThanOrEquals(from)
)
)
)
);
return query;
}

Append .keyword to fieldname in NEST elasticsearch query

Imagine I have my query as:
.Query(query =>
query.Bool(b => b.Must(m =>
m.Wildcard(w => w.Field(f => f.userName).Value(string.Format("*{0}*", searchModel.username).Suffix("keyword")))
)));
the output query (from DeubgInformation) will be like:
{
"query": {
"bool": {
"must": [{
"wildcard": {
"userName": "*alex*"
}
}
],
"must_not": [],
"should": []
}
}
}
how'ever this does not work. it needs the ".keyword" to be appended at the endof username. The query below works, but I can not generate it through NEST:
{
"query": {
"bool": {
"must": [{
"wildcard": {
"userName.keyword": "*alex*"
}
}
],
"must_not": [],
"should": []
}
}
}
any idea how to make NEST to add the ".keyword" at the end of the field name? (of course in Fluent fashion, otherwise w.Field("userName.keyword") works)
The Suffix() call needs to be part of the member access expression
.Query(query => query
.Bool(b => b
.Must(m => m
.Wildcard(w => w
.Field(f => f.userName.Suffix("keyword"))
.Value(string.Format("*{0}*", searchModel.username)
)
)
)
));

Elasticsearch - Term null?

I'm having trouble matching null terms in NEST.
I'm trying to get some details via a query.
It works ok, but one thing I can't understand -
For some reason I can't do a term that equals to null value.
What am I doing wrong?
My Code:
result = _mainManager.Client.Search<object>
(q => q
.Type("Mail")
.Query(c =>
c.Term("SentMail_Sender_Id", userId) &&
c.Term("SentMail_EmbedAccountId", null) &&
!c.Term("SentMail_Status", Status.REMOVED.ToString().ToLower()) &&
c.Range(v => v.OnField("SentMail_Upload_Files_Count").Greater(0)))
.Size(int.MaxValue)
.Sort(s => s.OnField("SentMail_Creation_Date").Descending()));
It works ok, no null term is found in my result Json:
{
"size": 2147483647,
"sort": [
{
"SentMail_Creation_Date": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"term": {
"SentMail_Sender_Id": {
"value": 7186
}
}
},
{
"range": {
"SentMail_Upload_Files_Count": {
"gt": "0"
}
}
}
],
"must_not": [
{
"term": {
"SentMail_Status": {
"value": "removed"
}
}
}
]
}
}
}
Found it!
result = _mainManager.Client.Search<object>
(q => q
.Type("Mail")
.Query(c =>
c.Term("SentMail_Sender_Id", userId) &&
!c.Term("SentMail_Status", Status.REMOVED.ToString().ToLower()) &&
c.Range(v => v.OnField("SentMail_Upload_Files_Count").Greater(0)))
.Filter(f => f.Missing("SentMail_EmbedAccountId"))
.Size(int.MaxValue)
.Sort(s => s.OnField("SentMail_Creation_Date").Descending()));

How to use ElasticSearch Query params (DSL query) for multiple types?

I have been working with the ElasticSearch from last few months, but still find it complicated when I have to pass an complicated query.
I want to run the query which will have to search the multiple "types" and each type has to be searched with its own "filters", but need to have combined "searched results"
For example:
I need to search the "user type" document which are my friends and on the same time i have to search the "object type" document which I like, according to the keyword provided.
OR
The query that has both the "AND" and "NOT" clause
Example query:
$options['query'] = array(
'query' => array(
'filtered' => array(
'query' => array(
'query_string' => array(
'default_field' => 'name',
'query' => $this->search_term . '*',
),
),
'filter' => array(
'and' => array(
array(
'term' => array(
'access_id' => 2,
),
),
),
'not' => array(
array(
'term' => array(
'follower' => 32,
),
),
array(
'term' => array(
'fan' => 36,
),
),
),
),
),
),
);
as this query is meant to search the user with access_id = 2, but must not have the follower of id 32 and fan of id 36
but this is not working..
Edit: Modified query
{
"query": {
"filtered": {
"filter": {
"and": [
{
"not": {
"filter": {
"and": [
{
"query": {
"query_string": {
"default_field": "fan",
"query": "*510*"
}
}
},
{
"query": {
"query_string": {
"default_field": "follower",
"query": "*510*"
}
}
}
]
}
}
},
{
"term": {
"access_id": 2
}
}
]
},
"query": {
"field": {
"name": "xyz*"
}
}
}
}
}
now after running this query, i am getting two results, one with follower: "34,518" & fan: "510" and second with fan:"34", but isn't it supposed to be only the second one in the result.
Any ideas?
You may want to look at the slides of a presentation that I gave this month, which explains the basics of how the query DSL works:
Terms of endearment - the ElasticSearch Query DSL explained
The problem with your query is that your filters are nested incorrectly. The and and not filters are at the same level, but the not filter should be under and:
curl -XGET 'http://127.0.0.1:9200/_all/_search?pretty=1' -d '
{
"query" : {
"filtered" : {
"filter" : {
"and" : [
{
"not" : {
"filter" : {
"and" : [
{
"term" : {
"fan" : 36
}
},
{
"term" : {
"follower" : 32
}
}
]
}
}
},
{
"term" : {
"access_id" : 2
}
}
]
},
"query" : {
"field" : {
"name" : "keywords to search"
}
}
}
}
}
'
I just tried it with the "BOOL"
{
"query": {
"bool": {
"must": [
{
"term": {
"access_id": 2
}
},
{
"wildcard": {
"name": "xyz*"
}
}
],
"must_not": [
{
"wildcard": {
"follower": "*510*"
}
},
{
"wildcard": {
"fan": "*510*"
}
}
]
}
}
}
It gives the correct answer.
but I'm not sure should it be used like this ?

Resources