How to filter the follwing query in elasticsearch? - elasticsearch

I am using the following search:
{
"_source": [
"title",
"bench",
"court",
"id_"
],
"size": 10,
"query": {
"bool": {
"must": {
"multi_match": {
"query": "murder"
,
"fields": [
"title",
"content"
]
}
},
"should": {
"multi_match": {
"query": "murder",
"fields": [
"title.standard",
"content.standard"
]
}
}
}
},
"highlight": {
"fields": {
"title": {},
"content": {}
}
}
}
I now want to filter the results using the id (_id) elastic search gave it during indexing. For example, {"_id" : 5903}. I guess you have to use the term query. The results should be such that only if the _id is matched, the document returns. How can I do that?

In order to get your query filtered by doc's id (one, or many), there is a special id query in elasticsearch. Here are the details: https://www.elastic.co/guide/en/elasticsearch/reference/master/query-dsl-ids-query.html

Related

Elasticsearch Query NOT searching in the specified fields

I am struggling with an elasticsearch query. In the fields option, we have specified '*' which means it should look in all fields as well as given the higher weights to a few fields. But it isn't working as it should.
This query was written by my colleague, it'd be great if you could explain it as well as point out the solution. Here's my query:
{
"query": {
"bool": {
"must": [
{
"simple_query_string": {
"query": "Atoms for Peace",
"default_operator": "AND",
"flags": "PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE",
"fields": [
"*",
"systemNumber^5",
"global_search",
"objectType^2",
"partTypes.text",
"partTypes.id",
"gs_am_people^2",
"gs_am_person^2",
"gs_am_org^2",
"gs_title^2",
"_currentLocation.displayName",
"briefDescription",
"physicalDescription",
"summaryDescription",
"_flatPersonsNameId",
"_flatPeoplesNameId",
"_flatOrganisationsNameId",
"_primaryDate",
"_primaryDateEarliest",
"_primaryDateLatest"
]
}
}
]
}
}
Your query is fine but it will not work on field with "nested" data type.
From doc
Searching across all eligible fields does not include nested documents. Use a nested query to search those documents.
You need to use nested query
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"simple_query_string": {
"query": "Atoms for Peace",
"default_operator": "AND",
"flags": "PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE",
"fields": [
"*",
"systemNumber^5",
"global_search",
"objectType^2",
"partTypes.text",
"partTypes.id",
"gs_am_people^2",
"gs_am_person^2",
"gs_am_org^2",
"gs_title^2",
"_currentLocation.displayName",
"briefDescription",
"physicalDescription",
"summaryDescription",
"_flatPersonsNameId",
"_flatPeoplesNameId",
"_flatOrganisationsNameId",
"_primaryDate",
"_primaryDateEarliest",
"_primaryDateLatest"
]
}
},
{
"nested": {
"path": "record",
"query": {
"simple_query_string": {
"query": "Atoms for Peace",
"default_operator": "AND",
"flags": "PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE",
"fields": [
"*"
]
}
}
}
}
]
}
}
}

ElasticSeach combine multi_match and match_phrase

I use ES 7, I want to search over multi fields, but on this field (title) must be shown firstly if it matches exactly. For now I tried :
{
"query": {
"bool": {
"must": {
"bool": {
"should": [
{
"match_phrase": {
"titre": {
"query": "test",
"boost": "20"
}
}
},
{
"multi_match": {
"fields": ["titre", "description^4", "subtitle^3"],
"query": "test",
"type": "most_fields"
}
}
]
}
}
}
}
}
It works, but I would like to order the match_phrase before other results.
The idea is the user type the exact phrase of a title, this result will appear before other based on multi_match.
Is it possible ?

Elastic : search two terms, one on _all, other one on a field

I would like to mix a search on a whole document (eg "developer") and a search on some field for another term (eg "php").
I can do each search separately but I can't mix them.
Here my example (simplified to show only my issue) :
{
"query": {
"function_score": {
"query": {
"match": {
"_all": "developer"
},
"multi_match": {
"query": "php",
"fields": [
"skills.description",
"skills.description",
"skills.details"
],
"operator": "or",
"type": "most_fields"
}
}
}
}
If I run this example I have an error :
Parse Failure [Failed to parse source
Is there a way to search on both _all and specific fields with two terms?
Thanks.
Yes, you're almost there, you need to combine them into a bool/must query:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"match": {
"_all": "developer"
}
},
{
"multi_match": {
"query": "php",
"fields": [
"skills.description",
"skills.description",
"skills.details"
],
"operator": "or",
"type": "most_fields"
}
}
]
}
}
}
}
}

Elasticsearch Multipal queries with limit

I am trying to write an Elasticsearch query where I match multiple words in my title and description. The below code works fine but it gives all the articles matching those words. My aim is I need 4 articles per query word for e.g. 4 results of Tim Cook and four articles of Steve Jobs
{
"query": {
"multi_match": {
"query": ["Tim Cook","Steve Jobs"],
"fields": ["Title", "Description" ],
"operator":"AND"
}
}
}
Top hits aggregations are what you are looking for -
Basically give 2 filter aggregation and then nest top hits aggregation side them.
So something like below should work fine
{
"size": 0,
"query": {
"multi_match": {
"query": [
"Tim Cook",
"Steve Jobs"
],
"fields": [
"Title",
"Description"
],
"operator": "AND"
}
},
"aggs": {
"tim": {
"aggs": {
"top_hits": {}
},
"filter": {
"query": {
"multi_match": {
"query": [
"Tim Cook"
],
"fields": [
"Title",
"Description"
],
"operator": "AND"
}
}
}
},
"steve": {
"aggs": {
"top_hits": {}
},
"filter": {
"query": {
"multi_match": {
"query": [
"Steve Jobs"
],
"fields": [
"Title",
"Description"
],
"operator": "AND"
}
}
}
}
}
}

elasticsearch : boosting documents in has_parent query

Is there a way to boost the docs that "come" from the has_parent query ?
{
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"multi_match": {
"fields": ["name^3", "tags^2", "content"],
"query": "xx"
}
},
{
"has_parent": {
"type": "theparent",
"query": {
"multi_match": {
"type": "best_fields",
"fields": ["name^5", "content"],
"query": "xx"
}
}
}
},
{
"has_child": {
"type": "thechild",
"query": {
"multi_match": {
"fields": ["name^3","content"],
"query": "xx"
}
}
}
}
]
}
},
"score_mode": "sum",
"functions": [
{
"linear": {
"date": {
"origin": "2014-08-29",
"scale": "700d",
"decay": 0.6
}
}
}
]
}
}
More exactly, I would like to boost those docs only when the query matches the name field of the parent
(I haven't found a way to refer to a parent field in the functions i.e. theparent._source.name ~= "xx")
According to the sources from Github (see line 104), the boost parameter is allowed within a has_parent query.
Based on this attribute, you can boost specifically the should clause containing the has_parent query. In your case, the result would be :
...
{
"has_parent": {
"type": "theparent",
"query": {
"multi_match": {
"type": "best_fields",
"fields": ["name^5", "content"],
"query": "xx"
}
},
"boost": 5
}
}
...
I don't know if it can help you, but you will find more insights about boosting query clauses here.

Resources