Filter with querystring in elasticsearch - elasticsearch

I send this query and it works fine. It returns filtered data:
{
"query": {
"bool": {
"filter": [
{
"match": {
"lang": "en"
}
}
]
}
},
"size": 10,
"from": 0,
"sort": []
}
If I want to search with searchstring then it workst fine too:
{
"query": {
"query_string": {
"query": "big size"
}
},
"size": 10,
"from": 0,
"sort": []
}
But I can't get data from elastic by filter and searchstring together:
{
"query": {
"query_string": {
"query": "big size"
},
"bool": {
"filter": [
{
"match": {
"lang": "en"
}
}
]
}
},
"size": 10,
"from": 0,
"sort": []
}
I receive next error:
Error 400.
{"error":{"root_cause":[{"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":76}],"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":76},"status":400}

Your query needs to be restructured as shown below.
Query:
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "big size"
}
}
],
"filter": [
{
"match": {
"lang": "en"
}
}
]
}
},
"size": 10,
"from": 0,
"sort": []
}

Related

Why am I getting different results for the same exact query when I aggregate it in elasticsearch?

So I have this query and I am trying to aggregate a certain field but when I use the same query in aggregation I dont get expected results
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "TEST",
"fields": ["TESTFIELD1", "TESTFIELD2"],
"lenient": true,
"default_operator": "OR"
}
}
]
}
},
"aggs": {
"All": {
"global": {},
"aggs": {
"TESTAGG": {
"filter": {
"bool": {
"must": [
{
"query_string": {
"query": "TEST",
"fields": ["TESTFIELD1", "TESTFIELD2"],
"lenient": true,
"default_operator": "OR"
}
}
]
}
},
"aggs": {
"subs": {
"terms": {
"field": "TESTFIELD1",
"size": 100,
"order": { "_term": "asc" }
}
}
}
}
}
}
}
}
The issue is in the aggregation is that I get values for TESTFIELD1 that dont exist in the hits in the main query and I am not sure why. Any ideas?

Limit the size per index when searching multiple index in Elastic

I have been following the guidelines from this post. I can get the desired output but in the same DSL how can I limit the size of results for each index ?
Full text Search with Multiple index in Elastic Search using NEST C#
POST http://localhost:9200/componenttypeindex%2Cprojecttypeindex/Componenttype%2CProjecttype/_search?pretty=true&typed_keys=true
{
"query": {
"bool": {
"should": [
{
"bool": {
"filter": [
{
"term": {
"_index": {
"value": "componenttypeindex"
}
}
}
],
"must": [
{
"multi_match": {
"fields": [
"Componentname",
"Summary^1.1"
],
"operator": "or",
"query": "test"
}
}
]
}
},
{
"bool": {
"filter": [
{
"term": {
"_index": {
"value": "projecttypeindex"
}
}
}
],
"must": [
{
"multi_match": {
"fields": [
"Projectname",
"Summary^0.3"
],
"operator": "or",
"query": "test"
}
}
]
}
}
]
}
}
}
With your given query, you could use aggregations to group and limit number of hits per index (in this case, limiting to 5):
{
"size": 0,
"query": {
... Same query as above ...
},
"aggs": {
"index_agg": {
"terms": {
"field": "_index",
"size": 20
},
"aggs": {
"hits_per_index": {
"top_hits": {
"size": 5
}
}
}
}
}
}

Elasticsearch query using more_like_this field renders a failed to parse search source. expected field name but got [START_OBJECT] error

We're using Elasticsearch 2.4.5. Have an application that can generate fairly complicated queries. I'm trying to add a more_like_this field to the query like so:
{
"query": {
"more_like_this": {
"fields": [
"title"
],
"ids": [
1234
],
"min_term_freq": 1,
"max_query_terms": 25
},
"function_score": {
"query": {
"bool": {
"must": [
{
"query_string": {
"default_operator": "AND",
"fields": [
"title",
"author"
],
"query": "((title:(\"Tale of Two Cities\"^2)))",
"lenient": true
}
}
],
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"geo_distance": {
"distance": "50mi",
"location": {
"lat": 49.32,
"lon": -45.67
},
"distance_type": "plane",
"_cache": true
}
}
]
}
},
{
"term": {
"merged": 0
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "title_type"
}
}
}
}
]
}
}
}
},
"functions": [
{
"field_value_factor": {
"field": "quality_score",
"factor": 1,
"missing": 0
}
}
]
}
},
"filter": {
"bool": {
"must": []
}
},
"sort": "_score",
"size": 20,
"from": 0
}
I'm getting a failed to parse search source. expected field name but got [START_OBJECT] error when I try to run the above code. When I remove that piece of code the query executes correctly. I've looked at documentation and other examples of more_like_this usage and I can't determine what's wrong with my query. I'm assuming it has something to do with the way the rest of the query is formed.

Elastic Search Query to get results for multiple keywords(i.e Country name)

Key String will be like
"india,singapore" without quotes.
How to split and search the keyword
Expected result will be match the country with india or singapore.
So far i tried..
{
"_source": "country_name",
"query": {
"bool": {
"must": [
{
"term": {
"country_name.keyword": "india,singapore"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}
But it will showing only those content have match the exact key string "india,singapore"
you can use terms query in place of term query like below:
{
"_source": "country_name",
"query": {
"bool": {
"must": [
{
"terms": {
"country_name.keyword": ["india","singapore"]
}
}
]
}
},
"from": 0,
"size": 10
}

Elasticsearch aggregation not being applied to filters

Here is my query. I am trying to get all products that are inside "men_fashion" and "men_shoes" category (categories are being used as terms/tags). Then i want to query the whole result set and search for products that have "men boots yellow" in them.
The below query works perfectly fine, but now i am not getting the correct aggregation results. It gives me all the brands where as i am only interested in the brands.
{
"size": 15,
"from": 0,
"query": {
"query_string": {
"query": "men boots yellow"
}
},
"filter": {
"bool": {
"must": [{
"match": {
"active": 1
}
}, {
"match": {
"category": "men_fashion"
}
}, {
"match": {
"category": "men_shoes"
}
}]
}
},
"aggs": {
"brands": {
"terms": {
"size": 100,
"field": "brand"
}
}
}
}
I think this might be due to the filter i have applied, but if this is somehow complicated i am ok with using a simple query that would achieve this without the filters.
You're using a post filter instead of a normal query filter, try like this instead:
{
"size": 15,
"from": 0,
"query": {
"bool": {
"must": {
"query_string": {
"query": "men boots yellow"
}
},
"filter": [
{
"match": {
"active": 1
}
},
{
"match": {
"category": "men_fashion"
}
},
{
"match": {
"category": "men_shoes"
}
}
]
}
},
"aggs": {
"brands": {
"terms": {
"size": 100,
"field": "brand"
}
}
}
}

Resources