Reputation: 45
i want to perform query like this on elastic search =>
select * from orders where customerName = 'google' and Type = 'stackoverflow' and query_string can be from any index. (i e query string can be 'google' or 'stackoverflow' or 'a' or 'b' or 'c 'or 'd'
table orders:
customerName type column1 column2 column3 column4
google stackoverflow a b c d
apple stackoverflow a b c d
google stackoverflow a b c d
microsoft stackoverflow a b c d
expected output:
google stackoverflow a b c d
google stackoverflow a b c d
i.e row 1 and row 3
i tried using
"query": {
"bool": {
"must": {
"multi_match": {
"query": "b"
}
},
"filter": {
"terms": {
"customerName": [ "google" ],
"type": [ "stackoverflow ]
}
}
}
}
please help :)
Upvotes: 0
Views: 820
Reputation: 16192
Adding a working example with index data, search query, and search result
Index Data:
{
"customerName": "google",
"type": "stackoverflow",
"column1": "a",
"column2": "b",
"column3": "c",
"column4": "d"
}
{
"customerName": "apple",
"type": "stackoverflow",
"column1": "a",
"column2": "b",
"column3": "c",
"column4": "d"
}
{
"customerName": "google",
"type": "stackoverflow",
"column1": "a",
"column2": "b",
"column3": "c",
"column4": "d"
}
{
"customerName": "microsoft",
"type": "stackoverflow",
"column1": "a",
"column2": "b",
"column3": "c",
"column4": "d"
}
Search Query:
{
"query": {
"bool": {
"filter": [
{
"term": {
"customerName": "google"
}
},
{
"term": {
"type": "stackoverflow"
}
}
],
"must": {
"multi_match": {
"query": "a",
"fields": [
"customerName",
"type",
"column1",
"column2",
"column3",
"column4"
]
}
}
}
}
}
Search Result:
"hits": [
{
"_index": "stof_63988272",
"_type": "_doc",
"_id": "1",
"_score": 0.10536051,
"_source": {
"customerName": "google",
"type": "stackoverflow",
"column1": "a",
"column2": "b",
"column3": "c",
"column4": "d"
}
},
{
"_index": "stof_63988272",
"_type": "_doc",
"_id": "3",
"_score": 0.10536051,
"_source": {
"customerName": "google",
"type": "stackoverflow",
"column1": "a",
"column2": "b",
"column3": "c",
"column4": "d"
}
}
]
Upvotes: 1
Reputation: 513
Seems like you are missing the fields(column) tag in your query. Assuming the schema from your relational table format, the query should be
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "b",
"fields": [
"column1",
"column2",
"column3",
"column4"
]
}
},
"filter": {
"terms": {
"customerName": [
"google"
],
"type": [
"stackoverflow"
]
}
}
}
}
}
Upvotes: 1