如何使用NEST QueryString并转义特殊字符? [英] How can I use NEST QueryString and escape the special characters?

查看:888
本文介绍了如何使用NEST QueryString并转义特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在这种情况下,用户输入搜索字词 F5503904902

code>返回正确的结果。但是,如果他们搜索查询 F5503904902-90190 F5503904902-90190_55F 结果不会回来。 p>

我假设这是因为特殊字符,所以我试图逃避他们 - 但是后来没有结果回来了。我的查询是否正确,我做错了什么? 另外我将通配符追加到转义查询的末尾,以匹配任何未结束的查询。



搜索方法:

  public IPagedSearchResult< MyFileObject> Find(ISearchQuery query)
{
ElasticClient client = ElasticClientManager.GetClient(_indexCluster,ElasticSearchIndexName.MyFileObjects);
string queryString = EscapeSearchQuery(query.Query)+*;
var searchResults = client.Search< MyFileObject>(s => s
.From(query.Skip)
.Size(query.Take)
.QueryString(queryString) );



IPagedSearchResult< MyFileObject> pagedSearchResult = new PagedSearchResult< MyFileObject>();
pagedSearchResult.Results = searchResults.Documents;
pagedSearchResult.Skip = query.Skip;
pagedSearchResult.Take = query.Take;
pagedSearchResult.Total = Convert.ToInt32(searchResults.Total);

return pagedSearchResult;
}

逃脱方法:

 私有字符串EscapeSearchQuery(字符串查询)
{
if(String.IsNullOrWhiteSpace(query))return query;

//&&& ||这里没有处理
char [] special = {'+',' - ','=','>',',','!','(' ,'''''''''''''''''''''' '';
char [] qArray = query.ToCharArray();

StringBuilder sb = new StringBuilder();

foreach(var chr in qArray)
{
if(special.Contains(chr))
{
sb.Append(String.Format(\\ {0},chr));
}

{
sb.Append(chr);
}
}

return sb.ToString();
}

我会喜欢任何帮助或指针,为什么这不工作或更好的方式

解决方案

在ElasticSearch中,破折号和下划线不是特殊字符,但它们是导致术语被拆分的字符引进者重要的是现场的索引,我建议设置一个多画面。



https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/multi-fields



pre $ < PUT hilden1

PUT hilden1 / type1 / _mapping
{
properties:{
multifield1:{
type:string ,
fields:{
raw:{
type:string,
index:not_analyzed
}
}
}
}
}

POST hilden1 / type1
{
multifield1:hello
}

POST hilden1 / type1
{
multifield1:hello_underscore
}

POST hilden1 / type1
{
multifield1:hello-dash
}

找到虚线值:

  GET hilden1 / type1 / _search 
{
query:{
filtered:{
过滤器:{
term:{
multifield1:hello-dash
}
}
}
}
}

这不会返回任何结果,因为ES将该字段分成两部分。但是,由于我们将此字段设置为多字段,我们可以根据我们设置的.raw进行查询。此查询将获取您要查找的结果。

  GET hilden1 / type1 / _search 
{
query:{
filtered:{
filter:{
term:{
multifield1.raw:hello-dash
}
}
}
}
}


I am using NEST to communicate with Elasticsearch in my applications.

In this case, the user enters in their search term F5503904902 which returns the correct result. However, if they search for the query F5503904902-90190 or F5503904902-90190_55F the results do not come back.

I assumed this was because of the special characters, so I attempted to escape them - but then no results come back either. Is my query correct, am I doing something wrong? Also I am appending a wildcard to the end of the escaped query to match anything open ended.

Search Method:

public IPagedSearchResult<MyFileObject> Find(ISearchQuery query)
{
    ElasticClient client = ElasticClientManager.GetClient(_indexCluster, ElasticSearchIndexName.MyFileObjects);
    string queryString = EscapeSearchQuery(query.Query) + "*"; 
    var searchResults = client.Search<MyFileObject>(s => s
        .From(query.Skip)
        .Size(query.Take)
        .QueryString(queryString));



    IPagedSearchResult<MyFileObject> pagedSearchResult = new PagedSearchResult<MyFileObject>();
    pagedSearchResult.Results = searchResults.Documents;
    pagedSearchResult.Skip = query.Skip;
    pagedSearchResult.Take = query.Take;
    pagedSearchResult.Total = Convert.ToInt32(searchResults.Total);

    return pagedSearchResult;
}

Escape Method:

private string EscapeSearchQuery(string query)
{
    if (String.IsNullOrWhiteSpace(query)) return query;

    //&& || not handled here
    char[] special = { '+', '-', '=', '>', '<', '!', '(', ')', '{', '}', '[', ']', '^', '\"', '~', '*', '?', ':', '\\', '/', ' ' };
    char[] qArray = query.ToCharArray();

    StringBuilder sb = new StringBuilder();

    foreach (var chr in qArray)
    {
        if (special.Contains(chr))
        {
            sb.Append(String.Format("\\{0}", chr));
        }
        else
        {
            sb.Append(chr);
        }
    }

    return sb.ToString();
}

I would love any help or pointers why this isn't working or better ways to accomplish this.

解决方案

In ElasticSearch dash and underscore are not special characters, but they are characters that cause the terms to be split. The important thing is the index on the field. I recommend setting up a multifield.

https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/multi-fields.html

Here is an example:

PUT hilden1

PUT hilden1/type1/_mapping
{
  "properties": {
    "multifield1": {
      "type": "string",
      "fields": {
        "raw": {
          "type": "string", 
          "index": "not_analyzed"
        }
      }
    }
  }
}

POST hilden1/type1
{
  "multifield1": "hello"
}

POST hilden1/type1
{
  "multifield1": "hello_underscore"
}

POST hilden1/type1
{
  "multifield1": "hello-dash"
}

Let's try to find the dashed value:

GET hilden1/type1/_search
{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "multifield1": "hello-dash"
        }
      }
    }
  }
}

That returns no results because ES is splitting the field into two parts behind the scenes. But, because we setup this field as a multi-field we can query for it based on the ".raw" that we set. This query will get the results you're looking for.

GET hilden1/type1/_search
{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "multifield1.raw": "hello-dash"
        }
      }
    }
  }
}

这篇关于如何使用NEST QueryString并转义特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆