elastic search query result sorting - elasticsearch

I have below query which is resulting in correct data. But looking to have sorting done, means have exact match first then rest.
{
"query": {
"query_string": {
"query": "Han* Ol*",
"fields": [
"firstName",
"lastName",
"customerNo"
],
"default_operator": "and",
"analyze_wildcard":true
}
}
}
I want to have exact match on top then others like -
Hanry Oliver,
M Hanry Oliver,
Hanry Tran Oliver

The term "exact matches" in the context of wildcard queries is ambiguous, if not meaningless. But if I understand you correctly, you could boost the "exact matches" through a phrase_prefix and by adjusting the query string to (Han).* (Ol).*:
{
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "(Han).* (Ol).*",
"fields": [
"firstName",
"lastName",
"customerNo"
],
"default_operator": "and",
"type": "phrase_prefix",
"analyze_wildcard": true,
"boost": 10
}
},
{
"query_string": {
"query": "Han* Ol*",
"fields": [
"firstName",
"lastName",
"customerNo"
],
"default_operator": "and",
"analyze_wildcard": true
}
}
]
}
}
}

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": [
"*"
]
}
}
}
}
]
}
}
}

How to boost exact match on multi match in elastic search

I have several documents with a field name title contain value like foo bar and foo:bar.
I'm trying to boost an exact match foo:bar documents and sort the result according to the score.
Here is my attempt, any help would be appreciated.
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "",
"fields": [
"title",
]
}
}
],
"should": [
{
"multi_match": {
"query": "",
"fields": [
"title",
]
"type": "phrase"
"boost": 20
}
}
],
}
}

elasticsearch cross fields query alternative for fuzziness?

I have a cross-fields query, and I understand already that you cant use fuzziness with cross-fields queries, but I dont understand the alternative...
this is my simple query:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "John Legend",
"fields": [
"fname^-4.0",
"lname^-1.0",
"city^-1.0",
],
"type": "cross_fields",
"lenient": "true",
"operator": "AND"
}
}
],
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
I want to be able to find:
John Legend
Joh
John Lege
is that possible?

how to perform partial match elasticsearch

i want to perform both exact match and partial match. for example, "Alize", so if i type "Ali" it should return the result of "Alize" as well. for this case i only can return the result if i type exact word "Alize".
POST /ecommerce/_search
'{
"query": {
"multi_match": {
"fields": [
"name"
],
"operator": "AND",
"query": "Ali*"
}
},
"size": 20,
"stored_fields": [
"uid",
"_source"
]
}`
You can use querystring query as following
"query": {
"query_string": {
"query": "Ali*",
"fields": ["name"]
}
}
Or use wildcard
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"query": {"wildcard": {"name": {"value": "Ali*"}}}},
]
}
}
}
}
Wildcard document
This solutions work perfectly for django_elasticsearch_dsl
search_keyword = search_keyword + "*"
query = document_class.search().query(
{
"query_string": {
"query": search_keyword,
"fields": ["name", "code"]
}
}

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"
}
}
}
}
}
}

Resources