在ElasticSearch.Nest客户端中进行布尔'和'搜索

时间:2016-07-07 05:43:39

标签: c# elasticsearch nest

我有一个弹性搜索查询,返回我认为'或'搜索。弹性搜索Nest客户端是v4.0.30319,弹性搜索服务器是版本1.6.0。 '和'搜索在索引'sc'上。对相应的相应数据库表运行“select * from clients where of profiles where profile'%business%'和... profile of'%Helping%'”的等效sql语句将不返回任何记录。如何更改以下查询和/或代码以执行布尔'和'搜索?

query = 'id:("7ee683c2-19eb-45c6-9ca8-985379ce34dd") 
         AND role:("User","OrganizationAdmin","SystemAdmin","follower") 
         AND (  
              sc:("business*") 
              and  sc:("objectives*") 
              and  sc:("memorable*") 
              and  sc:("Keith*") 
              and  sc:("Lawyer*") 
              and  sc:("accountant*") 
              and  sc:("Helping*") )' ;

result = _client.Search<T>(
 t => t.Types(type).Index(_index).Query(
    qt => qt.QueryString(qs => qs.Query(query).OnFields(fields))).Filter(
        x => x.Range(y => y.From(fromDate).To(toDate).OnField(rangeOnField))
    ).SortDescending(orderByField).SortDescending("_score").Skip(skipRecord).Take(pageSize));

我发现result.ConnectionStatus包含原始弹性搜索查询,这非常有用。使用基本查询,然后手动添加sc的布尔条件,将原始搜索条件显示为

    {
      "from": 0,
      "size": 100,
      "sort": {
        "cd": "desc",
        "_score": "desc"
      },
      "query": {
        "query_string": {
          "query": "id:(\"7ee683c2-19eb-45c6-9ca8-985379ce34dd\") AND role:(\"User\",\"OrganizationAdmin\",\"SystemAdmin\",\"follower\") AND ( sc:(\"business*\") AND sc:(\"objectives*\")  AND sc:(\"memorable*\")  AND  sc:(\"Keith*\") AND  sc:(\"Lawyer*\") AND  sc:(\"accountant*\") AND  sc:(\"Helping*\") ) ",
          "fields": [
            "id",
            "sc",
            "role",
            "nm"
          ]
        }
      },
      "filter": {
        "range": {
          "cd": {
            "from": "19160709",
            "to": "21160709"
          }
        }
      }
    }

没有返回结果所以我猜测代码有些奇怪。

1 个答案:

答案 0 :(得分:0)

事实证明需要更改查询。 &#39;和&#39;需要&#39; AND&#39;。

此查询

{
    "from": 0,
    "size": 100,
    "sort": {
        "cd": "desc",
        "_score": "desc"
    },
    "query": {
        "query_string": {
            "query": "id:(\"7ee683c2-19eb-45c6-9ca8-985379ce34dd\") AND role:(\"User\",\"OrganizationAdmin\",\"SystemAdmin\",\"follower\") AND ( sc:(\"business*\") AND sc:(\"objectives*\")  AND sc:(\"memorable*\")  AND  sc:(\"Keith*\") AND  sc:(\"Lawyer*\") AND  sc:(\"accountant*\") AND  sc:(\"Helping*\") ) ",
            "fields": [
                "id",
                "sc",
                "role",
                "nm"
            ]
        }
    },
    "filter": {
        "range": {
            "cd": {
                "from": "19160709",
                "to": "21160709"
            }
        }
    }
}

工作时没有返回任何记录,但此查询

{
    "from": 0,
    "size": 100,
    "sort": {
        "cd": "desc",
        "_score": "desc"
    },
    "query": {
        "query_string": {
            "query": "id:(\"7ee683c2-19eb-45c6-9ca8-985379ce34dd\") AND role:(\"User\",\"OrganizationAdmin\",\"SystemAdmin\",\"follower\") AND (   sc:(\"business\")  and sc:(\"objectives\")  and sc:(\"memorable\")  and sc:(\"Keith\")  and sc:(\"Lawyer\")  and sc:(\"accountant\")  and sc:(\"Helping\") ) ",
            "fields": [
                "id",
                "sc",
                "role",
                "nm"
            ]
        }
    },
    "filter": {
        "range": {
            "cd": {
                "from": "19160709",
                "to": "21160709"
            }
        }
    }
} 

无法返回3条记录。唯一的区别是该字段&#39; sc&#39;的支架中的&#39;和

相关问题