AbtPst
AbtPst

Reputation: 8018

Elsticsearch : Contains query

I have a column in my mapping that holds an array of strings

col1
["asd","fgh","wer"]
["qwer","cvbvbn","popop"]
["cvbml","fhjhfrjk","fsdfd"]
["asd","trth","fdf"]

The column col is not analyzed in the index and i do not want to change the mapping.

"col1":
{
    "type":"string",
    "index":"not_analyzed"
}

Now, i want to retrieve all records where the string asd appears. so in this case, i want the first and fourth records. I tried using the query

        query: {

                    wildcard:{
                        "col1":"asd"
                    }
                }

with

POST localhost:9200/indexName/test/_search

but that gives me empty results? Which query should i use in this case?

Edit

So i was able to solve the above problem. Here is a follow up. Consider that this was my data

col1
["asd fd","fgh bn","wer kl"]
["qwer","cvbvbn","popop"]
["cvbml","fhjhfrjk wewe","fsdfd rtr"]
["asd","trth","fdf"]

so now, the array contains some strings that have multiple words. Now, i still want to return the first and fourth record. If i go with the solution that i posted, i only get the fourth one. How can i apply the contains logic to each element of the array in col1?

Note

A partial solution is

{ "query": { "match_phrase_prefix": { "col1": "asd" } } }

so again, for the data

col1
["asd fd","fgh bn","wer kl"]
["qwer","cvbvbn","popop"]
["cvbml","fhjhfrjk wewe","fsdfd rtr"]
["asd","trth","fdf"]

it returns the first and fourth records. However, if i have

col1
["fd asd","fgh bn","wer kl"]
["qwer","cvbvbn","popop"]
["cvbml","fhjhfrjk wewe","fsdfd rtr"]
["asd","trth","fdf"]

then, once again it only returns the fourth one, which is understandable as now, asd is no longer a prefix for that value in the first record.

Is there a way to to a contains type match instead of just prefix match?

Upvotes: 0

Views: 69

Answers (2)

AbtPst
AbtPst

Reputation: 8018

so, here is the proper query

{

                    fields : ["col1","col2"],
                    query: {
                        filtered: {

                          query: {
                            match_all: {}
                          },
                          filter: {
                            terms: {
                              col1: ["asd"]
                            }
                          }
                        }
                      }

                }

Final Answer

    query: {

                wildcard:{

                  col1:{

                          value:"*asd*"
                       }
                }
            }

:)

Upvotes: 0

Val
Val

Reputation: 217474

You can use a simple term query and it should work

POST localhost:9200/indexName/test/_search
{
    "query": {
        "terms": { "col1" : "asd" }
    }
}

Upvotes: 2

Related Questions