0

To simplify:

PUT /test/vendors/1
{
  "type": "doctor",
  "name": "Ron",
  "place": "Boston"  
}

PUT /test/vendors/2
{
  "type": "doctor",
  "name": "Tom",
  "place": "Boston"  

}

PUT /test/vendors/3
{
  "type": "doctor",
  "name": "Jack",
  "place": "San Fran"  

}

Then search:

GET /test/_search
{
  "query": {
    "multi_match" : {
      "query":    "doctor in Boston", 
      "fields": [ "type", "place" ] 
    }
  }
}

I understand why I get Jack who works in San Fran -- it's because he's a doctor too. However, I can't figure out why the match score is the SAME for him. The other two were matched with the place too, weren't they? why aren't Ron and Tom scored higher?

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.9245277,
    "hits": [
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "2",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Tom",
          "place": "Boston"
        }
      },
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "1",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Ron",
          "place": "Boston"
        }
      },
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "3",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Jack",
          "place": "San Fran"
        }
      }
    ]
  }
}

Is there a way to force it to score less when less search keywords are found? Also, If I'n going to wrong way about this kind of search and there's a better pattern/way to do it -- I'd appreciate to be pointed in the right direction.

JasonGenX
  • 4,952
  • 27
  • 106
  • 198

2 Answers2

0

Your search structure is incorrect. The search query above is ignoring the place property and that's why you get the same score for all documents (only type property is taken into account). The reason for that is because works_at is a nested mapping, which should be treated differently when searching.

First, you should defined works_at as a nested mapping (read more here). Then you'll have to adjust your query to work with that nested mapping, see an example here.

tomericco
  • 1,544
  • 3
  • 19
  • 30
  • Interesting. Didn't cross my mind. I'll give it a try thanks! – JasonGenX Mar 11 '19 at 16:25
  • I tried removing the nesting. Still - I am getting the same score for everything (edited the post). I will definitely take your advice about nesting, but the question remains, why is my search results scored similarly despite the missing "boston" on the last document. – JasonGenX Mar 11 '19 at 16:37
-1
GET /test/_search
{
  "query": {
    "multi_match" : {
      "query":    "doctor in Boston", 
      "fields": [ "type", "place" ],
      "type": "most_fields" .   <---- I WAS MISSING THIS
    }
  }
}

once in, that gave the correct results, where the "San Fran" guy is scored lower.

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1.2122098,
    "hits": [
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "2",
        "_score": 1.2122098,
        "_source": {
          "type": "doctor",
          "name": "Tom",
          "place": "Boston"
        }
      },
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "1",
        "_score": 1.2122098,
        "_source": {
          "type": "doctor",
          "name": "Ron",
          "place": "Boston"
        }
      },
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "3",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Jack",
          "place": "San Fran"
        }
      }
    ]
  }
}
JasonGenX
  • 4,952
  • 27
  • 106
  • 198
  • Are you sure this fixed it? I still get the same score for all results. Pointing to the place field is still wrong, isn't it? – tomericco Mar 12 '19 at 11:57