【问题标题】:search query in elasticsearch with url in field在弹性搜索中使用字段中的 url 搜索查询
【发布时间】:2016-10-04 23:55:02
【问题描述】:

我必须在弹性搜索的索引数据中搜索文件位置路径。我以编码格式索引了位置路径(也尝试过不编码)。以下查询返回所有索引数据而不进行任何匹配。如果我用 idtitle 字段搜索,它会返回正确的结果。有人知道吗?

{
"query": {
     "match": {
     "location": "%5c%5c25.94.150.212%5cfoldername%5cintroduction_to_c_sharp.ppt"
    }
}
}

我从浏览器http://localhost:9200/documents得到的响应

{
  "documents": {
    "aliases": {},
    "mappings": {
      "indexdocument": {
        "properties": {
          "document": {
            "type": "attachment",
            "fields": {
              "content": {
                "type": "string"
              },
              "author": {
                "type": "string"
              },
              "title": {
                "type": "string",
                "term_vector": "with_positions_offsets"
              },
              "name": {
                "type": "string"
              },
              "date": {
                "type": "date",
                "format": "strict_date_optional_time||epoch_millis"
              },
              "keywords": {
                "type": "string"
              },
              "content_type": {
                "type": "string"
              },
              "content_length": {
                "type": "integer"
              },
              "language": {
                "type": "string"
              }
            }
          },
          "documentType": {
            "type": "string"
          },
          "id": {
            "type": "long"
          },
          "lastModifiedDate": {
            "type": "date",
            "format": "strict_date_optional_time||epoch_millis"
          },
          "location": {
            "type": "string"
          },
          "title": {
            "type": "string"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1465193502636",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "5kCRvhmsQAGyndkswLhLrg",
        "version": {
          "created": "2030399"
        }
      }
    },
    "warmers": {}
  }
}

创建具有一个字段附件的索引代码:

        public void CreateDocumentIndex()
           {
               this.client.CreateIndex("documents", c =>  c.Mappings(mp=>mp.Map<IndexDocument>
                   (m => m.Properties(ps => ps.Attachment
                                       (a => a.Name(o => o.Document)
                                             .TitleField(t => t.Name(x =>     x.Title).TermVector(TermVectorOption.WithPositionsOffsets))
                                              )))));
  }

要索引的属性

 public class IndexDocument
   {
    public long Id { get; set; }
    public string Title { get; set; }
    public string DocumentName { get; set; }
    // Base64-encoded file content.
    public string Document { get; set; }
    public string DocumentType { get; set; }
    [Nest.String(Store = true, Index = Nest.FieldIndexOption.NotAnalyzed)]
    public string Location { get; set; }        
    public DateTime LastModifiedDate { get; set; }

 }

【问题讨论】:

    标签: c# elasticsearch nest querydsl


    【解决方案1】:

    如果您的location 字段是not_analyzed,您可以使用term 查询而不是match

    {
       "query": {
          "term": {
             "location": "%5c%5c25.94.150.212%5cfoldername%5cintroduction_to_c_sharp.ppt"
          }
       }
    }
    

    否则,您需要将location 字段设为not_analyzed(见下文)并重新索引您的数据。

    PUT your_index/_mapping/your_type
    {
      "properties": {
        "location": {
          "type": "string"
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
    

    那么你可以使用下面的term查询

    {
       "query": {
          "term": {
             "location.raw": "%5c%5c25.94.150.212%5cfoldername%5cintroduction_to_c_sharp.ppt"
          }
       }
    }
    

    【讨论】:

    • 我怎样才能让我的位置字段不被分析?
    • 我这样设置属性 [Nest.String(Store = false, Index = Nest.FieldIndexOption.NotAnalyzed)] public string Location { get;放; } 然后用上面的查询搜索。现在没有行返回。
    • 您需要先删除您的索引,然后重新运行代码以再次创建索引,然后再重新索引您的数据。
    • 做到了。还是没有变化。我已经给出了这样的索引 this.client.CreateIndex("documents", c => c.Mappings(mp=>mp.Map (m => m.Properties(ps => ps.Attachment (a = > a.Name(o => o.Document) .TitleField(t => t.Name(x => x.Title).TermVector(TermVectorOption.WithPositionsOffsets)) .FileField(f=>f.Name(n=> n.Location).TermVector(TermVectorOption.WithPositionsOffsets)) )))));
    • 你能用你从curl -XGET localhost:9200/documents得到的信息更新你的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多