How to search by array elements in elasticsearch?

16,314

This is correct usage, as your update mentions.

If you import a document with a "mentions" array, searching on a matching array item, referring to it as "mentions", will retrieve the document. I had the same issue and verified it myself just now.

Share:
16,314
CheatEx
Author by

CheatEx

A curious engineer.

Updated on July 08, 2022

Comments

  • CheatEx
    CheatEx almost 2 years

    I have a document indexed in elasticsearch:

    {
        "content":"Some content with @someone mention",
        "mentions":["someone"],
        "userId":"4dff31eaf8815f4df04e2d62"
    }

    I try to find it with a query:

    {
        "query": {
            "filtered": {
                "filter": { "term":{"userId":"4dff31eaf8815f4df04e2d62"} },
                "query": {
                    term: {"mentions":"someone"}
                }
            }
        }
    }

    and receive no results.

    In the same time query for content works fine:

    {
        "query": {
            "filtered": {
                "filter": { "term":{"userId":"4dff31eaf8815f4df04e2d62"} },
                "query": {
                    "term": {"content":"some"}
                }
            }
        }
    }

    Is some special syntax required for search through arrays? I found several topics [1, 2] about arrays in elasticsearch, but there is no direct answer.

    UPD Get Mapping call returns the next result:

    {
        "records": {
            "all":{
                "properties":{
                    "content":{"type":"string"},
                    "userId":{"type":"string"},
                    "mentions":{"type":"string"}
                }
            }
        }
    }

    UPD2 I found the source of problem. I accidentally introduced an error into my question. The username I actually had in DB was in form "some_one" (underscore is significant). So standard index split it into 2 words and query for "some_one" of cause failing.