Elastic search 上的索引包含并以搜索开头
Index on Elastic search contains and starts with search
我们正在使用弹性搜索来更快地搜索我们的组织数据。数据模型有组织 ID、地址、组织名称、业务开始日期和组织联系人数组。
我们已要求执行字符串包含搜索和字符串开头搜索组织 ID and/or 组织名称字段
例如,
organization.name:”abc*” 或 organization.id:”<em>abc</em>”</p>
<pre><code>organization.name:”abc*” and organization.id:”*abc*”
organization.name:”*abc*” and organization.id:”abc*”
由于我们需要在同一字段上同时使用两者,因此使用 Ngram 分析器不起作用
请指教
据我所知,您需要找到那些文件,其中 organization.name
以 abc
开头并且 organization.id
包含 abc
(不在开头) .
为此,您可以使用 multi-field, which is useful to index the same field in different ways for different purposes along with n-gram tokenizer
添加包含索引数据、映射、搜索查询和搜索结果的工作示例
索引映射:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 20,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 20
},
"mappings": {
"properties": {
"organization": {
"properties": {
"name": {
"type": "keyword",
"fields": {
"raw": {
"type": "text",
"analyzer": "my_analyzer"
}
}
},
"id": {
"type": "keyword",
"fields": {
"raw": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
}
}
}
}
索引数据:
{
"organization": {
"id": "abc def",
"name": "Aspect abc Technology"
}
}
{
"organization": {
"id": "defabc",
"name": "abc Aspect Technology"
}
}
{
"organization": {
"id": "abcef",
"name": "abc Aspect Technology"
}
}
{
"organization": {
"id": "abc",
"name": "Aspect Technology"
}
}
搜索查询:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"organization.id.raw": "abc"
}
},
{
"prefix": {
"organization.name": "abc"
}
}
],
"must_not": {
"prefix": {
"organization.id": "abc"
}
}
}
},
{
"bool": {
"must": [
{
"prefix": {
"organization.id": "abc"
}
},
{
"match": {
"organization.name.raw": "abc"
}
}
],
"must_not": {
"prefix": {
"organization.name": "abc"
}
}
}
}
],
"minimum_should_match": 1
}
}
}
搜索结果:
"hits": [
{
"_index": "65054994",
"_type": "_doc",
"_id": "1",
"_score": 1.3590312,
"_source": {
"organization": {
"id": "abc def",
"name": "Aspect abc Technology"
}
}
},
{
"_index": "65054994",
"_type": "_doc",
"_id": "2",
"_score": 1.0725547,
"_source": {
"organization": {
"id": "defabc",
"name": "abc Aspect Technology"
}
}
}
]
我们正在使用弹性搜索来更快地搜索我们的组织数据。数据模型有组织 ID、地址、组织名称、业务开始日期和组织联系人数组。
我们已要求执行字符串包含搜索和字符串开头搜索组织 ID and/or 组织名称字段
例如,
organization.name:”abc*” 或 organization.id:”<em>abc</em>”</p>
<pre><code>organization.name:”abc*” and organization.id:”*abc*”
organization.name:”*abc*” and organization.id:”abc*”
由于我们需要在同一字段上同时使用两者,因此使用 Ngram 分析器不起作用
请指教
据我所知,您需要找到那些文件,其中 organization.name
以 abc
开头并且 organization.id
包含 abc
(不在开头) .
为此,您可以使用 multi-field, which is useful to index the same field in different ways for different purposes along with n-gram tokenizer
添加包含索引数据、映射、搜索查询和搜索结果的工作示例
索引映射:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 20,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 20
},
"mappings": {
"properties": {
"organization": {
"properties": {
"name": {
"type": "keyword",
"fields": {
"raw": {
"type": "text",
"analyzer": "my_analyzer"
}
}
},
"id": {
"type": "keyword",
"fields": {
"raw": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
}
}
}
}
索引数据:
{
"organization": {
"id": "abc def",
"name": "Aspect abc Technology"
}
}
{
"organization": {
"id": "defabc",
"name": "abc Aspect Technology"
}
}
{
"organization": {
"id": "abcef",
"name": "abc Aspect Technology"
}
}
{
"organization": {
"id": "abc",
"name": "Aspect Technology"
}
}
搜索查询:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"organization.id.raw": "abc"
}
},
{
"prefix": {
"organization.name": "abc"
}
}
],
"must_not": {
"prefix": {
"organization.id": "abc"
}
}
}
},
{
"bool": {
"must": [
{
"prefix": {
"organization.id": "abc"
}
},
{
"match": {
"organization.name.raw": "abc"
}
}
],
"must_not": {
"prefix": {
"organization.name": "abc"
}
}
}
}
],
"minimum_should_match": 1
}
}
}
搜索结果:
"hits": [
{
"_index": "65054994",
"_type": "_doc",
"_id": "1",
"_score": 1.3590312,
"_source": {
"organization": {
"id": "abc def",
"name": "Aspect abc Technology"
}
}
},
{
"_index": "65054994",
"_type": "_doc",
"_id": "2",
"_score": 1.0725547,
"_source": {
"organization": {
"id": "defabc",
"name": "abc Aspect Technology"
}
}
}
]