【问题标题】:elasticsearch simple_query_string fields different typeelasticsearch simple_query_string 字段不同类型
【发布时间】:2021-07-03 06:23:56
【问题描述】:

我正在使用 elasticsearch,我想进行此类查询 {"query": {"simple_query_string":{"fields":["field1","field2","field3","field4"],"query":"200 100 false message"}}} 我有 int 类型的 field1,field2 和 field3 布尔值和 field4 字符串 问题是 elasticsearch 将始终返回解析错误,因为他将尝试将 rxample field3 与 100 进行比较 任何可行的解决方案

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    根据Elasticsearch documentation,您可以在true 处添加lenient 参数,以忽略基于格式的错误。

    【讨论】:

    • 谢谢你,你工作得很好,我不知道我是怎么没有在文档中读到的
    【解决方案2】:

    与结果匹配的字段是否重要?如果不是,您可以使用copy_to 功能将所有文本复制到单个文本字段,然后搜索那个!像这样:

    PUT stackoverflow
    {
      "mappings": {
        "properties": {
          "field1": {
            "type": "integer",
            "copy_to": "all_fields"
          },
          "field2": {
            "type": "integer",
            "copy_to": "all_fields"
          },
          "field3": {
            "type": "text",
            "copy_to": "all_fields"
          },
          "field4": {
            "type": "text",
            "copy_to": "all_fields"
          },
          "all_fields": {
            "type": "text"
          }
        }
      }
    }
    

    还有查询:

    GET stackoverflow/_search
    {
      "query": {
        "simple_query_string": {
          "fields": [
            "field_all"
          ],
          "query": "200 100 false message"
        }
      }
    }
    

    【讨论】:

    • 感谢您的回复,但我不能使用它,因为我没有提供到弹性的映射
    【解决方案3】:

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

    索引映射:

    {
      "mappings": {
        "properties": {
          "field1": {
            "type": "integer"
          },
          "field2": {
            "type": "integer"
          },
          "field3": {
            "type": "boolean"
          },
          "field4": {
            "type": "text"
          }
        }
      }
    }
    

    索引数据:

    {
      "field1": 200,
      "field2": 100,
      "field3": "false",
      "field4": "message"
    }
    

    搜索查询:

    {
      "query": {
        "simple_query_string": {
          "fields": [
            "field1",
            "field2",
            "field3",
            "field4"
          ],
          "query": "200 100 false message",
          "lenient": true
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "66986084",
            "_type": "_doc",
            "_id": "1",
            "_score": 2.575364,
            "_source": {
              "field1": 200,
              "field2": 100,
              "field3": "false",
              "field4": "message"
            }
          }
        ]
    

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多