如何索引NotAnalyzed字段类型List <string>?

时间:2019-04-16 10:23:32

标签: c# elasticsearch

'(^|,)(SE)(,|$)'

如何为未标记字段标签的相同字段建立索引

我想在“标签”字段中精确搜索一个词组

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

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)
                )
            )
        )       
    )
);

1 个答案:

答案 0 :(得分:0)

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

希望它能起作用。让我知道您是否仍然遇到任何问题。