首页 > 解决方案 > 如何索引 NotAnalyzed 字段类型列表?

问题描述

class MyObject{
    public string Name{get;set;}
    public List<string> Tags{get;set;}
}

/*Create mapping */
client.Map<MyObject>(m =>
    m.Properties(props =>
        props.String(s =>
            s.Name(p => p.Name)
            .Path(MultiFieldMappingPath.Full)
            .Index(FieldIndexOption.NotAnalyzed)
            .Fields(f =>
                f.String(ps =>
                    ps.Name(p => p.Name.Suffix("searchable"))
                    .Index(FieldIndexOption.Analyzed)
                )
            )
        )       
    )
);

如何为字段标签相同的字段名称索引 NotAnalyzed

我想在标签字段中准确搜索一个短语

示例:我想搜索“弹性搜索”以找出在标签字段中哪个对象恰好包含该单词

Obj1:
{
    "Name":"Object 1",
    "Tags":["elastic search","how to code"]
}
Obj2:
{
    "Name":"Object 2",
    "Tags":["elastic","c#"]
}
Obj3:
{
    "Name":"Object 2",
    "Tags":["learn elastic search","learn C#"]
}
===> Result: Obj 1

标签: c#elasticsearch

解决方案


Based on your request I will create a test1 index.

    PUT test1/doc/1
    {
      "Name": "Object 1",
      "Tags": [
        "elastic search",
        "how to code"
      ]
    }


    PUT test1/doc/2
    {
        "Name":"Object 2",
        "Tags":["elastic","c#"]
    }

因此,我将编写查询以获取您在示例中提到的确切术语弹性搜索。

GET test1/doc/_search
        {
          "query": {
            "term": {
              "Tags.keyword": 
               "elastic search"

            }
          }
        }

所以结果是下面的查询是

curl -XGET "http://localhost:9200/test1/doc/_search"

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "test1",
        "_type": "doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "Name": "Object 2",
          "Tags": [
            "elastic",
            "c#"
          ]
        }
      },
      {
        "_index": "test1",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "Name": "Object 1",
          "Tags": [
            "elastic search",
            "how to code"
          ]
        }
      }
    ]
  }
}

所以现在查询根据您的字段获取文档。

curl -XGET "http://localhost:9200/test1/doc/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "term": {
      "Tags.keyword": 
       "elastic search"

    }
  }
}'

结果是

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test1",
        "_type": "doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "Name": "Object 1",
          "Tags": [
            "elastic search",
            "how to code"
          ]
        }
      }
    ]
  }
}

希望它有效。如果您仍然面临任何问题,请告诉我。


推荐阅读