【问题标题】:how to perform search in elasticsearch query?如何在弹性搜索查询中执行搜索?
【发布时间】:2020-09-21 18:50:21
【问题描述】:

我想在弹性搜索上执行这样的查询 =>

select * from orders where customerName = 'google' and Type = 'stackoverflow' and query_string 可以来自任何索引。 (即查询字符串可以是“google”或“stackoverflow”或“a”或“b”或“c”或“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

即第 1 行和第 3 行

我尝试使用

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

请帮忙:)

【问题讨论】:

  • 你试过什么?
  • 我更新了我的帖子@SahilGupta
  • 您能分享一下您的索引映射吗? GET my_index/_mapping 通过 kibana
  • @imDevD 你有机会看我的回答吗,期待得到你的反馈????

标签: elasticsearch


【解决方案1】:

添加一个包含索引数据、搜索查询和搜索结果的工作示例

索引数据:

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

搜索查询:

    {
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "customerName": "google"
                    }
                },
                {
                    "term": {
                        "type": "stackoverflow"
                    }
                }
            ],
            "must": {
                "multi_match": {
                    "query": "a",
                    "fields": [
                        "customerName",
                        "type",
                        "column1",
                        "column2",
                        "column3",
                        "column4"
                    ]
                }
            }
        }
    }
}

搜索结果:

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

【讨论】:

    【解决方案2】:

    您的查询中似乎缺少字段(列)标签。 假设您的关系表格式中的架构,查询应该是

    {
        "query": {
            "bool": {
                "must": {
                    "multi_match": {
                        "query": "b",
                        "fields": [
                            "column1",
                            "column2",
                            "column3",
                            "column4"
                        ]
                    }
                },
                "filter": {
                    "terms": {
                        "customerName": [
                            "google"
                        ],
                        "type": [
                            "stackoverflow"
                        ]
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 2020-09-12
      相关资源
      最近更新 更多