1

I got fields like that: names: ["Red:123", "Blue:45", "Green:56"] it's mapping is

"names": {
    "type": "keyword"
},

how could I search like this

{
  "query": {
    "match": {
      "names": "red"
    }
  }
}

to get all the documents where red is in element of names array? Now it works only with

{
  "query": {
    "match": {
      "names": "red:123"
    }
  }
}

1 Answer 1

1

You can add multi fields OR just change the type to text, to achieve your required result

Index Mapping using multi fields

{
  "mappings": {
    "properties": {
      "names": {
        "type": "text",
        "fields": {
          "raw": { 
            "type":  "keyword"
          }
        }
      }
    }
  }
}

Adding a working example with index data, mapping, search query, and search result

Index Mapping:

{
  "mappings":{
    "properties":{
      "names":{
        "type":"text"
      }
    }
  }
}

Index Data:

{
  "names": [
    "Red:123",
    "Blue:45",
    "Green:56"
  ]
}

Search Query:

{
  "query": {
    "match": {
      "names": "red"
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "64665127",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "names": [
            "Red:123",
            "Blue:45",
            "Green:56"
          ]
        }
      }
    ]
Sign up to request clarification or add additional context in comments.

10 Comments

Sorry I forgot to add that mapping is keyword
I think I am not, I could use prefix or wildcard instead of match but as I understand from docs it is expensive operations, what do you think?
Yes, @ЕгорЛебедев Searching with leading wildcards is quite expensive and is going to be extremely slow on a large index and will affect your search performance.
@ЕгорЛебедев can you add one extra field in your mapping, if yes then the match query will work? Let me know if you can do this so that I could provide a working example for the same
I need to contact DB admin for that, so what variants do I have? add additional element or change type to text?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.