【问题标题】:Pass "-" in Elastic Search Query在 Elastic Search 查询中传递“-”
【发布时间】:2016-02-19 15:16:59
【问题描述】:

当我们传递包含特殊字符的查询时,Elastic Search 会拆分文本。 例如。如果我们在查询中通过“test-test”,我们如何让 Elastic Search 将其视为一个单词而不是将其拆分。

我们正在搜索的领域使用的分析仪:

"text_search_filter": {
        "type":     "edge_ngram",
        "min_gram": 1,
        "max_gram": 15
     },
     "standard_stop_filter": {
       "type":       "stop",
       "stopwords":  "_english_"
     }
   },

   "analyzer": {

     "text_search_analyzer": {
        "type": "custom",
        "tokenizer": "whitespace",
        "filter": [
           "lowercase",
           "asciifolding",
           "text_search_filter"
        ]
     }

}

也是搜索的查询:

"query": {
    "multi_match": {
      "query": "test-test",
      "type": "cross_fields",
      "fields": [
        "FIELD_NAME"
      ],

    }
  }


{
"tokens": [
    {
        "token": "'",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'t",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'te",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'tes",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'test",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'test-",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'test-t",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'test-te",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'test-tes",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'test-test",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    },
    {
        "token": "'test-test'",
        "start_offset": 0,
        "end_offset": 11,
        "type": "word",
        "position": 1
    }
]

}

【问题讨论】:

  • 您的用例是什么?你的mapping是什么?因为有不同的方法可以实现这一点
  • 更新了使用的分析器和用于搜索的查询。我们可以看到使用分析器创建了一个令牌“test-test”。
  • curl -XGET 'localhost:9200/your_index_name/_analyze?analyzer=test_search_analyzer' -d 'test-test'的输出是什么
  • 您在输出中看到标记“test-test”吗?
  • 您确定您搜索的所有字段都应用了 test_search_analyzer 吗?因为 test-test 是令牌之一,它应该匹配。你没有使用不同的search_analyzer 对吗?

标签: elasticsearch lucene


【解决方案1】:

在我的代码中,我捕获了所有包含“-”的单词并为其添加了引号。

示例: 乔-多->“乔-多”

java 代码:

    static String placeWordsWithDashInQuote(String value) {
    return Arrays.stream(value.split("\\s"))
        .filter(v -> !v.isEmpty())
        .map(v -> v.contains("-") && !v.startsWith("\"") ? "\"" + v + "\"" : v)
        .collect(Collectors.joining(" "));
}

在这个示例查询之后看起来像:

{
"query": {
    "bool": {
        "must": [
            {
                "query_string": {
                    "fields": [
                        "lastName",
                        "firstName"
                    ],
                    "query": "\"joe-doe\"",
                    "default_operator": "AND"
                }
            }
        ]
    }
},
"sort": [],
"from": 0,
"size": 10 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多