Hi,
you shouldn't need to change the query to work the way you describe. If you test this with some simple documents like this:
PUT /index/type/1
{
"text" : "look for this and something more"
}
PUT /index/type/2
{
"text" : "look for this"
}
PUT /index/type/3
{
"text" : "this look is not so good"
}
PUT /index/type/4
{
"text" : "one look is not enough"
}
And you use the AND operator like you described you get only the documents that contain all terms:
"hits": [
{
"_index": "index",
"_type": "type",
"_id": "2",
"_score": 1.5686159,
"_source": {
"text": "look for this"
}
},
{
"_index": "index",
"_type": "type",
"_id": "1",
"_score": 0.80226827,
"_source": {
"text": "look for this and something more"
}
}
]
The reason for document "2" ranking highter is that in standard scoring terms appearing in shorter fields carry more weight (see e.g. https://www.elastic.co/guide/en/elasticsearch/guide/current/relevance-intro.html) for some explanation.
Using the OR operator you get all the documents that contain any of the terms, but the documents containing more terms ranked higher:
"hits": [
{
"_index": "index",
"_type": "type",
"_id": "2",
"_score": 1.5686159,
"_source": {
"text": "look for this"
}
},
{
"_index": "index",
"_type": "type",
"_id": "1",
"_score": 0.80226827,
"_source": {
"text": "look for this and something more"
}
},
{
"_index": "index",
"_type": "type",
"_id": "3",
"_score": 0.53484553,
"_source": {
"text": "this look is not so good"
}
},
{
"_index": "index",
"_type": "type",
"_id": "4",
"_score": 0.16203022,
"_source": {
"text": "one look is not enough"
}
}
]
When terms appear across different fields, scoring gets more complicated but thats another kind of discussion. Generally speaking, using OR should rank the documentes higher that contain all (or many) of the search terms.