Elastic Search “Terms” 查询未返回结果,但 “Match” 查询返回结果。

huangapple go评论68阅读模式
英文:

Elastic Search Terms query not giving result but match query is giving

问题

我正在使用Kibana查询,它给我返回了我期望的结果。但是当我使用terms查询时,它没有显示任何结果,尽管值相同。没有任何字符或大小写的区别,一切都是小写。

我的最终目标是在我的C#代码中获取结果。我的查询是:

.Query(q => q.Raw(strQuery) && q.Terms(c => c.Name("by_account").Field(p => p.AccountsList).Terms(accountTerms))

使用这个查询,我得到零个结果,但如果我注释掉第二个查询,我就能得到结果:

.Query(q => q.Raw(strQuery) //&& q.Terms(c => c.Name("by_account").Field(p => p.AccountsList).Terms(accountTerms))

现在我的查询字符串是:

strQuery ={"bool":{"must":[{"match_phrase_prefix":{"fullname":"test"}}]}}

List<string> accountTerms = new List<string>();
accountTerms.Add("bc73afda-d4f2");

AccountId 是一个 GUID 类型,我不能在这里粘贴完整的值,因此这不是完整的值。

英文:

I am using a Kibana query

GET /MY_INDEX_HERE/_search
{
  &quot;query&quot;: {
    &quot;match&quot;: {
      &quot;AccountId&quot;: 
        &quot;bc73afda-d4f2&quot;
      
    }
  }
}

and it is giving me the results, I expect. But when I use a terms query, it is not showing any result, although the values are same. There is no difference in any character or any case, everything is lower-case only.

My final goal is to get the result in my C# code. My query is

.Query(q =&gt; q.Raw(strQuery) &amp;&amp; q.Terms(c =&gt; c.Name(&quot;by_account&quot;).Field(p =&gt; p.AccountsList).Terms(accountTerms))

With this query I am getting zero results, but if I comment the second query I am getting results:

.Query(q =&gt; q.Raw(strQuery) //&amp;&amp; q.Terms(c =&gt; c.Name(&quot;by_account&quot;).Field(p =&gt; p.AccountsList).Terms(accountTerms))

Right now my

strQuery ={\&quot;bool\&quot;:{\&quot;must\&quot;:[{\&quot;match_phrase_prefix\&quot;:{\&quot;fullname\&quot;:\&quot;test\&quot;}}]}} 

and

  List&lt;string&gt; accountTerms = new List&lt;string&gt;();
                accountTerms.Add(&quot;bc73afda-d4f2&quot;);

The AccountId is a GUID type, I cant paste the full value here, hence it's not a complete value.

答案1

得分: 1

  1. 假设您的数据存储在一个名为 text 的字段中,该字段是一个经过分析的字段类型。

  2. 如果您使用标准分析器,您的源代码 bc73afda-d4f2 会被转换成两个标记,即 bc73afdad4f2。因此,如果您查询术语 bc73afda-d4f2,则没有匹配的标记。

  3. 现在您有不同的选择,具体取决于您想要实现什么,以及您可以影响哪些部分(Elasticsearch 和/或 C# 代码)。

  4. 您可以编辑第二个查询,改为使用 match_phrase 而不是 term

  5. 您可以将第二个条件添加到现有的 strQuery 中,类似于以下内容:

    "bool":{"must":[{"match_phrase_prefix":{"fullname":"test"}},{"match_phrase":{"AccountId":"bc73afda-d4f2"}}]}
    
  6. 如果您的映射是一个多类型的 text/keyword 字段,或者您可以将其转换为多类型字段(或者只需使用 keyword 字段而不是 text),您可以使用 AccountId.keyword 字段进行 terms 查询。keyword 字段不经过分析,因此对于 bc73afda-d4f2term 查询会匹配。

  7. 您还可以使用自定义分析器并重新索引您的数据。但这可能是过度复杂或错误的解决方案。

英文:

I assume that your data is stored in a text field, which is an analyzed field type.

If you are using the standard analyzer, your source bc73afda-d4f2 is transformed into the two tokens bc73afda and d4f2. Therefore, if you're querying for the term bc73afda-d4f2 there is no token that matches .

You have different options now, depending on what you want to achieve exactly and which parts you can influence (Elasticsearch and/or C# code).

  1. You could edit your second query to use match_phrase instead of term.

  2. You could add your second condition to the existing strQuery. That would be something along the lines of:

    \&quot;bool\&quot;:{\&quot;must\&quot;:[{\&quot;match_phrase_prefix\&quot;:{\&quot;fullname\&quot;:\&quot;test\&quot;}},{\&quot;match_phrase\&quot;:{\&quot;AccountId\&quot;:\&quot;bc73afda-d4f2\&quot;}}]}}
    
  3. If your mapping is a multi-type text/keyword field or you can make it into one (or just use a keyword field instead of text), you can query the AccountId.keyword field with your terms query. keyword fields are not analyzed, so the term query for bc73afda-d4f2 would be a hit.

  4. You could use a custom analyzer and reindex your data. But this is probably overkill/the wrong way to approach your issue.

答案2

得分: 1

Kibana故障排除 'terms' 查询

terms 查询是一种精确匹配查询,这意味着它不会在匹配之前分析查询输入或字段值。

如果您的索引中的 AccountId 字段已映射为 keyword 字段或带有 keyword 类型的文本字段,那么输入 bc73afda-d4f2 将被视为单个术语,只有在字段值完全匹配,包括大小写时才会匹配。

检查 AccountId 的映射:

GET /MY_INDEX_HERE/_mapping/field/AccountId

如果字段的 typetextkeyword,请调整查询以使用字段的确切大小写和值。

或者,您可以使用 match_phrase 查询而不是 terms 查询来匹配 AccountId 字段的确切短语,而不考虑大小写。

GET /MY_INDEX_HERE/_search
{
  "query": {
    "match_phrase": {
      "AccountId": "bc73afda-d4f2"
    }
  }
}
英文:

Troubleshooting Kibana 'terms' Query

The terms query is an exact match query, which means it doesn't analyze the query input or the field values before matching them.

If the AccountId field in your index has been mapped as a keyword field or a text field with the keyword type, then the input bc73afda-d4f2 will be treated as a single term, and it will only match if the field value is an exact match, including case.

Check the mapping of the AccountId:

GET /MY_INDEX_HERE/_mapping/field/AccountId

If the type of the field is text or keyword, Adjust your query to use the exact case and value of the field.

Alternatively, you can use a match_phrase query instead of the terms query to match the exact phrase of the AccountId field, regardless of the case.

GET /MY_INDEX_HERE/_search
{
  &quot;query&quot;: {
    &quot;match_phrase&quot;: {
      &quot;AccountId&quot;: &quot;bc73afda-d4f2&quot;
    }
  }
}

huangapple
  • 本文由 发表于 2023年2月19日 20:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定