繁体   English   中英

如何在 elasticsearch 查询中执行搜索?

[英]how to perform search in elasticsearch query?

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

select * 来自 customerName = 'google' 和 Type = 'stackoverflow' 且 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 ]
                        }
                    }
                }
            }

请帮忙:)

似乎您在查询中缺少 fields(column) 标记。 假设来自关系表格式的模式,查询应该是

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

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

指数数据:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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