Warm tip: This article is reproduced from serverfault.com, please click

java-Elastic search上的索引包含并从搜索开始

(java - Index on Elastic search contains and starts with search)

发布于 2020-11-28 22:15:14

我们正在使用弹性搜索来更快地搜索组织数据。数据模型具有组织ID,地址,组织名称,业务开始日期和组织联系人数组。
我们要求执行字符串包含搜索,并且字符串以组织ID和/或组织名称字段的搜索开头。例如, organization.name:”abc*” or organization.id:”abc

organization.name:”abc*” and organization.id:”*abc*”
organization.name:”*abc*” and organization.id:”abc*”
由于我们需要使用Ngram分析器在同一字段上同时使用两者,因此请注意
Questioner
arupc
Viewed
11
ESCoder 2020-11-29 08:46:10

据我了解,你需要找到那些organization.nameabcANDorganization.id包含abc开头(而不是开头)的文档

为此,你可以使用multi-field,这与n-gram标记器一起用于为不同目的以不同方式为同一字段建立索引很有用

添加带有索引数据,映射,搜索查询和搜索结果的工作示例

索引映射:

    {
  "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"
          }
        }
      }
    ]