简体   繁体   中英

how to perform search in elasticsearch query?

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. (ie 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

ie row 1 and row 3

i tried using

"query": {
                "bool": {
                    "must": {
                        "multi_match": {
                            "query": "b"
                        }
                    },
                    "filter": {
                        "terms": {
                            "customerName": [ "google" ],
                            "type": [ "stackoverflow ]
                        }
                    }
                }
            }

please help:)

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"
                    ]
                }
            }
        }
    }
}

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"
    }
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM