Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search by array elements in elasticsearch?

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.

like image 795
CheatEx Avatar asked Jun 24 '11 09:06

CheatEx


People also ask

How do you search data in Elasticsearch index?

You can use the search API to search and aggregate data stored in Elasticsearch data streams or indices. The API's query request body parameter accepts queries written in Query DSL. The following request searches my-index-000001 using a match query. This query matches documents with a user.id value of kimchy .

What is indexing Elasticsearch?

In Elasticsearch, an index (plural: indices) contains a schema and can have one or more shards and replicas. An Elasticsearch index is divided into shards and each shard is an instance of a Lucene index. Indices are used to store the documents in dedicated data structures corresponding to the data type of fields.

How does search in Elasticsearch work?

Elasticsearch uses a data structure called an inverted index, which is designed to allow very fast full-text searches. An inverted index lists every unique word that appears in any document and identifies all of the documents each word occurs in.

What is Terms query in Elasticsearch?

You can use the term query to find documents based on a precise value such as a price, a product ID, or a username. Avoid using the term query for text fields. By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.


1 Answers

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.

like image 158
mahemoff Avatar answered Oct 04 '22 14:10

mahemoff