Function_score query with filters - elasticsearch

I need to consider the priority of the company in the search result. The documentation has a similar "Boosting Filtered Subsetsedit" option. In the result, I get this json request and get an error. Perhaps I'm not in the same sequence I put a request for filtering. Tell me in what order it is necessary to write a question considering the filtering.
{
"index":"base",
"type":"info",
"from":0,
"size":10,
"fields":["cID","firm_html"],
"body":
{
"query":
{
"bool":
{
"must_not":[
{
"match":
{
"info.published":"no"
}
}
],
"should":[
{
"query_string":
{
"default_field":
"info.shortname",
"query":"value"
}
},
{
"match":
{
"_all":"value"
}
},
]
},
"function_score":
{
"functions":[
{
"filter":
{
"term":
{
"razm_prio":"0.4"
}
},
"weight":"0.4"
},
],
"score_mode":"sum"
}
}
}
}

Related

ElasticSearch adjacent words for nested queries

I'm using ES 7.14/Kibana 7.10, I have to search for adjacent words (any order), hence I'm using this query:
{
"query":{
"bool":{
"must":[
{
"query_string":{
"query":"*antonio* *banderas*",
"fields":[
"text"
],
"default_operator":"and",
}
}]
}
}
}
This works ok for a text plain field. Now, I have a nested field metadata, let's say the mapping is
{
"mappings:": {
"properties": {
"text": {
"type": "text"
},
"metadata": {
"type": "nested",
"properties": {
"text": {
"type": "text"
}
}
}
}
}
}
and I would like to search that nested field in the same way (adjacent words search), so assumed that it's is possibile to write a nested query for query_string in this way
{
"query": {
"query_string": {
"query": "metadata.text:*antonio* *banderas*"
}
}
}
How to adapt this approach to the previous one with default_operator=and etc.? If I do
{
"query": {
"query_string": {
"query": "metadata.text:*antonio* *banderas*",
"default_operator": "and"
}
}
}
I don't get any result (but any error too).
A similar question, but related to matching adjacent words for multiple nested fields is here.
Adjacent word with any order should not be search with query_string but wildcard or match or term or span_term
There is also a mapping type wildcard optimised for this usage, depends on what type of queries you will need.
So for you first example :
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"text": "*antonio*"
}
},
{
"wildcard": {
"text": "*banderas*"
}
}
]
}
}
}
OR
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"text": "*antonio*banderas*"
}
}
]
}
}
}
and for nested queries :
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "metadata",
"query": {
"bool": {
"must": [
{
"wildcard": {
"metadata.text": "*antonio*"
}
},
{
"wildcard": {
"metadata.text": "*banderas*"
}
}
]
}
}
}
}
]
}
}
}

How to join two queries in one using elasticsearch?

Hi I want to join two queries in one in elasticsearch, but I don't know how to do it: I think I should do an aggregation but I don't know very clear how to do it. Could you help me? My ES version is 5.1.2.
First filter by status and name:
POST test_lite/_search
{
"aggs": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"match": {
"STATUS": "Now"
}
},
{
"match": {
"NAME": "PRUDENTL"
}
}
]
}
}
}
}
}
Look for in the filtered records for the word filtered in description:
POST /test_lite/_search
{
"query": {
"wildcard" : { "DESCRIPTION" : "*english*" }
}
}
The only query needed is:
POST test_lite/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"STATUS": "Now"
}
},
{
"match": {
"NAME": "PRUDENTL"
}
},
{"wildcard" : { "DESCRIPTION" : "*english*" }}
]
}
}
}

Adding boost to Elasticsearch query

I'm trying to add weight to some results from Elasticsearch.
I'm currently only filtering on an 'active' boolean to grab only the published items:
query: {
filtered: {
query: {
match: {
_all: params[:q]
}
},
filter: {
term: {
active: true
}
}
},
}
I now want to add weight to some of my models. For example, a Market should get a +2 boost. I was trying something like this: (search_type is a field on my results, it's basically the Rails model name)
POST _search
{
"query": {
"function_score": {
"query": {
"match": {
"_all": "hospitality"
}
},
"functions": [
{
"filter": {
"term": {
"active": true
}
}
},
{
"filter": {
"term": {
"search_type": "Market"
}
},
"weight": 2
}
]
}
}
}
However, that does not seem to work: "One entry in functions list is missing a function". So I added "weight": 1 to the active filter.. But now it says it can't parse.
I have no experience with ElasticSearch and the docs are quite confusing. I have also tried using a custom_filters_score thing, but that doesn't seem to work for my version of ES (as described here: http://jontai.me/blog/2013/01/advanced-scoring-in-elasticsearch/). Another option I tried was combining a boolean query with must and should, but that returned zero results...
Not sure how to proceed. Some insights would be great.
you should be able to use a filtered query alongside function-score to achieve this
Example:
{
"query": {
"filtered": {
"query": {
"function_score": {
"query": {
"match": {
"_all": "hospitality"
}
},
"functions": [
{
"filter": {
"term": {
"search_type": "Market"
}
},
"weight": 2
}
]
}
},
"filter": {
"term": {
"active": true
}
}
}
}
}

ElasticSeach query not return results

I got 2 querys, the different between them is just 1 filter term.
The first query:
GET _search
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "*"
}
},
"filter": {
"and": {
"filters": [
{
"term": {
"type": "log"
}
},
{
"term": {
"context.blueprint_id": "adv1"
}
},
{
"term": {
"context.deployment_id": "deploy1"
}
}
]
}
}
}
}
}
return this result:
{
"_source": {
"level": "info",
"timestamp": "2014-03-24 10:12:41.925680",
"message_code": null,
"context": {
"blueprint_id": "Adv1",
"execution_id": "27efcba7-3a60-4270-bbe2-9f17d602dcef",
"deployment_id": "deploy1"
},
"type": "log",
"#version": "1",
"#timestamp": "2014-03-24T10:12:41.927Z"
}
}
The second query is:
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "*"
}
},
"filter": {
"and": {
"filters": [
{
"term": {
"type": "log"
}
},
{
"term": {
"context.blueprint_id": "adv1"
}
},
{
"term": {
"context.deployment_id": "deploy1"
}
},
{
"term": {
"context.execution_id": "27efcba7-3a60-4270-bbe2-9f17d602dcef"
}
}
]
}
}
}
}
}
return empty results.
the different between them is in the second query, i just add this term:
{
"term": {
"context.execution_id": "27efcba7-3a60-4270-bbe2-9f17d602dcef"
}
}
and in the result we can see that there is result match to that query, but it still not work.
what i'm doing wrong here?
thanks.
By default, ElasticSearch will treat string fields as text and will analyze them (i.e. tokenize, stem etc. before indexing). This means you might not be able to find them when searching for their exact content.
You should make sure the mapping for the execution_id field is not analyzed. Start with GET /_mappings and work from there. :)

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