ElasticSearch上的java索引包含并以搜索开始

plicqrtu  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(362)

我们使用ElasticSearch来更快地搜索组织数据。数据模型包含组织id、地址、组织名称、业务开始日期和组织联系人数组。
我们要求执行字符串包含搜索,字符串以搜索组织id和/或组织名称字段开始,例如,
organization.name:”abc*” or organization.id:”abc” organization.name:”*” and organization.id:”**” organization.name:”**” and organization.id:”*” 由于我们需要在同一个领域使用两个使用ngram分析仪是不工作的,请告知

uqzxnwby

uqzxnwby1#

据我所知,你需要找到那些文件,在哪里 organization.name 开始于 以及 `organization.id` 包含 (不是一开始)。
为此,您可以使用multi-field,这有助于使用n-gram标记器以不同的方式为同一字段编制索引,以达到不同的目的
添加索引数据、Map、搜索查询和搜索结果的工作示例
索引Map:

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

相关问题