弹性搜索多个字段中的多个关键字

 山海 发布于 2023-01-08 10:51

我正在尝试在弹性搜索中构建查询以在多个字段的文本内搜索,例如:标题和描述是字段,我想在这3个字段中搜索"obama","world","news".我tryed搜索和发现这里弹性网站信息:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#_literal_fields_literal_and_per_field_boosting 但问题是当我尝试这种结构时会产生错误:
首先:

{
  "multi_match" : {
    "query":      "obama world news",
    "type":       "cross_fields",
    "fields":     [ "Title", "Description" ],
    "operator":   "and"
  }
}

它会产生如下错误:

{
    "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][0]: SearchParseException[[republishan2][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][0]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][3]: SearchParseException[[republishan2][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][3]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][4]: SearchParseException[[republishan2][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][4]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][1]: SearchParseException[[republishan2][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][1]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][2]: SearchParseException[[republishan2][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][2]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }]",
    "status": 400
}

我也试过这个查询:

{
    "sort": {
        "re_max": {
            "order": "desc"
            }
            }, 
    "query": {
      "multi_match" : {
        "query":      "obama world news",
        "type":       "cross_fields",
        "fields":     [ "Title", "Description" ],
        "operator":   "and"
      }
    },
    "from": 0, 
    "size": 30}
}

这就是结果:

{
    "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][0]: SearchParseException[[republishan2][0]: from[-1],size[-1],sort[!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][3]: SearchParseException[[republishan2][3]: from[-1],size[-1],sort[!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][4]: SearchParseException[[republishan2][4]: from[-1],size[-1],sort[!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][1]: SearchParseException[[republishan2][1]: from[-1],size[-1],sort[!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][2]: SearchParseException[[republishan2][2]: from[-1],size[-1],sort[!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }]",
    "status": 400
}

我正在使用Postman Rest客户端来运行查询.我的代码有什么问题?

1 个回答
  • 试试这样吧

    {
      "query": {
        "multi_match": {
          "query": "obama world news",
          "type": "cross_fields",
          "fields": ["Title", "Description"],
          "operator": "and"
        }
      },
      "from": 0,
      "size": 20,
      "sort": [
        {
          "re_max": {
            "order": "desc"
          }
        }
      ]
    }
    

    希望能帮助到你..!

    2023-01-08 10:53 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有