2

I am using elastic search to find people in cities. So I have the properties firstname, lastname and city.

A possible search term is for example "Charlotte Renee Tucker" which should return the following people: {firstname: "Charlotte Renee", lastname: "Tucker", city: "New York"} or {firstname: "Renee", lastname: "Tucker", city: "Charlotte"}, but not {firstname: "Charlotte", lastname: "Tucker", city: "New Orleans"}.

So all terms must occur in some field. In the last example the word "Renee" is not part of the result so it should not be returned. It is ok if all words are found in just one property, for example if your firstname is "Charlotte Renee Tucker", it is a valid search result. My query currently looks like this:

{
  "bool": {
    "should": [
      {
        "match": {
          "lastname": {
            "query": "Charlotte Renee Tucker",
            "operator": "OR"
          }
        }
      },
      {
        "match": {
          "firstname": {
            "query": "Charlotte Renee Tucker",
            "operator": "OR"
          }
        }
      },
      {
        "match": {
          "city": {
            "query": "Charlotte Renee Tucker",
            "operator": "OR"
          }
        }
      }
    ]
  }
}

But this would also return people where just one word matches some property. I have played with the minimum_should_match parameter, but this won't work if all words are found in just one property. Can somebody help?

This is my mapping:

{
  "people": {
    "mappings": {
      "properties": {
        "city": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "firstname": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "lastname": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

And I have added the following documents:

{"firstname": "Charlotte Renee", "lastname": "Tucker", "city": "New York"}

{"firstname": "Renee", "lastname": "Tucker", "city": "Charlotte"}

{"firstname": "Charlotte", "lastname": "Tucker", "city": "New Orleans"}

With the aforementioned query all documents are found, but I don't want the last one to be found because it does not contain the word "Renee".

Matthew Darton
  • 551
  • 1
  • 7
  • 27

1 Answers1

0

I think what you search for is a copy_to parameter, by which you can compound a field of all 3 searched fields and then use AND instead of OR (as in the exeample here: https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html)

Petr
  • 1,159
  • 10
  • 20