Searching multiple fields and defining relevance of each field individually from a single search box


(sebastian.paetzold) #1

Hi there,

I'm currently trying to find a solution to the following problem which I
failed to solve via Google etc.

Imagine a index containing persons with the fields first name and last name
(in reality there are even more to be searched fields like address).
I would like to search these fields by providing the user a single line
search box. So for example the user enters "chr pohl" in the search box, a
query like "chr AND pohl" should be constructed from it. In the past I
used a combined field which just concats the first name and last name
before indexing in ES and searched on this field. That worked pretty well.

However now I would like to do rank documents with a hit in the last name
field higher than in the first name field. How could I do that? I tried a
multi match query like

{
"query": {
"multi_match": {
"query": "Christian AND Dorn",
"fields": ["firstName", "lastName"]
}
},
"fields": ["firstName", "lastName"]
}

Of course this works not quite as expected as the terms are rewritten as a
should clause. So instead of narrowing my search results by being more
precise (enter more letters basically) more and more search results are
returned. Furthermore I'm pretty sure that multi match does not parse the
query string as specified above (so the AND above is yet another search
term and leads for example to found Andrea's). However I need something
like a multi match to rank certain fields higher.

End of story: Documents are returned which do not feature ALL of the search
terms. More terms in the search box leads to more documents returned, not
less.

"firstName": "Andrea"
"lastName": "Christian"

"firstName": "Ilse"
"lastName": "Christian"

"firstName": "Maren"
"lastName": "Christian"

So how could I do this properly? How can I rank certain fields higher and
still be searching across fields as outlined above?

Thanks for your help.

-Sebastian

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Alexander Reelsen) #2

Hey there Sebastian,

having a quick peek at your query and reading the multi_match/match query
docs, it seems you are actually executing an or connected query. In order
to change this, you would need to set the 'operator' option inside of the
multi_match query to 'and'. What happens here (with the standard analyzer)
is that "christian and dorn" will remove stopwords and become 'christian
dorn', which then becomes an or query.

You can do field boosting by using the ^ notation in the fields field...
see
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html

Hope this helps, or otherwise I got your requirement wrong :slight_smile:

--Alex

On Mon, Nov 11, 2013 at 10:26 AM, sebastian.paetzold@ysura.com wrote:

Hi there,

I'm currently trying to find a solution to the following problem which I
failed to solve via Google etc.

Imagine a index containing persons with the fields first name and last
name (in reality there are even more to be searched fields like address).
I would like to search these fields by providing the user a single line
search box. So for example the user enters "chr pohl" in the search box, a
query like "chr AND pohl" should be constructed from it. In the past I
used a combined field which just concats the first name and last name
before indexing in ES and searched on this field. That worked pretty well.

However now I would like to do rank documents with a hit in the last name
field higher than in the first name field. How could I do that? I tried a
multi match query like

{
"query": {
"multi_match": {
"query": "Christian AND Dorn",
"fields": ["firstName", "lastName"]
}
},
"fields": ["firstName", "lastName"]
}

Of course this works not quite as expected as the terms are rewritten as a
should clause. So instead of narrowing my search results by being more
precise (enter more letters basically) more and more search results are
returned. Furthermore I'm pretty sure that multi match does not parse the
query string as specified above (so the AND above is yet another search
term and leads for example to found Andrea's). However I need something
like a multi match to rank certain fields higher.

End of story: Documents are returned which do not feature ALL of the
search terms. More terms in the search box leads to more documents
returned, not less.

"firstName": "Andrea"
"lastName": "Christian"

"firstName": "Ilse"
"lastName": "Christian"

"firstName": "Maren"
"lastName": "Christian"

So how could I do this properly? How can I rank certain fields higher and
still be searching across fields as outlined above?

Thanks for your help.

-Sebastian

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #3