【问题标题】:Multiple search field elasticsearphp多个搜索字段 elasticsearphp
【发布时间】:2021-08-05 02:53:40
【问题描述】:

您好,我想用 elasticsearch enter image description here 做类似的事情

我已经对elasticsearch有所了解,但我不明白如何做到这一点,多重搜索

【问题讨论】:

    标签: php symfony elasticsearch


    【解决方案1】:

    您可以使用bool/must/should子句的组合来组合多个条件

    {
      "query": {
        "bool": {
          "should": [
            {
              "multi_match": {
                "query": "tag"
              }
            },
            {
              "match": {
                "answers": 0
              }
            },
            {
              "match": {
                "user": 1234
              }
            },
            {
              "multi_match": {
                "query": "words here",
                "type": "phrase"
              }
            },
            {
              "match": {
                "score": 3
              }
            },
            {
              "match": {
                "isaccepted": "yes"
              }
            }
          ]
        }
      }
    }
    

    如果您想搜索多个字段,则可以使用multi_match query

    如果未提供任何字段,则 multi_match 查询默认为 index.query.default_field 索引设置,默认为 *. 这将提取映射中符合术语查询条件的所有字段并过滤元数据字段。然后所有提取的字段 结合起来构建一个查询。

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

    索引数据:

    {
      "answers": 0,
      "isaccepted": "no"
    }
    {
      "answers": 0,
      "isaccepted": "yes"
    }
    

    搜索查询:

    {
      "query": {
        "multi_match" : {
          "query" : "yes"
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "67542669",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.2876821,
            "_source": {
              "answers": 0,
              "isaccepted": "yes"
            }
          }
        ]
    

    【讨论】:

    • 如果我有 3 个字段要在其中搜索,有时用户只选择 2 个字段,有时只搜索一个字段或两个 3 个字段,我认为这不适用于我想做的事情
    • @ameell 如果所有字段都不是强制性的,那么您可以使用should 子句代替mustshould 子句的工作方式与逻辑 OR 运算符相同。请使用您的示例索引数据尝试此查询,如果这能解决您的问题,请告诉我?
    • 感谢您的回答,但我不明白它是如何工作的,因为如果用户选择在(年龄、内容)或(内容)中进行 expl 搜索,我还必须指定所有字段, title , age , autor) 和现在在 ( content , autor , age) ...那些值为 null 的字段呢?或者我将为每个案例创建一个函数搜索?这是不可能的
    • @ameell 在这种情况下您可以使用 multi_match 查询 --> elastic.co/guide/en/elasticsearch/reference/current/…,如果在 fields 参数中未指定任何字段,此查询将提取所有字段并在所有字段中搜索查询.即使其中一个字段为空值,也不会影响搜索结果
    • "multi_match" : { "query": "Will Smith", "type": "cross_fields", "fields": [ "first_name", "last_name" ], "operator": "and " } 被执行为: +(first_name:will last_name:will) +(first_name:smith last_name:smith) 我不能在确切的字段中指定确切的值,例如搜索(“alen”,“alex”)执行将(作者:“alen”,name=“alex”)或(作者:“alex”,name=“alen”)所以如果存在具有(作者:“alex”,name=“ alen") 他们将返回结果但结果错误
    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 2012-10-21
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2021-07-13
    相关资源
    最近更新 更多