简体   繁体   English

弹性搜索未返回术语查询的预期结果

[英]Elastic search is not returning expected results for term query

This is how ,my article data looks in elastic search 这就是我的文章数据在弹性搜索中的显示方式

id:123,
title:xyz,
keywords:"Test Example"


id:124,
title:xyzz,
keywords:"Test Example|test1"

When a keyword is clicked on the front end,say for example: 'Test Example' then i should get articles having that keyword ( i should get above two articles as my results).But i am getting only first article as my result and below is my mapping: 当在前端单击一个关键字时,例如说:“ Test Example”(测试示例),那么我应该获得包含该关键字的文章(我应该得到两篇以上的文章作为我的结果)。但是我得到的只是第一篇文章,而下面的内容是我的映射:

"keywords":
{
"type":"string",
"index":"not_analysed"
}

How can i get both articles in search results?Thank you 我怎样才能在搜索结果中获得这两篇文章?谢谢

Term Query searches for exact terms. Term Query搜索确切字词。 That's why when you search for Test Example you get only one result, as there is only one record that exactly matches Test Example . 这就是为什么当您搜索Test Example您只会得到一个结果的原因,因为只有一条记录与“ Test Example完全匹配。 If you want both the results you need to use something like match or query_string . 如果您想同时获得两个结果,则需要使用matchquery_string之类的东西。 You can use query_string like: 您可以像这样使用query_string

{
"query": {
    "query_string": {
       "default_field": "keywords",
        "query": "Test Example*"
    }
  }
}

您必须使用query_string进行查询,术语查询仅搜索确切的术语。

You set your keywords field to not_analyzed : if you want the field to be searchable you should remove the index clause like so 您将keywords字段设置为not_analyzed :如果希望该字段可搜索,则应删除index子句,如下所示

"keywords": {
    "type":"string"
}

Searching over this field with a match query, anyway, will return results containing a superset of the provided query: searching for test will return both documents even though the tag is actually Test Example . 无论如何,使用match查询搜索该字段将返回包含所提供查询超集的结果:即使标签实际上是Test Example搜索test也会返回两个文档。


If you can change your documents to something like this 如果您可以将文档更改为这样的内容

id:123,
title:xyz,
keywords:"Test Example"


id:124,
title:xyzz,
keywords: ["Test Example", "test1"]

you can use your original mapping with "index":"not_analysed" and a term query will return only documents containing exactly the tag you were looking for. 您可以将原始映射与"index":"not_analysed"并且字词查询将仅返回完全包含您要查找的标签的文档。

{
  "query": {
    "term": {
      "keywords": "test1"
    }
  }
}

Another option to accomplish the same result is to use a pattern tokenizer to split your tag string on the | 实现相同结果的另一种方法是使用模式标记器将您的标记字符串分割为| character to accomplish the same result 性格达到相同的结果

  "tokenizer": {
    "split_tags": {
      "type": "pattern",
      "group": "-1",
      "pattern": "\|"
    }
  }

I have got it working with the following tokenizer: 我已经与以下标记器一起使用:

"split_keywords": {
      "type": "pattern",
      "group": "0",
      "pattern": "([^|]+)"
    }

Keywords will split at pipe character(below is the example) 关键字将以竖线字符分割(下面是示例)

{
  "tokens" : [ {
    "token" : "TestExample",
    "start_offset" : 0,
    "end_offset" : 12,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "test",
    "start_offset" : 13,
    "end_offset" : 17,
    "type" : "word",
    "position" : 2
  }, {
    "token" : "1",
    "start_offset" : 17,
    "end_offset" : 18,
    "type" : "word",
    "position" : 3
  }, {
    "token" : "test1",
    "start_offset" : 13,
    "end_offset" : 18,
    "type" : "word",
    "position" : 3
  } ]
}

Now when i search for 'TestExample',i get above two articles. 现在,当我搜索“ TestExample”时,我得到了以上两篇文章。 Thanks a lot for your help :) 非常感谢你的帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM