elasticsearch Elasticsearch请求查询字符串以查找具有特殊字符的单词,elasticsearch,elasticsearch" /> elasticsearch Elasticsearch请求查询字符串以查找具有特殊字符的单词,elasticsearch,elasticsearch" />

elasticsearch Elasticsearch请求查询字符串以查找具有特殊字符的单词

elasticsearch Elasticsearch请求查询字符串以查找具有特殊字符的单词,elasticsearch,elasticsearch,我有一些有标题的文件 document 1: the C# language document 2: the C++ language document 3: the C language 默认映射: { "mappings": { "langs": { "properties": { "title": { "type": "string" } } } } 下一个查询将提供文档的全部3个,但我不需要文

我有一些有标题的文件

document 1: the C# language
document 2: the C++ language
document 3: the C language
默认映射:

{
  "mappings": {
    "langs": {
      "properties": {
        "title": {
          "type": "string"
        }
      }
  }
}
下一个查询将提供文档的全部3个,但我不需要文档3

{
  "query": {
    "query_string": {
      "query": "C# OR C++",
      "fields": [
        "title"
      ]
    }
  }
}

由于
标题
字段使用标准分析器,因此会返回所有三个文档。使用标准分析器,
C#
C++
C
都作为标记
C
进行分析和索引。当你搜索
“C”或C++“
,在引擎盖下搜索
“C或C”
,结果也是一样

您需要的是使用创建一个自定义分析器,并在
title
字段上使用它(精确地在
title.tokens
子字段上)

最后,您现在可以像这样搜索
title.tokens
字段,您将只获得前两个文档:

curl -XPOST localhost:9200/test/_search -d '{
  "query": {
    "query_string": {
      "query": "C# OR C++",
      "analyzer": "my_analyzer",         <--- use your custom analyzer
      "fields": [ "title.tokens" ]       <--- use the new field
    }
  }
}'
curl-XPOST localhost:9200/test/\u search-d'{
“查询”:{
“查询字符串”:{
“查询”:“C#或C++”,
“分析器”:“我的分析器”,
curl -XPUT localhost:9200/test/langs/1 -d '{"title":"The C++ language"}'
curl -XPUT localhost:9200/test/langs/2 -d '{"title":"The C# language"}'
curl -XPUT localhost:9200/test/langs/3 -d '{"title":"The C language"}'
curl -XPOST localhost:9200/test/_search -d '{
  "query": {
    "query_string": {
      "query": "C# OR C++",
      "analyzer": "my_analyzer",         <--- use your custom analyzer
      "fields": [ "title.tokens" ]       <--- use the new field
    }
  }
}'