elasticsearch giving more weight to different fields and scenarios
I have this ES query:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "test",
"fields": [
"name^-1.0",
"id^-1.0",
"address.city^-1.0",
"address.street^-1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1.0,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
and currently what happens is, when I search for person with the name john
, I will get bunch of results that the id, address.city, address.street
contains john in them, which is fine, but I want name
to be more important, and also if I have in the es 2 people john
and someone with 2 names like george john
I would want the just john
to come up first.
can I do that? :)
elasticsearch elasticsearch-5 elastic4s
add a comment |
I have this ES query:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "test",
"fields": [
"name^-1.0",
"id^-1.0",
"address.city^-1.0",
"address.street^-1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1.0,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
and currently what happens is, when I search for person with the name john
, I will get bunch of results that the id, address.city, address.street
contains john in them, which is fine, but I want name
to be more important, and also if I have in the es 2 people john
and someone with 2 names like george john
I would want the just john
to come up first.
can I do that? :)
elasticsearch elasticsearch-5 elastic4s
add a comment |
I have this ES query:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "test",
"fields": [
"name^-1.0",
"id^-1.0",
"address.city^-1.0",
"address.street^-1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1.0,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
and currently what happens is, when I search for person with the name john
, I will get bunch of results that the id, address.city, address.street
contains john in them, which is fine, but I want name
to be more important, and also if I have in the es 2 people john
and someone with 2 names like george john
I would want the just john
to come up first.
can I do that? :)
elasticsearch elasticsearch-5 elastic4s
I have this ES query:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "test",
"fields": [
"name^-1.0",
"id^-1.0",
"address.city^-1.0",
"address.street^-1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1.0,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
and currently what happens is, when I search for person with the name john
, I will get bunch of results that the id, address.city, address.street
contains john in them, which is fine, but I want name
to be more important, and also if I have in the es 2 people john
and someone with 2 names like george john
I would want the just john
to come up first.
can I do that? :)
elasticsearch elasticsearch-5 elastic4s
elasticsearch elasticsearch-5 elastic4s
asked Jan 1 at 16:45
JohnBigsJohnBigs
1,0131539
1,0131539
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
To make any field more important than other(s), you can set its boost to a higher value. So if fieldA^4
and fieldB^1
it implies that fieldA
is 4 times more important than fieldB
. Therefore you can give higher boost value to name
field to make it more important for scoring.
For second point the document with name
field value as john
will have higher score than with a document having name
field value as george john
(assuming that other fields have same data in both documents). The reason you are get the second doc (george john) higher in result is because you have boosted all the fields with negative value.
So to cater to both of your points
- give higher boost to
name
- make boost for all fields as positive value.
So the query should look as below:
{
//"explain": true,
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "john",
"fields": [
"name^4.0",
"id^1.0",
"address.city^1.0",
"address.street^1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
To understand more on how the score for the matching document is calculated by elastic, you can use the "explain": true
in your query. This will give detailed steps in result, taken by elastic to calculate the score.
thanks allot! @Nishant Saini
– JohnBigs
Jan 2 at 11:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997209%2felasticsearch-giving-more-weight-to-different-fields-and-scenarios%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To make any field more important than other(s), you can set its boost to a higher value. So if fieldA^4
and fieldB^1
it implies that fieldA
is 4 times more important than fieldB
. Therefore you can give higher boost value to name
field to make it more important for scoring.
For second point the document with name
field value as john
will have higher score than with a document having name
field value as george john
(assuming that other fields have same data in both documents). The reason you are get the second doc (george john) higher in result is because you have boosted all the fields with negative value.
So to cater to both of your points
- give higher boost to
name
- make boost for all fields as positive value.
So the query should look as below:
{
//"explain": true,
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "john",
"fields": [
"name^4.0",
"id^1.0",
"address.city^1.0",
"address.street^1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
To understand more on how the score for the matching document is calculated by elastic, you can use the "explain": true
in your query. This will give detailed steps in result, taken by elastic to calculate the score.
thanks allot! @Nishant Saini
– JohnBigs
Jan 2 at 11:48
add a comment |
To make any field more important than other(s), you can set its boost to a higher value. So if fieldA^4
and fieldB^1
it implies that fieldA
is 4 times more important than fieldB
. Therefore you can give higher boost value to name
field to make it more important for scoring.
For second point the document with name
field value as john
will have higher score than with a document having name
field value as george john
(assuming that other fields have same data in both documents). The reason you are get the second doc (george john) higher in result is because you have boosted all the fields with negative value.
So to cater to both of your points
- give higher boost to
name
- make boost for all fields as positive value.
So the query should look as below:
{
//"explain": true,
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "john",
"fields": [
"name^4.0",
"id^1.0",
"address.city^1.0",
"address.street^1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
To understand more on how the score for the matching document is calculated by elastic, you can use the "explain": true
in your query. This will give detailed steps in result, taken by elastic to calculate the score.
thanks allot! @Nishant Saini
– JohnBigs
Jan 2 at 11:48
add a comment |
To make any field more important than other(s), you can set its boost to a higher value. So if fieldA^4
and fieldB^1
it implies that fieldA
is 4 times more important than fieldB
. Therefore you can give higher boost value to name
field to make it more important for scoring.
For second point the document with name
field value as john
will have higher score than with a document having name
field value as george john
(assuming that other fields have same data in both documents). The reason you are get the second doc (george john) higher in result is because you have boosted all the fields with negative value.
So to cater to both of your points
- give higher boost to
name
- make boost for all fields as positive value.
So the query should look as below:
{
//"explain": true,
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "john",
"fields": [
"name^4.0",
"id^1.0",
"address.city^1.0",
"address.street^1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
To understand more on how the score for the matching document is calculated by elastic, you can use the "explain": true
in your query. This will give detailed steps in result, taken by elastic to calculate the score.
To make any field more important than other(s), you can set its boost to a higher value. So if fieldA^4
and fieldB^1
it implies that fieldA
is 4 times more important than fieldB
. Therefore you can give higher boost value to name
field to make it more important for scoring.
For second point the document with name
field value as john
will have higher score than with a document having name
field value as george john
(assuming that other fields have same data in both documents). The reason you are get the second doc (george john) higher in result is because you have boosted all the fields with negative value.
So to cater to both of your points
- give higher boost to
name
- make boost for all fields as positive value.
So the query should look as below:
{
//"explain": true,
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "john",
"fields": [
"name^4.0",
"id^1.0",
"address.city^1.0",
"address.street^1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
To understand more on how the score for the matching document is calculated by elastic, you can use the "explain": true
in your query. This will give detailed steps in result, taken by elastic to calculate the score.
answered Jan 2 at 3:35


Nishant SainiNishant Saini
2,0481019
2,0481019
thanks allot! @Nishant Saini
– JohnBigs
Jan 2 at 11:48
add a comment |
thanks allot! @Nishant Saini
– JohnBigs
Jan 2 at 11:48
thanks allot! @Nishant Saini
– JohnBigs
Jan 2 at 11:48
thanks allot! @Nishant Saini
– JohnBigs
Jan 2 at 11:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997209%2felasticsearch-giving-more-weight-to-different-fields-and-scenarios%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown