【问题标题】:executing multimatch ElasticSearch with boolean operations in query在查询中使用布尔运算执行多重匹配 ElasticSearch
【发布时间】:2016-10-19 18:23:43
【问题描述】:

我能否使用 ElasticSearch 和 NEST 执行多重匹配搜索,以在查询中使用布尔运算传递查询?看来我传递给 multimatch 的所有术语默认都与 OR 链接(可以更改为其他运算符)。

我希望 ES 从查询中评估布尔运算符,例如。 "A && B || C" 和多个字段一样搜索。

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

    是的,你可以https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#operator-min

    {
      "multi_match" : {
        "query":      "Will Smith",
        "type":       "best_fields",
        "fields":     [ "first_name", "last_name" ],
        "operator":   "and" 
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以设置operator to and 来更改multi_match query 的语义。以 NEST 为例

      void Main()
      {
          var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
          var connectionSettings = new ConnectionSettings(pool);
      
          var client = new ElasticClient(connectionSettings);
      
          client.Search<MyDocument>(s => s
              .Query (q => q
                  .MultiMatch(m => m
                      .Fields(f => f
                          .Field(p => p.FirstProperty)
                          .Field(p => p.SecondProperty)
                      )
                      .Query("this is the query")
                      .Operator(Operator.And)
                  )
              )
          );
      }
      
      public class MyDocument
      {
          public string FirstProperty { get; set; }
      
          public string SecondProperty { get; set; }
      }
      

      产生以下查询

      {
        "query": {
          "multi_match": {
            "query": "this is the query",
            "operator": "and",
            "fields": [
              "firstProperty",
              "secondProperty"
            ]
          }
        }
      }
      

      还可以查看MultiMatch query usage docs for NEST

      【讨论】:

        猜你喜欢
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多