Match a document when user keywords contains all words of a field

Hello everyone,

I would like to set up a simple elasticsearch index, to retrieve some documents.

I will have a field, named "attributes", which will contains some keywords:

"attributes": [
"Lebron James",
"Michael Jordan"
]

I want that when the end user will search for "Lebron", it won't match this document.
When he will search "Lebron James", it should match it.
When he will search "Lebron James Basketball", or "nba Lebron James", it should also match the document.

Is it possible to do?

I tried to to terms queries, but in that case, "Lebron James Basketball", or "nba Lebron James" doesn't match.
I tried to add analyzers, with standard tokenizer.
But the search "Lebron" matches the document, and I don't want that behaviour.
I also tried to add an analyzer with "keyword" tokenizer, and to change the type of my field from "text" to "keyword".
But none of those configurations match "Lebron James Basketball", or "nba Lebron James" .

I'm not sure it is possible to do in Elastic.
If you have any idea, I thank you in advance!

Thanks,

Marc

I think you will have to have your document type as nested or searches like "Lebron Jordan" would show up. Don't think this is an ideal solution but the below works.

PUT goats
{
  "mappings": {
    "properties": {
      "goat": {
        "type": "nested" 
      }
    }
  }
}
POST goats/_doc
{
  "goat" : [
    {
      "first" : "Michael",
      "last" :  "Jordan"
    },
    {
      "first" : "Lebron",
      "last" :  "James"
    }
  ]
}
GET goats/_search
{
  "query": {
    "nested": {
      "path": "goat",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "goat.first": "Lebron James Basketball"
              }
            },
            {
              "match": {
                "goat.last": "Lebron James Basketball"
              }
            }
          ]
        }
      }
    }
  }
}

So you would be passing in the full search phrase to both first/last name fields and you would need to get a match on both to return a result.

Thanks Aaron for your answer.
I will try to adapt your example.

To clarify my first example, the field I want to query wont always contain first and last names.
It will contains some keywords referencing the document.
It could be:
"attributes": [
"Lebron James",
"fast red car",
"this is a long keyword"
]

And, at query time, I don't know the content of the field "attributes".
In this exemple, I want the query "This is a fast red car" to match the document (because the input of the user match the complete string of attributes field, even if the user has added more keywords ("this is a") )

If I understand well, it is impossible to match a field as "full-text" with only a sub part of the user's input :cry:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.